예제 #1
0
        public IProtocolGatewayMessage CreateMessage(string address, IByteBuffer payload)
        {
            ProtocolGatewayMessage message = new ProtocolGatewayMessage.Builder(payload, address)
                                             .Build();

            return(message);
        }
예제 #2
0
        public Task <DirectMethodResponse> InvokeMethodAsync(DirectMethodRequest request)
        {
            string address = TwinAddressHelper.FormatDeviceMethodRequestAddress(request.CorrelationId, request.Name);
            IProtocolGatewayMessage pgMessage = new ProtocolGatewayMessage.Builder(this.byteBufferConverter.ToByteBuffer(request.Data), address)
                                                .WithCreatedTimeUtc(DateTime.UtcNow)
                                                .Build();

            this.channel.Handle(pgMessage);
            return(Task.FromResult(default(DirectMethodResponse)));
        }
        public IProtocolGatewayMessage FromMessage(IMessage message)
        {
            message.SystemProperties.TryGetValue(SystemProperties.LockToken, out string lockToken);

            DateTime createdTimeUtc = DateTime.UtcNow;

            if (message.SystemProperties.TryGetValue(SystemProperties.EnqueuedTime, out string createdTime))
            {
                createdTimeUtc = DateTime.Parse(createdTime, null, DateTimeStyles.RoundtripKind);
            }

            if (!message.SystemProperties.TryGetValue(SystemProperties.OutboundUri, out string uriTemplateKey))
            {
                throw new InvalidOperationException("Could not find key " + SystemProperties.OutboundUri + " in message system properties.");
            }

            IDictionary <string, string> properties = new Dictionary <string, string>();

            foreach (KeyValuePair <string, string> property in message.Properties)
            {
                properties.Add(property);
            }

            foreach (KeyValuePair <string, string> systemProperty in message.SystemProperties)
            {
                if (SystemProperties.OutgoingSystemPropertiesMap.TryGetValue(systemProperty.Key, out string onWirePropertyName))
                {
                    properties[onWirePropertyName] = systemProperty.Value;
                }
            }

            if (message.SystemProperties.TryGetValue(SystemProperties.RpConnectionDeviceIdInternal, out string rpDeviceId))
            {
                properties[SystemProperties.OutgoingSystemPropertiesMap[SystemProperties.RpConnectionDeviceIdInternal]] = rpDeviceId;
            }

            if (message.SystemProperties.TryGetValue(SystemProperties.RpConnectionModuleIdInternal, out string rpModuleId))
            {
                properties[SystemProperties.OutgoingSystemPropertiesMap[SystemProperties.RpConnectionModuleIdInternal]] = rpModuleId;
            }

            if (!this.addressConvertor.TryBuildProtocolAddressFromEdgeHubMessage(uriTemplateKey, message, properties, out string address))
            {
                throw new InvalidOperationException("Could not derive destination address using message system properties");
            }

            IByteBuffer            payload   = this.byteBufferConverter.ToByteBuffer(message.Body);
            ProtocolGatewayMessage pgMessage = new ProtocolGatewayMessage.Builder(payload, address)
                                               .WithId(lockToken)
                                               .WithCreatedTimeUtc(createdTimeUtc)
                                               .WithProperties(properties)
                                               .Build();

            return(pgMessage);
        }