Beispiel #1
0
        /// <summary>
        /// Get all the flights from this server and other servers.
        /// </summary>
        /// <param name="relativeTo"> Time at the student's. </param>
        /// <returns> All the flights. </returns>
        public async Task <IList <Flight.Flight> > GetAllFlightsSync(DateTime relativeTo)
        {
            // Get first all local flights.
            Task <IList <Flight.Flight> > localFlights = GetAllFlights(relativeTo);

            /* Create a list contains lists of external flights.
             * Each inner list represents flights from a specifiec server. */
            IList <Task <IList <Flight.Flight> > > externalFlights =
                new List <Task <IList <Flight.Flight> > >();

            IList <KeyValuePair <string, string> > serversUpdates =
                new List <KeyValuePair <string, string> >();

            await foreach (Server server in serversDB.GetAllServers())
            {
                // For each server, ask it for all its flights.
                HTTPClient client = new HTTPClient(server);
                Task <IList <Flight.Flight> > externals = client.GetFlights(
                    relativeTo.ToString("yyyy-MM-ddTHH:mm:ssZ"));
                IList <Flight.Flight> res = await externals;

                if (res == null)
                {
                    continue;
                }

                foreach (Flight.Flight flight in res)
                {
                    /* Change isExternal property of the flight to true,
                     * as it was retrieved from an external server. */
                    flight.IsExternal = true;
                    serversUpdates.Add(new KeyValuePair <string, string>(
                                           flight.FlightID, server.Id));
                }
                externalFlights.Add(externals);
            }

            IList <Flight.Flight> temp = await localFlights;

            /* Join all the lists from the servers and the list of the local flight to
             * one list of flights. */
            foreach (Task <IList <Flight.Flight> > flightsList in externalFlights)
            {
                temp = temp.Concat(await flightsList).ToList();
            }

            foreach (KeyValuePair <string, string> pair in serversUpdates)
            {
                /* Add the id of the flight to the data base. Connect it to its server.
                 * Next time we want the flight plan we can
                 * simply ask it from the relevant server. */
                await flightsServersDB.AddFlightServer(
                    new Servers.FlightServer(pair.Key, pair.Value));
            }

            return(temp);
        }