async Task ProcessTwinAsync(IProtocolGatewayMessage protocolGatewayMessage) { var properties = new Dictionary<StringSegment, StringSegment>(); if (TwinAddressHelper.TryParseOperation(protocolGatewayMessage.Address, properties, out TwinAddressHelper.Operation operation, out StringSegment subresource)) { bool hasCorrelationId = properties.TryGetValue(RequestId, out StringSegment correlationId); switch (operation) { case TwinAddressHelper.Operation.TwinGetState: EnsureNoSubresource(subresource); if (!hasCorrelationId || correlationId.Length == 0) { throw new InvalidOperationException("Correlation id is missing or empty."); } await this.deviceListener.SendGetTwinRequest(correlationId.ToString()); Events.GetTwin(this.deviceListener.Identity); break; case TwinAddressHelper.Operation.TwinPatchReportedState: EnsureNoSubresource(subresource); IMessage forwardMessage = new EdgeMessage.Builder(this.byteBufferConverter.ToByteArray(protocolGatewayMessage.Payload)) .Build(); await this.deviceListener.UpdateReportedPropertiesAsync(forwardMessage, hasCorrelationId ? correlationId.ToString() : string.Empty); Events.UpdateReportedProperties(this.deviceListener.Identity); break; default: throw new InvalidOperationException("Twin operation is not supported."); } }
public async Task TestMessageCleanupWhenException() { // Arrange IMessageConverter <IProtocolGatewayMessage> messageConverter = ProtocolGatewayMessageConverter.Value; Mock <IDeviceListener> deviceListener = MakeDeviceListenerSpy(); var payload = new Mock <IByteBuffer>(); payload.Setup(p => p.Release()).Returns(true); Exception expectedException = null; // Act var messagingServiceClient = new MessagingServiceClient(deviceListener.Object, messageConverter, ByteBufferConverter); IProtocolGatewayMessage protocolGatewayMessage = messagingServiceClient.CreateMessage(null, payload.Object); try { await messagingServiceClient.SendAsync(protocolGatewayMessage); } catch (Exception ex) { expectedException = ex; } // Assert payload.VerifyAll(); Assert.NotNull(expectedException); }
public async Task TestReceiveMessagingChannelDispose() { IProtocolGatewayMessage msg = null; IMessageConverter <IProtocolGatewayMessage> messageConverter = ProtocolGatewayMessageConverter.Value; var dp = new DeviceProxy(Channel.Object, MockIdentity, messageConverter, ByteBufferConverter); var cloudProxy = new Mock <ICloudProxy>(); cloudProxy.Setup(d => d.CloseAsync()).Callback( () => { }); var connectionManager = new Mock <IConnectionManager>(); connectionManager.Setup(c => c.GetCloudConnection(It.IsAny <string>())) .Returns(Task.FromResult(Option.Some(cloudProxy.Object))); var deviceListner = new DeviceMessageHandler(MockIdentity, EdgeHub.Object, connectionManager.Object, DefaultMessageAckTimeout); var messagingServiceClient = new MessagingServiceClient(deviceListner, messageConverter, ByteBufferConverter); Channel.Setup(r => r.Handle(It.IsAny <IProtocolGatewayMessage>())) .Callback <IProtocolGatewayMessage>( m => { msg = m; messagingServiceClient.DisposeAsync(new Exception("Some issue")); }); messagingServiceClient.BindMessagingChannel(Channel.Object); IMessage message = new EdgeMessage.Builder(new byte[] { 1, 2, 3 }).Build(); await dp.SendC2DMessageAsync(message); Assert.NotNull(msg); }
public async Task TestReceiveMessagingChannelAbandon() { IProtocolGatewayMessage msg = null; IMessageConverter <IProtocolGatewayMessage> messageConverter = ProtocolGatewayMessageConverter.Value; var dp = new DeviceProxy(Channel.Object, MockIdentity, messageConverter, ByteBufferConverter); var cloudProxy = new Mock <ICloudProxy>(); cloudProxy.Setup(d => d.SendFeedbackMessageAsync(It.IsAny <string>(), It.IsAny <FeedbackStatus>())).Callback <string, FeedbackStatus>( (mid, status) => { Assert.Equal(FeedbackStatus.Abandon, status); }); var connectionManager = new Mock <IConnectionManager>(); connectionManager.Setup(c => c.GetCloudConnection(It.IsAny <string>())) .Returns(Task.FromResult(Option.Some(cloudProxy.Object))); var deviceListner = new DeviceMessageHandler(MockIdentity, EdgeHub.Object, connectionManager.Object); var messagingServiceClient = new MessagingServiceClient(deviceListner, messageConverter, ByteBufferConverter); Channel.Setup(r => r.Handle(It.IsAny <IProtocolGatewayMessage>())) .Callback <IProtocolGatewayMessage>( m => { msg = m; messagingServiceClient.AbandonAsync(msg.Id); }); messagingServiceClient.BindMessagingChannel(Channel.Object); IMessage message = new EdgeMessage.Builder(new byte[] { 1, 2, 3 }).Build(); await dp.SendC2DMessageAsync(message); Assert.NotNull(msg); }
public Task SendC2DMessageAsync(IMessage message) { message.SystemProperties[TemplateParameters.DeviceIdTemplateParam] = this.Identity.Id; message.SystemProperties[SystemProperties.OutboundUri] = Constants.OutboundUriC2D; IProtocolGatewayMessage pgMessage = this.messageConverter.FromMessage(message); this.channel.Handle(pgMessage); Events.SendMessage(this.Identity); return(Task.FromResult(true)); }
public Task SendTwinUpdate(IMessage twin) { twin.SystemProperties[SystemProperties.LockToken] = Constants.TwinLockToken; twin.SystemProperties[SystemProperties.OutboundUri] = Constants.OutboundUriTwinEndpoint; IProtocolGatewayMessage pgMessage = this.messageConverter.FromMessage(twin); this.channel.Handle(pgMessage); Events.SentTwinUpdateToDevice(this.Identity); return(Task.CompletedTask); }
public async Task TestMessageCleanup() { // Arrange IMessageConverter <IProtocolGatewayMessage> messageConverter = ProtocolGatewayMessageConverter.Value; Mock <IDeviceListener> deviceListener = MakeDeviceListenerSpy(); var payload = new Mock <IByteBuffer>(); payload.Setup(p => p.Release()).Returns(true); // Act var messagingServiceClient = new MessagingServiceClient(deviceListener.Object, messageConverter, ByteBufferConverter); IProtocolGatewayMessage protocolGatewayMessage = messagingServiceClient.CreateMessage("devices/Device1/messages/events/", payload.Object); await messagingServiceClient.SendAsync(protocolGatewayMessage); // Assert payload.VerifyAll(); }
public Task SendMessageAsync(IMessage message, string input) { bool result = false; if (this.Identity is IModuleIdentity moduleIdentity) { message.SystemProperties[TemplateParameters.DeviceIdTemplateParam] = moduleIdentity.DeviceId; message.SystemProperties[Constants.ModuleIdTemplateParameter] = moduleIdentity.ModuleId; message.SystemProperties[SystemProperties.InputName] = input; message.SystemProperties[SystemProperties.OutboundUri] = Constants.OutboundUriModuleEndpoint; IProtocolGatewayMessage pgMessage = this.messageConverter.FromMessage(message); this.channel.Handle(pgMessage); result = true; } return(Task.FromResult(result)); }
public async Task SendAsync(IProtocolGatewayMessage message) { Preconditions.CheckNotNull(message, nameof(message)); using (message) { Preconditions.CheckNonWhiteSpace(message.Address, nameof(message.Address)); if (IsTwinAddress(message.Address)) { await this.ProcessTwinAsync(message); } else if (IsMethodResponseAddress(message.Address)) { await this.ProcessMethodResponse(message); } else { await this.ProcessMessageAsync(message); } } }