/// <summary> /// Initialises a new instance of the <see cref="MainForm"/> class. /// </summary> public MainForm() { this.InitializeComponent(); var catalog = new AggregateCatalog(); catalog.Catalogs.Add(new DirectoryCatalog("..\\..\\Crypto", "*.dll")); var batch = new CompositionBatch(); batch.AddPart(this); this.compositionContainer = new CompositionContainer(catalog); ////get all the exports and load them into the appropriate list tagged with the importmany this.compositionContainer.Compose(batch); this.CryptoAlgorithmComboBox.DataSource = (new List<CryptoAlgorithm> { CryptoAlgorithm.None }).Union( this.CryptoProviders.Select(c => c.Metadata.Algorithm).Distinct()).ToList(); this.JoinedUsers = new BindingList<User>(); this.ConnectedUsersDataGridView.DataSource = this.JoinedUsers; this.userProxy = new UserServiceClient(new InstanceContext(this)); this.userProxy.Open(); this.messagingProxy = new MessagingServiceClient(new InstanceContext(this)); this.messagingProxy.Open(); this.userProxy.Subscribe(); foreach (var u in this.userProxy.GetJoinedUsers()) { this.AddUser(u); } this.uiSyncContext = SynchronizationContext.Current; }
public async Task SendAsyncThrowsIfAGetTwinMessageDoesNotHaveACorrelationId() { ProtocolGatewayMessage message = new ProtocolGatewayMessage.Builder(ByteBufferConverter.ToByteBuffer(new byte[0]), "$iothub/twin/GET/") .Build(); var listener = new Mock <IDeviceListener>(MockBehavior.Strict); var client = new MessagingServiceClient(listener.Object, ProtocolGatewayMessageConverter.Value, ByteBufferConverter); await Assert.ThrowsAsync <InvalidOperationException>(() => client.SendAsync(message)); }
public async Task SendAsyncThrowsIfATwinMessageHasASubresource(string address) { ProtocolGatewayMessage message = new ProtocolGatewayMessage.Builder(ByteBufferConverter.ToByteBuffer(new byte[0]), address) .Build(); var listener = new Mock <IDeviceListener>(MockBehavior.Strict); var client = new MessagingServiceClient(listener.Object, ProtocolGatewayMessageConverter.Value, ByteBufferConverter); await Assert.ThrowsAsync <InvalidOperationException>(() => client.SendAsync(message)); }
public async Task SendAsyncThrowsIfMessageAddressIsNullOrWhiteSpace() { ProtocolGatewayMessage message = new ProtocolGatewayMessage.Builder(ByteBufferConverter.ToByteBuffer(new byte[] { 0 }), null) .Build(); var listener = Mock.Of <IDeviceListener>(); var converter = Mock.Of <IMessageConverter <IProtocolGatewayMessage> >(); IMessagingServiceClient client = new MessagingServiceClient(listener, converter, ByteBufferConverter); await Assert.ThrowsAsync <ArgumentException>(() => client.SendAsync(message)); }
public async Task SendAsyncThrowsIfTheTwinMessageIsInvalid() { ProtocolGatewayMessage message = new ProtocolGatewayMessage.Builder(ByteBufferConverter.ToByteBuffer(new byte[0]), "$iothub/twin/unknown") .Build(); var listener = new Mock <IDeviceListener>(MockBehavior.Strict); var client = new MessagingServiceClient(listener.Object, ProtocolGatewayMessageConverter.Value, ByteBufferConverter); await Assert.ThrowsAsync(typeof(InvalidOperationException), () => client.SendAsync(message)); }
public async Task SendAsyncSendsTheRequestedMethod() { Mock <IDeviceListener> listener = MakeDeviceListenerSpy(); ProtocolGatewayMessage message = new ProtocolGatewayMessage.Builder(ByteBufferConverter.ToByteBuffer(new byte[0]), "$iothub/methods/res/200/?$rid=123") .Build(); var client = new MessagingServiceClient(listener.Object, ProtocolGatewayMessageConverter.Value, ByteBufferConverter); await client.SendAsync(message); listener.Verify(p => p.ProcessMethodResponseAsync(It.Is <IMessage>(x => x.Properties[SystemProperties.StatusCode] == "200" && x.Properties[SystemProperties.CorrelationId] == "123")), Times.Once); }
public async Task SendAsyncForwardsMessagesToTheDeviceListener() { Messages m = MakeMessages(); Mock <IDeviceListener> listener = MakeDeviceListenerSpy(); var client = new MessagingServiceClient(listener.Object, ProtocolGatewayMessageConverter.Value, ByteBufferConverter); await client.SendAsync(m.Source); listener.Verify( x => x.ProcessDeviceMessageAsync(It.Is((IMessage actual) => actual.Equals(m.Expected))), Times.Once); }
public async Task TestAbandonAsyncDoesNothingWhenMessageIdIsInvalid(string messageId) { // Arrange IMessageConverter <IProtocolGatewayMessage> messageConverter = ProtocolGatewayMessageConverter.Value; var deviceListener = new Mock <IDeviceListener>(MockBehavior.Strict); // Act var messagingServiceClient = new MessagingServiceClient(deviceListener.Object, messageConverter, ByteBufferConverter); await messagingServiceClient.AbandonAsync(messageId); // Assert deviceListener.VerifyAll(); }
public async Task TestCompleteAsyncDoesNothingWhenMessageIdIsInvalid(string messageId) { // Arrange IMessageConverter <IProtocolGatewayMessage> messageConverter = ProtocolGatewayMessageConverter.Value; var deviceListener = new Mock <IDeviceListener>(MockBehavior.Strict); // Act var messagingServiceClient = new MessagingServiceClient(deviceListener.Object, messageConverter, ByteBufferConverter, this.GetSessionStatePersistenceProvider()); await messagingServiceClient.CompleteAsync(messageId); // Assert deviceListener.VerifyAll(); }
public async Task SendAsyncForwardsMessagesToTheDeviceListener() { Messages m = MakeMessages("devices/d1/messages/events/"); Mock <IDeviceListener> listener = MakeDeviceListenerSpy(); m.Expected.SystemProperties[SystemProperties.ConnectionDeviceId] = "d1"; var client = new MessagingServiceClient(listener.Object, ProtocolGatewayMessageConverter.Value, ByteBufferConverter, this.GetSessionStatePersistenceProvider()); await client.SendAsync(m.Source); listener.Verify( x => x.ProcessDeviceMessageAsync(It.Is((IMessage actual) => actual.Equals(m.Expected))), Times.Once); }
public async Task SendAsyncRecognizesAGetTwinMessage() { ProtocolGatewayMessage message = new ProtocolGatewayMessage.Builder(ByteBufferConverter.ToByteBuffer(new byte[0]), "$iothub/twin/GET/?$rid=123") .Build(); Mock <IDeviceListener> listener = MakeDeviceListenerSpy(); var channel = Mock.Of <IMessagingChannel <IProtocolGatewayMessage> >(); var client = new MessagingServiceClient(listener.Object, ProtocolGatewayMessageConverter.Value, ByteBufferConverter); client.BindMessagingChannel(channel); await client.SendAsync(message); listener.Verify(x => x.ProcessDeviceMessageAsync(It.IsAny <IMessage>()), Times.Never); listener.Verify(x => x.SendGetTwinRequest(It.IsAny <string>()), Times.Once); }
/// <summary> /// Vrati vsechny kontakty systemu /// </summary> /// <returns></returns> public static List<ImUser> Contact_GetAllContacts() { try { using (MessagingServiceClient api = new MessagingServiceClient()) { List<ImUser> users = new List<ImUser>(api.Contact_GetAllContacts().ToList()); api.Close(); return users; } } catch (Exception) { return null; } }
/// <summary> /// Vraci objekt uzivatele dle jeho ID /// </summary> /// <param name="id_usr"></param> /// <returns></returns> public static ImUser Contact_GetContactById(int id_usr) { try { using (MessagingServiceClient api = new MessagingServiceClient()) { ImUser user = api.Contact_GetAllContacts().Where(c => c.ID_USR.Equals(id_usr)).FirstOrDefault(); api.Close(); return user; } } catch (Exception) { return null; } }
public async Task SendAsyncDoesNotSendAPatchResponseWithoutACorrelationId() { var edgeHub = Mock.Of <IEdgeHub>(e => e.UpdateReportedPropertiesAsync(It.IsAny <IIdentity>(), It.IsAny <IMessage>()) == Task.CompletedTask); IDeviceListener listener = new DeviceMessageHandler(Mock.Of <IIdentity>(i => i.Id == "d1"), edgeHub, Mock.Of <IConnectionManager>()); var channel = new Mock <IMessagingChannel <IProtocolGatewayMessage> >(); ProtocolGatewayMessage message = new ProtocolGatewayMessage.Builder(ByteBufferConverter.ToByteBuffer(new byte[0]), "$iothub/twin/PATCH/properties/reported/") .Build(); var client = new MessagingServiceClient(listener, ProtocolGatewayMessageConverter.Value, ByteBufferConverter); client.BindMessagingChannel(channel.Object); await client.SendAsync(message); channel.Verify(x => x.Handle(It.IsAny <IProtocolGatewayMessage>()), Times.Never); }
public async Task SendAsyncRecognizesAPatchTwinMessage() { Mock <IDeviceListener> listener = MakeDeviceListenerSpy(); var channel = Mock.Of <IMessagingChannel <IProtocolGatewayMessage> >(); var client = new MessagingServiceClient(listener.Object, ProtocolGatewayMessageConverter.Value, ByteBufferConverter); client.BindMessagingChannel(channel); string patch = "{\"name\":\"value\"}"; ProtocolGatewayMessage message = new ProtocolGatewayMessage.Builder(ByteBufferConverter.ToByteBuffer(Encoding.UTF8.GetBytes(patch)), "$iothub/twin/PATCH/properties/reported/?$rid=123") .Build(); await client.SendAsync(message); listener.Verify(x => x.UpdateReportedPropertiesAsync(It.Is((IMessage m) => Encoding.UTF8.GetString(m.Body).Equals(patch)), It.IsAny <string>()), Times.Once); }
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 async Task TestAbandonAsyncCallsDeviceListener() { // Arrange string messageId = Guid.NewGuid().ToString(); IMessageConverter <IProtocolGatewayMessage> messageConverter = ProtocolGatewayMessageConverter.Value; var deviceListener = new Mock <IDeviceListener>(MockBehavior.Strict); deviceListener.Setup( d => d.ProcessMessageFeedbackAsync( It.Is <string>(s => s.Equals(messageId, StringComparison.OrdinalIgnoreCase)), It.Is <FeedbackStatus>(f => f == FeedbackStatus.Abandon))) .Returns(TaskEx.Done); // Act var messagingServiceClient = new MessagingServiceClient(deviceListener.Object, messageConverter, ByteBufferConverter); await messagingServiceClient.AbandonAsync(messageId); // Assert deviceListener.VerifyAll(); }
/// <summary> /// Vrati osobni kontakty daneho uzivatele /// </summary> /// <param name="nick"></param> /// <param name="password"></param> /// <returns></returns> public static List<ImUser> Contact_GetMyContacts(int id_usr, string password) { try { using (MessagingServiceClient api = new MessagingServiceClient()) { if (api.User_Validate(id_usr, password)) { List<ImUser> users = new List<ImUser>(api.Contact_GetMyContacts(id_usr, password).ToList()); return users; } else { return null; } } } catch (Exception) { return null; } }
/// <summary> /// Windows the closed. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void WindowClosed(object sender, EventArgs e) { this.LeaveUser(); this.userProxy.Unsubscribe(); this.messagingProxy.Close(); this.messagingProxy = null; this.userProxy.Close(); this.userProxy = null; }
/// <summary> /// Initialises a new instance of the <see cref="MessagingWindow"/> class. /// </summary> /// <param name="name"> /// The name. /// </param> /// <param name="userServiceClient"> /// The user service client. /// </param> /// <param name="messagingServiceClient"> /// The messaging service client. /// </param> public MessagingWindow( string name, UserServiceClient userServiceClient, MessagingServiceClient messagingServiceClient) : this(name) { this.userProxy = userServiceClient; this.messagingProxy = messagingServiceClient; }
/// <summary> /// Handles the FormClosing event of the MainForm control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param> private void MainFormFormClosing(object sender, FormClosingEventArgs e) { this.userProxy.Unsubscribe(); this.messagingProxy.Close(); this.messagingProxy = null; this.userProxy.Close(); this.userProxy = null; }
/// <summary> /// Odeslani(vlozeni) zpravy do systemu /// </summary> /// <param name="nick"></param> /// <param name="password"></param> /// <param name="msg"></param> /// <returns></returns> public static bool Message_InsertMessageForUser(int id_usr, string password, string msg_body, int msg_type, int[] dest_users) { try { using (MessagingServiceClient api = new MessagingServiceClient()) { if (api.User_Validate(id_usr, password)) { return api.Message_InsertMessageForUser(id_usr, password, msg_body, msg_type, dest_users); } else { return false; } } } catch (Exception) { return false; } }
/// <summary> /// Registrace noveho uzivatele do systemu /// </summary> /// <param name="nick"></param> /// <param name="password"></param> /// <returns></returns> public static int User_RegisterNewContact(string nick, string password) { try { using (MessagingServiceClient api = new MessagingServiceClient()) { return api.User_RegisterNewContact(nick, password); } } catch (Exception) { return 0; } return 0; }
private void button1_Click(object sender, EventArgs e) { MessagingServiceClient proxy = new MessagingServiceClient(); MessageBox.Show(proxy.SendMessage(string.Format("Hello from {0}", this.Text))); }
/// <summary> /// Overeni spravnosti zdali jmeno souhlasi s heslem /// </summary> /// <param name="nick"></param> /// <param name="password"></param> /// <returns></returns> public static bool User_Validate(int id_usr, string password) { try { using (MessagingServiceClient api = new MessagingServiceClient()) { return api.User_Validate(id_usr, password); } } catch (Exception) { return false; } }
/// <summary> /// Vrati vsechny uzivatelske zpravy /// </summary> /// <param name="nick"></param> /// <param name="password"></param> /// <returns></returns> public static List<ImMessage> Message_GetMyMessages(int id_usr, string password) { try { using (MessagingServiceClient api = new MessagingServiceClient()) { if (api.User_Validate(id_usr, password)) { List<ImMessage> messages = new List<ImMessage>(api.Message_GetMyMessages(id_usr, password)); return messages; } else { return null; } } } catch (Exception) { return null; } }
/// <summary> /// Windows the loaded. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param> private void WindowLoaded(object sender, RoutedEventArgs e) { var catalog = new AggregateCatalog(); if (Directory.Exists("..\\..\\Crypto")) { catalog.Catalogs.Add(new DirectoryCatalog("..\\..\\Crypto", "*.dll")); } var batch = new CompositionBatch(); batch.AddPart(this); this.compositionContainer = new CompositionContainer(catalog); ////get all the exports and load them into the appropriate list tagged with the importmany this.compositionContainer.Compose(batch); this.CryptoAlgorithmComboBox.ItemsSource = (new List<CryptoAlgorithm> { CryptoAlgorithm.None }).Union( this.CryptoProviders.Select(c => c.Metadata.Algorithm).Distinct()).ToList(); if (this.userProxy == null) { this.userProxy = new UserServiceClient(new InstanceContext(this)); } this.userProxy.Open(); if (this.messagingProxy == null) { this.messagingProxy = new MessagingServiceClient(new InstanceContext(this)); } this.messagingProxy.Open(); this.userProxy.Subscribe(); foreach (var u in this.userProxy.GetJoinedUsers()) { this.AddUser(u); } this.uiSyncContext = SynchronizationContext.Current; this.JoinUser(this.name); }