Beispiel #1
0
        /// <summary>
        /// Main entry for the application
        /// </summary>
        public static void Main()
        {
            // Initialize the connection string
            InitConnection();

            try {
                // Get the server version from the external text file
                UpdateServerVersion();
            } catch {
                // If could not get the version, default it to 0.0
                Console.WriteLine("Could not get version.");
                return;
            }

            var TM = new ThreadManager();

            // Start the monitors for the Directions Handler
            DirectionsFinder.EnqueueMonitors(TM);

            // Start the expiration checker for admin logins
            TM.Enqueue(AdminHandler.DeleteExpiredLogins(TM), ThreadPriority.Low);

            // Start the listener for console commands
            TM.Enqueue(HandleConsoleRequests(TM), ThreadPriority.Low);

            // Start the listener for HTTP requests
            TM.Enqueue(WebController.HandleClients(TM));

            // Start the ThreadManager
            TM.Start(1);
        }
Beispiel #2
0
        protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
        {
            var currentThread = TM.CurrentThread;

            if ((int)state >= 0)
            {
                TM.Enqueue(GetDirections(TM, (int)state, query));
            }

            yield return(TM.Sleep(currentThread, 1000));

            while (_done == 0)
            {
                yield return(TM.Sleep(currentThread, 1000));
            }

            bool wait;

            if (query.ContainsKey("wait") && Boolean.TryParse(query["wait"], out wait) && wait)
            {
                while (_done < 100)
                {
                    yield return(TM.Sleep(currentThread, 1000));
                }
            }

            int done = _done;

            if (done < 100)
            {
                yield return(TM.Return(currentThread, new JsonResponse(new DirectionsResponse(done, _id))));
            }
            else
            {
                var directions = new DirectionsResponse(100, _id);
                directions.Result = new Directions(_paths);
                yield return(TM.Return(currentThread, new JsonResponse(directions)));
            }
        }
Beispiel #3
0
        public static IEnumerable <ThreadInfo> HandleClients(ThreadManager TM)
        {
            var currentThread = TM.CurrentThread;

            while (true)
            {
                int          tries    = 1;
                HttpListener listener = null;
                while (true)
                {
                    try
                    {
                        listener = new HttpListener();
                        listener.Prefixes.Add("http://+:5600/");
                        Console.WriteLine("[{0}]\nAttempt #{1} to start HttpListener...", DateTime.Now, tries);
                        listener.Start();
                        Console.WriteLine("HttpListener successfully started.\n");
                        break;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("HttpListener failed to start with exception:\nMessage: {0}\nStack trace:\n{1}\n", ex.Message, ex.StackTrace);
                        tries++;
                    }
                    if (tries > 5)
                    {
                        Console.WriteLine("[{0}]\nMaximum number of attempts reached.  Try again later.\n", DateTime.Now);
                        yield return(TM.Return(currentThread));
                    }
                    else
                    {
                        Console.WriteLine("[{0}]\nWaiting 5 seconds before attemping again...", DateTime.Now);
                        yield return(TM.Sleep(currentThread, 5000));
                    }
                }
                Console.WriteLine("[{0}]\nWaiting for requests...\n", DateTime.Now);
                while (true)
                {
                    yield return(TM.WaitForClient(currentThread, listener));

                    HttpListenerContext context = null;
                    try
                    {
                        context = TM.GetResult <HttpListenerContext>(currentThread);
                        if (context.Request.RawUrl.StartsWith("/admin/authenticate/"))
                        {
                            Console.WriteLine("[{0}]\nHandling request: {1}\n", DateTime.Now, "/admin/authenticate/...");
                        }
                        else
                        {
                            Console.WriteLine("[{0}]\nHandling request: {1}\n", DateTime.Now, context.Request.RawUrl);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("HttpListener threw an exception while waiting for a client:\nMessage: {0}\nStack trace:\n{1}\n\nGoing to restart the HttpListener...", ex.Message, ex.StackTrace);
                        break;
                    }
                    TM.Enqueue(HandleRequest(TM, context));
                }
                listener.Close();
            }
        }
Beispiel #4
0
 public static void EnqueueMonitors(ThreadManager TM)
 {
     TM.Enqueue(_directions.LookForNewIndex(TM), ThreadPriority.Low);
     TM.Enqueue(_directions.CheckForIncreaseSize(TM), ThreadPriority.Low);
 }