Exemple #1
0
        /* Keeping a user logged in indefinitely solves one issue.
         * How do we handle host accessibility issues?
         * Ping Fishbowl server periodically so that we can
         * automatically reconnect when it's back online.
         * TODO: Verify that this works.
         */
        public static void WatchDog()
        {
            Console.WriteLine("WatchDog() started");

            // Grab Fishbowl host address
            string FishbowlServer = ConfigurationManager.AppSettings.Get("FishbowlServer");

            Ping        pingSender = new Ping();
            PingOptions options    = new PingOptions();

            // Use the default TTL value which is 128,
            // but change the fragmentation behavior.
            options.DontFragment = true;

            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "****RESTfulFish Gateway Ping****";

            byte[] buffer  = Encoding.ASCII.GetBytes(data);
            int    timeout = 120;

            while (true)
            {
                try
                {
                    PingReply reply = pingSender.Send(FishbowlServer, timeout, buffer, options);

                    if (reply.Status == IPStatus.DestinationHostUnreachable)
                    {
                        // Tell KeepAlive() and everyone else to stop sending data to Fishbowl since it's down.
                        FishbowlServerDown = true;

                        Console.WriteLine("Fishbowl server is down!");
                        Console.WriteLine("Checking if Fishbowl server is up in 5 seconds.");
                        Thread.Sleep(5000);

                        // TODO: Verify that pinging a specific port signifies that an
                        // application (Fishbowl) is listening on it and not just the OS responding.
                        while (!ConnectionObject.IsFishbowlPortOpen())
                        {
                            Console.WriteLine("Fishbowl server is still down, chaecking again in 5 seconds.");
                            // Wait five seconds before checking again
                            Thread.Sleep(5000);
                        }

                        // TODO: Determine best option. Restart app or recall methods/functions?
                        // Restart app
                        System.Diagnostics.Process.Start(Assembly.GetExecutingAssembly().Location);
                        Environment.Exit(0);
                    }
                    // Ping every 2 seconds
                    Thread.Sleep(2000);
                }
                catch (Exception e)
                {
                    Misc.ExceptionMessage("WatchDog", e, null, false);
                }
            }

            // Reconnect
        }