public override void StopMessaging() { if (DSender.IsDuplexOutputChannelAttached) { DSender.DetachDuplexOutputChannel(); } }
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); }
private void Form1_FormClosed(object sender, FormClosedEventArgs e) { mySender.DetachDuplexOutputChannel(); }
public void CalculatePi() { //EneterTrace.DetailLevel = EneterTrace.EDetailLevel.Debug; //EneterTrace.TraceLog = new StreamWriter("d:/tracefile.txt"); IMessagingSystemFactory aThreadMessaging = new ThreadMessagingSystemFactory(); List <CalculatorService> aServices = new List <CalculatorService>(); ILoadBalancer aDistributor = null; IDuplexTypedMessageSender <double, Interval> aSender = null; // Create 50 calculating services. try { for (int i = 0; i < 50; ++i) { aServices.Add(new CalculatorService("a" + i.ToString(), aThreadMessaging)); } // Create Distributor ILoadBalancerFactory aDistributorFactory = new RoundRobinBalancerFactory(aThreadMessaging); aDistributor = aDistributorFactory.CreateLoadBalancer(); // Attach available services to the distributor. for (int i = 0; i < aServices.Count; ++i) { aDistributor.AddDuplexOutputChannel("a" + i.ToString()); } // Attach input channel to the distributor. IDuplexInputChannel anInputChannel = aThreadMessaging.CreateDuplexInputChannel("DistributorAddress"); aDistributor.AttachDuplexInputChannel(anInputChannel); // Create client that needs to calculate PI. IDuplexTypedMessagesFactory aTypedMessagesFactory = new DuplexTypedMessagesFactory(); aSender = aTypedMessagesFactory.CreateDuplexTypedMessageSender <double, Interval>(); AutoResetEvent aCalculationCompletedEvent = new AutoResetEvent(false); int aCount = 0; double aPi = 0.0; aSender.ResponseReceived += (x, y) => { ++aCount; EneterTrace.Debug("Completed interval: " + aCount.ToString()); aPi += y.ResponseMessage; if (aCount == 400) { aCalculationCompletedEvent.Set(); } }; IDuplexOutputChannel anOutputChannel = aThreadMessaging.CreateDuplexOutputChannel("DistributorAddress"); aSender.AttachDuplexOutputChannel(anOutputChannel); // Sender sends several parallel requests to calculate specified intervals. // 2 / 0.005 = 400 intervals. for (double i = -1.0; i <= 1.0; i += 0.005) { Interval anInterval = new Interval(i, i + 0.005); aSender.SendRequestMessage(anInterval); } // Wait until all requests are calculated. EneterTrace.Debug("Test waits until completion."); aCalculationCompletedEvent.WaitOne(); EneterTrace.Info("Calculated PI = " + aPi.ToString()); } catch (Exception err) { EneterTrace.Error("Test failed", err); throw; } finally { aSender.DetachDuplexOutputChannel(); aDistributor.DetachDuplexInputChannel(); aServices.ForEach(x => x.Dispose()); } }
public void Close() { myMessageSender.DetachDuplexOutputChannel(); }