Beispiel #1
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 #2
0
        /// <summary>
        /// Checks to see if the array needs to increase in size
        /// </summary>
        /// <param name="TM">ThreadManager</param>
        /// <returns>No return</returns>
        public IEnumerable <ThreadInfo> CheckForIncreaseSize(ThreadManager TM)
        {
            var currentThread = TM.CurrentThread;

            while (true)
            {
                yield return(TM.Sleep(currentThread, 60000));

                if (_items > _size / 2)
                {
                    IncreaseSize();
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Moves the index pointer to an empty slot
        /// </summary>
        /// <param name="TM">ThreadManager</param>
        /// <returns>No return</returns>
        public IEnumerable <ThreadInfo> LookForNewIndex(ThreadManager TM)
        {
            var currentThread = TM.CurrentThread;

            while (true)
            {
                yield return(TM.Sleep(currentThread, 1000));

                if (_array[_index] != null)
                {
                    _index = (_index + 1) % _size;
                }
            }
        }
Beispiel #4
0
        public static IEnumerable <ThreadInfo> DeleteExpiredLogins(ThreadManager TM)
        {
            var currentThread = TM.CurrentThread;

            while (true)
            {
                // Sleep for an hour
                yield return(TM.Sleep(currentThread, 3600000));

                var expired = Logins.Where(kvp => kvp.Value.Expiration < DateTime.Now);
                foreach (var kvp in expired)
                {
                    Logins.Remove(kvp.Key);
                }
            }
        }
Beispiel #5
0
        public IEnumerable <ThreadInfo> GetDirections(ThreadManager TM, int to, Dictionary <string, string> query)
        {
            var currentThread = TM.CurrentThread;

            _settings = new DirectionsSettings(query);

            yield return(TM.Await(currentThread, GetShortestPath(TM, to)));

            _pathFindingDone = 1;

            yield return(TM.Await(currentThread, GetDirectionMessages(TM)));

            _messageFindingDone = 1;
            yield return(TM.Sleep(currentThread, 60000));

            _directions.Remove(_id);

            yield return(TM.Return(currentThread));
        }
Beispiel #6
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();
            }
        }