예제 #1
0
 // Method processing notification messages from the publisher.
 private void OnNotificationMessageReceived(object sender, BrokerMessageReceivedEventArgs e)
 {
     // The notification event does not come in UI thread.
     // Therefore, if we want to work with UI controls we must execute it in the UI thread.
     InvokeInUIThread(() =>
         {
             if (e.ReceivingError == null)
             {
                 if (e.MessageTypeId == "MyNotifyMsg1")
                 {
                     NotifyMsg1 aDeserializedMsg = mySerializer.Deserialize<NotifyMsg1>(e.Message);
                     Received1TextBox.Text = aDeserializedMsg.CurrentTime.ToString();
                 }
                 else if (e.MessageTypeId == "MyNotifyMsg2")
                 {
                     NotifyMsg2 aDeserializedMsg = mySerializer.Deserialize<NotifyMsg2>(e.Message);
                     Received2TextBox.Text = aDeserializedMsg.Number.ToString();
                 }
                 else if (e.MessageTypeId == "MyNotifyMsg3")
                 {
                     NotifyMsg3 aDeserializedMsg = mySerializer.Deserialize<NotifyMsg3>(e.Message);
                     Received3TextBox.Text = aDeserializedMsg.TextMessage;
                 }
             }
         });
 }
예제 #2
0
        public void NotifySubscribers()
        {
            //EneterTrace.DetailLevel = EneterTrace.EDetailLevel.Debug;
            //EneterTrace.TraceLog = new StreamWriter("d:/tracefile.txt");

            // Create channels
            IMessagingSystemFactory aMessagingSystem = new SynchronousMessagingSystemFactory();

            IDuplexInputChannel  aBrokerInputChannel             = aMessagingSystem.CreateDuplexInputChannel("BrokerChannel");
            IDuplexOutputChannel aSubscriber1ClientOutputChannel = aMessagingSystem.CreateDuplexOutputChannel("BrokerChannel");
            IDuplexOutputChannel aSubscriber2ClientOutputChannel = aMessagingSystem.CreateDuplexOutputChannel("BrokerChannel");
            IDuplexOutputChannel aSubscriber3ClientOutputChannel = aMessagingSystem.CreateDuplexOutputChannel("BrokerChannel");

            IDuplexBrokerFactory aBrokerFactory = new DuplexBrokerFactory();

            IDuplexBroker aBroker = aBrokerFactory.CreateBroker();
            BrokerMessageReceivedEventArgs aBrokerReceivedMessage = null;

            aBroker.BrokerMessageReceived += (x, y) =>
            {
                aBrokerReceivedMessage = y;
            };
            aBroker.AttachDuplexInputChannel(aBrokerInputChannel);

            IDuplexBrokerClient            aBrokerClient1          = aBrokerFactory.CreateBrokerClient();
            BrokerMessageReceivedEventArgs aClient1ReceivedMessage = null;

            aBrokerClient1.BrokerMessageReceived += (x, y) =>
            {
                aClient1ReceivedMessage = y;
            };
            aBrokerClient1.AttachDuplexOutputChannel(aSubscriber1ClientOutputChannel);

            IDuplexBrokerClient            aBrokerClient2          = aBrokerFactory.CreateBrokerClient();
            BrokerMessageReceivedEventArgs aClient2ReceivedMessage = null;

            aBrokerClient2.BrokerMessageReceived += (x, y) =>
            {
                aClient2ReceivedMessage = y;
            };
            aBrokerClient2.AttachDuplexOutputChannel(aSubscriber2ClientOutputChannel);

            IDuplexBrokerClient            aBrokerClient3          = aBrokerFactory.CreateBrokerClient();
            BrokerMessageReceivedEventArgs aClient3ReceivedMessage = null;

            aBrokerClient3.BrokerMessageReceived += (x, y) =>
            {
                aClient3ReceivedMessage = y;
            };
            aBrokerClient3.AttachDuplexOutputChannel(aSubscriber3ClientOutputChannel);


            string[] aSubscription1 = { "TypeA", "TypeB" };
            aBrokerClient1.Subscribe(aSubscription1);

            string[] aSubscription2 = { "TypeA" };
            aBrokerClient2.Subscribe(aSubscription2);

            string[] aSubscription3 = { "MTypeC" };
            aBrokerClient3.Subscribe(aSubscription3);


            aBroker.Subscribe("TypeA");

            aBrokerClient3.SendMessage("TypeA", "Message A");
            Assert.AreEqual("TypeA", aClient1ReceivedMessage.MessageTypeId);
            Assert.AreEqual("Message A", (string)aClient1ReceivedMessage.Message);
            Assert.AreEqual(null, aClient1ReceivedMessage.ReceivingError);

            Assert.AreEqual("TypeA", aClient2ReceivedMessage.MessageTypeId);
            Assert.AreEqual("Message A", (string)aClient2ReceivedMessage.Message);
            Assert.AreEqual(null, aClient2ReceivedMessage.ReceivingError);

            Assert.AreEqual(null, aClient3ReceivedMessage);

            Assert.AreEqual("TypeA", aBrokerReceivedMessage.MessageTypeId);
            Assert.AreEqual("Message A", (string)aBrokerReceivedMessage.Message);
            Assert.AreEqual(null, aBrokerReceivedMessage.ReceivingError);


            aClient1ReceivedMessage = null;
            aClient2ReceivedMessage = null;
            aClient3ReceivedMessage = null;
            aBrokerReceivedMessage  = null;

            aBrokerClient2.Unsubscribe();

            aBrokerClient3.SendMessage("MTypeC", "Message MTC");

            Assert.AreEqual(null, aClient1ReceivedMessage);

            Assert.AreEqual(null, aClient2ReceivedMessage);

            Assert.AreEqual("MTypeC", aClient3ReceivedMessage.MessageTypeId);
            Assert.AreEqual("Message MTC", (string)aClient3ReceivedMessage.Message);
            Assert.AreEqual(null, aClient3ReceivedMessage.ReceivingError);

            Assert.AreEqual(null, aBrokerReceivedMessage);


            aClient1ReceivedMessage = null;
            aClient2ReceivedMessage = null;
            aClient3ReceivedMessage = null;
            aBrokerReceivedMessage  = null;

            aBroker.SendMessage("TypeA", "Message A");
            Assert.AreEqual("TypeA", aClient1ReceivedMessage.MessageTypeId);
            Assert.AreEqual("Message A", (string)aClient1ReceivedMessage.Message);
            Assert.AreEqual(null, aClient1ReceivedMessage.ReceivingError);

            Assert.AreEqual(null, aClient2ReceivedMessage);

            Assert.AreEqual(null, aClient3ReceivedMessage);


            Assert.AreEqual("TypeA", aBrokerReceivedMessage.MessageTypeId);
            Assert.AreEqual("Message A", (string)aBrokerReceivedMessage.Message);
            Assert.AreEqual(null, aBrokerReceivedMessage.ReceivingError);


            aClient1ReceivedMessage = null;
            aClient2ReceivedMessage = null;
            aClient3ReceivedMessage = null;
            aBrokerReceivedMessage  = null;


            aBroker.Unsubscribe("TypeA");

            string[] aNewMessageType = { "TypeA" };
            aBrokerClient3.Subscribe(aNewMessageType);

            aBrokerClient1.DetachDuplexOutputChannel();

            aBrokerClient3.SendMessage("TypeA", "Message A");
            Assert.AreEqual(null, aClient1ReceivedMessage);

            Assert.AreEqual(null, aClient2ReceivedMessage);

            Assert.AreEqual("TypeA", aClient3ReceivedMessage.MessageTypeId);
            Assert.AreEqual("Message A", (string)aClient3ReceivedMessage.Message);
            Assert.AreEqual(null, aClient3ReceivedMessage.ReceivingError);

            Assert.AreEqual(null, aBrokerReceivedMessage);
        }