Esempio n. 1
0
        public async Task QueueEvent_DeQueue()
        {
            // Arrange
            var compassEvent = new CompassEvent
            {
                EventName = "test"
            };

            var queuedEvents = new QueuedEvents()
            {
                Events = new List <CompassEvent>()
                {
                    compassEvent
                }
            };

            A.CallTo(() => _dataStore.GetQueuedEventsAsync())
            .Returns(Task.FromResult(queuedEvents));

            // Act
            await _sut.DeQueueEventAsync(compassEvent);

            // Assert
            A.CallTo(() => _dataStore.UpsertAsync(queuedEvents))
            .MustHaveHappened(Repeated.Exactly.Once);
            A.CallTo(() => _dataStore.UpsertAsync(A <QueuedEvents> .That.Matches(arg => !arg.Events.Contains(compassEvent))))
            .MustHaveHappened(Repeated.Exactly.Once);
        }
        public async Task DeQueueEventAsync(CompassEvent compassEvent)
        {
            var queuedEvents = await _dataStore.GetQueuedEventsAsync();

            queuedEvents.Events.Remove(compassEvent);

            await _dataStore.UpsertAsync(queuedEvents);
        }
Esempio n. 3
0
        private async Task SendEvent(ServiceSubscription serviceSubscription, CompassEvent compassEvent)
        {
            await _sendToEndpointService.SendToEndpointAsync(new List <ServiceSubscription> {
                serviceSubscription
            },
                                                             compassEvent);

            await _queueEventService.DeQueueEventAsync(compassEvent);
        }
        public async Task <CompassResult> RouteRequest(CompassEvent compassEvent)
        {
            await _validateApplicationTokenService.ValidateApplicationTokenAsync(compassEvent.ApplicationToken);

            var subscriptions =
                await _getServiceSubscriptionsForEventService.GetServiceSubscriptionsAsync(compassEvent);

            _kafkaProducerService.Produce(compassEvent);

            return(await SendToSubscriptionsAsync(compassEvent, subscriptions));
        }
        public async Task QueueEventAsync(CompassEvent compassEvent)
        {
            compassEvent.Identifier  = Guid.NewGuid();
            compassEvent.DateCreated = DateTime.Now;

            var queuedEvents = await GetQueuedEventsAsync();

            queuedEvents.Events.Add(compassEvent);

            await _dataStore.UpsertAsync(queuedEvents);
        }
        public async Task <CompassResult> SendRequestAsync(CompassEvent compassEvent)
        {
            // if no compass url is set, then we bail
            if (_options.CompassUri == null)
            {
                throw new NoCompassEndpointSetException(compassEvent.EventName);
            }

            // build the request and send
            return(await CompassRestClient.SendRequestAsync <CompassResult>(_options.CompassUri, compassEvent));
        }
 public void Produce(CompassEvent compassEvent)
 {
     try
     {
         _producer.ProduceAsync(_compassEnvironment.GetKafkaTopic(), null, JsonConvert.SerializeObject(compassEvent));
         _producer.Flush(_compassEnvironment.GetKafkaProducerTimeout());
     }
     catch (Exception e)
     {
         // TODO: Log
         Console.WriteLine(e);
     }
 }
        public Policy GetPolicy(CompassEvent compassEvent)
        {
            var fallback = Policy
                           .Handle <NoValidSubscriptionsException>()
                           .FallbackAsync(
                (context, cancellationToken) => _queueEventService.QueueEventAsync(compassEvent),
                (exception, context) => throw exception
                );

            var retry = Policy
                        .Handle <NoValidSubscriptionsException>()
                        .WaitAndRetryAsync(GetTimeSpans());

            return(Policy
                   .WrapAsync(fallback, retry));
        }
        public async Task RouteRequest_ValidateAppToken()
        {
            // Arrange
            var appToken = Guid.NewGuid();
            var compassEvent = new CompassEvent() { EventName = "test", ApplicationToken = appToken };
            var subscriptions = new List<ServiceSubscription>
            {
                new ServiceSubscription()
            };
            A.CallTo(() => _getServiceSubscriptionsForEventService.GetServiceSubscriptionsAsync(compassEvent))
             .Returns(subscriptions);

            // Act
            await _sut.RouteRequest(compassEvent);

            // Assert
            A.CallTo(() => _validateApplicationTokenService.ValidateApplicationTokenAsync(appToken))
             .MustHaveHappened(Repeated.Exactly.Once);
        }
        private async Task <CompassResult> SendToSubscriptionsAsync(CompassEvent compassEvent, IReadOnlyCollection <ServiceSubscription> subscriptions)
        {
            var registeredApplicationTask =
                _dataStore.GetByDocumentsIdAsync <RegisteredApplication>(
                    subscriptions.Select(subscription => subscription.ApplicationToken.ToString()));
            var sendToEndpointTask = _sendToEndpointService.SendToEndpointAsync(subscriptions, compassEvent);
            await Task.WhenAll(registeredApplicationTask, sendToEndpointTask);

            var registeredApplications = await registeredApplicationTask;
            var responses     = await sendToEndpointTask;
            var compassResult = new CompassResult {
                Status = CompassResponseStatus.Success
            };

            if (responses != null)
            {
                compassResult.Status  = GetCompassResponseStatus(responses);
                compassResult.Payload = BuildResponse(registeredApplications, responses);
            }

            return(compassResult);
        }
