private IPooledBuffer Serialize(IServiceDiscoveryRequest msg, Nothing _)
 {
     using (var envelope = ClientToBrokerRequestEnvelope.Rent())
     {
         var proto = ServiceDiscoveryRequest.Rent();
         proto.DiscoveryMode              = ConvertToProto(msg.DiscoveryMode);
         proto.ConsumedService            = ConvertToProto(msg.ConsumedService);
         envelope.ServiceDiscoveryRequest = proto;
         return(envelope.Serialize());
     }
 }
        public async Task HandleAsync(
            IServiceDiscoveryRequest request,
            IAppConnection sourceConnection,
            ITransportChannel sourceChannel)
        {
            IReadOnlyCollection <(IConsumedMethod Consumed, IProvidedMethod Provided)> methodMatches;

            if (request.ConsumedService.HasValue)
            {
                methodMatches = _registryService.GetMethodMatches(
                    sourceConnection.Info.ApplicationId,
                    request.ConsumedService.Value);
            }
            else
            {
                methodMatches = _registryService.GetMethodMatches(sourceConnection.Info.ApplicationId);
            }
            IEnumerable <IGrouping <(IConsumedService ConsumedService, IProvidedService ProvidedService, Maybe <UniqueId> ConnectionId), IProvidedMethod> > groupedMethods;

            if (request.DiscoveryMode == DiscoveryMode.Offline)
            {
                groupedMethods = methodMatches
                                 .GroupBy(
                    x => (
                        x.Consumed.ConsumedService,
                        x.Provided.ProvidedService,
                        ConnectionId: Maybe <UniqueId> .Nothing),
                    x => x.Provided);
            }
            else
            {
                var onlineConnections = _connectionTracker.GetOnlineConnections();
                groupedMethods = methodMatches
                                 .Join(onlineConnections, x => x.Provided.ProvidedService.Application.Id, y => y.Info.ApplicationId, (x, y) => (Match: x, ConnectionId: y.Id))
                                 .GroupBy(
                    x => (
                        x.Match.Consumed.ConsumedService,
                        x.Match.Provided.ProvidedService,
                        new Maybe <UniqueId>(x.ConnectionId)),
                    x => x.Match.Provided);
            }
            var discoveredServices =
                from s in groupedMethods
                let consumedService = s.Key.ConsumedService
                                      let providedService = s.Key.ProvidedService
                                                            let connectionId = s.Key.ConnectionId
                                                                               select _protocol.MessageFactory.CreateDiscoveredService(
                    _protocol.MessageFactory.CreateConsumedServiceReference(
                        consumedService.Service.Id,
                        consumedService.Alias),
                    _protocol.MessageFactory.CreateProvidedServiceReference(
                        providedService.Service.Id,
                        providedService.Alias,
                        providedService.Application.Id,
                        connectionId),
                    s.Key.ProvidedService.Title,
                    s.Select(m =>
                             _protocol.MessageFactory.CreateDiscoveredServiceMethod(
                                 m.Method.Name,
                                 m.Title,
                                 m.Method.InputMessage.Id,
                                 m.Method.OutputMessage.Id,
                                 Convert(m.Method.Type))).ToList());

            using (var response = _protocol.MessageFactory.CreateServiceDiscoveryResponse(discoveredServices.ToList()))
            {
                Log.Info("Completed service discovery request {{{0}}} from {{{1}}}: {2}", request, sourceConnection, response);
                var serializedResponse = _protocol.Serializer.Serialize(response);
                try
                {
                    await sourceChannel.Out
                    .WriteAsync(new TransportMessageFrame(serializedResponse))
                    .ConfigureAwait(false);
                }
                catch
                {
                    serializedResponse.Dispose();
                    throw;
                }
            }
        }
Пример #3
0
 public T Handle(IServiceDiscoveryRequest message, TArgs args)
 {
     return(_handleServiceDiscoveryRequest(message, args));
 }
