示例#1
0
        private string NearestNodeToDeparture(string departure)
        {
            Location departureLoc = GMapsAPI.addressToLocation(departure);

            string   targetNode   = this.NodeName;
            Location tempLocation = this.NodeLocation;
            double   tempDistance;
            double   minDistance = tempLocation.distance(departureLoc);

            //Console.WriteLine("Distance between {0} and {1}({2}) is  {3} km", departure, targetNode, NodeGeoName, minDistance);

            foreach (ServiceNode neighbour in Neighbours)
            {
                tempLocation = neighbour.Location;
                tempDistance = tempLocation.distance(departureLoc);

                //Console.WriteLine("Distance between {0} and {1}({2}) is  {3} km", departure, neighbour.NodeName,neighbour.NodeGeoName, tempDistance);

                // Error due to decimal precision
                if (tempDistance == Double.NaN)
                {
                    tempDistance = 0.0;
                }

                if (tempDistance < minDistance)
                {
                    minDistance = tempDistance;
                    targetNode  = neighbour.NodeName;
                }
            }

            return(targetNode);
        }
示例#2
0
        /// <summary>
        /// Translate an address to a Location object, containing the latitude and
        /// the longitude of the address.
        /// </summary>
        /// <param name="address">The address to be translated</param>
        /// <returns>A Location instance</returns>
        public static Location addressToLocation(string address)
        {
            string[] coords;
            double   latitude;
            double   longitude;
            Location location;

            coords    = GMapsAPI.addressToLatLng(address);
            latitude  = double.Parse(coords[0]);
            longitude = double.Parse(coords[1]);
            location  = new Location(address, latitude, longitude);

            return(location);
        }
示例#3
0
        /// <summary>
        /// Retrieve the coordinates from location names, build the ServiceNode
        /// and ServiceNodeCore objects, set the topology of the net of service nodes.
        /// </summary>
        public static void InitializeNodes(bool fullTopology)
        {
            Console.WriteLine("**** DarPooling Service Nodes ****\n");

            string[] locNames = new string[] { "Venezia", "Milano", "Torino", "Genova", "Pisa", "Roma",
                                               "Napoli", "Bari", "CT Aci Trezza", "CT Nicolosi", "Bologna" };

            // Obtain the Location coordinates from the location name
            Console.Write("Retrieving Coordinates from GMap Server....   ");
            foreach (string locName in locNames)
            {
                Location location = GMapsAPI.addressToLocation(locName);
                nameLoc.Add(locName, location);
            }

            foreach (string locName in locNames)
            {
                //Console.WriteLine("Dtance between {0} and {1} is  {2} km", locNames[4], locName, nameLoc[locNames[4]].distance(nameLoc[locName]));
            }

            Console.WriteLine("Done!");

            // ServiceNode
            ServiceNode veneziaSN  = new ServiceNode("Venezia", locNames[0], nameLoc[locNames[0]]);
            ServiceNode milanoSN   = new ServiceNode("Milano", locNames[1], nameLoc[locNames[1]]);
            ServiceNode torinoSN   = new ServiceNode("Torino", locNames[2], nameLoc[locNames[2]]);
            ServiceNode genovaSN   = new ServiceNode("Genova", locNames[3], nameLoc[locNames[3]]);
            ServiceNode pisaSN     = new ServiceNode("Pisa", locNames[4], nameLoc[locNames[4]]);
            ServiceNode romaSN     = new ServiceNode("Roma", locNames[5], nameLoc[locNames[5]]);
            ServiceNode napoliSN   = new ServiceNode("Napoli", locNames[6], nameLoc[locNames[6]]);
            ServiceNode bariSN     = new ServiceNode("Bari", locNames[7], nameLoc[locNames[7]]);
            ServiceNode cataniaSN  = new ServiceNode("Catania", locNames[8], nameLoc[locNames[8]]);
            ServiceNode catania2SN = new ServiceNode("Catania2", locNames[9], nameLoc[locNames[9]]);
            ServiceNode bolognaSN  = new ServiceNode("Bologna", locNames[10], nameLoc[locNames[10]]);
            // ServiceNodeCore
            ServiceNodeCore venezia  = new ServiceNodeCore(veneziaSN);
            ServiceNodeCore milano   = new ServiceNodeCore(milanoSN);
            ServiceNodeCore torino   = new ServiceNodeCore(torinoSN);
            ServiceNodeCore genova   = new ServiceNodeCore(genovaSN);
            ServiceNodeCore pisa     = new ServiceNodeCore(pisaSN);
            ServiceNodeCore roma     = new ServiceNodeCore(romaSN);
            ServiceNodeCore napoli   = new ServiceNodeCore(napoliSN);
            ServiceNodeCore bari     = new ServiceNodeCore(bariSN);
            ServiceNodeCore catania  = new ServiceNodeCore(cataniaSN);
            ServiceNodeCore catania2 = new ServiceNodeCore(catania2SN);
            ServiceNodeCore bologna  = new ServiceNodeCore(bolognaSN);



            // Set Topology
            if (fullTopology)
            {
                venezia.addNeighbour(milano);
                venezia.addNeighbour(bologna);

                milano.addNeighbour(torino);
                milano.addNeighbour(genova);

                torino.addNeighbour(genova);

                genova.addNeighbour(pisa);

                bologna.addNeighbour(pisa);

                pisa.addNeighbour(roma);
                roma.addNeighbour(napoli);
                napoli.addNeighbour(bari);
                napoli.addNeighbour(catania);
                napoli.addNeighbour(catania2);
                catania.addNeighbour(catania2);
            }
            else
            {
                milano.addNeighbour(roma);
                roma.addNeighbour(catania);
            }

            ServiceNodeCore[] nodes;

            if (fullTopology)
            {
                nodes =
                    new ServiceNodeCore[] { venezia, milano, torino, genova, bologna, pisa, roma, napoli, bari, catania, catania2 };
            }
            else
            {
                nodes =
                    new ServiceNodeCore[] { milano, roma, catania //,venezia, torino, genova, pisa, roma, napoli, bari, catania2, bologna
                };
            }
            sncList.AddRange(nodes);
        }
