public async Task <IEnumerable <ShortEntityId> > QueryObservablesAsync(GatewayRequest request)
        {
            try
            {
                // Validates parameter
                if (string.IsNullOrWhiteSpace(request?.Topic))
                {
                    throw new ArgumentException($"Parameter {nameof(request)} is null or invalid.", nameof(request));
                }

                // Gets service proxy
                IRegistryService proxy = GetServiceProxy(
                    PartitionResolver.Resolve(request.Topic, OwinCommunicationListener.RegistryServicePartitionCount),
                    OwinCommunicationListener.RegistryServiceUri);
                if (proxy == null)
                {
                    throw new ApplicationException("The ServiceProxy cannot be null.");
                }

                // Invokes actor using proxy
                ServiceEventSource.Current.Message($"Retrieving observables...\r\n[Publication]: Topic=[{request.Topic}]");
                IEnumerable <EntityId> entityIds = await proxy.QueryObservablesAsync(
                    request.Topic,
                    (request.FilterExpressions != null) && request.FilterExpressions.Any()
                    ?request.FilterExpressions.First()
                        : null);

                if ((entityIds == null) || !entityIds.Any())
                {
                    return(null);
                }
                return(entityIds.Select(e => new ShortEntityId(e)));
            }
            catch (AggregateException ex)
            {
                if (!(ex.InnerExceptions?.Count > 0))
                {
                    throw new HttpResponseException(this.Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message));
                }
                foreach (Exception exception in ex.InnerExceptions)
                {
                    ServiceEventSource.Current.Message(exception.Message);
                }
                throw new HttpResponseException(this.Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message));
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.Message(ex.Message);
                throw new HttpResponseException(this.Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message));
            }
        }
        public async Task WriteMessagesAsync(GatewayRequest request)
        {
            try
            {
                // Validates parameter
                if ((request.ObserverEntityId == null) ||
                    (request.Messages == null) ||
                    !request.Messages.Any())
                {
                    throw new ArgumentException($"Parameter {nameof(request)} is null or invalid.", nameof(request));
                }

                // Gets service proxy
                IMessageBoxService proxy = GetServiceProxy(
                    PartitionResolver.Resolve(
                        request.ObserverEntityId.EntityUri.AbsoluteUri,
                        OwinCommunicationListener.MessageBoxServicePartitionCount),
                    OwinCommunicationListener.MessageBoxServiceUri);
                if (proxy == null)
                {
                    throw new ApplicationException("The ServiceProxy cannot be null.");
                }

                // Invokes actor using proxy
                ServiceEventSource.Current.Message($"Writing messages to the MessageBox...\r\n[Observer]: {request.ObserverEntityId}");
                await proxy.WriteMessagesAsync(request.ObserverEntityId.EntityUri, request.Messages);
            }
            catch (AggregateException ex)
            {
                if (!(ex.InnerExceptions?.Count > 0))
                {
                    throw new HttpResponseException(this.Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message));
                }
                foreach (Exception exception in ex.InnerExceptions)
                {
                    ServiceEventSource.Current.Message(exception.Message);
                }
                throw new HttpResponseException(this.Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message));
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.Message(ex.Message);
                throw new HttpResponseException(this.Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message));
            }
        }
        public async Task RegisterObservableAsync(GatewayRequest request)
        {
            try
            {
                // Validates parameter
                if (string.IsNullOrWhiteSpace(request?.Topic) ||
                    (request.ObservableEntityId == null))
                {
                    throw new ArgumentException($"Parameter {nameof(request)} is null or invalid.", nameof(request));
                }

                // Gets service proxy
                IRegistryService proxy = GetServiceProxy(
                    PartitionResolver.Resolve(request.Topic, OwinCommunicationListener.RegistryServicePartitionCount),
                    OwinCommunicationListener.RegistryServiceUri);
                if (proxy == null)
                {
                    throw new ApplicationException("The ServiceProxy cannot be null.");
                }

                // Invokes actor using proxy
                ServiceEventSource.Current.Message(
                    $"Registering observable...\r\n[Observable]: {request.ObservableEntityId}\r\n[Observer]: {request.ObserverEntityId}\r\n[Publication]: Topic=[{request.Topic}]");
                await proxy.RegisterObservableAsync(request.Topic, new EntityId(request.ObservableEntityId));
            }
            catch (AggregateException ex)
            {
                if (!(ex.InnerExceptions?.Count > 0))
                {
                    throw new HttpResponseException(this.Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message));
                }
                foreach (Exception exception in ex.InnerExceptions)
                {
                    ServiceEventSource.Current.Message(exception.Message);
                }
                throw new HttpResponseException(this.Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message));
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.Message(ex.Message);
                throw new HttpResponseException(this.Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message));
            }
        }
Ejemplo n.º 4
0
        public async Task <IEnumerable <Message> > ReadMessagesAsync(GatewayRequest request)
        {
            try
            {
                // Validates parameter
                if (request.ObserverEntityId == null)
                {
                    throw new ArgumentException($"Parameter {nameof(request)} is null or invalid.", nameof(request));
                }

                // Gets service proxy
                IMessageBoxService proxy = GetServiceProxy(PartitionResolver.Resolve(request.ObserverEntityId.EntityUri.AbsoluteUri,
                                                                                     OwinCommunicationListener.MessageBoxServicePartitionCount),
                                                           OwinCommunicationListener.MessageBoxServiceUri);
                if (proxy == null)
                {
                    throw new ApplicationException("The ServiceProxy cannot be null.");
                }

                // Invokes actor using proxy
                ServiceEventSource.Current.Message($"Reading messages from the MessageBox...\r\n[Observer]: {request.ObserverEntityId}");
                return(await proxy.ReadMessagesAsync(request.ObserverEntityId.EntityUri));
            }
            catch (AggregateException ex)
            {
                if (!(ex.InnerExceptions?.Count > 0))
                {
                    throw;
                }
                foreach (Exception exception in ex.InnerExceptions)
                {
                    ServiceEventSource.Current.Message(exception.Message);
                }
                throw;
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.Message(ex.Message);
                throw;
            }
        }