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
        }
        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