示例#4
0
        public Result SearchTrip(QueryBuilder queryTrip)
        {
            // Error! Search range cannot be a negative number!
            if (queryTrip.Range < 0)
            {
                SearchTripError error = new SearchTripError();
                error.Comment = "Search range must be a non-negative number!";
                return(error);
            }

            /** Check if the current node is the nearest node to the departure location. */
            string targetNode = NearestNodeToDeparture(queryTrip.DepartureName);

            if (!targetNode.Equals(NodeName))
            {
                Console.WriteLine("Decision: sending SearchTripCommand to : {0}", targetNode);
                ForwardRequiredResult forwardRequest = new ForwardRequiredResult();
                forwardRequest.RequestID   = serviceImpl.generateGUID();
                forwardRequest.Destination = baseForwardAddress + targetNode;

                return(forwardRequest);
            }

            // The Search has no range criteria. We can perform a simple search or
            // a simple forward request.
            if (queryTrip.Range == 0)
            {
                Console.WriteLine("Testing Range 0");
                List <Trip> matchingTrip = GetTripZeroRange(queryTrip);

                SearchTripResult searchResult = new SearchTripResult(matchingTrip);
                searchResult.OriginalQueryID = queryTrip.ID;
                Console.WriteLine("{0} {1} Trip(s) were found in {2}", serviceImpl.LogTimestamp, matchingTrip.Count, NodeName);

                return(searchResult);
            }
            // The client specified a range for search: we need to do a more complex search
            // and potentially a multiple forwarding.
            else
            {
                Location      departureLoc      = GMapsAPI.addressToLocation(queryTrip.DepartureName);
                LocationRange departureLocRange = new LocationRange(departureLoc.Latitude, departureLoc.Longitude, queryTrip.Range);

                List <Trip> matchingTrip = GetTripWithRange(queryTrip, departureLocRange);

                SearchTripResult searchResult = new SearchTripResult(matchingTrip);
                searchResult.OriginalQueryID = queryTrip.ID;
                Console.WriteLine("{0} {1} Trip(s) were found in {2}", serviceImpl.LogTimestamp, matchingTrip.Count, NodeName);

                /** Check if there are other neighbour nodes within the range of search. */
                string[] targetNeighbours = NeighbourNodesInRange(departureLocRange);
                if (targetNeighbours.Length > 0)
                {
                    foreach (string n in targetNeighbours)
                    {
                        Console.WriteLine("{0} is in range.", n);
                    }

                    return(new NullResult());
                }
                else
                {
                    //Console.WriteLine("No neighbour is in range.");
                    return(searchResult);
                }
            }
        }//End SearchTrip
