Ejemplo n.º 1
0
        protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
        {
            var currentThread = TM.CurrentThread;

            if (!(state is List <int>))
            {
                yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.BadRequest)));
            }

            var tags      = (List <int>)state;
            var locations = new Dictionary <int, LocationRank>();
            int order     = 0;

            foreach (int tag in tags)
            {
                yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetTaggedLocationIds",
                                           new SqlParameter("tag", tag)));

                foreach (DataRow row in TM.GetResult <DataTable>(currentThread).Rows)
                {
                    int id = (int)row["location"];

                    if (!locations.ContainsKey(id))
                    {
                        locations[id] = new LocationRank(id, order++);
                    }
                    locations[id].AddHit((int)row["priority"]);
                }
            }

            var response = new LocationIdsResponse(locations.Values.OrderBy(x => x, new LocationRankComparer()).Select(l => l.LocId).ToList());

            yield return(TM.Return(currentThread, new JsonResponse(response)));
        }
Ejemplo n.º 2
0
        public static IEnumerable <ThreadInfo> GetNames(ThreadManager TM, string procedure, Dictionary <string, string> query)
        {
            var currentThread = TM.CurrentThread;

            if (query.ContainsKey("version"))
            {
                double version;
                if (Double.TryParse(query["version"], out version))
                {
                    if (version >= Program.LocationsVersion)
                    {
                        yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.NoContent)));
                    }
                }
            }

            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, procedure));

            var table    = TM.GetResult <DataTable>(currentThread);
            var response = new LocationNamesResponse(Program.LocationsVersion);

            foreach (DataRow row in table.Rows)
            {
                var location = new LocationName(row);
                if (location.Id >= 0)
                {
                    response.Names.Add(location);
                }
            }

            response.Names = LocationsHandler.ApplySearchFilter(query, locName => locName.Name, (locName, name) => locName.Name = name, response.Names);

            yield return(TM.Return(currentThread, new JsonResponse(response)));
        }
Ejemplo n.º 3
0
        public static IEnumerable <ThreadInfo> GetLocations(ThreadManager TM, string procedure, bool hideDescs, Dictionary <string, string> query, params SqlParameter[] parameters)
        {
            var currentThread = TM.CurrentThread;

            if (query.ContainsKey("version"))
            {
                double version;
                if (Double.TryParse(query["version"], out version))
                {
                    if (version >= Program.LocationsVersion)
                    {
                        yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.NoContent)));
                    }
                }
            }

            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, procedure, parameters));

            DataTable table    = TM.GetResult <DataTable>(currentThread);
            var       response = new LocationsResponse(Program.LocationsVersion);

            foreach (DataRow row in table.Rows)
            {
                var location = new Location(row, hideDescs);
                if (location.Id >= 0)
                {
                    if (location.IsMapArea())
                    {
                        yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetMapAreaCorners",
                                                   new SqlParameter("@maparea", location.Id)));

                        DataTable cornerTable = TM.GetResult <DataTable>(currentThread);
                        foreach (DataRow cornerRow in cornerTable.Rows)
                        {
                            location.MapArea.Corners.Add(new LatLong(cornerRow));
                        }
                    }
                    if (location.HasAltNames)
                    {
                        yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetLocationAltNames",
                                                   new SqlParameter("@location", location.Id)));

                        location.AddAltNames(TM.GetResult <DataTable>(currentThread));
                    }
                    if (location.HasLinks && !hideDescs)
                    {
                        yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetLocationLinks",
                                                   new SqlParameter("@location", location.Id)));

                        location.AddLinks(TM.GetResult <DataTable>(currentThread));
                    }
                    response.Locations.Add(location);
                }
            }

            response.Locations = LocationsHandler.ApplySearchFilter(query, location => location.Name, (location, name) => location.Name = name, response.Locations);

            yield return(TM.Return(currentThread, new JsonResponse(response)));
        }
Ejemplo n.º 4
0
        protected override IEnumerable <ThreadInfo> HandleIntPath(ThreadManager TM, int value, object state)
        {
            var currentThread = TM.CurrentThread;

            IntRedirect = DirectionsFinder.GetFinder(value);
            if (IntRedirect == null)
            {
                yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.BadRequest)));
            }
            yield return(TM.Return(currentThread, -1));
        }
