Inheritance: IDisposable
            public void RunPipeline(PairSocket shim)
            {
                publisherSocket = context.CreatePublisherSocket();
                publisherSocket.Bind("tcp://*:" + StreamingProtocol.Port);

                snapshotSocket = context.CreateResponseSocket();
                snapshotSocket.Bind("tcp://*:" + SnapshotProtocol.Port);
                snapshotSocket.ReceiveReady += OnSnapshotReady;

                shim.ReceiveReady += OnShimReady;

                heartbeatTimer          = new NetMQTimer(StreamingProtocol.HeartbeatInterval);
                heartbeatTimer.Elapsed += OnHeartbeatTimerElapsed;

                shim.SignalOK();

                poller = new Poller();
                poller.AddSocket(shim);
                poller.AddSocket(snapshotSocket);
                poller.AddTimer(heartbeatTimer);
                poller.Start();

                publisherSocket.Dispose();
                snapshotSocket.Dispose();
            }
Beispiel #2
0
        private static void Main(string[] args)
        {
            using (var ctx = NetMQContext.Create())
            {
                using (NetMQSocket frontend = ctx.CreateRouterSocket(), backend = ctx.CreateDealerSocket())
                {
                    frontend.Bind(FRONTEND_ENDPOINT);
                    backend.Bind(BACKEND_ENDPOINT);

                    //Handler for messages coming in to the frontend
                    frontend.ReceiveReady += (sender, e) =>
                        {
                            var msg = frontend.ReceiveMessage();
                            backend.SendMessage(msg); //Relay this message to the backend
                        };

                    //Handler for messages coming in to the backend
                    backend.ReceiveReady += (sender, e) =>
                        {
                            var msg = backend.ReceiveMessage();
                            frontend.SendMessage(msg); //Relay this message to the frontend
                        };

                    using (var poller = new Poller())
                    {
                        poller.AddSocket(backend);
                        poller.AddSocket(frontend);

                        //Listen out for events on both sockets and raise events when messages come in
                        poller.Start();
                    }
                }
            }
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            using (var context = NetMQContext.Create())
            using (var udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
            using (var poller = new Poller())
            {
                // Ask OS to let us do broadcasts from socket
                udpSocket.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.Broadcast, 1);

                // Bind UDP socket to local port so we can receive pings
                udpSocket.Bind(new IPEndPoint(IPAddress.Any, PingPortNumber));

                // We use zmq_poll to wait for activity on the UDP socket, because
                // this function works on non-0MQ file handles. We send a beacon
                // once a second, and we collect and report beacons that come in
                // from other nodes:

                poller.AddPollInSocket(udpSocket, socket => { });
                poller.PollTillCancelledNonBlocking();
                //poller.ad
                //var poller = new z
                //var pollItemsList = new List<ZPollItem>();
                //pollItemsList.Add(new ZPollItem(ZPoll.In));
                //var pollItem = ZPollItem.CreateReceiver();
                //ZMessage message;
                //ZError error;
                //pollItem.ReceiveMessage(udpSocket, out message, out error);
                //pollItem.ReceiveMessage = (ZSocket socket, out ZMessage message, out ZError error) =>

                // Send first ping right away

            }
        }
Beispiel #4
0
 private void PollerThread()
 {
     while (true)
     {
         try
         {
             if (poller == null || !poller.IsStarted)
             {
                 SignalService.Logger.Info("Start NetMQ Poller");
                 poller = new Poller();
                 poller.AddSocket(router);
                 poller.Start();
             }
         }
         catch (Exception e)
         {
             SignalService.Logger.Error("NetMQ Poller Thread Exception.\n{0}", e.StackTrace);
             if (poller != null)
             {
                 poller.Stop();
                 poller.Dispose();
             }
         }
     }
 }
Beispiel #5
0
        public void Receive_BrokerDisconnectedWithLogging_ShouldReturnRequest()
        {
            const string host_address    = "tcp://localhost:5555";
            var          loggingMessages = new List <string> ();

            // setup the counter socket for communication
            using (var ctx = NetMQContext.Create())
                using (var broker = ctx.CreateRouterSocket())
                    using (var poller = new NetMQ.Poller())
                        using (var session = new MDPWorker(host_address, "test"))
                        {
                            broker.Bind(host_address);
                            // we need to pick up any message in order to avoid errors but don't answer
                            broker.ReceiveReady += (s, e) => e.Socket.ReceiveMessage();

                            poller.AddSocket(broker);
                            var t = Task.Factory.StartNew(() => poller.Start());

                            // speed up the test
                            session.HeartbeatDelay = TimeSpan.FromMilliseconds(250);
                            session.ReconnectDelay = TimeSpan.FromMilliseconds(250);
                            // set the event handler to receive the logging messages
                            session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                            // initialize the worker - broker protocol
                            session.Receive(null);

                            poller.Stop();
                            poller.RemoveSocket(broker);

                            Assert.That(loggingMessages.Count(m => m.Contains("retrying")), Is.EqualTo(3));
                            // 3 times retrying and 1 time initial connecting
                            Assert.That(loggingMessages.Count(m => m.Contains("localhost")), Is.EqualTo(4));
                            Assert.That(loggingMessages.Last().Contains("abandoning"));
                        }
        }
		public async Task Start ()
		{
			ThrowIfDisposed ();
			var ct = cancellationTokenSource.Token;

			nmqPoller = new Poller ();
			nmqScheduler = new NetMQScheduler (nmqContext, nmqPoller);
			nmqServer = nmqContext.CreateResponseSocket ();
			nmqServer.Bind (Address.AbsoluteUri.TrimEnd ('/'));

			serverTask = Task.Factory.StartNew (() => {
				ct.ThrowIfCancellationRequested ();

				while (true) {
					if (ct.IsCancellationRequested) {
						// clean up here
						ct.ThrowIfCancellationRequested ();
					}
					var msg = nmqServer.Receive ();
					var request = Request.Deserialize (msg);
					var result = Handle (request);

					byte[] output_buffer = result.Serialize ();
					nmqServer.Send (output_buffer);
				}
			}, ct);

			await serverTask;
		}
Beispiel #7
0
        static void Main(string[] args)
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (WSRouter router = context.CreateWSRouter())
                using (WSPublisher publisher = context.CreateWSPublisher())
                {
                    router.Bind("ws://localhost:80");                    
                    publisher.Bind("ws://localhost:81");

                    router.ReceiveReady += (sender, eventArgs) =>
                    {
                        string identity = router.ReceiveString();
                        string message = router.ReceiveString();

                        router.SendMore(identity).Send("OK");

                        publisher.SendMore("chat").Send(message);
                    };
                        
                    Poller poller = new Poller();
                    poller.AddSocket(router);

                    // we must add the publisher to the poller although we are not registering to any event.
                    // The internal stream socket handle connections and subscriptions and use the events internally
                    poller.AddSocket(publisher);
                    poller.Start();

                }
            }
        }
Beispiel #8
0
        public void Run(params string[] addresses)
        {
            using (m_context = NetMQContext.Create())
              {
            var subscriber = Connect(addresses);

            if (subscriber == null)
              throw new Exception("cannot connect to eny of the endpoints");

            // timeout timer, when heartbeat was not arrived for 5 seconds
            m_timeoutTimer = new NetMQTimer(TimeSpan.FromSeconds(5));
            m_timeoutTimer.Elapsed += (sender, args) =>
            {
              // timeout happend, first dispose existing subscriber
              subscriber.Dispose();
              m_poller.RemoveSocket(subscriber);

              // connect again
              subscriber = Connect(addresses);

              if (subscriber == null)
            throw new Exception("cannot connect to eny of the endpoints");

              m_poller.AddSocket(subscriber);
            };

            m_poller = new Poller(subscriber);
            m_poller.AddTimer(m_timeoutTimer);

            m_poller.PollTillCancelled();
              }
        }
Beispiel #9
0
        public static void Run()
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (var publisherSocket = context.CreateXPublisherSocket())
                {
                    publisherSocket.SetWelcomeMessage("WM");
                    publisherSocket.Bind("tcp://*:6669");

                    // we just drop subscriptions
                    publisherSocket.ReceiveReady += (sender, eventArgs) => publisherSocket.SkipMultipartMessage();

                    Poller poller = new Poller(publisherSocket);

                    // send a message every second
                    NetMQTimer sendMessageTimer = new NetMQTimer(1000);
                    poller.AddTimer(sendMessageTimer);
                    sendMessageTimer.Elapsed +=
                        (sender, eventArgs) =>
                            publisherSocket.SendMoreFrame("A").SendFrame(new Random().Next().ToString());

                    // send heartbeat every two seconds
                    NetMQTimer heartbeatTimer = new NetMQTimer(2000);
                    poller.AddTimer(heartbeatTimer);
                    heartbeatTimer.Elapsed += (sender, eventArgs) => publisherSocket.SendFrame("HB");

                    poller.PollTillCancelled();
                }
            }
        }
