/// <inheritdoc />
        public virtual async Task SendAsync(IAgentContext agentContext, AgentMessage message, string recipientKey,
                                            string endpointUri, string[] routingKeys = null, string senderKey = null)
        {
            Logger.LogInformation(LoggingEvents.SendMessage, "Recipient {0} Endpoint {1}", recipientKey,
                                  endpointUri);

            if (string.IsNullOrEmpty(message.Id))
            {
                throw new AriesFrameworkException(ErrorCode.InvalidMessage, "@id field on message must be populated");
            }

            if (string.IsNullOrEmpty(message.Type))
            {
                throw new AriesFrameworkException(ErrorCode.InvalidMessage, "@type field on message must be populated");
            }

            if (string.IsNullOrEmpty(endpointUri))
            {
                throw new ArgumentNullException(nameof(endpointUri));
            }

            var uri = new Uri(endpointUri);

            var dispatcher = GetDispatcher(uri.Scheme);

            if (dispatcher == null)
            {
                throw new AriesFrameworkException(ErrorCode.A2AMessageTransmissionError, $"No registered dispatcher for transport scheme : {uri.Scheme}");
            }

            var wireMsg = await CryptoUtils.PrepareAsync(agentContext, message, recipientKey, routingKeys, senderKey);

            await dispatcher.DispatchAsync(uri, new PackedMessageContext(wireMsg));
        }
Esempio n. 2
0
        /// <inheritdoc />
        public async Task <MessageContext> SendReceiveAsync(Wallet wallet, AgentMessage message, string recipientKey,
                                                            string endpointUri, string[] routingKeys = null, string senderKey = null)
        {
            Logger.LogInformation(LoggingEvents.SendMessage, "Recipient {0} Endpoint {1}", recipientKey,
                                  endpointUri);

            if (string.IsNullOrEmpty(message.Id))
            {
                throw new AriesFrameworkException(ErrorCode.InvalidMessage, "@id field on message must be populated");
            }

            if (string.IsNullOrEmpty(message.Type))
            {
                throw new AriesFrameworkException(ErrorCode.InvalidMessage, "@type field on message must be populated");
            }

            if (string.IsNullOrEmpty(endpointUri))
            {
                throw new ArgumentNullException(nameof(endpointUri));
            }

            var uri = new Uri(endpointUri);

            var dispatcher = GetDispatcher(uri.Scheme);

            if (dispatcher == null)
            {
                throw new AriesFrameworkException(ErrorCode.A2AMessageTransmissionError, $"No registered dispatcher for transport scheme : {uri.Scheme}");
            }

            message.AddReturnRouting();
            var wireMsg = await CryptoUtils.PrepareAsync(wallet, message, recipientKey, routingKeys, senderKey);

            var response = await dispatcher.DispatchAsync(uri, new PackedMessageContext(wireMsg));

            if (response is PackedMessageContext responseContext)
            {
                return(await UnpackAsync(wallet, responseContext, senderKey));
            }
            throw new InvalidOperationException("Invalid or empty response");
        }
        /// <summary>
        /// Sends the message and receives a response by adding return routing decorator
        /// according to the Routing RFC. It also tries to cast the response to an expected
        /// type of <see cref="AgentMessage" />
        /// </summary>
        /// <param name="service"></param>
        /// <param name="agentContext"></param>
        /// <param name="message"></param>
        /// <param name="recipientKey"></param>
        /// <param name="endpointUri"></param>
        /// <param name="routingKeys"></param>
        /// <param name="senderKey"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static async Task <T> SendReceiveAsync <T>(this IMessageService service, IAgentContext agentContext, AgentMessage message, string recipientKey,
                                                          string endpointUri, string[] routingKeys = null, string senderKey = null)
            where T : AgentMessage, new()
        {
            var response = await service.SendReceiveAsync(agentContext, message, recipientKey, endpointUri, routingKeys, senderKey);

            if (response is UnpackedMessageContext unpackedContext)
            {
                return(unpackedContext.GetMessage <T>());
            }
            throw new InvalidOperationException("Couldn't cast the message to the expexted type or response was invalid");
        }
        /// <summary>
        /// Sends the message and receives a response by adding return routing decorator
        /// according to the Routing RFC. It also tries to cast the response to an expected
        /// type of <see cref="AgentMessage" />
        /// </summary>
        /// <param name="service"></param>
        /// <param name="agentContext"></param>
        /// <param name="message"></param>
        /// <param name="connection"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static async Task <T> SendReceiveAsync <T>(this IMessageService service, IAgentContext agentContext, AgentMessage message, ConnectionRecord connection)
            where T : AgentMessage, new()
        {
            var response = await service.SendReceiveAsync(agentContext, message, connection);

            if (response is UnpackedMessageContext unpackedContext)
            {
                return(unpackedContext.GetMessage <T>());
            }
            throw new InvalidOperationException("Couldn't cast the message to the expexted type or response was invalid");
        }
        /// <summary>
        /// Sends the message and receives a response by adding return routing decorator
        /// according to the Routing RFC.
        /// </summary>
        /// <param name="service"></param>
        /// <param name="agentContext"></param>
        /// <param name="message"></param>
        /// <param name="connection"></param>
        /// <returns></returns>
        public static async Task <MessageContext> SendReceiveAsync(this IMessageService service, IAgentContext agentContext, AgentMessage message, ConnectionRecord connection)
        {
            var routingKeys  = connection.Endpoint?.Verkey != null ? connection.Endpoint.Verkey : new string[0];
            var recipientKey = connection.TheirVk ?? connection.GetTag("InvitationKey") ?? throw new InvalidOperationException("Cannot locate a recipient key");

            if (connection.Endpoint?.Uri == null)
            {
                throw new AriesFrameworkException(ErrorCode.A2AMessageTransmissionError, "Cannot send to connection that does not have endpoint information specified");
            }

            return(await service.SendReceiveAsync(agentContext, message, recipientKey, connection.Endpoint.Uri, routingKeys, connection.MyVk));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="UnpackedMessageContext" /> class.
 /// </summary>
 /// <param name="message"></param>
 /// <param name="connection"></param>
 /// <returns></returns>
 public UnpackedMessageContext(AgentMessage message, ConnectionRecord connection) : base(message, connection)
 {
     SenderVerkey = connection.TheirVk;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="UnpackedMessageContext" /> class.
 /// </summary>
 /// <param name="message"></param>
 /// <param name="senderVerkey"></param>
 /// <returns></returns>
 public UnpackedMessageContext(AgentMessage message, string senderVerkey) : base(message)
 {
     SenderVerkey = senderVerkey;
 }
Esempio n. 8
0
 /// <summary>Wraps the message in payload.</summary>
 /// <param name="agentMessage">The agent message.</param>
 /// <param name="senderKey"></param>
 /// <returns></returns>
 public static UnpackedMessageContext AsMessageContext(this AgentMessage agentMessage, string senderKey) =>
 new UnpackedMessageContext(agentMessage, senderKey);
Esempio n. 9
0
 /// <inheritdoc />
 /// <param name="message">The message.</param>
 /// <param name="connection">The connection.</param>
 protected MessageContext(AgentMessage message, ConnectionRecord connection)
     : this(message.ToJson(), false)
 {
     Connection = connection;
 }
Esempio n. 10
0
 /// <inheritdoc />
 /// <param name="message">The message.</param>
 protected MessageContext(AgentMessage message)
     : this(message.ToJson(), false)
 {
 }