public Form1()
        {
            InitializeComponent();

            // Create ProtoBuf serializer.
            mySerializer = new ProtoBufSerializer();

            // Create the broker client that will receive notification messages.
            IDuplexBrokerFactory aBrokerFactory = new DuplexBrokerFactory(mySerializer);
            myBrokerClient = aBrokerFactory.CreateBrokerClient();
            myBrokerClient.BrokerMessageReceived += OnNotificationMessageReceived;

            // Create the Tcp messaging for the communication with the publisher.
            // Note: For the interprocess communication you can use: Tcp, NamedPipes and Http.
            IMessagingSystemFactory aMessagingFactory = new TcpMessagingSystemFactory();

            // Create duplex output channel for the communication with the publisher.
            // Note: The duplex output channel can send requests and receive responses.
            //       In our case, the broker client will send requests to subscribe/unsubscribe
            //       and receive notifications as response messages.
            myOutputChannel = aMessagingFactory.CreateDuplexOutputChannel("tcp://127.0.0.1:7091/");

            // Attach the output channel to the broker client
            myBrokerClient.AttachDuplexOutputChannel(myOutputChannel);
        }
Beispiel #2
0
        public void DoNotNotifyPublisher()
        {
            // Create channels
            IMessagingSystemFactory aMessagingSystem = new SynchronousMessagingSystemFactory();

            IDuplexInputChannel  aBrokerInputChannel   = aMessagingSystem.CreateDuplexInputChannel("BrokerChannel");
            IDuplexOutputChannel aClient1OutputChannel = aMessagingSystem.CreateDuplexOutputChannel("BrokerChannel");
            IDuplexOutputChannel aClient2OutputChannel = aMessagingSystem.CreateDuplexOutputChannel("BrokerChannel");

            // Specify in the factory that the publisher shall not be notified from its own published events.
            IDuplexBrokerFactory aBrokerFactory = new DuplexBrokerFactory()
            {
                IsPublisherNotified = false
            };

            IDuplexBroker aBroker = aBrokerFactory.CreateBroker();

            aBroker.AttachDuplexInputChannel(aBrokerInputChannel);

            IDuplexBrokerClient aClient1 = aBrokerFactory.CreateBrokerClient();
            List <BrokerMessageReceivedEventArgs> aClient1ReceivedMessage = new List <BrokerMessageReceivedEventArgs>();

            aClient1.BrokerMessageReceived += (x, y) =>
            {
                aClient1ReceivedMessage.Add(y);
            };
            aClient1.AttachDuplexOutputChannel(aClient1OutputChannel);

            IDuplexBrokerClient aClient2 = aBrokerFactory.CreateBrokerClient();
            List <BrokerMessageReceivedEventArgs> aClient2ReceivedMessage = new List <BrokerMessageReceivedEventArgs>();

            aClient2.BrokerMessageReceived += (x, y) =>
            {
                aClient2ReceivedMessage.Add(y);
            };
            aClient2.AttachDuplexOutputChannel(aClient2OutputChannel);


            aClient1.Subscribe("TypeA");
            aClient2.Subscribe("TypeA");


            // Notify the message.
            aClient2.SendMessage("TypeA", "Message A");

            // Client 2 should not get the notification.
            Assert.AreEqual(1, aClient1ReceivedMessage.Count);
            Assert.AreEqual(0, aClient2ReceivedMessage.Count);
            Assert.AreEqual("TypeA", aClient1ReceivedMessage[0].MessageTypeId);
            Assert.AreEqual("Message A", (String)aClient1ReceivedMessage[0].Message);
        }
        static void Main(string[] args)
        {


            // Create broker.
            IDuplexBrokerFactory aBrokerFactory = new DuplexBrokerFactory();
            IDuplexBroker aBroker = aBrokerFactory.CreateBroker();

            // Communicate using WebSockets.
            IMessagingSystemFactory aMessaging = new WebSocketMessagingSystemFactory();
            IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("ws://127.0.0.1:8843/CpuUsage/");

            anInputChannel.ResponseReceiverConnected += (x, y) =>
            {
                Console.WriteLine("Connected client: " + y.ResponseReceiverId);
            };
            anInputChannel.ResponseReceiverDisconnected += (x, y) =>
            {
                Console.WriteLine("Disconnected client: " + y.ResponseReceiverId);
            };

            // Attach input channel and start listeing.
            aBroker.AttachDuplexInputChannel(anInputChannel);


            var tokenSource = new CancellationTokenSource();

            var sendMonitoringMessages = Task.Run(() => SendMonitoringMessages(aBroker, tokenSource.Token), tokenSource.Token);

            Console.WriteLine("CpuUsageService is running press ENTER to stop.");
            Console.ReadLine();
            tokenSource.Cancel();

            try
            {
                sendMonitoringMessages.Wait();
            }
            catch (AggregateException)
            {
            }
            catch (TaskCanceledException)
            {
            }

            // Detach the input channel and stop listening.
            aBroker.DetachDuplexInputChannel();
        }
        public Form1()
        {
            InitializeComponent();

            // Create ProtoBuf serializer.
            mySerializer = new ProtoBufSerializer();

            // Create broker client responsible for sending messages to the broker.
            IDuplexBrokerFactory aBrokerFactory = new DuplexBrokerFactory(mySerializer);
            myBrokerClient = aBrokerFactory.CreateBrokerClient();

            // Create output channel to send messages via Tcp.
            IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
            myOutputChannel = aMessaging.CreateDuplexOutputChannel("tcp://127.0.0.1:7091/");

            // Attach the output channel to the broker client to be able to send messages.
            myBrokerClient.AttachDuplexOutputChannel(myOutputChannel);
        }