Ejemplo n.º 5
0
        protected virtual IEnumerable <ThreadInfo> HandleFloatPath(ThreadManager TM, double value, object state)
        {
            var currentThread = TM.CurrentThread;

            if (FloatRedirect != null)
            {
                yield return(TM.Return(currentThread, value));
            }
            else
            {
                yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.BadRequest)));
            }
        }
Ejemplo n.º 6
0
        protected virtual IEnumerable <ThreadInfo> HandleUnknownPath(ThreadManager TM, string path, object state)
        {
            var currentThread = TM.CurrentThread;

            if (UnknownRedirect != null)
            {
                yield return(TM.Return(currentThread, path));
            }
            else
            {
                yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.BadRequest)));
            }
        }
Ejemplo n.º 7
0
        protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
        {
            var currentThread = TM.CurrentThread;

            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetLocationDesc", new SqlParameter("@location", (int)state)));

            var table = TM.GetResult <DataTable>(currentThread);

            foreach (DataRow row in table.Rows)
            {
                yield return(TM.Return(currentThread, new JsonResponse(new LocationDescResponse(row))));
            }
            yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.BadRequest)));
        }
Ejemplo n.º 8
0
        protected virtual IEnumerable <ThreadInfo> HandleIntPath(ThreadManager TM, int value, object state)
        {
            var currentThread = TM.CurrentThread;

            if (IntRedirect != null)
            {
                yield return(TM.Return(currentThread, value));
            }
            else
            {
                yield return(TM.Await(currentThread, HandleFloatPath(TM, value, state)));

                yield return(TM.Return(currentThread));
            }
        }
Ejemplo n.º 9
0
        protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
        {
            var currentThread = TM.CurrentThread;

            yield return(TM.Return(currentThread, new JsonResponse(new DirectionsResponse(100, 0)
            {
                Result = new Directions(new List <DirectionPath>()
                {
                    new DirectionPath(39.4830085089037, -87.3240387806918, "Go down the stairs", false, "DS", 0, null, false),
                    new DirectionPath(39.4830037513645, -87.3240594201352, null, false, null, 0, null, false),
                    new DirectionPath(39.4829310360043, -87.3240312773421, "Turn left in the hallway", false, "L2", 0, null, false),
                    new DirectionPath(39.4829595714821, -87.3239144478958, "Turn right and head outside", false, "EX", 0, null, false),
                    new DirectionPath(39.4829201855182, -87.3238995842144, null, false, null, 0, null, true),
                    new DirectionPath(39.4828827044101, -87.32387560499762, "Go around the circle", false, "FP", 0, null, true),
                    new DirectionPath(39.48269224869126, -87.32380184424972, null, false, null, 0, null, true),
                    new DirectionPath(39.482547336381884, -87.32394534243201, null, false, null, 0, null, true),
                    new DirectionPath(39.48244900285711, -87.32391852034186, null, false, null, 0, null, true),
                    new DirectionPath(39.48243968704229, -87.32385817063903, null, false, null, 0, null, true),
                    new DirectionPath(39.482352739379756, -87.32380720866774, "Cross the street", false, "CS", 0, null, true),
                    new DirectionPath(39.482256475766555, -87.32372137797927, "Follow the path", false, "FP", 0, null, true),
                    new DirectionPath(39.48215193126149, -87.32370528472518, null, false, null, 0, null, true),
                    new DirectionPath(39.48205670246667, -87.32352691782569, null, false, null, 0, null, true),
                    new DirectionPath(39.48196354373688, -87.3234759558544, null, false, null, 0, null, true),
                    new DirectionPath(39.48196871922512, -87.32338878406142, null, false, null, 0, null, true),
                    new DirectionPath(39.48189936765145, -87.32332575214957, "Travel around the Flame of the Millennium", false, "FP", 0, null, true),
                    new DirectionPath(39.48189315705929, -87.3232037116394, null, false, null, 0, null, true),
                    new DirectionPath(39.482012193312364, -87.32314202083205, null, false, null, 0, null, true),
                    new DirectionPath(39.481946982172865, -87.32293683184241, "Take the right path", false, "R1", 0, null, true),
                    new DirectionPath(39.48197492981158, -87.32283088458632, "Cross the street", false, "CS", 0, null, true),
                    new DirectionPath(39.48196664903091, -87.3227370072708, null, false, null, 0, null, true),
                    new DirectionPath(39.48198942117538, -87.32248085630988, null, true, null, 0, 0, true),
                })
            })));
        }