Beispiel #10
0
        public void ReceiveImplicitConnect_ValidScenario_ShouldReturnRequest()
        {
            const string host_address    = "tcp://localhost:5557";
            var          loggingMessages = new List <string> ();

            // setup the counter socket for communication
            using (var ctx = NetMQContext.Create())
                using (var broker = ctx.CreateRouterSocket())
                    using (var poller = new NetMQ.Poller())
                        using (var session = new MDPWorker(host_address, "test", new[] { (byte)'1' }))
                        {
                            broker.Bind(host_address);
                            // we need to pick up any message in order to avoid errors
                            broker.ReceiveReady += (s, e) =>
                            {
                                var msg = e.Socket.ReceiveMessage();
                                // we expect to receive a 5 Frame mesage
                                // [WORKER ADR][EMPTY]["MDPW01"]["READY"]["test"]
                                if (msg.FrameCount != 5)
                                {
                                    Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                                }
                                // make sure the frames are as expected
                                Assert.That(msg[1], Is.EqualTo(NetMQFrame.Empty));
                                Assert.That(msg[2].ConvertToString(), Is.EqualTo("MDPW01"));
                                Assert.That(msg[3].BufferSize, Is.EqualTo(1));
                                Assert.That(msg[3].Buffer[0], Is.EqualTo((byte)MDPCommand.Ready));
                                Assert.That(msg[4].ConvertToString(), Is.EqualTo("test"));

                                // tell worker to stop gracefully
                                var reply = new NetMQMessage();
                                reply.Push(new[] { (byte)MDPCommand.Kill });
                                // push MDP Version
                                reply.Push(msg[2]);
                                // push separator
                                reply.Push(NetMQFrame.Empty);
                                // push worker address
                                reply.Push(msg[0]);
                                // send reply which is a request for the worker
                                e.Socket.SendMessage(reply);
                            };

                            poller.AddSocket(broker);
                            var t = Task.Factory.StartNew(() => poller.Start());

                            // set the event handler to receive the logging messages
                            session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                            // initialize the worker - broker protocol
                            session.Receive(null);

                            poller.Stop();
                            poller.RemoveSocket(broker);

                            Assert.That(loggingMessages.Count, Is.EqualTo(5));
                            Assert.That(loggingMessages[0], Is.EqualTo("[WORKER] connected to broker at tcp://localhost:5557"));
                            Assert.That(loggingMessages[1].Contains("[WORKER] sending"), Is.True);
                            Assert.That(loggingMessages[2].Contains("[WORKER] received"));
                            Assert.That(loggingMessages[4].Contains("abandoning"));
                        }
        }
        private static void Main()
        {
            using (var context = NetMQContext.Create())
            using (var frontend = context.CreateRouterSocket())
            using (var backend = context.CreateDealerSocket())
            {
                frontend.Bind(FrontendEndpoint);
                backend.Bind(BackendEndpoint);

                // Handler for messages coming in to the frontend
                frontend.ReceiveReady += (sender, e) =>
                {
                    var msg = frontend.ReceiveMessage();
                    backend.SendMessage(msg); // Relay this message to the backend
                };

                // Handler for messages coming in to the backend
                backend.ReceiveReady += (sender, e) =>
                {
                    var msg = backend.ReceiveMessage();
                    frontend.SendMessage(msg); // Relay this message to the frontend
                };

                using (var poller = new Poller())
                {
                    poller.AddSocket(backend);
                    poller.AddSocket(frontend);

                    // Listen out for events on both sockets and raise events when messages come in
                    poller.PollTillCancelled();
                }
            }
        }
Beispiel #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="XForwarder"/> class.
 /// </summary>
 /// <param name="context">The <see cref="NetMQContext"/> to use when creating the sockets.</param>
 /// <param name="poller">The <see cref="Poller"/> to use.</param>
 /// <param name="frontendBindAddress">The endpoint used to bind the frontend socket.</param>
 /// <param name="backendBindAddress">The endpoint used to bind the backend socket.</param>
 /// <param name="mode">The <see cref="DeviceMode"/> for the device.</param>		
 public XForwarder(NetMQContext context, Poller poller, string frontendBindAddress, string backendBindAddress,
                   DeviceMode mode = DeviceMode.Threaded)
     : base(poller, context.CreateXSubscriberSocket(), context.CreateXPublisherSocket(), mode)
 {
     this.FrontendSetup.Bind(frontendBindAddress);
     this.BackendSetup.Bind(backendBindAddress);
 }
Beispiel #13
0
        /// <summary>
        /// Start the proxy work, this will block until one of the sockets is closed
        /// </summary>
        public void Start()
        {
            m_frontend.ReceiveReady += OnFrontendReady;
            m_backend.ReceiveReady  += OnBackendReady;

            m_poller = new Poller(m_frontend, m_backend);
            m_poller.PollTillCancelled();
        }
Beispiel #14
0
 public Proxy([NotNull] NetMQSocket frontend, [NotNull] NetMQSocket backend, [CanBeNull] NetMQSocket control = null, Poller poller = null)
 {
     m_frontend       = frontend;
     m_backend        = backend;
     m_control        = control;
     m_externalPoller = poller != null;
     m_poller         = poller;
 }
Beispiel #15
0
        public void ReceiveImplicitConnect_ValidScenario_ShouldReturnRequest()
        {
            const string hostAddress = "tcp://localhost:5557";
            var loggingMessages = new List<string> ();

            // setup the counter socket for communication
            using (var context = NetMQContext.Create ())
            using (var broker = context.CreateRouterSocket ())
            using (var poller = new Poller ())
            using (var session = new MDPWorker (hostAddress, "test", new[] { (byte) '1' }))
            {
                broker.Bind (hostAddress);
                // we need to pick up any message in order to avoid errors
                broker.ReceiveReady += (s, e) =>
                {
                    var msg = e.Socket.ReceiveMultipartMessage ();
                    // we expect to receive a 5 Frame message
                    // [WORKER ADR][EMPTY]["MDPW01"]["READY"]["test"]
                    if (msg.FrameCount != 5)
                        Assert.Fail ("Message with wrong count of frames {0}", msg.FrameCount);
                    // make sure the frames are as expected
                    Assert.That (msg[1], Is.EqualTo (NetMQFrame.Empty));
                    Assert.That (msg[2].ConvertToString (), Is.EqualTo ("MDPW01"));
                    Assert.That (msg[3].BufferSize, Is.EqualTo (1));
                    Assert.That (msg[3].Buffer[0], Is.EqualTo ((byte) MDPCommand.Ready));
                    Assert.That (msg[4].ConvertToString (), Is.EqualTo ("test"));

                    // tell worker to stop gracefully
                    var reply = new NetMQMessage ();
                    reply.Push (new[] { (byte) MDPCommand.Kill });
                    // push MDP Version
                    reply.Push (msg[2]);
                    // push separator
                    reply.Push (NetMQFrame.Empty);
                    // push worker address
                    reply.Push (msg[0]);
                    // send reply which is a request for the worker
                    e.Socket.SendMessage (reply);
                };

                poller.AddSocket (broker);
                Task.Factory.StartNew (poller.PollTillCancelled);

                // set the event handler to receive the logging messages
                session.LogInfoReady += (s, e) => loggingMessages.Add (e.Info);
                // initialise the worker - broker protocol
                session.Receive (null);

                poller.CancelAndJoin ();
                poller.RemoveSocket (broker);

                Assert.That (loggingMessages.Count, Is.EqualTo (5));
                Assert.That (loggingMessages[0], Is.EqualTo ("[WORKER] connected to broker at tcp://localhost:5557"));
                Assert.That (loggingMessages[1].Contains ("[WORKER] sending"), Is.True);
                Assert.That (loggingMessages[2].Contains ("[WORKER] received"));
                Assert.That (loggingMessages[4].Contains ("abandoning"));
            }
        }
Beispiel #16
0
        public void Receive_RequestWithWrongFirstFrame_ShouldThrowApplicationException()
        {
            const string host_address = "tcp://localhost:5555";

            // setup the counter socket for communication
            using (var ctx = NetMQContext.Create())
                using (var broker = ctx.CreateRouterSocket())
                    using (var poller = new NetMQ.Poller())
                        using (var session = new MDPWorker(host_address, "test"))
                        {
                            broker.Bind(host_address);
                            // we need to pick up any message in order to avoid errors
                            broker.ReceiveReady += (s, e) =>
                            {
                                var msg = e.Socket.ReceiveMessage();
                                // we expect to receive a 5 Frame mesage
                                // [WORKER ADR][EMPTY]["MDPW01"]["READY"]["test"]
                                if (msg.FrameCount != 5)
                                {
                                    Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                                }
                                // make sure the frames are as expected
                                Assert.That(msg[1], Is.EqualTo(NetMQFrame.Empty));
                                Assert.That(msg[2].ConvertToString(), Is.EqualTo("MDPW01"));
                                Assert.That(msg[3].BufferSize, Is.EqualTo(1));
                                Assert.That(msg[3].Buffer[0], Is.EqualTo((byte)MDPCommand.Ready));
                                Assert.That(msg[4].ConvertToString(), Is.EqualTo("test"));

                                // tell worker to stop gracefully
                                var reply = new NetMQMessage();
                                reply.Push(new[] { (byte)MDPCommand.Kill });
                                // push MDP Version
                                reply.Push("MDPW01");
                                // push separator
                                reply.Push("Should be empty");
                                // push worker address
                                reply.Push(msg[0]);
                                // send reply which is a request for the worker
                                e.Socket.SendMessage(reply);
                            };

                            poller.AddSocket(broker);
                            var t = Task.Factory.StartNew(() => poller.Start());

                            try
                            {
                                var reply = session.Receive(null);
                            }
                            catch (ApplicationException ex)
                            {
                                Assert.That(ex.Message, Is.EqualTo("First frame must be an empty frame!"));
                            }

                            poller.Stop();
                            poller.RemoveSocket(broker);
                        }
        }
