private void OnMessageReceived(object sender, TypedRequestReceivedEventArgs <MyRequest> e) { // Insert received message at the beginning of the listbox. // Note: we can directly access the listbox because we set threading mode of // InputChannelThreading to the main UI thread. if (e.RequestMessage.Ack == false) { Random rnd = new Random(); ReceivedMessagesListBox.Items.Insert(0, e.RequestMessage); MyResponse resp = new MyResponse(); resp.Temperature = (float)rnd.Next(0, 300) / 10.0f; resp.Pressure = (float)rnd.Next(900, 1100) / 10.0f; resp.Humidity = (float)rnd.Next(0, 1000) / 10.0f; resp.Time = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; // Send message to client which is selected in the listbox. myReceiver.SendResponseMessage(e.ResponseReceiverId, resp); } else { listBoxAcknowledge.Items.Insert(0, e.ResponseReceiverId + ": ACK"); } }
private void OnMessageReceived(object sender, TypedRequestReceivedEventArgs <MyRequest> e) { if (e.RequestMessage.Ack == false) { MyResponse resp = SendWeatherInfo(e.RequestMessage.TimeUnit, e.RequestMessage.numberOfTimeUnitsBack); // Send message to client which is selected in the listbox. if (resp != null) { myReceiver.SendResponseMessage(e.ResponseReceiverId, resp); } } else { if (Serial.IsOpen) { if (clients.IndexOf(GetClient(e.ResponseReceiverId)) != null) { int clientNumver = clients.IndexOf(GetClient(e.ResponseReceiverId)); Serial.Write((clientNumver + 1).ToString() + "#"); LedSerialArgs led = new LedSerialArgs(clientNumver + 1); SendLedInfo?.Invoke(null, led); } else { return; } } } }
// It is called when a message is received. private static void OnMessageReceived(object sender, TypedRequestReceivedEventArgs <MyRequest> e) { Console.WriteLine("Received: " + e.RequestMessage.Text); // Create the response message. MyResponse aResponse = new MyResponse(); aResponse.Length = e.RequestMessage.Text.Length; // Send the response message back to the client. myReceiver.SendResponseMessage(e.ResponseReceiverId, aResponse); String msg = e.RequestMessage.Text; if (msg.Equals("Open")) { Console.WriteLine("Simulate Open Action"); //MainWindow.simulateOpenAction(); } else if (msg.Equals("Close")) { Console.WriteLine("Simulate Close Action"); // MainWindow.simulateCloseAction(); } }
public void SyncRequestResponse() { IDuplexTypedMessageReceiver <string, int> aReceiver = DuplexTypedMessagesFactory.CreateDuplexTypedMessageReceiver <string, int>(); aReceiver.MessageReceived += (x, y) => { aReceiver.SendResponseMessage(y.ResponseReceiverId, (y.RequestMessage * 10).ToString()); }; ISyncDuplexTypedMessageSender <string, int> aSender = DuplexTypedMessagesFactory.CreateSyncDuplexTypedMessageSender <string, int>(); try { aReceiver.AttachDuplexInputChannel(InputChannel); aSender.AttachDuplexOutputChannel(OutputChannel); string aResult = aSender.SendRequestMessage(100); Assert.AreEqual("1000", aResult); } finally { aSender.DetachDuplexOutputChannel(); aReceiver.DetachDuplexInputChannel(); } }
public void ThreadDispatching() { IDuplexTypedMessageReceiver <string, int> aReceiver = DuplexTypedMessagesFactory.CreateDuplexTypedMessageReceiver <string, int>(); aReceiver.MessageReceived += (x, y) => { aReceiver.SendResponseMessage(y.ResponseReceiverId, (y.RequestMessage * 10).ToString()); }; // Set windows working thread dispatcher. Dispatcher aWindowsDispatcher = WindowsDispatching.StartNewWindowsDispatcher(); WindowsDispatching aThreadDispatching = new WindowsDispatching(aWindowsDispatcher); ((DuplexTypedMessagesFactory)DuplexTypedMessagesFactory).SyncDuplexTypedSenderThreadMode = aThreadDispatching; ISyncDuplexTypedMessageSender <string, int> aSender = DuplexTypedMessagesFactory.CreateSyncDuplexTypedMessageSender <string, int>(); int aOpenConnectionThreadId = 0; aSender.ConnectionOpened += (x, y) => { aOpenConnectionThreadId = Thread.CurrentThread.ManagedThreadId; }; ManualResetEvent aConnectionClosedEvent = new ManualResetEvent(false); int aCloseConnectionThreadId = 0; aSender.ConnectionClosed += (x, y) => { aCloseConnectionThreadId = Thread.CurrentThread.ManagedThreadId; aConnectionClosedEvent.Set(); }; try { aReceiver.AttachDuplexInputChannel(InputChannel); aSender.AttachDuplexOutputChannel(OutputChannel); string aResult = aSender.SendRequestMessage(100); aReceiver.AttachedDuplexInputChannel.DisconnectResponseReceiver(aSender.AttachedDuplexOutputChannel.ResponseReceiverId); aConnectionClosedEvent.WaitOne(); Assert.AreEqual("1000", aResult); Assert.AreEqual(aWindowsDispatcher.Thread.ManagedThreadId, aOpenConnectionThreadId); Assert.AreEqual(aWindowsDispatcher.Thread.ManagedThreadId, aCloseConnectionThreadId); } finally { aSender.DetachDuplexOutputChannel(); aReceiver.DetachDuplexInputChannel(); if (aWindowsDispatcher != null) { WindowsDispatching.StopWindowsDispatcher(aWindowsDispatcher); } } }
// Отправка сообщения message клиенту recID public void SendMessage(string recID, string str) { // Create the response message. MyResponse aResponse = new MyResponse(); aResponse.Text = str; // Send the response message back to the client. myReceiver.SendResponseMessage(recID, aResponse); }
public ChannelWrapper() { // Internal messaging used for messaging between channel unwrapper // and typed message receivers. // We want that requests do not block each other. So every request will be processed in its own thread. IMessagingSystemFactory anInternalMessaging = new ThreadPoolMessagingSystemFactory(); // All messages are received via one channel. So we must provide "unwrapper" forwarding incoming messages // to correct receivers. IChannelWrapperFactory aChannelWrapperFactory = new ChannelWrapperFactory(); myDuplexChannelUnwrapper = aChannelWrapperFactory.CreateDuplexChannelUnwrapper(anInternalMessaging); // To connect receivers and the unwrapper with duplex channels we can use the following helper class. IConnectionProviderFactory aConnectionProviderFactory = new ConnectionProviderFactory(); IConnectionProvider aConnectionProvider = aConnectionProviderFactory.CreateConnectionProvider(anInternalMessaging); // Factory to create message receivers. IDuplexTypedMessagesFactory aMessageReceiverFactory = new DuplexTypedMessagesFactory(); // Create receiver to sum two numbers. plusReceiver = aMessageReceiverFactory.CreateDuplexTypedMessageReceiver <TestOutput, TestInput>(); plusReceiver.MessageReceived += (s, e) => { Console.WriteLine("PLUS: " + e.RequestMessage.Name + " | " + e.RequestMessage.Value); plusReceiver.SendResponseMessage(e.ResponseReceiverId, new TestOutput { Value = e.RequestMessage.Name + "+" + e.RequestMessage.Value }); }; // attach method handling the request aConnectionProvider.Attach(plusReceiver, "plus"); // attach the input channel to get messages from unwrapper // Create receiver to sum two numbers. minusReceiver = aMessageReceiverFactory.CreateDuplexTypedMessageReceiver <TestOutput, TestInput>(); minusReceiver.MessageReceived += (s, e) => { Console.WriteLine("MINUS: " + e.RequestMessage.Name + " | " + e.RequestMessage.Value); minusReceiver.SendResponseMessage(e.ResponseReceiverId, new TestOutput { Value = e.RequestMessage.Name + "-" + e.RequestMessage.Value }); }; // attach method handling the request aConnectionProvider.Attach(minusReceiver, "minus"); // attach the input channel to get messages from unwrapper // Create receiver to sum two numbers. dotReceiver = aMessageReceiverFactory.CreateDuplexTypedMessageReceiver <TestOutput, TestInput>(); dotReceiver.MessageReceived += (s, e) => { Console.WriteLine("DOT: " + e.RequestMessage.Name + " | " + e.RequestMessage.Value); dotReceiver.SendResponseMessage(e.ResponseReceiverId, new TestOutput { Value = e.RequestMessage.Name + "." + e.RequestMessage.Value }); }; // attach method handling the request aConnectionProvider.Attach(dotReceiver, "dot"); // attach the input channel to get messages from unwrapper }
// It is called when a message is received. private static void OnMessageReceived(object sender, TypedRequestReceivedEventArgs <MyRequest> e) { Console.WriteLine("Received: " + e.RequestMessage.Text); // Create the response message. MyResponse aResponse = new MyResponse(); aResponse.Length = e.RequestMessage.Text.Length; // Send the response message back to the client. myReceiver.SendResponseMessage(e.ResponseReceiverId, aResponse); }
// It is called when a message is received. private static void OnMessageReceived(object sender, TypedRequestReceivedEventArgs <MyRequest> e) { Console.WriteLine("Jumlah PC : " + e.RequestMessage.Text); Console.WriteLine("Waktu Mulai : " + e.RequestMessage.Text2); Console.WriteLine("Lama Pemakaian : " + e.RequestMessage.Text3); // Create the response message. MyResponse aResponse = new MyResponse(); //aResponse.Length = e.RequestMessage.Text.Length; // Send the response message back to the client. myReceiver.SendResponseMessage(e.ResponseReceiverId, aResponse); }
/// <summary> /// Sends data to the andriod application /// </summary> public void SendQuestion(String s) { // Create the response message. MyResponse aResponse = new MyResponse(); aResponse.ResponseMessage = s; aResponse.Length = s.Length; if (SenderID == null || SenderID == "") { Debug.WriteLine("Send ID value is :" + SenderID); return; } // Send the response message back to the client. myReceiver.SendResponseMessage(SenderID, aResponse.ResponseMessage); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnMessageReceived(object sender, TypedRequestReceivedEventArgs <String> e) { Console.WriteLine("Received: " + e); AccelerometerDataChangedEventArgs args = new AccelerometerDataChangedEventArgs(); args.s = e.RequestMessage; OnAccDataChanged(args); // Create the response message. MyResponse aResponse = new MyResponse(); aResponse.Length = e.RequestMessage.Length; // Send the response message back to the client. myReceiver.SendResponseMessage(e.ResponseReceiverId, aResponse.Length.ToString()); }
private static void OnMessageReceived(object sender, TypedRequestReceivedEventArgs <Range> e) { Console.WriteLine("Calculate From: {0} To: {1}", e.RequestMessage.From, e.RequestMessage.To); // Calculate requested range. double aResult = 0.0; double aDx = 0.000000001; for (double x = e.RequestMessage.From; x < e.RequestMessage.To; x += aDx) { aResult += 2 * Math.Sqrt(1 - x * x) * aDx; } // Response back the result. IDuplexTypedMessageReceiver <double, Range> aReceiver = (IDuplexTypedMessageReceiver <double, Range>)sender; aReceiver.SendResponseMessage(e.ResponseReceiverId, aResult); }
public void SendResponse(string responseReceiverId, ResponseType response) { try { switch (MessagesFactoryType) { case YZXMessagesFactoryType.Duplex: if (DReceiver.IsDuplexInputChannelAttached) { DReceiver.SendResponseMessage(responseReceiverId, response); } else { } break; } } catch (Exception ex) { Console.WriteLine(responseReceiverId); Console.WriteLine(ex.ToString()); } }
public void SendReceive_1Message_PerClientSerializer() { string aClient1Id = null; IDuplexTypedMessagesFactory aSender1Factory = new DuplexTypedMessagesFactory(new XmlStringSerializer()); IDuplexTypedMessagesFactory aSender2Factory = new DuplexTypedMessagesFactory(new BinarySerializer()); IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory() { SerializerProvider = x => (x == aClient1Id) ? (ISerializer) new XmlStringSerializer() : (ISerializer) new BinarySerializer() }; IDuplexTypedMessageSender <int, int> aSender1 = aSender1Factory.CreateDuplexTypedMessageSender <int, int>(); IDuplexTypedMessageSender <int, int> aSender2 = aSender2Factory.CreateDuplexTypedMessageSender <int, int>(); IDuplexTypedMessageReceiver <int, int> aReceiver = aReceiverFactory.CreateDuplexTypedMessageReceiver <int, int>(); aReceiver.ResponseReceiverConnected += (x, y) => aClient1Id = aClient1Id ?? y.ResponseReceiverId; int aReceivedMessage = 0; aReceiver.MessageReceived += (x, y) => { aReceivedMessage = y.RequestMessage; // Send the response aReceiver.SendResponseMessage(y.ResponseReceiverId, 1000); }; AutoResetEvent aSender1MessageReceivedEvent = new AutoResetEvent(false); int aSender1ReceivedResponse = 0; aSender1.ResponseReceived += (x, y) => { aSender1ReceivedResponse = y.ResponseMessage; // Signal that the response message was received -> the loop is closed. aSender1MessageReceivedEvent.Set(); }; AutoResetEvent aSender2MessageReceivedEvent = new AutoResetEvent(false); int aSender2ReceivedResponse = 0; aSender2.ResponseReceived += (x, y) => { aSender2ReceivedResponse = y.ResponseMessage; // Signal that the response message was received -> the loop is closed. aSender2MessageReceivedEvent.Set(); }; try { aReceiver.AttachDuplexInputChannel(DuplexInputChannel); aSender1.AttachDuplexOutputChannel(MessagingSystemFactory.CreateDuplexOutputChannel(DuplexInputChannel.ChannelId)); aSender2.AttachDuplexOutputChannel(MessagingSystemFactory.CreateDuplexOutputChannel(DuplexInputChannel.ChannelId)); aSender1.SendRequestMessage(2000); aSender2.SendRequestMessage(2000); // Wait for the signal that the message is received. aSender1MessageReceivedEvent.WaitIfNotDebugging(2000); aSender2MessageReceivedEvent.WaitIfNotDebugging(2000); //Assert.IsTrue(aMessageReceivedEvent.WaitOne(2000)); } finally { aSender1.DetachDuplexOutputChannel(); aSender2.DetachDuplexOutputChannel(); aReceiver.DetachDuplexInputChannel(); // Give some time to complete tracing. Thread.Sleep(300); } // Check received values Assert.AreEqual(2000, aReceivedMessage); Assert.AreEqual(1000, aSender1ReceivedResponse); Assert.AreEqual(1000, aSender2ReceivedResponse); }