Ejemplo n.º 10
0
        protected override IEnumerable <ThreadInfo> HandleIntPath(ThreadManager TM, int value, object state)
        {
            var currentThread = TM.CurrentThread;

            IntRedirect = (DirectionsFinder)state;
            yield return(TM.Return(currentThread, value));
        }
Ejemplo n.º 11
0
        protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
        {
            var    currentThread = TM.CurrentThread;
            double version       = (double)state;

            Program.WriteServerVersion(version);
            yield return(TM.Return(currentThread, new JsonResponse(new MessageResponse("Version update successful."))));
        }
Ejemplo n.º 12
0
        protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
        {
            var currentThread  = TM.CurrentThread;
            var storedProcData = (SqlStoredProcData)state;
            var parameters     = new SqlParameter[query.Count];
            int i = 0;

            foreach (var kvp in query)
            {
                parameters[i] = new SqlParameter(kvp.Key, kvp.Value);
                i++;
            }
            yield return(TM.MakeDbCall(currentThread, Program.GetConnectionString(storedProcData.LoginData.Username, storedProcData.LoginData.Password), storedProcData.StoredProcName, parameters));

            bool success  = true;
            var  response = new StoredProcedureResponse();

            try {
                var table = TM.GetResult <DataTable>(currentThread);
                foreach (DataColumn column in table.Columns)
                {
                    response.Columns.Add(column.ColumnName);
                }
                int columns = response.Columns.Count;
                foreach (DataRow row in table.Rows)
                {
                    var rowList = new List <string>();
                    for (int j = 0; j < columns; j++)
                    {
                        rowList.Add(row.IsNull(j) ? null : row[j].ToString());
                    }
                    response.Table.Add(rowList);
                }
            } catch {
                success = false;
            }

            if (success)
            {
                yield return(TM.Return(currentThread, new JsonResponse(response)));
            }
            else
            {
                yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.BadRequest)));
            }
        }
Ejemplo n.º 13
0
        protected override IEnumerable <ThreadInfo> HandleIntPath(ThreadManager TM, int value, object state)
        {
            var currentThread = TM.CurrentThread;

            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetLocationDepartNode",
                                       new SqlParameter("@location", value)));

            var table = TM.GetResult <DataTable>(currentThread);

            if (table.Rows.Count > 0)
            {
                yield return(TM.Return(currentThread, new DirectionsFinder(table.Rows[0])));
            }
            else
            {
                yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.BadRequest)));
            }
        }
Ejemplo n.º 14
0
        protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
        {
            var currentThread = TM.CurrentThread;

            if (query.ContainsKey("version"))
            {
                double version;
                if (Double.TryParse(query["version"], out version))
                {
                    if (version >= Program.TagsVersion)
                    {
                        yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.NoContent)));
                    }
                }
            }

            var response   = new TagsResponse(Program.TagsVersion);
            var categories = new Dictionary <string, TagCategory>();

            categories.Add("", response.Root);

            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetTagCategories"));

            using (var table = TM.GetResult <DataTable>(currentThread)) {
                foreach (DataRow row in table.Rows)
                {
                    var category = new TagCategory(row);
                    categories[category.Parent ?? ""].Children.Add(category);
                    categories.Add(category.Name, category);
                }
            }

            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetTags"));

            using (var table = TM.GetResult <DataTable>(currentThread)) {
                foreach (DataRow row in table.Rows)
                {
                    var tag = new Tag(row);
                    categories[(string)row["category"]].Tags.Add(tag);
                }
            }

            yield return(TM.Return(currentThread, new JsonResponse(response)));
        }
Ejemplo n.º 15
0
        protected override IEnumerable <ThreadInfo> HandleIntPath(ThreadManager TM, int value, object state)
        {
            var currentThread = TM.CurrentThread;

            var tags = state is List <int>?(List <int>)state : new List <int>();

            tags.Add(value);

            yield return(TM.Return(currentThread, tags));
        }
