/// <summary>
 /// Parses a Delivery Request command from a client.
 /// </summary>
 /// <param name="client">Client the command originated from.</param>
 /// <param name="tokens">The network message.</param>
 private void DeliveryRequest(Client client, string[] tokens)
 {
     try
     {
         int count = 1;
         int originID = Convert.ToInt32(tokens[count++]);
         int destinationID = Convert.ToInt32(tokens[count++]);
         int weight = Convert.ToInt32(tokens[count++]);
         int volume = Convert.ToInt32(tokens[count++]);
         IDictionary<PathType, Delivery> options = deliveryService.GetBestRoutes(client.ID, originID, destinationID, weight, volume);
         if (options.Count <= 0)
             client.SendMessage(NetCodes.BuildNetworkString(NetCodes.SV_DELIVERY_PRICES, NetCodes.PATH_CANCEL));
         else
             client.SendMessage(NetCodes.BuildNetworkString(NetCodes.SV_DELIVERY_PRICES, PathTypeExtensions.BuildOptionsNetString(options)));
     }
     catch (FormatException e)
     {
         SendErrorMessage(client, "Malformed network message.");
     }
     catch (Exception e)
     {
         SendErrorMessage(client, e.Message);
     }
 }
 public static void PassMessage(string message, Client sendingClient)
 {
     sendingClient.SendMessage("Hello!");
 }
        /// <summary>
        /// Parses a Sync State command from a client.
        /// </summary>
        /// <param name="client">Client the command originated from.</param>
        /// <param name="tokens">The network message.</param>
        private void SyncState(Client client, string[] tokens)
        {
            client.SendMessage(NetCodes.BuildNetworkString(NetCodes.SV_STATS_BEGIN, eventService.GetDateOfFirstEvent().ToString()));

            DateTime clientTime = DateTime.Parse(tokens[1]);
            var companies = companyService.GetAll();
            foreach (Company c in companies)
                SendUpdateForSync(client, NetCodes.OBJECT_COMPANY, c.ToNetString());

            var countries = countryService.GetAll();
            foreach (Country l in countries)
                SendUpdateForSync(client, NetCodes.OBJECT_COUNTRY, l.ToNetString());

            var routeNodes = locationService.GetAll();
            foreach (RouteNode n in routeNodes)
                SendUpdateForSync(client, NetCodes.OBJECT_ROUTENODE, n.ToNetString());

            var domesticPrices = priceService.GetAllDomesticPrices();
            foreach (DomesticPrice d in domesticPrices)
                SendUpdateForSync(client, NetCodes.OBJECT_DOMESTIC_PRICE, d.ToNetString());

            var prices = priceService.GetAll();
            foreach (Price p in prices)
                SendUpdateForSync(client, NetCodes.OBJECT_PRICE, p.ToNetString());

            var routes = routeService.GetAll();
            foreach (Route r in routes)
                SendUpdateForSync(client, NetCodes.OBJECT_ROUTE, r.ToNetString());

            client.SendMessage(NetCodes.SV_SYNC_DONE);
        }
 /// <summary>
 /// Parses a Stats Retrieval command from a client.
 /// </summary>
 /// <param name="client">Client the command originated from.</param>
 /// <param name="tokens">The network message.</param>
 private void StatsRequest(Client client, string[] tokens)
 {
     Statistics stats;
     if (tokens[1] == NetCodes.STATS_NOW)
         stats = statisticsService.GetStatisticsFromPoint(DateTime.UtcNow);
     else
     {
         DateTime time = DateTime.Parse(tokens[1]);
         stats = statisticsService.GetStatisticsFromPoint(time);
     }
     client.SendMessage(NetCodes.BuildNetworkString(NetCodes.SV_STATS_ANSWER, stats.ToNetString()));
 }
 private void SendUpdateForSync(Client client, string objectType, string objectDef)
 {
     client.SendMessage(NetCodes.BuildNetworkString(NetCodes.SV_SYNC_UPDATE, DateTime.UtcNow.ToString(), objectType, objectDef));
 }
 private void SendErrorMessage(Client client, string error)
 {
     Logger.WriteLine("Error processing Client ({0}) request: {1}", client.ID, error);
     client.SendMessage(NetCodes.BuildNetworkString(NetCodes.SV_ERROR, error));
 }
 /// <summary>
 /// Parses a Delivery Select command from a client.
 /// </summary>
 /// <param name="client">Client the command originated from.</param>
 /// <param name="tokens">The network message.</param>
 private void DeliverySelect(Client client, string[] tokens)
 {
     int count = 1;
     // TODO Implement the timeout stuff
     if (tokens[count] == NetCodes.PATH_CANCEL)
         return;                 // client cancelled request - TODO nicer handling
     PathType type = PathTypeExtensions.ParseNetString(tokens[count]);
     deliveryService.SelectDeliveryOption(client.ID, type);
     client.SendMessage(NetCodes.SV_DELIVERY_CONFIRMED);
 }