Cancel() public method

Cancel the poller job when PollTillCancelled is called
public Cancel ( ) : void
return void
示例#1
0
文件: Program.cs 项目: dshaneg/zmqpoc
        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();
            }
        }
示例#2
0
            private void OnPipeReady(object sender, NetMQSocketEventArgs e)
            {
                NetMQMessage message = m_pipe.ReceiveMessage();

                string command = message.Pop().ConvertToString();

                switch (command)
                {
                case ConfigureCommand:
                    string interfaceName = message.Pop().ConvertToString();
                    int    port          = message.Pop().ConvertToInt32();
                    Configure(interfaceName, port);
                    break;

                case PublishCommand:
                    m_transmit           = message.Pop();
                    m_pingTimer.Interval = message.Pop().ConvertToInt32();

                    m_pingTimer.Enable = true;

                    SendUdpFrame(m_transmit);
                    break;

                case SilenceCommand:
                    m_transmit         = null;
                    m_pingTimer.Enable = false;
                    break;

                case SubscribeCommand:
                    m_filter = message.Pop();
                    break;

                case UnsubscribeCommand:
                    m_filter = null;
                    break;

                case NetMQActor.EndShimMessage:
                    m_poller.Cancel();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        private void Connect()
        {
            List<SubscriberSocket> sockets = new List<SubscriberSocket>();
            Poller poller = new Poller();

            SubscriberSocket connectedSocket = null;

            // event handler to handle message from socket
            EventHandler<NetMQSocketEventArgs> handleMessage = (sender, args) =>
            {
                connectedSocket = (SubscriberSocket)args.Socket;
                poller.Cancel();
            };

            NetMQTimer timeoutTimer = new NetMQTimer(TimeOut);

            // just cancel the poller without seting the connected socket
            timeoutTimer.Elapsed += (sender, args) => poller.Cancel();
            poller.AddTimer(timeoutTimer);

            foreach (var address in m_addresses)
            {
                var socket = m_context.CreateSubscriberSocket();
                sockets.Add(socket);

                socket.ReceiveReady += handleMessage;
                poller.AddSocket(socket);

                // Subscribe to welcome message
                socket.Subscribe(WelcomeMessage);
                socket.Connect(address);
            }

            poller.PollTillCancelled();

            // if we a connected socket the connection attempt succeed
            if (connectedSocket != null)
            {
                // remove the connected socket form the list
                sockets.Remove(connectedSocket);

                // close all exsiting connections
                foreach (var socket in sockets)
                {
                    // to close them immediatly we set the linger to zero
                    socket.Options.Linger = TimeSpan.Zero;
                    socket.Dispose();
                }

                // set the socket
                m_subscriber = connectedSocket;

                // drop the welcome message
                m_subscriber.SkipMultipartMessage();

                // subscribe to heartbeat
                m_subscriber.Subscribe(HeartbeatMessage);

                // subscribe to all subscriptions
                foreach (string subscription in m_subscriptions)
                {
                    m_subscriber.Subscribe(subscription);
                }

                m_subscriber.ReceiveReady -= handleMessage;
                m_subscriber.ReceiveReady += OnSubscriberMessage;
                m_poller.AddSocket(m_subscriber);

                m_timeoutTimer.Enable = true;
                m_reconnectTimer.Enable = false;
            }
            else
            {
                // close all exsiting connections
                foreach (var socket in sockets)
                {
                    // to close them immediatly we set the linger to zero
                    socket.Options.Linger = TimeSpan.Zero;
                    socket.Dispose();
                }

                m_reconnectTimer.Enable = true;
                m_timeoutTimer.Enable = false;
            }
        }
示例#4
0
        private SubscriberSocket Connect(string[] addresses)
        {
            List<SubscriberSocket> sockets = new List<SubscriberSocket>();
              Poller poller = new Poller();

              SubscriberSocket connectedSocket = null;

              // event handler to handle message from socket
              EventHandler<NetMQSocketEventArgs> handleMessage = (sender, args) =>
              {
            if (connectedSocket == null)
            {
              connectedSocket = (SubscriberSocket) args.Socket;
              poller.Cancel();
            }
              };

              // If timeout elapsed just cancel the poller without seting the connected socket
              NetMQTimer timeoutTimer = new NetMQTimer(TimeSpan.FromSeconds(5));
              timeoutTimer.Elapsed += (sender, args) => poller.Cancel();
              poller.AddTimer(timeoutTimer);

              foreach (var address in addresses)
              {
            var socket = m_context.CreateSubscriberSocket();
            sockets.Add(socket);

            socket.ReceiveReady += handleMessage;
            poller.AddSocket(socket);

            // Subscribe to welcome message
            socket.Subscribe("WM");
            socket.Connect(address);
              }

              poller.PollTillCancelled();

              // if we a connected socket the connection attempt succeed
              if (connectedSocket != null)
              {
            // remove the connected socket form the list
            sockets.Remove(connectedSocket);

            // close all existing sockets
            CloseSockets(sockets);

            // drop the welcome message
            connectedSocket.SkipMultipartMessage();

            // subscribe to heartbeat
            connectedSocket.Subscribe("HB");

            // subscribe to our only topic
            connectedSocket.Subscribe("A");

            connectedSocket.ReceiveReady -= handleMessage;
            connectedSocket.ReceiveReady += OnSubscriberMessage;

            return connectedSocket;
              }
              else
              {
            // close all existing sockets
            CloseSockets(sockets);

            return null;
              }
        }