Esempio n. 11
0
        private async Task <object> SendAsync(Uri endpoint, CompassEvent compassEvent)
        {
            const string header       = "application/json";
            var          client       = GetHttpClient(header);
            var          queryContent =
                new StringContent(
                    JsonConvert.SerializeObject(new
            {
                ApplicationToken = compassEvent.ApplicationToken,
                ApplicationName  = compassEvent.ApplicationName,
                EventName        = compassEvent.EventName,
                Payload          = compassEvent.Payload
            }), Encoding.UTF8, header);

            var response = await _sendToEndpointPolicy.GetPolicy()
                           .ExecuteAsync <HttpResponseMessage>(() => client.PostAsync(endpoint, queryContent));

            response.EnsureSuccessStatusCode();

            var result = await response.Content.ReadAsStringAsync();

            return(result == null ? null : JsonConvert.DeserializeObject <object>(result));
        }
        public async Task RouteRequest_SendToAllSubscriptions()
        {
            // Arrange
            var subscriptions = new List<ServiceSubscription>
            {
                new ServiceSubscription()
                {
                    ApplicationToken = Guid.NewGuid(),
                    ApplicationUri = new Uri("http://www.example.com/"),
                    SubscribedEvents = new List<string>() { "test"}
                },
                new ServiceSubscription()
                {
                    ApplicationToken = Guid.NewGuid(),
                    ApplicationUri = new Uri("http://www.example2.com/"),
                    SubscribedEvents = new List<string>() { "test"}
                }
            };

            var payload = new {test = "test"};
            var compassEvent = new CompassEvent
            {
                EventName = "test", 
                ApplicationToken = Guid.NewGuid(),
                Payload = payload
            };

            A.CallTo(() => _getServiceSubscriptionsForEventService.GetServiceSubscriptionsAsync(compassEvent))
             .Returns(subscriptions);

            // Act
            await _sut.RouteRequest(compassEvent);

            // Assert
            A.CallTo(() => _sendToEndpointService.SendToEndpointAsync(subscriptions, compassEvent))
             .MustHaveHappened(Repeated.Exactly.Once);
        }
Esempio n. 13
0
        public async Task <IReadOnlyCollection <SendToEndpointResult> > SendToEndpointAsync(IReadOnlyCollection <ServiceSubscription> subscriptions, CompassEvent compassEvent)
        {
            var tasks = subscriptions.Select(async subscription =>
            {
                var sendToEndpointResult =
                    new SendToEndpointResult {
                    ApplicationToken = subscription.ApplicationToken.ToString()
                };
                try
                {
                    sendToEndpointResult.Result  = await SendAsync(subscription.ApplicationUri, compassEvent);
                    sendToEndpointResult.Success = true;
                }
                catch (Exception)
                {
                    sendToEndpointResult.Success = false;
                }

                return(sendToEndpointResult);
            }).ToList();

            return(await Task.WhenAll(tasks));
        }