Beispiel #17
0
 public void Start()
 {
     poller = new Poller();
     clientSocket = context.CreateDealerSocket();
     clientSocket.ReceiveReady += clientSocket_ReceiveReady;
     clientSocket.Connect(address);
     scheduler = new NetMQScheduler(context, poller);
     Task.Factory.StartNew(poller.Start, TaskCreationOptions.LongRunning);
 }
		public void Start ()
		{
			ThrowIfDisposed ();
			nmqPoller = new Poller ();
			nmqClient = nmqContext.CreateRequestSocket ();
			nmqClient.Connect (Address.AbsoluteUri.TrimEnd ('/'));
			nmqScheduler = new NetMQScheduler (nmqContext, nmqPoller);
			Task.Factory.StartNew (() => nmqPoller.Start (), TaskCreationOptions.LongRunning);
		}
Beispiel #19
0
        public void Send_WrongHeaderFromBrokerNoLogging_ShouldThrowApplicationException()
        {
            const string host_address = "tcp://localhost:5555";

            // setup the counter socket for communication
            using (var ctx = NetMQContext.Create())
                using (var broker = ctx.CreateRouterSocket())
                    using (var poller = new Poller())
                        using (var session = new MDPClient(host_address))
                        {
                            broker.Bind(host_address);
                            // we need to pick up any message in order to avoid errors
                            broker.ReceiveReady += (s, e) =>
                            {
                                // return empty reply
                                var msg = e.Socket.ReceiveMessage();
                                // we expect to receive a 4 Frame mesage
                                // [REQ ADR][EMPTY]["MDPC01"]["echo"]["REQUEST"]
                                if (msg.FrameCount != 5)
                                {
                                    Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                                }
                                // REQUEST socket will strip the his address + empty frame
                                // ROUTER has to add the address prelude in order to identify the correct socket(!)
                                // [REQ ADR][EMPTY]["MDPC00"]["echo"]["REQUEST"]
                                var clientAddress = msg.Pop();
                                msg.Pop(); // forget empty frame
                                var mdpVersion = msg.Pop();
                                msg.Pop(); // drop service name version
                                msg.Push("NoService");
                                msg.Push(mdpVersion);
                                msg.Push(NetMQFrame.Empty);
                                msg.Push(clientAddress); // reinsert the client's address

                                e.Socket.SendMessage(msg);
                            };

                            poller.AddSocket(broker);
                            var t = Task.Factory.StartNew(() => poller.Start());

                            // well formed message
                            var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });

                            try
                            {
                                session.Send("echo", requestMessage);
                            }
                            catch (ApplicationException ex)
                            {
                                Assert.That(ex.Message, Is.EqualTo("[CLIENT INFO] answered by wrong service: NoService"));
                            }

                            poller.Stop();
                            poller.RemoveSocket(broker);
                        }
        }
		public MessageHelper(string address, string topic)
		{
			_context = NetMQContext.Create();
			_subscribeSocket = _context.CreateSubscriberSocket();
			_subscribeSocket.Connect(address);
			_subscribeSocket.ReceiveReady += SubscribeSocketOnReceiveReady;
			_subscribeSocket.Subscribe(topic);
			_poller = new Poller();
			_poller.AddSocket(_subscribeSocket);
			Task.Factory.StartNew(_poller.Start);
		}
Beispiel #21
0
        public void Listen(CancellationToken cancellationToken)
        {
            using (var electionServer = context.CreateResponseSocket())
            using (var leaderServer = context.CreateResponseSocket())
            using (var pingServer = context.CreateResponseSocket())
            using (var poller = new Poller())
            {
                electionServer.Bind(Settings.ElectionListenerEndpoint);
                leaderServer.Bind(Settings.LeaderListenerEndpoint);
                pingServer.Bind(Settings.PingListenerEndpoint);

                logger.Log($"ElectionListenerEndpoint: {Settings.ElectionListenerEndpoint}");
                logger.Log($"LeaderListenerEndpoint: {Settings.LeaderListenerEndpoint}");
                logger.Log($"PingListenerEndpoint: {Settings.PingListenerEndpoint}");

                poller.AddSocket(electionServer);
                poller.AddSocket(leaderServer);
                poller.AddSocket(pingServer);

                // Listen for new messages on the electionServer socket
                electionServer.ReceiveReady += (s, a) =>
                {
                    var msg = a.Socket.ReceiveFrameString();

                    logger.Log($"ELECTION MESSAGE: {msg}");
                    if (msg == Message.Election)
                    {
                        a.Socket.SendFrame(msg == Message.Election
                            ? Message.Ok
                            : Message.Fail);
                    }
                };

                // Listen for new messages on the leaderServer socket
                leaderServer.ReceiveReady += (s, a) =>
                {
                    var winnerMessage = a.Socket.ReceiveFrameString();
                    OnLeaderChanged(winnerMessage);
                    logger.Log($"NEW LEADER MESSAGE RECEIVED: {winnerMessage}");
                };

                // Listen for pings
                pingServer.ReceiveReady += (s, a) =>
                {
                    a.Socket.ReceiveFrameString();
                    a.Socket.SendFrame(Message.Ok);
                };

                logger.Log("-----------------------------------");

                poller.PollTillCancelled();
            }
        }
Beispiel #22
0
        public void Send_CorrectInputWithLogging_ShouldReturnCorrectReply()
        {
            const string host_address    = "tcp://localhost:5555";
            var          loggingMessages = new List <string> ();

            // setup the counter socket for communication
            using (var ctx = NetMQContext.Create())
                using (var broker = ctx.CreateRouterSocket())
                    using (var poller = new Poller())
                        using (var session = new MDPClient(host_address))
                        {
                            broker.Bind(host_address);
                            // we need to pick up any message in order to avoid errors
                            broker.ReceiveReady += (s, e) =>
                            {
                                var msg = e.Socket.ReceiveMessage();
                                // we expect to receive a 4 Frame mesage
                                // [client adrR][e][mdp header][service][request]
                                if (msg.FrameCount != 5)
                                {
                                    Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                                }
                                // REQUEST socket will strip the his address + empty frame
                                // ROUTER has to add the address prelude in order to identify the correct socket(!)
                                // [client adr][e][mdp header][service][reply]
                                var request = msg.Last.ConvertToString();                   // get the request string
                                msg.RemoveFrame(msg.Last);                                  // remove the request frame
                                msg.Append(new NetMQFrame(request + " OK"));                // append the reply frame
                                e.Socket.SendMessage(msg);
                            };

                            poller.AddSocket(broker);
                            var t = Task.Factory.StartNew(() => poller.Start());

                            // set the event handler to receive the logging messages
                            session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                            // well formed message
                            var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });
                            // correct call
                            var reply = session.Send("echo", requestMessage);

                            poller.Stop();
                            poller.RemoveSocket(broker);

                            Assert.That(reply.FrameCount, Is.EqualTo(1));
                            Assert.That(reply.First.ConvertToString(), Is.EqualTo("REQUEST OK"));
                            Assert.That(loggingMessages.Count, Is.EqualTo(3));
                            Assert.That(loggingMessages[0], Is.EqualTo("[CLIENT] connecting to broker at tcp://localhost:5555"));
                            Assert.That(loggingMessages[1].Contains("[CLIENT INFO] sending"), Is.True);
                            Assert.That(loggingMessages[2].Contains("[CLIENT INFO] received"), Is.True);
                        }
        }
Beispiel #23
0
        private NetMQMonitor CreateMonitor(NetMQContext context, NetMQSocket socket, Poller poller)
        {
            var monitor = new NetMQMonitor(context, socket, $"inproc://{Guid.NewGuid()}.inproc",
                SocketEvents.Connected | SocketEvents.Disconnected | SocketEvents.ConnectRetried);

            monitor.Connected += Monitor_Connected;
            monitor.Disconnected += Monitor_Disconnected;
            monitor.ConnectRetried += Monitor_ConnectRetried;

            monitor.AttachToPoller(poller);

            return monitor;
        }
Beispiel #24
0
        void IShimHandler.Run(PairSocket pipe)
        {
            _pipe = pipe;
            _pipe.SignalOK();
            _pipe.ReceiveReady += OnPipeReady;

            _zre = Zre.Create(_ctx, _name);
            _zre.ReceiveReady += OnZreReady;

            _poller = new Poller();
            _poller.AddSocket(_zre);
            _poller.AddSocket(_pipe);
            _poller.PollTillCancelled();
        }
Beispiel #25
0
        public IntermediarySocket(IAddressBinder frontendAddressBinder, IAddressBinder backendAddressBinder,
            XSubscriberSocket frontendSocket, XPublisherSocket backendSocket)
        {
            _frontendAddressBinder = frontendAddressBinder;
            _backendAddressBinder = backendAddressBinder;
            _frontendSocket = frontendSocket;
            _backendSocket = backendSocket;

            _frontendAddressBinder.ConnectOrBindAddress(_frontendSocket);
            _backendAddressBinder.ConnectOrBindAddress(_backendSocket);

            _poller = new Poller(_frontendSocket, _backendSocket);
            _proxy = new Proxy(frontendSocket, backendSocket, poller: _poller);
        }