Ejemplo n.º 16
0
        private static IEnumerable <ThreadInfo> HandleRequest(ThreadManager TM, HttpListenerContext context)
        {
            var currentThread = TM.CurrentThread;

            JsonResponse result = null;

            var path = Uri.UnescapeDataString(context.Request.Url.LocalPath).Split('/').SkipWhile(String.IsNullOrEmpty).TakeWhile(s => !String.IsNullOrEmpty(s));
            Dictionary <string, string> query = new Dictionary <string, string>();

            if (!String.IsNullOrEmpty(context.Request.Url.Query))
            {
                var querySplit = Uri.UnescapeDataString(context.Request.Url.Query).Split('?', '&', ';').Skip(1);
                foreach (string field in querySplit)
                {
                    int equalsPos = field.IndexOf('=');
                    if (equalsPos == -1 || equalsPos == 0 || equalsPos == field.Length - 1)
                    {
                        result = new JsonResponse(HttpStatusCode.BadRequest);
                        break;
                    }
                    query.Add(field.Substring(0, equalsPos), field.Substring(equalsPos + 1, field.Length - equalsPos - 1));
                }
            }

            if (result == null)
            {
                yield return(TM.Await(currentThread, _handler.HandlePath(TM, path, query, new object())));

                try
                {
                    result = TM.GetResult <JsonResponse>(currentThread);
                }
                catch (Exception)
                {
                    result = new JsonResponse(HttpStatusCode.InternalServerError);
                }
            }

            var encoding = new ASCIIEncoding();

            byte[] b;
            if (result.Json != null)
            {
                b = encoding.GetBytes(result.Json.Serialize());
            }
            else
            {
                b = encoding.GetBytes(result.Message);
            }
            context.Response.OutputStream.Write(b, 0, b.Length);
            context.Response.StatusCode = (int)result.StatusCode;
            context.Response.Close();
            yield return(TM.Return(currentThread, null));
        }
Ejemplo n.º 17
0
        public IEnumerable <ThreadInfo> HandlePath(ThreadManager TM, IEnumerable <string> path, Dictionary <string, string> query, object state)
        {
            var currentThread = TM.CurrentThread;

            if (path.Any())
            {
                if (Redirects.ContainsKey(path.First()))
                {
                    yield return(TM.Await(currentThread, Redirects[path.First()].HandlePath(TM, path.Skip(1), query, state)));
                }
                else
                {
                    int intValue;
                    if (Int32.TryParse(path.First(), out intValue))
                    {
                        yield return(TM.Await(currentThread, HandleIntPath(TM, intValue, state)));

                        if (!(TM.GetResultNoException(currentThread) is JsonResponse))
                        {
                            yield return(TM.Await(currentThread, IntRedirect.HandlePath(TM, path.Skip(1), query, TM.GetResult(currentThread))));
                        }
                    }
                    else
                    {
                        double floatValue;
                        if (Double.TryParse(path.First(), out floatValue))
                        {
                            yield return(TM.Await(currentThread, HandleFloatPath(TM, floatValue, state)));

                            if (!(TM.GetResultNoException(currentThread) is JsonResponse))
                            {
                                yield return(TM.Await(currentThread, FloatRedirect.HandlePath(TM, path.Skip(1), query, TM.GetResult(currentThread))));
                            }
                        }
                        else
                        {
                            yield return(TM.Await(currentThread, HandleUnknownPath(TM, path.First(), state)));

                            if (!(TM.GetResultNoException(currentThread) is JsonResponse))
                            {
                                yield return(TM.Await(currentThread, UnknownRedirect.HandlePath(TM, path.Skip(1), query, TM.GetResult(currentThread))));
                            }
                        }
                    }
                }
            }
            else
            {
                yield return(TM.Await(currentThread, HandleNoPath(TM, query, state)));
            }

            yield return(TM.Return(currentThread));
        }
Ejemplo n.º 18
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)));
            }
        }
Ejemplo n.º 19
0
        protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
        {
            var currentThread = TM.CurrentThread;
            var loginData     = (SqlLoginData)state;

            yield return(TM.MakeDbCall(currentThread, Program.GetConnectionString(loginData.Username, loginData.Password), "spTestConnection"));

            bool success = true;

            try {
                var result = TM.GetResult <DataTable>(currentThread);
                if (result.Rows.Count != 1 || (int)result.Rows[0][0] != 56)
                {
                    success = false;
                }
            } catch {
                success = false;
            }

            if (success)
            {
                var alreadyLoggedIn = AdminHandler.Logins.Where(kvp => kvp.Value.Username == loginData.Username);
                if (alreadyLoggedIn.Any())
                {
                    AdminHandler.Logins.Remove(alreadyLoggedIn.First().Key);
                }
                Guid id = Guid.NewGuid();
                while (AdminHandler.Logins.ContainsKey(id))
                {
                    id = Guid.NewGuid();
                }
                AdminHandler.Logins[id] = loginData;
                yield return(TM.Return(currentThread, new JsonResponse(new AuthenticationResponse(loginData.Expiration, id))));
            }
            else
            {
                yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.BadRequest)));
            }
        }