Esempio n. 14
0
 // send standard event through compass
 public async Task <CompassResult> SendRequestAsync(CompassEvent compassEvent)
 {
     return(await _compassRequest.SendRequestAsync(compassEvent));
 }
 public NoValidSubscriptionsException(CompassEvent compassEvent)
     : base($"There were no valid services subscribed to this event: {compassEvent.EventName}.")
 {
     CompassEvent = compassEvent;
 }
Esempio n. 16
0
 public async Task <CompassResult> RouteRequest([FromBody] CompassEvent compassEvent)
 {
     return(await _routeRequestService.RouteRequest(compassEvent));
 }
Esempio n. 17
0
        private async Task <IReadOnlyCollection <ServiceSubscription> > GetSubscriptionsAsync(CompassEvent compassEvent)
        {
            var subscriptions = await _dataStore.GetServiceSubscribedToEventAsync(compassEvent.EventName);

            // TODO: Determine how to handle one service going down,
            // but another still alive
            if (subscriptions == null || subscriptions.Count == 0)
            {
                throw new NoValidSubscriptionsException(compassEvent);
            }

            return(subscriptions.ToList());
        }
Esempio n. 18
0
        private void Initialize()
        {
            _compassTimerCount  = 0;
            _accelTimerCount    = 0;
            _carCrashTimerCount = 0;

            SessionStatus = SessionStatus.Stopped;

            if (CurrentDriver == null)
            {
                CurrentDriver = new Driver {
                    Image = "/Assets/user_unknow.png"
                };
            }

            CompassEvent = new CompassEvent
            {
                HeadingDegrees = 0
            };

            AccelerometerEvent = new AccelerometerEvent
            {
                X = 0000.00,
                Z = 0000.00,
                Y = 0000.00,
            };

            AvailableDrivers = new ObservableCollection <Driver>
            {
                new Driver {
                    Id = 1, LicensePlate = "FGH-9876", Image = "/Assets/Drivers/1.jpg"
                },
                new Driver {
                    Id = 2, LicensePlate = "SLV-4335", Image = "/Assets/Drivers/2.jpg"
                },
                new Driver {
                    Id = 3, LicensePlate = "MAX-9876", Image = "/Assets/Drivers/3.jpg"
                },
                new Driver {
                    Id = 4, LicensePlate = "CCC-1432", Image = "/Assets/Drivers/4.jpg"
                },
                new Driver {
                    Id = 5, LicensePlate = "JNV-9876", Image = "/Assets/Drivers/5.jpg"
                },
                new Driver {
                    Id = 6, LicensePlate = "CUA-1456", Image = "/Assets/Drivers/6.jpg"
                },
                new Driver {
                    Id = 7, LicensePlate = "JOP-9876", Image = "/Assets/Drivers/7.jpg"
                },
                new Driver {
                    Id = 8, LicensePlate = "HTY-1243", Image = "/Assets/Drivers/8.jpg"
                },
                new Driver {
                    Id = 9, LicensePlate = "VVV-4444", Image = "/Assets/Drivers/9.jpg"
                },
                new Driver {
                    Id = 10, LicensePlate = "ERT-1256", Image = "/Assets/Drivers/10.jpg"
                }
            };
        }
Esempio n. 19
0
 public async Task <IReadOnlyCollection <ServiceSubscription> > GetServiceSubscriptionsAsync(CompassEvent compassEvent)
 {
     return(await _getServiceSubscriptionsForEventPolicy.GetPolicy(compassEvent)
            .ExecuteAsync(() => GetSubscriptionsAsync(compassEvent)));
 }