void Upload(int repeat, int bytesLength) { // create data to send var data = new byte[bytesLength]; var random = new Random(); random.NextBytes(data); int i = 0; Log("Start sending " + bytesLength + " bytes of data " + repeat + " times to " + Worker4504_OutputChannel.ResponseReceiverId); _stopwatch = new Stopwatch(); _stopwatch.Start(); // notify server Command_MessageSender.SendMessage("1|receive|" + Worker4504_OutputChannel.ResponseReceiverId + "|" + bytesLength + "|" + repeat); do { Worker4504_OutputChannel.SendMessage(data); i++; } while (i < repeat); // notify server that sending complete var b = new byte[0]; Worker4504_OutputChannel.SendMessage(b); Log("Finished sending " + bytesLength + " bytes of data " + repeat + " times to " + Worker4504_OutputChannel.ResponseReceiverId + " in " + _stopwatch.ElapsedMilliseconds + " milliseconds"); }
public void MulticastLoopback() { UdpMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory(new EasyProtocolFormatter()) { UnicastCommunication = false, MulticastGroupToReceive = "234.1.2.3", MulticastLoopback = true }; ManualResetEvent aMessageReceived = new ManualResetEvent(false); string aReceivedMessage = null; IDuplexOutputChannel anOutputChannel = aMessaging.CreateDuplexOutputChannel("udp://234.1.2.3:8090/", "udp://0.0.0.0:8090/"); anOutputChannel.ResponseMessageReceived += (x, y) => { aReceivedMessage = (string)y.Message; aMessageReceived.Set(); }; try { anOutputChannel.OpenConnection(); anOutputChannel.SendMessage("Hello"); aMessageReceived.WaitIfNotDebugging(1000); } finally { anOutputChannel.CloseConnection(); } Assert.AreEqual("Hello", aReceivedMessage); }
private bool InitializeWorkerConnection() { // Create TCP messaging IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory(); Worker4504OutputChannel = aMessaging.CreateDuplexOutputChannel("tcp://127.0.0.1:4504/"); // Subscribe to response messages. Worker4504OutputChannel.ConnectionClosed += Worker4504OutputChannel_ConnectionClosed; Worker4504OutputChannel.ConnectionOpened += Worker4504OutputChannel_ConnectionOpened; Worker4504OutputChannel.ResponseMessageReceived += Worker4504OutputChannel_ResponseMessageReceived; // Open connection and be able to send messages and receive response messages. Worker4504OutputChannel.OpenConnection(); Log("Channel id : " + Worker4504OutputChannel.ChannelId); // Send a message. byte[] data = new byte[1048576]; // initialize 1MB data //byte[] data = new byte[10]; // initialize 1MB data Random random = new Random(); random.NextBytes(data); Worker4504OutputChannel.SendMessage(data); Log("Sent data length : " + data.Length); // Close connection. //Worker4504OutputChannel.CloseConnection(); return(true); }
public void BroadcastFromClientToAllServices() { UdpMessagingSystemFactory anOutputChannelMessaging = new UdpMessagingSystemFactory(new EasyProtocolFormatter()) { UnicastCommunication = false, AllowSendingBroadcasts = true }; UdpMessagingSystemFactory anInputChannelMessaging = new UdpMessagingSystemFactory(new EasyProtocolFormatter()) { UnicastCommunication = false, ReuseAddress = true }; ManualResetEvent aMessage1Received = new ManualResetEvent(false); string aReceivedMessage1 = null; IDuplexInputChannel anInputChannel1 = anInputChannelMessaging.CreateDuplexInputChannel("udp://127.0.0.1:8095/"); anInputChannel1.MessageReceived += (x, y) => { aReceivedMessage1 = (string)y.Message; aMessage1Received.Set(); }; ManualResetEvent aMessage2Received = new ManualResetEvent(false); string aReceivedMessage2 = null; IDuplexInputChannel anInputChannel2 = anInputChannelMessaging.CreateDuplexInputChannel("udp://127.0.0.1:8095/"); anInputChannel2.MessageReceived += (x, y) => { aReceivedMessage2 = (string)y.Message; aMessage2Received.Set(); }; IDuplexOutputChannel anOutputChannel = anOutputChannelMessaging.CreateDuplexOutputChannel("udp://255.255.255.255:8095/", "udp://127.0.0.1"); try { anInputChannel1.StartListening(); anInputChannel2.StartListening(); anOutputChannel.OpenConnection(); anOutputChannel.SendMessage("Hello"); aMessage1Received.WaitIfNotDebugging(1000); aMessage2Received.WaitIfNotDebugging(1000); } finally { anOutputChannel.CloseConnection(); anInputChannel1.StopListening(); anInputChannel2.StopListening(); } Assert.AreEqual("Hello", aReceivedMessage1); Assert.AreEqual("Hello", aReceivedMessage2); }
protected void SendMessage(string duplexInputChannelId, string duplexInputChannelResponseReceiverId, string duplexOutputChannelId, object message) { using (EneterTrace.Entering()) { using (ThreadLock.Lock(myDuplexInputChannelContextManipulatorLock)) { try { // Get (or create) the duplex output channel that will be used IDuplexOutputChannel aDuplexOutputChannel = GetAssociatedDuplexOutputChannel(duplexInputChannelId, duplexInputChannelResponseReceiverId, duplexOutputChannelId); aDuplexOutputChannel.SendMessage(message); } catch (Exception err) { EneterTrace.Error(TracedObject + "failed to send the message to the duplex output channel '" + duplexOutputChannelId + "'.", err); throw; } } } }
public void A01_SimpleRequestResponse() { IDuplexOutputChannel aDuplexOutputChannel = MessagingSystem.CreateDuplexOutputChannel(ChannelId); IDuplexInputChannel aDuplexInputChannel = MessagingSystem.CreateDuplexInputChannel(ChannelId); // Received messages. List <int> aReceivedMessages = new List <int>(); aDuplexInputChannel.MessageReceived += (x, y) => { //EneterTrace.Info("Message Received"); lock (aReceivedMessages) { string aReceivedMessage = y.Message as string; int k = int.Parse(aReceivedMessage); aReceivedMessages.Add(k); k += 1000; aDuplexInputChannel.SendResponseMessage(y.ResponseReceiverId, k.ToString()); } }; // Received response messages. List <int> aReceivedResponseMessages = new List <int>(); AutoResetEvent anAllMessagesProcessedEvent = new AutoResetEvent(false); aDuplexOutputChannel.ResponseMessageReceived += (x, y) => { //EneterTrace.Info("Response Received"); lock (aReceivedResponseMessages) { string aReceivedMessage = y.Message as string; int k = int.Parse(aReceivedMessage); aReceivedResponseMessages.Add(k); if (k == 1019) { anAllMessagesProcessedEvent.Set(); } } }; try { aDuplexInputChannel.StartListening(); aDuplexOutputChannel.OpenConnection(); for (int i = 0; i < 20; ++i) { aDuplexOutputChannel.SendMessage(i.ToString()); } // Wait untill all messages are processed. anAllMessagesProcessedEvent.WaitOne(); } finally { aDuplexOutputChannel.CloseConnection(); aDuplexInputChannel.StopListening(); } aReceivedMessages.Sort(); Assert.AreEqual(20, aReceivedMessages.Count); for (int i = 0; i < 20; ++i) { Assert.AreEqual(i, aReceivedMessages[i]); } aReceivedResponseMessages.Sort(); Assert.AreEqual(20, aReceivedResponseMessages.Count); for (int i = 0; i < 20; ++i) { Assert.AreEqual(i + 1000, aReceivedResponseMessages[i]); } }
private bool InitializeWorkerConnection() { // Create TCP messaging IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory(); Worker4504OutputChannel = aMessaging.CreateDuplexOutputChannel("tcp://127.0.0.1:4504/"); // Subscribe to response messages. Worker4504OutputChannel.ConnectionClosed += Worker4504OutputChannel_ConnectionClosed; Worker4504OutputChannel.ConnectionOpened += Worker4504OutputChannel_ConnectionOpened; Worker4504OutputChannel.ResponseMessageReceived += Worker4504OutputChannel_ResponseMessageReceived; // Open connection and be able to send messages and receive response messages. Worker4504OutputChannel.OpenConnection(); Log("Channel id : " + Worker4504OutputChannel.ChannelId); // Send a message. byte[] data = new byte[1048576]; // initialize 1MB data //byte[] data = new byte[10]; // initialize 1MB data Random random = new Random(); random.NextBytes(data); Worker4504OutputChannel.SendMessage(data); Log("Sent data length : " + data.Length); // Close connection. //Worker4504OutputChannel.CloseConnection(); return true; }
public void LoadBalancerConnectionLogic() { IMessagingSystemFactory aMessaging = new SynchronousMessagingSystemFactory(); string aReceiverChannelId = ""; EventHandler <DuplexChannelMessageEventArgs> aRequestReceived = (x, y) => { // Echo the message. IDuplexInputChannel anInputChannel = (IDuplexInputChannel)x; aReceiverChannelId = anInputChannel.ChannelId; anInputChannel.SendResponseMessage(y.ResponseReceiverId, y.Message); }; string aResponseReceiverId = ""; ResponseReceiverEventArgs aDisconnectedLoadBalancerConnection = null; EventHandler <ResponseReceiverEventArgs> aDisconnectedFromReceiver = (x, y) => { aResponseReceiverId = ((IDuplexInputChannel)x).ChannelId; aDisconnectedLoadBalancerConnection = y; }; // Receivers. IDuplexInputChannel[] aReceivers = new IDuplexInputChannel[3]; for (int i = 0; i < aReceivers.Length; ++i) { aReceivers[i] = aMessaging.CreateDuplexInputChannel((i + 1).ToString()); aReceivers[i].MessageReceived += aRequestReceived; aReceivers[i].ResponseReceiverDisconnected += aDisconnectedFromReceiver; } // Clients. IDuplexOutputChannel aClient1 = aMessaging.CreateDuplexOutputChannel("LB"); string aReceivedResponse1 = ""; aClient1.ResponseMessageReceived += (x, y) => { aReceivedResponse1 = (string)y.Message; }; IDuplexOutputChannel aClient2 = aMessaging.CreateDuplexOutputChannel("LB"); string aReceivedResponse2 = ""; aClient2.ResponseMessageReceived += (x, y) => { aReceivedResponse2 = (string)y.Message; }; // Load Balancer ILoadBalancerFactory aLoadBalancerFactory = new RoundRobinBalancerFactory(aMessaging); ILoadBalancer aLoadBalancer = aLoadBalancerFactory.CreateLoadBalancer(); AutoResetEvent aReceiverRemovedEvent = new AutoResetEvent(false); string aRemovedReceiverId = ""; aLoadBalancer.RequestReceiverRemoved += (x, y) => { aRemovedReceiverId = y.ChannelId; aReceiverRemovedEvent.Set(); }; try { // Receivers start listening. foreach (IDuplexInputChannel aReceiver in aReceivers) { aReceiver.StartListening(); } // Load Balancer starts listening. for (int i = 0; i < aReceivers.Length; ++i) { aLoadBalancer.AddDuplexOutputChannel((i + 1).ToString()); } aLoadBalancer.AttachDuplexInputChannel(aMessaging.CreateDuplexInputChannel("LB")); // Clients connect the load balancer. aClient1.OpenConnection(); aClient2.OpenConnection(); // It shall use the next (second) receiver. aClient1.SendMessage("Hello."); Assert.AreEqual("Hello.", aReceivedResponse1); Assert.AreEqual("", aReceivedResponse2); Assert.AreEqual("2", aReceiverChannelId); aReceivedResponse1 = ""; aReceivedResponse2 = ""; // It shall use the next (third) receiver. aClient2.SendMessage("Hello."); Assert.AreEqual("", aReceivedResponse1); Assert.AreEqual("Hello.", aReceivedResponse2); Assert.AreEqual("3", aReceiverChannelId); aReceivedResponse1 = ""; aReceivedResponse2 = ""; // It is at the end of the pool so it shall starts from the beginning. aClient2.SendMessage("Hello."); Assert.AreEqual("", aReceivedResponse1); Assert.AreEqual("Hello.", aReceivedResponse2); Assert.AreEqual("1", aReceiverChannelId); aReceivedResponse1 = ""; aReceivedResponse2 = ""; // Let's remove the second receiver. aLoadBalancer.RemoveDuplexOutputChannel("2"); Assert.IsNotNull(aDisconnectedLoadBalancerConnection); Assert.AreEqual("2", aResponseReceiverId); // The 2nd is removed so it shall use the 3rd one. aClient2.SendMessage("Hello."); Assert.AreEqual("", aReceivedResponse1); Assert.AreEqual("Hello.", aReceivedResponse2); Assert.AreEqual("3", aReceiverChannelId); aReceivedResponse1 = ""; aReceivedResponse2 = ""; // 1st receiver stops to listen. aReceivers[0].StopListening(); // It should start from the beginnng but 1st is not listening and 2nd was removed. // So it shall use the 3rd one. aClient1.SendMessage("Hello."); Assert.AreEqual("Hello.", aReceivedResponse1); Assert.AreEqual("", aReceivedResponse2); Assert.AreEqual("3", aReceiverChannelId); aReceivedResponse1 = ""; aReceivedResponse2 = ""; aReceiverRemovedEvent.WaitOne(); Assert.AreEqual("1", aRemovedReceiverId); aReceivers[2].StopListening(); aClient2.SendMessage("Hello."); Assert.AreEqual("", aReceivedResponse1); Assert.AreEqual("", aReceivedResponse2); aReceivedResponse1 = ""; aReceivedResponse2 = ""; aReceiverRemovedEvent.WaitOne(); Assert.AreEqual("3", aRemovedReceiverId); aReceivers[0].StartListening(); aReceivers[2].StartListening(); for (int i = 0; i < aReceivers.Length; ++i) { aLoadBalancer.AddDuplexOutputChannel((i + 1).ToString()); } aClient2.SendMessage("Hello."); Assert.AreEqual("", aReceivedResponse1); Assert.AreEqual("Hello.", aReceivedResponse2); Assert.AreEqual("2", aReceiverChannelId); aReceivedResponse1 = ""; aReceivedResponse2 = ""; } finally { aClient1.CloseConnection(); aClient2.CloseConnection(); aLoadBalancer.DetachDuplexInputChannel(); foreach (IDuplexInputChannel aReceiver in aReceivers) { aReceiver.StopListening(); } } }
public void A16_RequestResponse_100_ConstantlyInterrupted() { IDuplexOutputChannel aDuplexOutputChannel = MessagingSystem.CreateDuplexOutputChannel(ChannelId); IDuplexInputChannel aDuplexInputChannel = MessagingSystem.CreateDuplexInputChannel(ChannelId); IDuplexInputChannel anUnderlyingDuplexInputChannel = aDuplexInputChannel.GetField <IDuplexInputChannel>("myInputChannel"); Assert.NotNull(anUnderlyingDuplexInputChannel); AutoResetEvent anAllMessagesProcessedEvent = new AutoResetEvent(false); // Received messages. List <int> aReceivedMessages = new List <int>(); aDuplexInputChannel.MessageReceived += (x, y) => { lock (aReceivedMessages) { string aReceivedMessage = y.Message as string; EneterTrace.Info("Received message: " + aReceivedMessage); int k = int.Parse(aReceivedMessage); aReceivedMessages.Add(k); k += 1000; EneterTrace.Info("Sent response message: " + k.ToString()); aDuplexInputChannel.SendResponseMessage(y.ResponseReceiverId, k.ToString()); } }; aDuplexOutputChannel.ConnectionClosed += (x, y) => { EneterTrace.Info("ConnectionClosed invoked in duplex output channel"); // The buffered duplex output channel exceeded the max offline time. anAllMessagesProcessedEvent.Set(); }; // Received response messages. List <int> aReceivedResponseMessages = new List <int>(); aDuplexOutputChannel.ResponseMessageReceived += (x, y) => { lock (aReceivedResponseMessages) { string aReceivedMessage = y.Message as string; EneterTrace.Info("Received response message: " + aReceivedMessage); int k = int.Parse(aReceivedMessage); aReceivedResponseMessages.Add(k); if (aReceivedResponseMessages.Count == 100) { anAllMessagesProcessedEvent.Set(); } } }; try { bool aTestFinishedFlag = false; aDuplexInputChannel.StartListening(); aDuplexOutputChannel.OpenConnection(); Thread anInteruptingThread = new Thread(() => { for (int i = 0; i < 100 && !aTestFinishedFlag; ++i) { anUnderlyingDuplexInputChannel.DisconnectResponseReceiver(aDuplexOutputChannel.ResponseReceiverId); Thread.Sleep(ConnectionInterruptionFrequency); } }); // Start constant disconnecting. anInteruptingThread.Start(); for (int i = 0; i < 100; ++i) { aDuplexOutputChannel.SendMessage(i.ToString()); } // Wait until all messages are processed. //anAllMessagesProcessedEvent.WaitOne(); Assert.IsTrue(anAllMessagesProcessedEvent.WaitOne(20000), "The timeout occured."); aTestFinishedFlag = true; anInteruptingThread.Join(); } finally { aDuplexOutputChannel.CloseConnection(); aDuplexInputChannel.StopListening(); } aReceivedMessages.Sort(); Assert.AreEqual(100, aReceivedMessages.Count); for (int i = 0; i < 100; ++i) { Assert.AreEqual(i, aReceivedMessages[i]); } aReceivedResponseMessages.Sort(); Assert.AreEqual(100, aReceivedResponseMessages.Count); for (int i = 0; i < 100; ++i) { Assert.AreEqual(i + 1000, aReceivedResponseMessages[i]); } }