Beispiel #26
0
        public void Send_CorrectInputWithLogging_ShouldReturnCorrectReply()
        {
            const string hostAddress = "tcp://localhost:5555";
            var loggingMessages = new List<string>();

            // setup the counter socket for communication
            using (var context = NetMQContext.Create())
            using (var broker = context.CreateRouterSocket())
            using (var poller = new Poller())
            using (var session = new MDPClient(hostAddress))
            {
                broker.Bind(hostAddress);
                // we need to pick up any message in order to avoid errors
                broker.ReceiveReady += (s, e) =>
                {
                    var msg = e.Socket.ReceiveMessage();
                    // we expect to receive a 4 Frame message
                    // [client adrR][e][mdp header][service][request]
                    if (msg.FrameCount != 5)
                        Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                    // REQUEST socket will strip the his address + empty frame
                    // ROUTER has to add the address prelude in order to identify the correct socket(!)
                    // [client adr][e][mdp header][service][reply]
                    var request = msg.Last.ConvertToString(); // get the request string
                    msg.RemoveFrame(msg.Last); // remove the request frame
                    msg.Append(new NetMQFrame(request + " OK")); // append the reply frame
                    e.Socket.SendMessage(msg);
                };

                poller.AddSocket(broker);
                Task.Factory.StartNew(poller.PollTillCancelled);

                // set the event handler to receive the logging messages
                session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                // well formed message
                var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });
                // correct call
                var reply = session.Send("echo", requestMessage);

                poller.CancelAndJoin();
                poller.RemoveSocket(broker);

                Assert.That(reply.FrameCount, Is.EqualTo(1));
                Assert.That(reply.First.ConvertToString(), Is.EqualTo("REQUEST OK"));
                Assert.That(loggingMessages.Count, Is.EqualTo(3));
                Assert.That(loggingMessages[0], Is.EqualTo("[CLIENT] connecting to broker at tcp://localhost:5555"));
                Assert.That(loggingMessages[1].Contains("[CLIENT INFO] sending"), Is.True);
                Assert.That(loggingMessages[2].Contains("[CLIENT INFO] received"), Is.True);
            }
        }
Beispiel #27
0
        public NetMQScheduler(NetMQContext context, Poller poller = null)
        {
            m_context = context;
            if (poller == null)
            {
                m_ownPoller = true;

                m_poller = new Poller();
            }
            else
            {
                m_ownPoller = false;

                m_poller = poller;
            }

            m_clientSockets = new ConcurrentBag<NetMQSocket>();

            m_schedulerId = Interlocked.Increment(ref s_schedulerCounter);

            m_address = string.Format("{0}://scheduler-{1}", NetMQ.zmq.Address.InProcProtocol, m_schedulerId);

            m_serverSocket = context.CreatePullSocket();
            m_serverSocket.Options.Linger = TimeSpan.Zero;
            m_serverSocket.Bind(m_address);

            m_currentMessageHandler = OnMessageFirstTime;

            m_serverSocket.ReceiveReady += m_currentMessageHandler;

            m_poller.AddSocket(m_serverSocket);

            m_clientSocket = new ThreadLocal<NetMQSocket>(() =>
                    {
                        var socket = m_context.CreatePushSocket();
                        socket.Connect(m_address);

                        m_clientSockets.Add(socket);

                        return socket;
                    });

            m_schedulerThread = new ThreadLocal<bool>(() => false);

            if (m_ownPoller)
            {
                Task.Factory.StartNew(m_poller.Start, TaskCreationOptions.LongRunning);
            }
        }
Beispiel #28
0
        public NetMQScheduler(NetMQContext context, Poller poller = null)
        {
            m_context = context;
            if (poller == null)
            {
                m_ownPoller = true;

                m_poller = new Poller();
            }
            else
            {
                m_ownPoller = false;

                m_poller = poller;
            }

            m_clientSockets = new ConcurrentBag <NetMQSocket>();

            m_schedulerId = Interlocked.Increment(ref s_schedulerCounter);

            m_address = string.Format("{0}://scheduler-{1}", NetMQ.zmq.Address.InProcProtocol, m_schedulerId);

            m_serverSocket = context.CreatePullSocket();
            m_serverSocket.Options.Linger = TimeSpan.Zero;
            m_serverSocket.Bind(m_address);

            m_currentMessageHandler = OnMessageFirstTime;

            m_serverSocket.ReceiveReady += m_currentMessageHandler;

            m_poller.AddSocket(m_serverSocket);

            m_clientSocket = new ThreadLocal <NetMQSocket>(() =>
            {
                var socket = m_context.CreatePushSocket();
                socket.Connect(m_address);

                m_clientSockets.Add(socket);

                return(socket);
            });

            m_schedulerThread = new ThreadLocal <bool>(() => false);

            if (m_ownPoller)
            {
                Task.Factory.StartNew(m_poller.Start, TaskCreationOptions.LongRunning);
            }
        }
Beispiel #29
0
        public void Initialize()
        {
            LoggingExtensions.Logging.Log.InitializeWith <LoggingExtensions.log4net.Log4NetLog>();


            _context = NetMQContext.Create();

            var frontAdress   = ConfigurationManager.AppSettings["frontendBindAddress"];
            var bakdendAdress = ConfigurationManager.AppSettings["backendBindAddress"];

            _poller = new NetMQ.Poller();
            _queue  = new NetMQ.Devices.QueueDevice(_context, _poller, frontAdress, bakdendAdress);

            this.Log().Info("初始化ZeroMq完成");
        }
Beispiel #30
0
        public void Send_EmptyReplyFromBrokerWithLogging_ShouldThrowApplicationException()
        {
            const string host_address    = "tcp://localhost:5555";
            var          loggingMessages = new List <string> ();

            // setup the counter socket for communication
            using (var ctx = NetMQContext.Create())
                using (var broker = ctx.CreateRouterSocket())
                    using (var poller = new Poller())
                        using (var session = new MDPClient(host_address))
                        {
                            broker.Bind(host_address);
                            // we need to pick up any message in order to avoid errors
                            broker.ReceiveReady += (s, e) =>
                            {
                                // return empty reply
                                var msg = e.Socket.ReceiveMessage();
                                // we expect to receive a 4 Frame mesage
                                // [REQ ADR][EMPTY]["MDPC01"]["echo"]["REQUEST"]
                                if (msg.FrameCount != 5)
                                {
                                    Assert.Fail("Message with wrong count of frames {0}", msg.FrameCount);
                                }
                                // REQUEST socket will strip the his address + empty frame
                                // ROUTER has to add the address prelude in order to identify the correct socket(!)
                                // [REQ ADR][EMPTY]["MDPC01"]["echo"]["REQUEST"]
                                e.Socket.SendMessage(msg);
                            };

                            poller.AddSocket(broker);
                            var t = Task.Factory.StartNew(() => poller.Start());

                            // set the event handler to receive the logging messages
                            session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                            // well formed message
                            var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });
                            // correct call
                            session.Send("echo", requestMessage);

                            poller.Stop();
                            poller.RemoveSocket(broker);

                            Assert.That(loggingMessages.Count, Is.EqualTo(3));
                            Assert.That(loggingMessages[0], Is.EqualTo("[CLIENT] connecting to broker at tcp://localhost:5555"));
                            Assert.That(loggingMessages[1].Contains("[CLIENT INFO] sending"), Is.True);
                            Assert.That(loggingMessages[2].Contains("[CLIENT INFO] received"), Is.True);
                        }
        }
Beispiel #31
0
        /// <summary>
        /// Create a new instance of a Proxy (NetMQ.Proxy)
        /// with the given sockets to serve as a front-end, a back-end, and a control socket.
        /// </summary>
        /// <param name="frontend">the socket that messages will be forwarded from</param>
        /// <param name="backend">the socket that messages will be forwarded to</param>
        /// <param name="controlIn">this socket will have incoming messages also sent to it - you can set this to null if not needed</param>
        /// <param name="controlOut">this socket will have outgoing messages also sent to it - you can set this to null if not needed</param>
        /// <param name="poller">an optional external poller to use within this proxy</param>
        /// <exception cref="InvalidOperationException"><paramref name="poller"/> is not <c>null</c> and either <paramref name="frontend"/> or <paramref name="backend"/> are not contained within it.</exception>
        public Proxy([NotNull] NetMQSocket frontend, [NotNull] NetMQSocket backend, [NotNull] NetMQSocket controlIn, [NotNull] NetMQSocket controlOut, [CanBeNull] Poller poller = null)
        {
            if (poller != null)
            {
                if (!poller.ContainsSocket(backend) || !poller.ContainsSocket(frontend))
                    throw new InvalidOperationException("When using an external poller, both the frontend and backend sockets must be added to it.");

                m_externalPoller = true;
                m_poller = poller;
            }

            m_frontend = frontend;
            m_backend = backend;
            m_controlIn = controlIn;
            m_controlOut = controlOut ?? controlIn;
        }
Beispiel #32
0
        void IShimHandler.Run(PairSocket shim)
        {
            _pipe = shim;
            _pipe.SignalOK();
            _pipe.ReceiveReady += OnPipeReady;

            _timer = new NetMQTimer(TimeSpan.FromSeconds(1));
            _timer.Elapsed += OnPingPeer;

            _inbox = _context.CreateRouterSocket();
            _inbox.ReceiveReady += OnInboxReady;

            _poller = new Poller(_pipe);
            _poller.AddTimer(_timer);
            _poller.PollTillCancelled();
        }