Ejemplo n.º 20
0
        protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
        {
            var currentThread = TM.CurrentThread;
            var result        = new PathDataResponse(Program.LocationsVersion);

            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetPaths"));

            foreach (DataRow row in TM.GetResult <DataTable>(currentThread).Rows)
            {
                result.Paths.Add(new Path(row));
            }
            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetNodes"));

            foreach (DataRow row in TM.GetResult <DataTable>(currentThread).Rows)
            {
                result.Nodes.Add(new Node(row));
            }
            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetPartitions"));

            foreach (DataRow row in TM.GetResult <DataTable>(currentThread).Rows)
            {
                result.Partitions.Add(new Partition(row));
            }
            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetPartitions"));

            foreach (DataRow row in TM.GetResult <DataTable>(currentThread).Rows)
            {
                result.Partitions.Add(new Partition(row));
            }
            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetDirectionMessages"));

            foreach (DataRow row in TM.GetResult <DataTable>(currentThread).Rows)
            {
                result.Messages.Add(new DirectionMessage(row));
            }
            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetDirections"));

            foreach (DataRow row in TM.GetResult <DataTable>(currentThread).Rows)
            {
                var direction = new Direction(row);
                yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetDirectionPaths", new SqlParameter("direction", direction.Id)));

                foreach (DataRow pathRow in TM.GetResult <DataTable>(currentThread).Rows)
                {
                    direction.Paths.Add((int)pathRow["path"]);
                }
                result.Directions.Add(direction);
            }

            yield return(TM.Return(currentThread, new JsonResponse(result)));
        }
Ejemplo n.º 21
0
        protected override IEnumerable <ThreadInfo> HandleUnknownPath(ThreadManager TM, string path, object state)
        {
            var  currentThread = TM.CurrentThread;
            bool success       = true;
            Guid id            = new Guid();

            try {
                id = new Guid((string)path);
                if (!AdminHandler.Logins.ContainsKey(id) || Logins[id].Expiration < DateTime.Now)
                {
                    success = false;
                }
            } catch {
                success = false;
            }
            if (success)
            {
                yield return(TM.Return(currentThread, Logins[id]));
            }
            else
            {
                yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.BadRequest)));
            }
        }
Ejemplo n.º 22
0
        private IEnumerable <ThreadInfo> GetPartitionPaths(ThreadManager TM, Stack <PartitionPass> partitions, Dictionary <int, SortedSet <Node> > goalNodes)
        {
            var currentThread = TM.CurrentThread;

            var currentNode = partitions.Peek().Target;
            var paths       = new List <List <PartitionPass> >();

            // Travel to other partitions, and get the paths through them
            yield return(TM.Await(currentThread, GetNodePartitions(TM, currentNode, "spGetNodePartitions")));

            var nodePartitions = TM.GetResult <SortedSet <int> >(currentThread).Except(partitions.Select((PartitionPass pp) => pp.Partition));

            foreach (int partition in nodePartitions)
            {
                // See if any goal nodes are in this partition
                if (goalNodes.ContainsKey(partition))
                {
                    foreach (var goalNode in goalNodes[partition])
                    {
                        partitions.Push(new PartitionPass(partition, goalNode, _settings.WeightedDist(currentNode, goalNode)));
                        var path = partitions.Reverse().Skip(1).ToList();
                        paths.Add(path);
                        partitions.Pop();
                    }
                }

                // Go to all the boundaries of this partition
                yield return(TM.Await(currentThread, GetPartitionBoundaries(TM, partition)));

                var targetNodes = TM.GetResult <SortedSet <Node> >(currentThread);
                if (goalNodes.ContainsKey(partition))
                {
                    targetNodes.ExceptWith(goalNodes[partition]);
                }
                targetNodes.Remove(currentNode);
                foreach (var targetNode in targetNodes)
                {
                    partitions.Push(new PartitionPass(partition, targetNode, _settings.WeightedDist(currentNode, targetNode)));
                    yield return(TM.Await(currentThread, GetPartitionPaths(TM, partitions, goalNodes)));

                    paths.AddRange(TM.GetResult <List <List <PartitionPass> > >(currentThread));
                    partitions.Pop();
                }
            }

            yield return(TM.Return(currentThread, paths));
        }
