コード例 #1
0
        /// <summary>
        /// Invoke the handler pipeline and process the passed message.
        /// </summary>
        /// <param name="body">The body.</param>
        /// <param name="wallet">The wallet.</param>
        /// <param name="pool">The pool.</param>
        /// <returns></returns>
        /// <exception cref="Exception">Expected inner message to be of type 'ForwardMessage'</exception>
        /// <exception cref="AgentFrameworkException">Couldn't locate a message handler for type {messageType}</exception>
        protected async Task ProcessAsync(byte[] body, Wallet wallet, Pool pool = null)
        {
            EnsureConfigured();

            var agentContext = new AgentContext {
                Wallet = wallet, Pool = pool
            };

            agentContext.AddNext(new MessagePayload(body, true));

            while (agentContext.TryGetNext(out var message))
            {
                MessagePayload messagePayload;
                if (message.Packed)
                {
                    var unpacked = await CryptoUtils.UnpackAsync(agentContext.Wallet, message.Payload);

                    messagePayload = new MessagePayload(unpacked.Message, false);
                }
                else
                {
                    messagePayload = message;
                }

                if (_handlers.Where(handler => handler != null).FirstOrDefault(
                        handler => handler.SupportedMessageTypes.Any(
                            type => type.Equals(messagePayload.GetMessageType(), StringComparison.OrdinalIgnoreCase))) is IMessageHandler messageHandler)
                {
                    await messageHandler.ProcessAsync(agentContext, messagePayload);
                }
                else
                {
                    throw new AgentFrameworkException(ErrorCode.InvalidMessage,
                                                      $"Couldn't locate a message handler for type {messagePayload.GetMessageType()}");
                }
            }
        }
コード例 #2
0
        private async Task <AgentMessage> ProcessMessage(IAgentContext agentContext, MessagePayload message)
        {
            MessagePayload messagePayload;

            if (message.Packed)
            {
                var unpacked = await CryptoUtils.UnpackAsync(agentContext.Wallet, message.Payload);

                Logger.LogInformation($"Agent Message Received : {unpacked.Message}");
                messagePayload = new MessagePayload(unpacked.Message, false);
                if (unpacked.SenderVerkey != null && agentContext.Connection == null)
                {
                    agentContext.Connection =
                        await ConnectionService.ResolveByMyKeyAsync(agentContext, unpacked.RecipientVerkey);
                }
            }
            else
            {
                messagePayload = message;
            }

            if (_handlers.Where(handler => handler != null).FirstOrDefault(
                    handler => handler.SupportedMessageTypes.Any(
                        type => type.Equals(messagePayload.GetMessageType(), StringComparison.OrdinalIgnoreCase))) is
                IMessageHandler messageHandler)
            {
                Logger.LogDebug("Processing message type {MessageType}, {MessageData}", messagePayload.GetMessageType(),
                                messagePayload.Payload.GetUTF8String());
                var outboundMessage = await messageHandler.ProcessAsync(agentContext, messagePayload);

                return(outboundMessage);
            }

            throw new AgentFrameworkException(ErrorCode.InvalidMessage,
                                              $"Couldn't locate a message handler for type {messagePayload.GetMessageType()}");
        }
コード例 #3
0
 internal bool TryGetNext(out MessagePayload message) => _queue.TryDequeue(out message);
コード例 #4
0
 /// <summary>
 /// Adds a message to the current processing queue
 /// </summary>
 /// <param name="message"></param>
 public void AddNext(MessagePayload message) => _queue.Enqueue(message);
コード例 #5
0
 /// <inheritdoc />
 public Task ProcessAsync(IAgentContext agentContext, MessagePayload messagePayload) =>
 ProcessAsync(messagePayload.GetMessage <T>(), agentContext);