Beispiel #33
0
        public static void Main(string[] args)
        {
            //
            // Task worker - design 2
            // Adds pub-sub flow to receive and respond to kill signal
            //
            // Author: metadings
            //

            // Socket to receive messages on,
            // Socket to send messages to and
            // Socket for control input
            using (var context = NetMQContext.Create())
            using (var receiver = context.CreatePullSocket())
            using (var sender = context.CreatePushSocket())
            using (var controller = context.CreateSubscriberSocket())
            {
                receiver.Connect("tcp://127.0.0.1:5557");
                sender.Connect("tcp://127.0.0.1:5558");
                controller.Connect("tcp://127.0.0.1:5559");

                controller.SubscribeToAnyTopic();

                var poller = new Poller();
                poller.AddSocket(receiver);
                poller.AddSocket(controller);

                receiver.ReceiveReady += (o, eventArgs) =>
                {
                    var workload = int.Parse(eventArgs.Socket.ReceiveFrameString());

                    Console.WriteLine("{0}.", workload); // Show progress

                    Thread.Sleep(workload); // Do the work

                    sender.SendFrame(new byte[0]); // Send results to sink
                };

                controller.ReceiveReady += (o, eventArgs) =>
                {
                    if (eventArgs.Socket.ReceiveFrameString() == "KILL");
                        poller.Cancel();
                };

                poller.PollTillCancelled();
            }
        }
Beispiel #34
0
        /// <summary>
        /// Create a new instance of a Proxy (NetMQ.Proxy)
        /// with the given sockets to serve as a front-end, a back-end, and a control socket.
        /// </summary>
        /// <param name="frontend">the socket that messages will be forwarded from</param>
        /// <param name="backend">the socket that messages will be forwarded to</param>
        /// <param name="control">this socket will have messages also sent to it - you can set this to null if not needed</param>
        /// <param name="poller">an optional external poller to use within this proxy</param>
        /// <exception cref="InvalidOperationException"><paramref name="poller"/> is not <c>null</c> and either <paramref name="frontend"/> or <paramref name="backend"/> are not contained within it.</exception>
        public Proxy([NotNull] NetMQSocket frontend, [NotNull] NetMQSocket backend, [CanBeNull] NetMQSocket control = null, Poller poller = null)
        {
            if (poller != null)
            {
                if (!poller.ContainsSocket(backend) || !poller.ContainsSocket(frontend))
                {
                    throw new InvalidOperationException("When using an external poller, both the frontend and backend sockets must be added to it.");
                }

                m_externalPoller = true;
                m_poller         = poller;
            }

            m_frontend = frontend;
            m_backend  = backend;
            m_control  = control;
        }
Beispiel #35
0
        public void Start()
        {
            using (m_serverSocket = m_context.CreateResponseSocket())
            {
                foreach (var address in m_addresses)
                {
                    m_log.InfoFormat("Listening on {0}", address);
                    m_serverSocket.Bind(address);
                }
                m_serverSocket.ReceiveReady += OnMessage;

                m_poller = new Poller();
                m_poller.AddSocket(m_serverSocket);

                m_poller.Start();
            }
        }
Beispiel #36
0
        /// <summary>
        /// Starts the server.
        /// </summary>
        public void StartServer()
        {
            if (_runServer)
            {
                return;
            }

            _runServer = true;
            _context   = NetMQContext.Create();

            _socket = _context.CreateSocket(NetMQ.zmq.ZmqSocketType.Rep);
            _socket.Bind("tcp://*:" + _socketPort);
            _socket.ReceiveReady += _socket_ReceiveReady;
            _poller = new Poller(new[] { _socket });

            Task.Factory.StartNew(_poller.Start, TaskCreationOptions.LongRunning);
        }
Beispiel #37
0
        public void SingleSocketPollTest()
        {
            using (NetMQContext contex = NetMQContext.Create())
            {
                using (var rep = contex.CreateResponseSocket())
                {
                    rep.Bind("tcp://127.0.0.1:5002");

                    using (var req = contex.CreateRequestSocket())
                        using (Poller poller = new Poller())
                        {
                            req.Connect("tcp://127.0.0.1:5002");

                            //The ReceiveReady event is raised by the Poller
                            rep.ReceiveReady += (s, a) =>
                            {
                                bool   more;
                                string m = a.Socket.ReceiveString(out more);

                                Assert.False(more);
                                Assert.AreEqual("Hello", m);

                                a.Socket.Send("World");
                            };


                            poller.AddSocket(rep);

                            Task pollerTask = Task.Factory.StartNew(poller.Start);
                            req.Send("Hello");

                            bool   more2;
                            string m1 = req.ReceiveString(out more2);

                            Assert.IsFalse(more2);
                            Assert.AreEqual("World", m1);

                            poller.Stop();

                            Thread.Sleep(100);
                            Assert.IsTrue(pollerTask.IsCompleted);
                        }
                }
            }
        }
Beispiel #38
0
        /// <summary>
        /// Stops the proxy, blocking until the underlying <see cref="Poller"/> has completed.
        /// </summary>
        /// <exception cref="InvalidOperationException">The proxy has not been started.</exception>
        public void Stop()
        {
            if (Interlocked.CompareExchange(ref m_state, StateStopping, StateStarted) != StateStarted)
            {
                throw new InvalidOperationException("Proxy has not been started");
            }

            if (!m_externalPoller)
            {
                m_poller.CancelAndJoin();
                m_poller = null;
            }

            m_frontend.ReceiveReady -= OnFrontendReady;
            m_backend.ReceiveReady  -= OnBackendReady;

            m_state = StateStopped;
        }
Beispiel #39
0
        /// <summary>
        ///     run the broker - if not bound to endpoint automatically binds to known endpoint
        /// </summary>
        /// <param name="token">CancellationToken to cancel the method</param>
        /// <exception cref="InvalidOperationException">Can't start same broker more than once!</exception>
        public void RunSynchronous(CancellationToken token)
        {
            if (m_isRunning)
            {
                throw new InvalidOperationException("Can't start same broker more than once!");
            }

            if (!m_isBound)
            {
                Bind();
            }

            m_isRunning = true;

            using (var poller = new NetMQ.Poller())
            {
                Socket.ReceiveReady += ProcessReceiveMessage;
                // get timer for scheduling heartbeat
                var timer = new NetMQTimer(HeartbeatInterval);
                // send every 'HeartbeatInterval' a heartbeat to all not expired workers
                timer.Elapsed += (s, e) => SendHeartbeat();

                poller.AddSocket(Socket);
                poller.AddTimer(timer);

                Log("[BROKER] Starting to listen for incoming messages ...");

                // start the poller and wait for the return, which will happen once token is
                // signalling Cancel(!)
                Task.Factory.StartNew(poller.Start, token).Wait();

                Log("[BROKER] ... Stopped!");

                // clean up
                poller.RemoveTimer(timer);
                poller.RemoveSocket(Socket);
                // unregister event handler
                Socket.ReceiveReady -= ProcessReceiveMessage;
            }

            m_isRunning = false;
        }
Beispiel #40
0
        /// <summary>
        /// Starts the publishing and request servers.
        /// </summary>
        public void StartServer()
        {
            if (!ServerRunning)
            {
                _context = NetMQContext.Create();

                //the publisher socket
                _pubSocket = _context.CreatePublisherSocket();
                _pubSocket.Bind("tcp://*:" + PublisherPort);

                //the request socket
                _reqSocket = _context.CreateSocket(ZmqSocketType.Rep);
                _reqSocket.Bind("tcp://*:" + RequestPort);
                _reqSocket.ReceiveReady += _reqSocket_ReceiveReady;

                _poller = new Poller(new[] { _reqSocket });
                Task.Factory.StartNew(_poller.Start, TaskCreationOptions.LongRunning);
            }
            ServerRunning = true;
        }
Beispiel #41
0
        static void Main(string[] args)
        {
            using (var context = NetMQContext.Create())
            {
                string[] topics = new string[] { "A", "B", "C" };
                Random random = new Random();

                using (var publisher = context.CreateXPublisherSocket())
                {
                    // Set publisher to manual subscriptions mode
                    publisher.Options.ManualPublisher = true;
                    publisher.Bind("tcp://*:5556");
                    publisher.ReceiveReady += (sender, eventArgs) =>
                    {
                        var message = publisher.ReceiveString();

                        // we only handle subscription requests, unsubscription and any
                        // other type of messages will be dropped
                        if (message[0] == (char)1)
                        {
                            // calling Subscribe to add subscription to the last subscriber
                            publisher.Subscribe(message.Substring(1));
                        }
                    };

                    NetMQTimer sendTimer = new NetMQTimer(1000);
                    sendTimer.Elapsed += (sender, eventArgs) =>
                    {
                        // sends a message every second with random topic and current time
                        publisher.
                            SendMore(topics[random.Next(3)]).
                            Send(DateTime.Now.ToString());
                    };

                    Poller poller = new Poller();
                    poller.AddSocket(publisher);
                    poller.AddTimer(sendTimer);
                    poller.PollTillCancelled();
                }
            }
        }