Ejemplo n.º 23
0
        private IEnumerable <ThreadInfo> GetPartitionBoundaries(ThreadManager TM, int partition)
        {
            var currentThread = TM.CurrentThread;

            var nodes = new SortedSet <Node>(new NodeComparer());

            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetPartitionBoundaries", new SqlParameter("@partition", partition)));

            using (var boundariesTable = TM.GetResult <DataTable>(currentThread)) {
                foreach (DataRow boundaryRow in boundariesTable.Rows)
                {
                    nodes.Add(new Node(boundaryRow));
                }
            }

            yield return(TM.Return(currentThread, nodes));
        }
Ejemplo n.º 24
0
        private IEnumerable <ThreadInfo> GetNodePartitions(ThreadManager TM, Node node, string procedure)
        {
            var currentThread = TM.CurrentThread;

            var partitions = new SortedSet <int>();

            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, procedure, new SqlParameter("@node", node.Id)));

            using (var partitionsTable = TM.GetResult <DataTable>(currentThread)) {
                foreach (DataRow partitionRow in partitionsTable.Rows)
                {
                    partitions.Add((int)partitionRow["partition"]);
                }
            }

            yield return(TM.Return(currentThread, partitions));
        }
Ejemplo n.º 25
0
        protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
        {
            var currentThread = TM.CurrentThread;

            yield return(TM.Return(currentThread, new JsonResponse(new DirectionsResponse(100, 0)
            {
                Result = new Directions(
                    new List <DirectionPath>()
                {
                    new DirectionPath(39.4828627941384, -87.324008279888, "Turn right in the hallway", false, "R2", 0, null, false),
                    new DirectionPath(39.4828306460166, -87.3241490677771, null, false, null, 0, null, false),
                    new DirectionPath(39.4828620904229, -87.3241637888807, null, false, null, 0, null, false),
                    new DirectionPath(39.4828494067565, -87.3242174834412, null, false, null, 0, null, false),

                    new DirectionPath(39.4828290215239, -87.32431169557, "Turn right", false, "R2", 0, null, false),
                    new DirectionPath(39.4828717938695, -87.3243267616227, null, true, null, 0, null, false),
                    new DirectionPath(39.4828290215239, -87.32431169557, "Turn left", false, "L2", 0, 0, false),

                    new DirectionPath(39.4828494067565, -87.3242174834412, null, false, null, 0, null, false),
                    new DirectionPath(39.4828620904229, -87.3241637888807, null, false, null, 0, null, false),
                    new DirectionPath(39.4828306460166, -87.3241490677771, null, false, null, 0, null, false),
                    new DirectionPath(39.4828627941384, -87.324008279888, null, false, null, 0, null, false),
                    new DirectionPath(39.4828963086285, -87.3238687475089, null, false, null, 0, null, false),
                    new DirectionPath(39.4829231475358, -87.3238794992205, null, false, null, 0, null, false),
                    new DirectionPath(39.4829323644, -87.3238428275053, null, false, null, 0, null, false),
                    new DirectionPath(39.482976927114, -87.3238606794038, null, false, null, 0, null, false),
                    new DirectionPath(39.4829813587565, -87.3238419186537, null, false, null, 0, null, false),

                    new DirectionPath(39.4829698321545, -87.3238144421359, null, false, null, 0, null, false),
                    new DirectionPath(39.4830197430914, -87.3236155710439, null, false, null, 0, null, false),
                    new DirectionPath(39.483097836962, -87.3236405966255, "Go up the stairs", false, "US", 0, null, false),
                    new DirectionPath(39.4831005618263, -87.3236292285931, null, false, null, 0, null, false),
                    new DirectionPath(39.4837052580385, -87.3238335793924, "Turn right", false, "R2", 0, null, false),
                    new DirectionPath(39.483714772609, -87.3237985626965, null, true, null, 0, 0, false),
                    new DirectionPath(39.4837052580385, -87.3238335793924, null, false, null, 0, null, false),
                    new DirectionPath(39.4836460794462, -87.3240577916873, "Go down all the stairs", true, "DS", 0, 0, false),
                    new DirectionPath(39.483619680332296, -87.32420685781096, "Go outside", false, "EX", 0, null, false),
                    new DirectionPath(39.4835741371045, -87.32441607011413, "Continue straight", false, "GS", 0, null, true),
                    new DirectionPath(39.48338471835569, -87.32457566155051, "Make a slight left", false, "L1", 0, null, true),
                    new DirectionPath(39.483353735847345, -87.32472215518095, "Enter the building", false, "EN", 0, null, true),
                    new DirectionPath(39.48339203368583, -87.324730201808, null, true, null, 0, 0, false),
                })
            })));
        }
