Exemplo n.º 1
0
        /// <summary>
        /// Processes the request async.
        /// </summary>
        public async override Task ProcessAsync()
        {
            try
            {
                RouterRequest routerReq = await Receive <RouterRequest>();

                Logging.Log($"Received router request from { ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString() }. Data: { routerReq.ToString() }");

                RouterResponse routerRes = null;

                await Task.Run(() =>
                {
                    using (var routerProcessing = new Interop.RouterManaged(DataFeed.Full, routerReq))
                    {
                        routerProcessing.ObtainJourneys();
                        routerRes = routerProcessing.ShowJourneys();
                    }
                });

                Send(routerRes);

                Logging.Log($"Router response to { ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString() } was successfully sent.");
            }
            catch (Exception ex)
            {
                Logging.Log($"Router request from { ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString() } could not be processed. { Logging.LogException(ex) }");
            }
            Dispose();
        }
Exemplo n.º 2
0
        public static async Task <RouterResponse> SendRouterRequestAsync(RouterRequest routerRequest, bool forceCache = false)
        {
            RouterResponse routerResponse = null;

            if (DataFeedDesktop.OfflineMode)
            {
                await Task.Run(() =>
                {
                    using (var routerProcessing = new Interop.RouterManaged(DataFeedDesktop.Full, routerRequest))
                    {
                        routerProcessing.ObtainJourneys();
                        routerResponse = routerProcessing.ShowJourneys();
                    }
                });
            }

            else
            {
                using (var routerProcessing = new RouterProcessing())
                {
                    var cached = JourneyCached.Select(routerRequest.SourceStationID, routerRequest.TargetStationID);

                    if (cached == null || (cached.ShouldBeUpdated || forceCache))
                    {
                        try
                        {
                            if (!await CheckBasicDataValidity())
                            {
                                var results = cached?.FindResultsSatisfyingRequest(routerRequest);
                                return(results?.Journeys.Count == 0 ? null : results);
                            }

                            // Process the request immediately so the user does not have to wait until the caching is completed.

                            routerResponse = await routerProcessing.ProcessAsync(routerRequest, routerRequest.Count == -1?int.MaxValue : Settings.TimeoutDuration);

                            // Then update the cache.

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                            if (cached != null && cached.ShouldBeUpdated && routerRequest.Count != -1)
                            {
                                Task.Run(async() => cached.UpdateCache(await routerProcessing.ProcessAsync(cached.ConstructNewRequest(), int.MaxValue)));
                            }
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                        }
                        catch (System.Net.WebException)
                        {
                            MessageBox.Show(Settings.Localization.UnreachableHost, Settings.Localization.Offline, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }

                    else
                    {
                        routerResponse = cached?.FindResultsSatisfyingRequest(routerRequest);
                        if (routerResponse?.Journeys.Count == 0)
                        {
                            routerResponse = await routerProcessing.ProcessAsync(routerRequest, Settings.TimeoutDuration);
                        }
                    }
                }
            }

            return(routerResponse);
        }