Beispiel #42
0
        public NetMQScheduler(NetMQContext context, Poller poller = null)
        {
            m_context = context;
            if (poller == null)
            {
                m_ownPoller = true;
                m_poller    = new Poller();
            }
            else
            {
                m_ownPoller = false;
                m_poller    = poller;
            }

            m_tasksQueue = new ConcurrentQueue <Task>();
            m_syncObject = new object();

            m_schedulerId = Interlocked.Increment(ref s_schedulerCounter);

            m_address = string.Format("{0}://scheduler-{1}", NetMQ.zmq.Address.InProcProtocol, m_schedulerId);

            m_serverSocket = context.CreatePullSocket();
            m_serverSocket.Options.Linger = TimeSpan.Zero;
            m_serverSocket.Bind(m_address);

            m_currentMessageHandler = OnMessageFirstTime;

            m_serverSocket.ReceiveReady += m_currentMessageHandler;

            m_poller.AddSocket(m_serverSocket);

            m_clientSocket = m_context.CreatePushSocket();
            m_clientSocket.Connect(m_address);

            m_schedulerThread = new ThreadLocal <bool>(() => false);

            if (m_ownPoller)
            {
                m_poller.PollTillCancelledNonBlocking();
            }
        }
        static void Main(string[] args)
        {
            using (NetMQContext context = NetMQContext.Create())
            using (NetMQSocket subscribeSocket = context.CreateSubscriberSocket())
            {
                subscribeSocket.Connect("tcp://127.0.0.1:5002");
                subscribeSocket.ReceiveReady += SubSocketOnReceiveReady;
                subscribeSocket.Subscribe(""); //Prefix of messages to receive. Empty string receives all messages
                subscribeSocket.Options.TcpKeepalive = true;
                subscribeSocket.Options.TcpKeepaliveIdle = TimeSpan.FromSeconds(5);
                subscribeSocket.Options.TcpKeepaliveInterval = TimeSpan.FromSeconds(1);

                Poller poller = new Poller();
                poller.AddSocket(subscribeSocket);
                Task.Factory.StartNew(poller.Start);
                Console.WriteLine("Waiting to receive messages.  Press 'q' to quit.");
                while (Console.ReadLine() != "q")
                { }
                poller.Stop(true);
            }
        }
Beispiel #44
0
        /// <summary>
        /// Start proxying messages between the front and back ends. Blocks, unless using an external <see cref="Poller"/>.
        /// </summary>
        /// <exception cref="InvalidOperationException">The proxy has already been started.</exception>
        public void Start()
        {
            if (Interlocked.CompareExchange(ref m_state, StateStarting, StateStopped) != StateStopped)
            {
                throw new InvalidOperationException("Proxy has already been started");
            }

            m_frontend.ReceiveReady += OnFrontendReady;
            m_backend.ReceiveReady  += OnBackendReady;

            if (m_externalPoller)
            {
                m_state = StateStarted;
            }
            else
            {
                m_poller = new Poller(m_frontend, m_backend);
                m_state  = StateStarted;
                m_poller.PollTillCancelled();
            }
        }
Beispiel #45
0
        /// <summary>
        /// Create a new NetMQScheduler object within the given context, and optionally using the given poller.
        /// </summary>
        /// <param name="context">the NetMQContext to create this NetMQScheduler within</param>
        /// <param name="poller">(optional)the Poller for this Net to use</param>
        public NetMQScheduler([NotNull] NetMQContext context, [CanBeNull] Poller poller = null)
        {
            if (poller == null)
            {
                m_ownPoller = true;
                m_poller = new Poller();
            }
            else
            {
                m_ownPoller = false;
                m_poller = poller;
            }

            m_tasksQueue = new ConcurrentQueue<Task>();
            m_syncObject = new object();

            var schedulerId = Interlocked.Increment(ref s_schedulerCounter);

            var address = string.Format("{0}://scheduler-{1}", Address.InProcProtocol, schedulerId);

            m_serverSocket = context.CreatePullSocket();
            m_serverSocket.Options.Linger = TimeSpan.Zero;
            m_serverSocket.Bind(address);

            m_currentMessageHandler = OnMessageFirstTime;

            m_serverSocket.ReceiveReady += m_currentMessageHandler;

            m_poller.AddSocket(m_serverSocket);

            m_clientSocket = context.CreatePushSocket();
            m_clientSocket.Connect(address);

            m_schedulerThread = new ThreadLocal<bool>(() => false);

            if (m_ownPoller)
            {
                m_poller.PollTillCancelledNonBlocking();
            }
        }
            public void RunPipeline(PairSocket shim)
            {
                // we should signal before running the poller but this will block the application
                shim.SignalOK();

                this.poller = new Poller();

                shim.ReceiveReady += OnShimReady;
                poller.AddSocket(shim);

                timeoutTimer          = new NetMQTimer(StreamingProtocol.Timeout);
                timeoutTimer.Elapsed += TimeoutElapsed;
                poller.AddTimer(timeoutTimer);

                Connect();

                poller.Start();

                if (subscriberSocket != null)
                {
                    subscriberSocket.Dispose();
                }
            }
Beispiel #47
0
        protected void Dispose(bool disposing)
        {
            if (!_disposedValue)
            {
                if (disposing)
                {
                    _proxy.Stop();
                    _proxy = null;

                    _poller.CancelAndJoin();
                    _poller.Dispose();
                    _poller = null;

                    _backendSocket.Dispose();
                    _backendSocket = null;

                    _frontendSocket.Dispose();
                    _frontendSocket = null;
                }

                _disposedValue = true;
            }
        }
Beispiel #48
0
        private NetMQScheduler(Poller poller, PushSocket pushSocket, PullSocket pullSocket)
        {
            if (poller == null)
            {
                m_ownPoller = true;
                m_poller    = new Poller();
            }
            else
            {
                m_ownPoller = false;
                m_poller    = poller;
            }

            var address = string.Format("{0}://scheduler-{1}",
                                        Address.InProcProtocol,
                                        Interlocked.Increment(ref s_schedulerCounter));

            m_serverSocket = pullSocket;
            m_serverSocket.Options.Linger = TimeSpan.Zero;
            m_serverSocket.Bind(address);

            m_currentMessageHandler = OnMessageFirstTime;

            m_serverSocket.ReceiveReady += m_currentMessageHandler;

            m_poller.AddSocket(m_serverSocket);

            m_clientSocket = pushSocket;
            m_clientSocket.Connect(address);

            m_isSchedulerThread = new ThreadLocal <bool>(() => false);

            if (m_ownPoller)
            {
                m_poller.PollTillCancelledNonBlocking();
            }
        }
Beispiel #49
0
        private NetMQScheduler(Poller poller, PushSocket pushSocket, PullSocket pullSocket)
        {
            if (poller == null)
            {
                m_ownPoller = true;
                m_poller = new Poller();
            }
            else
            {
                m_ownPoller = false;
                m_poller = poller;
            }

            var address = string.Format("{0}://scheduler-{1}",
                Address.InProcProtocol,
                Interlocked.Increment(ref s_schedulerCounter));

            m_serverSocket = pullSocket;
            m_serverSocket.Options.Linger = TimeSpan.Zero;
            m_serverSocket.Bind(address);

            m_currentMessageHandler = OnMessageFirstTime;

            m_serverSocket.ReceiveReady += m_currentMessageHandler;

            m_poller.AddSocket(m_serverSocket);

            m_clientSocket = pushSocket;
            m_clientSocket.Connect(address);

            m_isSchedulerThread = new ThreadLocal<bool>(() => false);

            if (m_ownPoller)
            {
                m_poller.PollTillCancelledNonBlocking();
            }
        }
Beispiel #50
0
        public void Send_WrongServiceNameWithLogging_ShouldLogPermanentError()
        {
            const string host_address    = "tcp://localhost:5555";
            var          loggingMessages = new List <string> ();

            // setup the counter socket for communication
            using (var ctx = NetMQContext.Create())
                using (var broker = ctx.CreateRouterSocket())
                    using (var poller = new Poller())
                        using (var session = new MDPClient(host_address))
                        {
                            broker.Bind(host_address);
                            // we need to pick up any message in order to avoid errors
                            broker.ReceiveReady += (s, e) =>
                            {
                                // just swallow message -> wrong service name
                                var msg = e.Socket.ReceiveMessage();
                            };

                            poller.AddSocket(broker);
                            var t = Task.Factory.StartNew(() => poller.Start());

                            // set the event handler to receive the logging messages
                            session.LogInfoReady += (s, e) => loggingMessages.Add(e.Info);
                            // well formed message
                            var requestMessage = new NetMQMessage(new[] { new NetMQFrame("REQUEST") });
                            // wrong service name
                            session.Send("xyz", requestMessage);

                            poller.Stop();
                            poller.RemoveSocket(broker);

                            Assert.That(loggingMessages.Count, Is.EqualTo(7));
                            Assert.That(loggingMessages[6], Is.EqualTo("[CLIENT ERROR] permanent error, abandoning!"));
                        }
        }
Beispiel #51
0
            public void Run(PairSocket shim)
            {
                m_pipe = shim;

                shim.SignalOK();

                m_pipe.ReceiveReady += OnPipeReady;

                m_pingTimer          = new NetMQTimer(0);
                m_pingTimer.Elapsed += PingElapsed;
                m_pingTimer.Enable   = false;

                m_poller = new Poller();
                m_poller.AddSocket(m_pipe);
                m_poller.AddTimer(m_pingTimer);

                m_poller.PollTillCancelled();

                // the beacon might never been configured
                if (m_udpSocket != null)
                {
                    m_udpSocket.Close();
                }
            }
Beispiel #52
0
 /// <summary>
 /// Create a new instance of a Proxy (NetMQ.Proxy)
 /// with the given sockets to serve as a front-end, a back-end, and a control socket.
 /// </summary>
 /// <param name="frontend">the socket that messages will be forwarded from</param>
 /// <param name="backend">the socket that messages will be forwarded to</param>
 /// <param name="control">this socket will have messages also sent to it - you can set this to null if not needed</param>
 /// <param name="poller">an optional external poller to use within this proxy</param>
 /// <exception cref="InvalidOperationException"><paramref name="poller"/> is not <c>null</c> and either <paramref name="frontend"/> or <paramref name="backend"/> are not contained within it.</exception>
 public Proxy([NotNull] NetMQSocket frontend, [NotNull] NetMQSocket backend, [CanBeNull] NetMQSocket control = null, [CanBeNull] Poller poller = null)
     : this(frontend, backend, control, null, poller)
 {
 }