Ejemplo n.º 26
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));
        }
Ejemplo n.º 27
0
        protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
        {
            var currentThread = TM.CurrentThread;

            yield return(TM.Return(currentThread, new JsonResponse(new VersionResponse())));
        }
Ejemplo n.º 28
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();
            }
        }
Ejemplo n.º 29
0
        private IEnumerable <ThreadInfo> GetDirectionMessages(ThreadManager TM)
        {
            var currentThread = TM.CurrentThread;

            for (int i = 0; i < _paths.Count; i++)
            {
                _messageFindingDone = (double)(i * i) / (double)(_paths.Count * _paths.Count);
                yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetDirection",
                                           new SqlParameter("@startpath", _paths[i].Id)));

                using (var directionTable = TM.GetResult <DataTable>(currentThread)) {
                    foreach (DataRow directionRow in directionTable.Rows)
                    {
                        yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetDirectionPaths",
                                                   new SqlParameter("@direction", (int)directionRow["id"])));

                        using (var pathTable = TM.GetResult <DataTable>(currentThread)) {
                            bool match = true;
                            int  dir   = 0;
                            int  j     = i;
                            foreach (DataRow pathRow in pathTable.Rows)
                            {
                                if (dir != 0)
                                {
                                    if (j < 0 || j >= _paths.Count || (int)pathRow["path"] != _paths[j].Id)
                                    {
                                        match = false;
                                        break;
                                    }
                                    j += dir;
                                }
                                else
                                {
                                    if (i > 0 && (int)pathRow["path"] == _paths[i - 1].Id)
                                    {
                                        dir = -1;
                                        j   = i - 2;
                                    }
                                    else if (i < _paths.Count - 1 && (int)pathRow["path"] == _paths[i + 1].Id)
                                    {
                                        dir = 1;
                                        j   = i + 2;
                                    }
                                    else
                                    {
                                        match = false;
                                        break;
                                    }
                                }
                            }
                            if (match)
                            {
                                // TODO: Deal with within's
                                bool forward = dir == 0 ? _paths[i].Forward : dir > 0;
                                int  index   = Math.Min(j - dir, i) + (int)directionRow["nodeoffset"];
                                if (forward)
                                {
                                    if (!directionRow.IsNull("message1"))
                                    {
                                        _paths[index].Dir    = (string)directionRow["message1"];
                                        _paths[index].Action = (string)directionRow["action1"];
                                    }
                                }
                                else
                                {
                                    if (!directionRow.IsNull("message1"))
                                    {
                                        _paths[index].Dir    = (string)directionRow["message2"];
                                        _paths[index].Action = (string)directionRow["action2"];
                                    }
                                }
                            }
                        }
                    }
                }
            }

            yield return(TM.Return(currentThread));
        }