示例#5
0
        public Result InsertTrip(Trip newTrip)
        {
            Result insertionResult;

            /** Check if the current node is the nearest node to the
             * departure location.
             */
            string targetNode = NearestNodeToDeparture(newTrip.DepartureName);

            if (!targetNode.Equals(NodeName))
            {
                //Console.WriteLine("Decision: sending newTripCommand to : {0}", targetNode);
                ForwardRequiredResult forwardRequest = new ForwardRequiredResult();
                forwardRequest.RequestID   = serviceImpl.generateGUID();
                forwardRequest.Destination = baseForwardAddress + targetNode;

                return(forwardRequest);
            }
            else
            {
                Location departureLoc = GMapsAPI.addressToLocation(newTrip.DepartureName);
                Location arrivalLoc   = GMapsAPI.addressToLocation(newTrip.ArrivalName);

                //Save the trip
                tripDatabaseLock.EnterWriteLock();
                try
                {
                    tripDatabase = XDocument.Load(tripDatabasePath);

                    int nextAvailableID = Convert.ToInt32(
                        (from trip in tripDatabase.Descendants("Trip")
                         orderby Convert.ToInt32(trip.Element("ID").Value) descending
                         select trip.Element("ID").Value).FirstOrDefault()) + 1;

                    newTrip.ID = nextAvailableID;

                    XElement newXmlTrip = new XElement("Trip",
                                                       new XElement("ID", newTrip.ID),
                                                       new XElement("Owner", newTrip.Owner),
                                                       new XElement("DepartureName", newTrip.DepartureName.ToLower()),
                                                       new XElement("DepartureLatitude", departureLoc.Latitude),
                                                       new XElement("DepartureLongitude", departureLoc.Longitude),
                                                       new XElement("DepartureDateTime", newTrip.DepartureDateTime),
                                                       new XElement("ArrivalName", newTrip.ArrivalName.ToLower()),
                                                       new XElement("ArrivalLatitude", arrivalLoc.Latitude),
                                                       new XElement("ArrivalLongitude", arrivalLoc.Longitude),
                                                       new XElement("ArrivalDateTime", newTrip.ArrivalDateTime),
                                                       new XElement("Smoke", newTrip.Smoke),
                                                       new XElement("Music", newTrip.Music),
                                                       new XElement("Cost", newTrip.Cost),
                                                       new XElement("FreeSits", newTrip.FreeSits),
                                                       new XElement("Notes", newTrip.Notes),
                                                       new XElement("Modifiable", newTrip.Modifiable)
                                                       );
                    tripDatabase.Element("Trips").Add(newXmlTrip);
                    tripDatabase.Save(tripDatabasePath);

                    Console.WriteLine("{0} Trip saved in {1}", serviceImpl.LogTimestamp, NodeName);
                    insertionResult         = new InsertOkResult();
                    insertionResult.Comment = "The trip has been successfully inserted";
                    return(insertionResult);
                }
                finally
                {
                    tripDatabaseLock.ExitWriteLock();
                }
            } // end else
        }     //End savetrip