Beispiel #53
0
        public void Run()
        {
            //NOTES
            //1. Use many threads each writing to ConcurrentQueue
            //2. Extra thread to read from ConcurrentQueue, and this is the one that
            //   will deal with writing to the server
            ConcurrentQueue<string> messages = new ConcurrentQueue<string>();
            int delay = 3000;
            Poller poller = new Poller();

            using (NetMQContext ctx = NetMQContext.Create())
            {
                using (var server = ctx.CreateRouterSocket())
                {
                    server.Bind("tcp://127.0.0.1:5556");

                    //start some threads, where each thread, will use a client side
                    //broker (simple thread that monitors a CooncurrentQueue), where
                    //ONLY the client side broker talks to the server
                    for (int i = 0; i < 3; i++)
                    {
                        Task.Factory.StartNew((state) =>
                        {
                            while (true)
                            {
                                messages.Enqueue(state.ToString());
                                Thread.Sleep(delay);
                            }

                        }, string.Format("client {0}", i), TaskCreationOptions.LongRunning);
                    }

                    //single sender loop
                    Task.Factory.StartNew((state) =>
                    {
                        var client = ctx.CreateDealerSocket();
                        client.Connect("tcp://127.0.0.1:5556");
                        client.ReceiveReady += Client_ReceiveReady;
                        poller.AddSocket(client);

                        while (true)
                        {
                            string clientMessage = null;
                            if (messages.TryDequeue(out clientMessage))
                            {
                                var messageToServer = new NetMQMessage();
                                messageToServer.AppendEmptyFrame();
                                messageToServer.Append(clientMessage);
                                client.SendMessage(messageToServer);
                            }
                        }

                    }, TaskCreationOptions.LongRunning);

                    //start the poller
                    Task task = Task.Factory.StartNew(poller.Start);

                    //server loop
                    while (true)
                    {
                        var clientMessage = server.ReceiveMessage();
                        Console.WriteLine("========================");
                        Console.WriteLine(" INCOMING CLIENT MESSAGE ");
                        Console.WriteLine("========================");
                        for (int i = 0; i < clientMessage.FrameCount; i++)
                        {
                            Console.WriteLine("Frame[{0}] = {1}", i,
                                clientMessage[i].ConvertToString());
                        }

                        if (clientMessage.FrameCount == 3)
                        {
                            var clientAddress = clientMessage[0];
                            var clientOriginalMessage = clientMessage[2].ConvertToString();
                            string response = string.Format("{0} back from server {1}",
                                clientOriginalMessage, DateTime.Now.ToLongTimeString());
                            var messageToClient = new NetMQMessage();
                            messageToClient.Append(clientAddress);
                            messageToClient.AppendEmptyFrame();
                            messageToClient.Append(response);
                            server.SendMessage(messageToClient);
                        }
                    }
                }
            }
        }
Beispiel #54
0
        public void Run()
        {
            Console.WriteLine("[CLIENT {0}] Starting", m_id);

            var rnd = new Random(m_id);
            // each request shall have an unique id in order to recognize an reply for a request
            var messageId = new byte[5];
            // create clientId for messages
            var clientId = new[] { m_id };
            // a flag to signal that an answer has arrived
            bool messageAnswered = false;
            // we use a poller because we have a socket and a timer to monitor
            using (var clientPoller = new Poller())
            using (var context = NetMQContext.Create())
            using (var client = context.CreateRequestSocket())
            using (var monitor = context.CreatePushSocket())
            {
                client.Connect(m_localFrontendAddress);
                monitor.Connect(m_monitorAddress);

                client.Options.Identity = new[] { m_id };
                var timer = new NetMQTimer((int)TimeSpan.FromSeconds(10).TotalMilliseconds);

                // use as flag to indicate exit
                var exit = false;

                // every 10 s check if message has been received, if not then send error message and ext
                // and restart timer otherwise
                timer.Elapsed += (s, e) =>
                {
                    if (messageAnswered)
                    {
                        e.Timer.Enable = true;
                    }
                    else
                    {
                        var msg = string.Format("[CLIENT {0}] ERR - EXIT - lost message {1}", m_id, messageId);
                        // send an error message
                        monitor.Send(msg);

                        // if poller is started than stop it
                        if (clientPoller.IsStarted)
                            clientPoller.CancelAndJoin();
                        // mark the required exit
                        exit = true;
                    }
                };

                // process arriving answers
                client.ReceiveReady += (s, e) =>
                {
                    // mark the arrival of an answer
                    messageAnswered = true;
                    // worker is supposed to answer with our request id
                    var reply = e.Socket.ReceiveMultipartMessage();

                    if (reply.FrameCount == 0)
                    {
                        // something went wrong
                        monitor.Send(string.Format("[CLIENT {0}] Received an empty message!", m_id));
                        // mark the exit flag to ensure the exit
                        exit = true;
                    }
                    else
                    {
                        var sb = new StringBuilder();

                        // create success message
                        foreach (var frame in reply)
                            sb.Append("[" + frame.ConvertToString() + "]");

                        // send the success message
                        monitor.Send(string.Format("[CLIENT {0}] Received answer {1}",
                            m_id,
                            sb.ToString()));
                    }
                };

                // add socket & timer to poller
                clientPoller.AddSocket(client);
                clientPoller.AddTimer(timer);

                // start poller in another thread to allow the continued processing
                clientPoller.PollTillCancelledNonBlocking();

                // if true the message has been answered
                // the 0th message is always answered
                messageAnswered = true;

                while (!exit)
                {
                    // simulate sporadic activity by randomly delaying
                    Thread.Sleep((int)TimeSpan.FromSeconds(rnd.Next(5)).TotalMilliseconds);

                    // only send next message if the previous one has been replied to
                    if (messageAnswered)
                    {
                        // generate random 5 byte as identity for for the message
                        rnd.NextBytes(messageId);

                        messageAnswered = false;

                        // create message [client adr][empty][message id] and send it
                        var msg = new NetMQMessage();

                        msg.Push(messageId);
                        msg.Push(NetMQFrame.Empty);
                        msg.Push(clientId);

                        client.SendMessage(msg);
                    }
                }

                // stop poller if needed
                if (clientPoller.IsStarted)
                    clientPoller.CancelAndJoin();
            }
        }
Beispiel #55
0
        public void Run()
        {
            //NOTES
            //1. Use ThreadLocal<DealerSocket> where each thread has
            //  its own client DealerSocket to talk to server
            //2. Each thread can send using it own socket
            //3. Each thread socket is added to poller

            ThreadLocal<DealerSocket> clientSocketPerThread =
                new ThreadLocal<DealerSocket>();
            int delay = 3000;
            Poller poller = new Poller();

            using (NetMQContext ctx = NetMQContext.Create())
            {
                using (var server = ctx.CreateRouterSocket())
                {
                    server.Bind("tcp://127.0.0.1:5556");

                    //start some threads, each with its own DealerSocket
                    //to talk to the server socket. Creates lots of sockets,
                    //but no nasty race conditions no shared state, each
                    //thread has its own socket, happy days
                    for (int i = 0; i < 3; i++)
                    {
                        Task.Factory.StartNew((state) =>
                        {
                            DealerSocket client = null;

                            if (!clientSocketPerThread.IsValueCreated)
                            {
                                client = ctx.CreateDealerSocket();
                                client.Connect("tcp://127.0.0.1:5556");
                                client.ReceiveReady += Client_ReceiveReady;
                                clientSocketPerThread.Value = client;
                                poller.AddSocket(client);
                            }
                            else
                            {
                                client = clientSocketPerThread.Value;
                            }

                            while (true)
                            {
                                var messageToServer = new NetMQMessage();
                                messageToServer.AppendEmptyFrame();
                                messageToServer.Append(state.ToString());
                                client.SendMessage(messageToServer);
                                Thread.Sleep(delay);
                            }

                        },string.Format("client {0}", i), TaskCreationOptions.LongRunning);
                    }

                    //start the poller
                    Task task = Task.Factory.StartNew(poller.Start);

                    //server loop
                    while (true)
                    {
                        var clientMessage = server.ReceiveMessage();
                        Console.WriteLine("========================");
                        Console.WriteLine(" INCOMING CLIENT MESSAGE ");
                        Console.WriteLine("========================");
                        for (int i = 0; i < clientMessage.FrameCount; i++)
                        {
                            Console.WriteLine("Frame[{0}] = {1}", i,
                                clientMessage[i].ConvertToString());
                        }

                        if (clientMessage.FrameCount == 3)
                        {
                            var clientAddress = clientMessage[0];
                            var clientOriginalMessage = clientMessage[2].ConvertToString();
                            string response = string.Format("{0} back from server {1}",
                                clientOriginalMessage, DateTime.Now.ToLongTimeString());
                            var messageToClient = new NetMQMessage();
                            messageToClient.Append(clientAddress);
                            messageToClient.AppendEmptyFrame();
                            messageToClient.Append(response);
                            server.SendMessage(messageToClient);
                        }
                    }
                }
            }
        }