Ejemplo n.º 30
0
        private IEnumerable <ThreadInfo> GetShortestPath(ThreadManager TM, int to)
        {
            var currentThread = TM.CurrentThread;

            // See if the start node is already in the goal location
            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetLocationNodes", new SqlParameter("@location", to)));

            using (var table = TM.GetResult <DataTable>(currentThread)) {
                if (table.Rows.Count == 0)
                {
                    // Error - no nodes in goal location
                    yield return(TM.Return(currentThread));
                }

                foreach (DataRow row in table.Rows)
                {
                    if (new Node(row).Id == _start.Id)
                    {
                        // Already at goal
                        yield return(TM.Return(currentThread));
                    }
                }
            }

            // Get the nodes on the boundary of the goal location
            var goalNodes = new Dictionary <int, SortedSet <Node> >();

            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetLocationBoundaryNodes", new SqlParameter("@location", to)));

            using (var table = TM.GetResult <DataTable>(currentThread)) {
                if (table.Rows.Count == 0)
                {
                    // Error - no nodes on goal location's boundary
                    yield return(TM.Return(currentThread));
                }

                foreach (DataRow row in table.Rows)
                {
                    var node = new Node(row);
                    if (!goalNodes.ContainsKey(node.Partition.Value))
                    {
                        goalNodes[node.Partition.Value] = new SortedSet <Node>(new NodeComparer());
                    }
                    goalNodes[node.Partition.Value].Add(node);
                }
            }

            // Get the possible partition paths
            Stack <PartitionPass> partitions = new Stack <PartitionPass>();

            partitions.Push(new PartitionPass(-1, _start, 0));
            yield return(TM.Await(currentThread, GetPartitionPaths(TM, partitions, goalNodes)));

            var passes = TM.GetResult <List <List <PartitionPass> > >(currentThread);

            SortedSet <TargetPath>         queue         = new SortedSet <TargetPath>(new TargetPathComparer());
            Dictionary <int, ShortestPath> shortestPaths = new Dictionary <int, ShortestPath>();

            shortestPaths[_start.Id] = new ShortestPath();
            foreach (var pass in passes)
            {
                queue.Add(new TargetPath(_start, 0, pass, queue, _settings));
            }

            while (true)
            {
                var currentPath = queue.First();

                _pathFindingDone = Math.Max(_pathFindingDone, currentPath.DistSoFar * currentPath.DistSoFar / (currentPath.BestDist * currentPath.BestDist));

                if (goalNodes.Any(kvp => kvp.Value.Contains(currentPath.CurrentNode)))
                {
                    var currentNode = currentPath.CurrentNode;
                    var prevNode    = shortestPaths[currentNode.Id].PrevNode;
                    _paths.Add(new DirectionPath(currentNode.Lat, currentNode.Lon, null, true, null, currentNode.Altitude, currentNode.Location, currentNode.Outside, shortestPaths[currentNode.Id].Id, shortestPaths[currentNode.Id].Forward));
                    while (currentNode.Id != _start.Id)
                    {
                        _paths.Insert(0, new DirectionPath(prevNode.Lat, prevNode.Lon, null, false, null, prevNode.Altitude, prevNode.Location, prevNode.Outside, shortestPaths[currentNode.Id].Id, shortestPaths[currentNode.Id].Forward));
                        currentNode = prevNode;
                        prevNode    = shortestPaths[currentNode.Id].PrevNode;
                    }
                    _paths.First().Flag = true;
                    yield return(TM.Return(currentThread));
                }

                yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetPathsFromNode",
                                           new SqlParameter("@node", currentPath.CurrentNode.Id),
                                           new SqlParameter("@part", currentPath.PartitionsLeft.First().Partition)));

                using (var table = TM.GetResult <DataTable>(currentThread)) {
                    foreach (DataRow row in table.Rows)
                    {
                        var    path     = new PathToNode(row, currentPath.CurrentNode.Id, currentPath.PartitionsLeft.First().Partition);
                        double pathDist = _settings.WeightedDist(currentPath.CurrentNode, path.Node);

                        if (shortestPaths.ContainsKey(path.Node.Id))
                        {
                            if (shortestPaths[path.Node.Id].TotalDist > currentPath.DistSoFar + pathDist)
                            {
                                shortestPaths[path.Node.Id].SetPrevNode(currentPath.CurrentNode, pathDist, path.Path.Id, path.Forward, shortestPaths);
                            }
                            else if (shortestPaths[path.Node.Id].TotalDist < currentPath.DistSoFar + pathDist)
                            {
                                continue;
                            }
                            // TODO
                        }
                        else
                        {
                            shortestPaths[path.Node.Id] = new ShortestPath(currentPath.CurrentNode, pathDist, path.Path.Id, path.Forward, shortestPaths);
                            // TODO
                        }

                        if (path.Node.Id == currentPath.PartitionsLeft.First().Target.Id)
                        {
                            queue.Add(new TargetPath(path.Node, shortestPaths[path.Node.Id].TotalDist, currentPath.PartitionsLeft.Skip(1).ToList(), queue, _settings));
                        }
                        else
                        {
                            queue.Add(new TargetPath(path.Node, shortestPaths[path.Node.Id].TotalDist, currentPath.PartitionsLeft, queue, _settings));
                        }
                    }
                }

                queue.Remove(currentPath);
            }
        }