Beispiel #5
0
        public void SubscribeSameMessageTwice()
        {
            // Create channels
            IMessagingSystemFactory aMessagingSystem = new SynchronousMessagingSystemFactory();

            IDuplexInputChannel  aBrokerInputChannel            = aMessagingSystem.CreateDuplexInputChannel("BrokerChannel");
            IDuplexOutputChannel aSubscriberClientOutputChannel = aMessagingSystem.CreateDuplexOutputChannel("BrokerChannel");
            IDuplexOutputChannel aPublisherClientOutputChannel  = aMessagingSystem.CreateDuplexOutputChannel("BrokerChannel");

            IDuplexBrokerFactory aBrokerFactory = new DuplexBrokerFactory();

            IDuplexBroker aBroker = aBrokerFactory.CreateBroker();

            aBroker.AttachDuplexInputChannel(aBrokerInputChannel);

            IDuplexBrokerClient aSubscriber = aBrokerFactory.CreateBrokerClient();
            List <BrokerMessageReceivedEventArgs> aClient1ReceivedMessage = new List <BrokerMessageReceivedEventArgs>();

            aSubscriber.BrokerMessageReceived += (x, y) =>
            {
                aClient1ReceivedMessage.Add(y);
            };
            aSubscriber.AttachDuplexOutputChannel(aSubscriberClientOutputChannel);

            IDuplexBrokerClient aPublisher = aBrokerFactory.CreateBrokerClient();

            aPublisher.AttachDuplexOutputChannel(aPublisherClientOutputChannel);

            // Subscribe the 1st time.
            aSubscriber.Subscribe("TypeA");

            // Subscribe the 2nd time.
            aSubscriber.Subscribe("TypeA");

            // Notify the message.
            aPublisher.SendMessage("TypeA", "Message A");

            // Although the client is subscribed twice, the message shall be notified once.
            Assert.AreEqual(1, aClient1ReceivedMessage.Count);
            Assert.AreEqual("TypeA", aClient1ReceivedMessage[0].MessageTypeId);
            Assert.AreEqual("Message A", (String)aClient1ReceivedMessage[0].Message);
        }
        static void Main(string[] args)
        {
            // Create ProtoBuf serializer.
            ISerializer aSerializer = new ProtoBufSerializer();

            // Create the broker.
            IDuplexBrokerFactory aBrokerFactory = new DuplexBrokerFactory(aSerializer);
            IDuplexBroker aBroker = aBrokerFactory.CreateBroker();

            // Create the Input channel receiving messages via Tcp.
            // Note: You can also choose NamedPipes or Http.
            //       (if you choose http, do not forget to execute it with sufficient user rights)
            IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
            IDuplexInputChannel aBrokerInputChannel = aMessaging.CreateDuplexInputChannel("tcp://127.0.0.1:7091/");

            aBroker.AttachDuplexInputChannel(aBrokerInputChannel);

            // Attach the input channel to the broker and start listening.
            Console.WriteLine("The broker application is running. Press ENTER to stop.");
            Console.ReadLine();

            aBroker.DetachDuplexInputChannel();
        }
Beispiel #7
0
        private void Notify(int numberOfTimes, ISerializer serializer, IMessagingSystemFactory messaging, string aBrokerAddress)
        {
            IDuplexInputChannel  aBrokerInputChannel   = messaging.CreateDuplexInputChannel(aBrokerAddress);
            IDuplexOutputChannel aClient1OutputChannel = messaging.CreateDuplexOutputChannel(aBrokerAddress);
            IDuplexOutputChannel aClient2OutputChannel = messaging.CreateDuplexOutputChannel(aBrokerAddress);

            // Specify in the factory that the publisher shall not be notified from its own published events.
            IDuplexBrokerFactory aBrokerFactory = new DuplexBrokerFactory(serializer)
            {
                IsPublisherNotified = false
            };

            IDuplexBroker aBroker = aBrokerFactory.CreateBroker();

            aBroker.AttachDuplexInputChannel(aBrokerInputChannel);

            IDuplexBrokerClient aClient1   = aBrokerFactory.CreateBrokerClient();
            int            aCount          = 0;
            AutoResetEvent aCompletedEvent = new AutoResetEvent(false);

            aClient1.BrokerMessageReceived += (x, y) =>
            {
                ++aCount;
                if (aCount == numberOfTimes)
                {
                    aCompletedEvent.Set();
                }
            };
            aClient1.AttachDuplexOutputChannel(aClient1OutputChannel);

            IDuplexBrokerClient aClient2 = aBrokerFactory.CreateBrokerClient();

            aClient2.AttachDuplexOutputChannel(aClient2OutputChannel);

            try
            {
                var aTimer = new PerformanceTimer();
                aTimer.Start();

                aClient1.Subscribe("TypeA");

                for (int i = 0; i < numberOfTimes; ++i)
                {
                    // Notify the message.
                    aClient2.SendMessage("TypeA", "Message A");
                }

                aCompletedEvent.WaitOne();

                aTimer.Stop();

                // Client 2 should not get the notification.
                Assert.AreEqual(numberOfTimes, aCount);
            }
            finally
            {
                aClient1.DetachDuplexOutputChannel();
                aClient2.DetachDuplexOutputChannel();
                aBroker.DetachDuplexInputChannel();
            }
        }
Beispiel #8
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);
        }