Beispiel #56
0
 public NetMQScheduler([NotNull] NetMQContext context, [CanBeNull] Poller poller = null) :
     this(poller, context.CreatePushSocket(), context.CreatePullSocket())
 {
 }
Beispiel #57
0
 /// <summary>
 /// Create a new NetMQScheduler object within the given context, and optionally using the given poller.
 /// </summary>
 /// <param name="poller">(optional)the Poller for this Net to use</param>
 public NetMQScheduler([CanBeNull] Poller poller = null) :
     this(poller, new PushSocket(), new PullSocket())
 {
 }
Beispiel #58
0
        public void AddSocketDuringWorkTest()
        {
            using (NetMQContext contex = NetMQContext.Create())
            {
                // we are using three responses to make sure we actually
                //move the correct socket and other sockets still work
                using (var router = contex.CreateRouterSocket())
                    using (var router2 = contex.CreateRouterSocket())
                    {
                        router.Bind("tcp://127.0.0.1:5002");
                        router2.Bind("tcp://127.0.0.1:5003");

                        using (var dealer = contex.CreateDealerSocket())
                            using (var dealer2 = contex.CreateDealerSocket())
                                using (Poller poller = new Poller())
                                {
                                    dealer.Connect("tcp://127.0.0.1:5002");
                                    dealer2.Connect("tcp://127.0.0.1:5003");

                                    bool router1arrived = false;
                                    bool router2arrived = false;


                                    bool more;

                                    //The ReceiveReady event is raised by the Poller
                                    router2.ReceiveReady += (s, a) =>
                                    {
                                        router2.Receive(out more);
                                        router2.Receive(out more);
                                        router2arrived = true;
                                    };

                                    //The ReceiveReady event is raised by the Poller
                                    router.ReceiveReady += (s, a) =>
                                    {
                                        router1arrived = true;

                                        router.Receive(out more);
                                        router.Receive(out more);

                                        poller.AddSocket(router2);
                                    };

                                    poller.AddSocket(router);

                                    Task task = Task.Factory.StartNew(poller.Start);

                                    dealer.Send("1");
                                    Thread.Sleep(300);
                                    dealer2.Send("2");
                                    Thread.Sleep(300);

                                    poller.Stop(true);
                                    task.Wait();

                                    Assert.IsTrue(router1arrived);
                                    Assert.IsTrue(router2arrived);
                                }
                    }
            }
        }
Beispiel #59
0
        public void CancelSocketTest()
        {
            using (NetMQContext contex = NetMQContext.Create())
            {
                // we are using three responses to make sure we actually
                //move the correct socket and other sockets still work
                using (var router = contex.CreateRouterSocket())
                    using (var router2 = contex.CreateRouterSocket())
                        using (var router3 = contex.CreateRouterSocket())
                        {
                            router.Bind("tcp://127.0.0.1:5002");
                            router2.Bind("tcp://127.0.0.1:5003");
                            router3.Bind("tcp://127.0.0.1:5004");

                            using (var dealer = contex.CreateDealerSocket())
                                using (var dealer2 = contex.CreateDealerSocket())
                                    using (var dealer3 = contex.CreateDealerSocket())
                                        using (Poller poller = new Poller())
                                        {
                                            dealer.Connect("tcp://127.0.0.1:5002");
                                            dealer2.Connect("tcp://127.0.0.1:5003");
                                            dealer3.Connect("tcp://127.0.0.1:5004");

                                            bool first = true;

                                            //The ReceiveReady event is raised by the Poller
                                            router2.ReceiveReady += (s, a) =>
                                            {
                                                bool more;

                                                // identity
                                                byte[] identity = a.Socket.Receive(out more);

                                                // message
                                                a.Socket.Receive(out more);

                                                a.Socket.SendMore(identity);
                                                a.Socket.Send("2");
                                            };

                                            poller.AddSocket(router2);

                                            //The ReceiveReady event is raised by the Poller
                                            router.ReceiveReady += (s, a) =>
                                            {
                                                if (!first)
                                                {
                                                    Assert.Fail("This should happen because we cancelled the socket");
                                                }
                                                first = false;

                                                bool more;

                                                // identity
                                                a.Socket.Receive(out more);

                                                string m = a.Socket.ReceiveString(out more);

                                                Assert.False(more);
                                                Assert.AreEqual("Hello", m);

                                                // cancelling the socket
                                                poller.RemoveSocket(a.Socket);
                                            };

                                            poller.AddSocket(router);

                                            //The ReceiveReady event is raised by the Poller
                                            router3.ReceiveReady += (s, a) =>
                                            {
                                                bool more;

                                                // identity
                                                byte[] identity = a.Socket.Receive(out more);

                                                // message
                                                a.Socket.Receive(out more);

                                                a.Socket.SendMore(identity).Send("3");
                                            };

                                            poller.AddSocket(router3);

                                            Task pollerTask = Task.Factory.StartNew(poller.Start);

                                            dealer.Send("Hello");

                                            // sending this should not arrive on the poller,
                                            //therefore response for this will never arrive
                                            dealer.Send("Hello2");

                                            Thread.Sleep(100);

                                            // sending this should not arrive on the poller,
                                            //therefore response for this will never arrive
                                            dealer.Send("Hello3");

                                            Thread.Sleep(500);

                                            bool more2;

                                            // making sure the socket defined before the one cancelled still works
                                            dealer2.Send("1");
                                            string msg = dealer2.ReceiveString(out more2);
                                            Assert.AreEqual("2", msg);

                                            // making sure the socket defined after the one cancelled still works
                                            dealer3.Send("1");
                                            msg = dealer3.ReceiveString(out more2);
                                            Assert.AreEqual("3", msg);

                                            // we have to give this some time if we want to make sure
                                            //it's really not happening and it not only because of time
                                            Thread.Sleep(300);

                                            poller.Stop();

                                            Thread.Sleep(100);
                                            Assert.IsTrue(pollerTask.IsCompleted);
                                        }
                        }
            }
        }
Beispiel #60
0
        public void AddTwoSocketAfterRemovingTest()
        {
            using (NetMQContext contex = NetMQContext.Create())
            {
                // we are using three responses to make sure we actually
                //move the correct socket and other sockets still work
                using (var router = contex.CreateRouterSocket())
                    using (var router2 = contex.CreateRouterSocket())
                        using (var router3 = contex.CreateRouterSocket())
                            using (var router4 = contex.CreateRouterSocket())
                            {
                                router.Bind("tcp://127.0.0.1:5002");
                                router2.Bind("tcp://127.0.0.1:5003");
                                router3.Bind("tcp://127.0.0.1:5004");
                                router4.Bind("tcp://127.0.0.1:5005");

                                using (var dealer = contex.CreateDealerSocket())
                                    using (var dealer2 = contex.CreateDealerSocket())
                                        using (var dealer3 = contex.CreateDealerSocket())
                                            using (var dealer4 = contex.CreateDealerSocket())
                                                using (Poller poller = new Poller())

                                                {
                                                    dealer.Connect("tcp://127.0.0.1:5002");
                                                    dealer2.Connect("tcp://127.0.0.1:5003");
                                                    dealer3.Connect("tcp://127.0.0.1:5004");
                                                    dealer4.Connect("tcp://127.0.0.1:5005");


                                                    int  router1arrived = 0;
                                                    int  router2arrived = 0;
                                                    bool router3arrived = false;
                                                    bool router4arrived = false;

                                                    bool more;

                                                    //The ReceiveReady event is raised by the Poller
                                                    router.ReceiveReady += (s, a) =>
                                                    {
                                                        router1arrived++;

                                                        router.Receive(out more);
                                                        router.Receive(out more);

                                                        poller.RemoveSocket(router);
                                                    };

                                                    poller.AddSocket(router);

                                                    //The ReceiveReady event is raised by the Poller
                                                    router3.ReceiveReady += (s, a) =>
                                                    {
                                                        router3.Receive(out more);
                                                        router3.Receive(out more);
                                                        router3arrived = true;
                                                    };

                                                    //The ReceiveReady event is raised by the Poller
                                                    router4.ReceiveReady += (s, a) =>
                                                    {
                                                        router4.Receive(out more);
                                                        router4.Receive(out more);
                                                        router4arrived = true;
                                                    };

                                                    //The ReceiveReady event is raised by the Poller
                                                    router2.ReceiveReady += (s, a) =>
                                                    {
                                                        router2arrived++;
                                                        router2.Receive(out more);
                                                        router2.Receive(out more);

                                                        if (router2arrived == 1)
                                                        {
                                                            poller.AddSocket(router3);

                                                            poller.AddSocket(router4);
                                                        }
                                                    };

                                                    poller.AddSocket(router2);

                                                    Task task = Task.Factory.StartNew(poller.Start);

                                                    dealer.Send("1");
                                                    Thread.Sleep(300);
                                                    dealer2.Send("2");
                                                    Thread.Sleep(300);
                                                    dealer3.Send("3");
                                                    dealer4.Send("4");
                                                    dealer2.Send("2");
                                                    dealer.Send("1");
                                                    Thread.Sleep(300);

                                                    poller.Stop(true);
                                                    task.Wait();

                                                    router.Receive(true, out more);

                                                    Assert.IsTrue(more);

                                                    router.Receive(true, out more);

                                                    Assert.IsFalse(more);

                                                    Assert.AreEqual(1, router1arrived);
                                                    Assert.AreEqual(2, router2arrived);
                                                    Assert.IsTrue(router3arrived);
                                                    Assert.IsTrue(router4arrived);
                                                }
                            }
            }
        }