TrainNotifier.Console.WebSocketServer.NMSWrapper.Start C# (CSharp) Method

Start() public method

public Start ( ) : Task
return Task
        public Task Start()
        {
            _nmsDownloader.TrainDataRecieved += (src, feedData) =>
            {
                // run this as a task to return to callee quicker
                Task.Run(() =>
                {
                    dynamic evtData = JsonConvert.DeserializeObject<dynamic>(feedData.Data);

                    switch (feedData.FeedSource)
                    {
                        case Feed.TrainMovement:

                            Parallel.ForEach(_userManager.ActiveUsers
                                .Where(u => u.Value.State == UserContextState.SubscribeToFeed), uc => SendData(uc, evtData as IEnumerable<dynamic>));

                            Parallel.ForEach(_userManager.ActiveUsers
                                .Where(u => u.Value.State == UserContextState.SubscribeToStanox)
                                .Where(u => DataContainsStanox(evtData, u.Value.StateArgs)), uc => SendStanoxData(uc, evtData as IEnumerable<dynamic>));

                            break;
                    }

                    var eh = FeedDataRecieved;
                    if (null != eh)
                        eh(this, new FeedEventArgs(feedData.FeedSource, evtData));
                }, _cancellationTokenSource.Token);
            };

            return Task.Run(() => _nmsDownloader.SubscribeToFeeds(), _cancellationTokenSource.Token);
        }

Usage Example

        protected override void OnStart(string[] args)
        {
            int port = int.Parse(ConfigurationManager.AppSettings["WebSocketServerPort"]);
            _wsServerWrapper = new WebSocketServerWrapper(port: port);
            _userManager = new UserManager(_wsServerWrapper);
            _wsServerWrapper.Start();
            Trace.TraceInformation("Started server on {0}:{1}", IPAddress.Any, port);

            _nmsWrapper = new NMSWrapper(_userManager, _cancellationTokenSource);
            _cacheController = new CacheController(_nmsWrapper, _wsServerWrapper, _userManager);
            _nmsTask = _nmsWrapper.Start();

            _nmsTimer = new Timer((s) =>
            {
                if (_cancellationTokenSource.IsCancellationRequested)
                {
                    Trace.TraceError("Cancellation Requested");
                    ExitCode = -1;
                    throw new OperationCanceledException(_cancellationTokenSource.Token);
                }
                else if (_nmsTask.IsFaulted)
                {
                    Trace.TraceError("NMS Task Faulted: {0}", _nmsTask.Exception);
                    ExitCode = -1;
                    throw _nmsTask.Exception;
                }
                else if (_nmsTask.IsCompleted)
                {
                    Trace.TraceInformation("NMS Task Finished");

                    Stop();
                }
            }, null, TimeSpan.Zero, TimeSpan.FromSeconds(30));
        }