Пример #4
0
        public async Task HandleAsync(
            IServiceDiscoveryRequest request,
            IAppConnection sourceConnection,
            ITransportChannel sourceChannel)
        {
            IReadOnlyCollection <(IConsumedMethod Consumed, IProvidedMethod Provided)> methodMatches;

            if (request.ConsumedService.HasValue)
            {
                methodMatches = _registryService.GetMethodMatches(
                    sourceConnection.Info.ApplicationId,
                    request.ConsumedService.Value);
            }
            else
            {
                methodMatches = _registryService.GetMethodMatches(sourceConnection.Info.ApplicationId);
            }
            IEnumerable <IGrouping <(IConsumedService ConsumedService, IProvidedService ProvidedService, Maybe <UniqueId> ConnectionId, Maybe <UniqueId> ApplicationInstanceId), IProvidedMethod> > groupedMethods;

            var online = request.DiscoveryMode == DiscoveryMode.Online;

            if (_contextLinkageManager.IsContextShouldBeConsidered(request.ContextLinkageOptions, sourceConnection))
            {
                groupedMethods = _contextLinkageManager.GetAppsInContexts(request.ContextLinkageOptions, sourceConnection, online)
                                 .Join(methodMatches, x => x.AppId, y => y.Provided.ProvidedService.Service.Id,
                                       (x, y) => (y.Consumed, y.Provided, x.AppInstanceId, x.ConnectionId))
                                 .GroupBy(x => (x.Consumed.ConsumedService, x.Provided.ProvidedService, x.ConnectionId, new Maybe <UniqueId>(x.AppInstanceId)), x => x.Provided);
            }
            else
            {
                if (online)
                {
                    var onlineConnections = _appLifecycleManager.GetOnlineConnections();
                    groupedMethods = methodMatches
                                     .Join(onlineConnections, x => x.Provided.ProvidedService.Application.Id,
                                           y => y.Info.ApplicationId,
                                           (x, y) => (Match: x, ConnectionId: y.Id, y.Info.ApplicationInstanceId))
                                     .GroupBy(
                        x => (
                            x.Match.Consumed.ConsumedService,
                            x.Match.Provided.ProvidedService,
                            new Maybe <UniqueId>(x.ConnectionId),
                            new Maybe <UniqueId>(x.ApplicationInstanceId)),
                        x => x.Match.Provided);
                }
                else
                {
                    var providerApps = methodMatches.Select(x => x.Provided.ProvidedService.Application.Id).Distinct()
                                       .ToArray();
                    var availableProviderApps = FilterAvailableApps(providerApps);
                    groupedMethods = methodMatches
                                     .Join(
                        availableProviderApps,
                        x => x.Provided.ProvidedService.Application.Id,
                        y => y,
                        (x, y) => x)
                                     .GroupBy(
                        x => (
                            x.Consumed.ConsumedService,
                            x.Provided.ProvidedService,
                            ConnectionId: Maybe <UniqueId> .Nothing,
                            ApplicationInstanceId: Maybe <UniqueId> .Nothing),
                        x => x.Provided);
                }
            }

            var discoveredServices =
                from s in groupedMethods
                let consumedService = s.Key.ConsumedService
                                      let providedService = s.Key.ProvidedService
                                                            let connectionId = s.Key.ConnectionId
                                                                               let applicationInstanceId = s.Key.ApplicationInstanceId
                                                                                                           select _protocol.MessageFactory.CreateDiscoveredService(
                    _protocol.MessageFactory.CreateConsumedServiceReference(
                        consumedService.Service.Id,
                        consumedService.Alias),
                    _protocol.MessageFactory.CreateProvidedServiceReference(
                        providedService.Service.Id,
                        providedService.Alias,
                        providedService.Application.Id,
                        connectionId,
                        applicationInstanceId),
                    s.Key.ProvidedService.Title,
                    s.Select(m =>
                             _protocol.MessageFactory.CreateDiscoveredServiceMethod(
                                 m.Method.Name,
                                 m.Title,
                                 m.Method.InputMessage.Id,
                                 m.Method.OutputMessage.Id,
                                 Convert(m.Method.Type),
                                 m.Options.Select(o => _protocol.MessageFactory.CreateOption(o.Id, o.Value)).ToList()))
                    .ToList());

            using (var response = _protocol.MessageFactory.CreateServiceDiscoveryResponse(discoveredServices.ToList()))
            {
                Log.Info("Completed service discovery request {{{0}}} from {{{1}}}: {2}", request, sourceConnection, response);
                var serializedResponse = _protocol.Serializer.Serialize(response);
                try
                {
                    await sourceChannel.Out
                    .WriteAsync(new TransportMessageFrame(serializedResponse))
                    .ConfigureAwait(false);
                }
                catch
                {
                    serializedResponse.Dispose();
                    throw;
                }
            }
        }