コード例 #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();
        }
コード例 #2
0
        public void RoutesTest()
        {
            var container       = CreateEmptyContainer();
            var componentRouter = container.Resolve <ComponentRouter>();

            var component = componentRouter.Resolve(RouterRequest.FromQuery("RoutesTestNestedComponent > NestedComponent"));

            Assert.IsNotNull(component);
            Assert.IsInstanceOfType(component, typeof(RoutesTestNestedComponent));
        }
コード例 #3
0
        public static T ResolveContract <T>(this ComponentRouter componentRouter, string query)
            where T : class
        {
            var component = componentRouter.Resolve(RouterRequest.FromQuery(query)) as T;

            if (component == null)
            {
                throw new Exception($"Component must implement '{typeof(T).Name}' contract");
            }
            return(component);
        }
コード例 #4
0
        public void TestRouterRequestConstructor()
        {
            var testPath = "test";
            var testBody = 666;
            var testReq  = new RouterRequest <int>(testPath, testBody);

            var actual    = testReq.Path;
            var actualTwo = testReq.Body;

            Assert.AreEqual(testPath, actual);
            Assert.AreEqual(testBody, actualTwo);
        }
コード例 #5
0
        public static async Task <RouterResponse> SendRouterRequestAsync(RouterRequest routerRequest, bool forceCache = false)
        {
            RouterResponse routerResponse = null;

            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 && CanBeCached)
                        {
                            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)
                    {
                        PlatformDependentSettings.ShowMessage(Settings.Localization.UnreachableHost);
                    }
                }

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

            return(routerResponse);
        }
コード例 #6
0
        public static void Main(string[] args)
        {
            var routerDict    = new Dictionary <string, Interfaces.IServable <string> >();
            var exampleRouter = new Classes.Router <string>(routerDict);
            var example       = new ExampleRouterImplentation(exampleRouter);

            var firstRequest  = new RouterRequest <string>("Reverse String", "Winner winner chicken dinner");
            var secondRequest = new RouterRequest <string>("Capitalize Vowels", "all of the vowels in this string will be big");
            var thirdRequest  = new RouterRequest <string>("All", "This will do a bunch of stuff :D");

            example.CallRouter(firstRequest);
            example.CallRouter(secondRequest);
            example.CallRouter(thirdRequest);
            Console.ReadLine();
        }
コード例 #7
0
ファイル: Request.cs プロジェクト: TheSpeedding/Timetables
        /// <summary>
        /// Tries to obtain journeys and returns window with results.
        /// </summary>
        /// <param name="sourceName">Source station name.</param>
        /// <param name="targetName">Target station name.</param>
        /// <param name="dt">Datetime.</param>
        /// <param name="transfers">Max transfers.</param>
        /// <param name="count">Number of journeys.</param>
        /// <param name="coefficient">Coefficient for the footpaths.</param>
        /// <param name="mot">Mean of transport.</param>
        /// <param name="win">Window with request.</param>
        /// <param name="comp">Comparer for journeys.</param>
        /// <returns>Window with results</returns>
        public static async Task <JourneyResultsWindow> GetRouterWindowAsync(string sourceName, string targetName, DateTime dt, int transfers, int count, double coefficient, MeanOfTransport mot, NewJourneyWindow win, IComparer <Journey> comp = null)
        {
            Structures.Basic.StationsBasic.StationBasic source = GetStationFromString(sourceName);
            Structures.Basic.StationsBasic.StationBasic target = GetStationFromString(targetName);

            if (source == null || target == null)
            {
                return(null);
            }

            var routerRequest  = new RouterRequest(source.ID, target.ID, dt, transfers, count, coefficient, mot);
            var routerResponse = await SendRouterRequestAsync(routerRequest);

            if (comp != null && routerResponse != null)
            {
                routerResponse.Journeys.Sort(comp);
            }

            return(routerResponse == null ? null : new JourneyResultsWindow(routerResponse, source.Name, target.Name, dt, win));
        }
コード例 #8
0
        private async void FindButtonClicked(object sender, EventArgs e)
        {
            try
            {
                Structures.Basic.StationsBasic.StationBasic source = Request.GetStationFromString(sourceStopEntry.Text);
                Structures.Basic.StationsBasic.StationBasic target = Request.GetStationFromString(targetStopEntry.Text);

                if (source == null)
                {
                    PlatformDependentSettings.ShowMessage(Settings.Localization.UnableToFindStation + ": " + sourceStopEntry.Text);
                    return;
                }
                if (target == null)
                {
                    PlatformDependentSettings.ShowMessage(Settings.Localization.UnableToFindStation + ": " + targetStopEntry.Text);
                    return;
                }

                var routerRequest = new RouterRequest(source.ID, target.ID, leavingTimeDatePicker.Date.Add(leavingTimeTimePicker.Time),
                                                      (int)transfersSlider.Value, (int)countSlider.Value, Settings.WalkingSpeedCoefficient, Settings.GetMoT());

                findButton.IsEnabled = false;
                var routerResponse = await Request.SendRouterRequestAsync(routerRequest);

                findButton.IsEnabled = true;

                if (routerResponse != null)
                {
                    await Navigation.PushAsync(new FindJourneyResultsPage(routerResponse, source.Name, target.Name), true);
                }
            }
            catch
            {
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                Request.CheckBasicDataValidity();
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            }
        }
コード例 #9
0
ファイル: Request.cs プロジェクト: TheSpeedding/Timetables
 /// <summary>
 /// Caches the journeys according to router request.
 /// </summary>
 public static async Task <bool> CacheJourneyAsync(RouterRequest routerRequest, bool forceCache = false) =>
 JourneyCached.CacheResults(DataFeedDesktop.Basic.Stations.FindByIndex(routerRequest.SourceStationID), DataFeedDesktop.Basic.Stations.FindByIndex(routerRequest.TargetStationID), DataFeedDesktop.OfflineMode ? new RouterResponse() : await SendRouterRequestAsync(routerRequest, forceCache)) != null;
コード例 #10
0
 private RouterResponse Get(RouterRequest request)
 {
     return(AsyncHelpers.RunSync(() => ControllerBase.ProcessAsync <RouterRequest, RouterProcessing, RouterResponse>(request)) ?? new RouterResponse());
 }
コード例 #11
0
 /// <summary>
 /// Caches the journeys according to router request.
 /// </summary>
 public static async Task <bool> CacheJourneyAsync(RouterRequest routerRequest, bool forceUpdate = false) => CanBeCached?
 JourneyCached.CacheResults(DataFeedClient.Basic.Stations.FindByIndex(routerRequest.SourceStationID), DataFeedClient.Basic.Stations.FindByIndex(routerRequest.TargetStationID), await SendRouterRequestAsync(routerRequest, forceUpdate)) != null : false;
コード例 #12
0
        public void ThenComponentShouldPassInternalCheck(string query)
        {
            var component = componentRouter.Resolve(RouterRequest.FromQuery(query)) as IInternalComponentStatus;

            Assert.IsTrue(component.InternalComponentStatus);
        }