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;
            }
        }
Example #2
0
        private SubscriberSocket Connect(string[] addresses)
        {
            Console.WriteLine("a1");
            List <SubscriberSocket> sockets = new List <SubscriberSocket>();
            NetMQPoller             poller  = new NetMQPoller();

            SubscriberSocket connectedSocket = null;

            // event handler to handle message from socket
            EventHandler <NetMQSocketEventArgs> handleMessage = (sender, args) =>
            {
                Console.WriteLine("a5");
                //var topic = args.Socket.ReceiveFrameString();
                //Console.WriteLine("handleMessage Topic: {0}", topic);

                if (connectedSocket == null)
                {
                    Console.WriteLine("a6");
                    connectedSocket = (SubscriberSocket)args.Socket;
                    poller.Stop();
                }
            };

            // If timeout elapsed just cancel the poller without seting the connected socket
            NetMQTimer timeoutTimer = new NetMQTimer(TimeSpan.FromSeconds(5));

            timeoutTimer.Elapsed += (sender, args) => poller.Stop();
            poller.Add(timeoutTimer);

            foreach (var address in addresses)
            {
                var socket = new SubscriberSocket(address);
                sockets.Add(socket);

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

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

            Console.WriteLine("a2");
            poller.Run();
            Console.WriteLine("a9");

            // if we a connected socket the connection attempt succeed
            if (connectedSocket != null)
            {
                Console.WriteLine("a10");
                // 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);
            }
        }
Example #3
0
        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;
            }
        }
Example #4
0
        private void Connect()
        {
            _reconnectTimer.Enable = false;
            _timeoutTimer.Enable   = false;

            var sockets = new List <SubscriberSocket>();
            var poller  = new NetMQPoller();

            SubscriberSocket connectedSocket = null;

            // event handler to handle message from socket
            void HandleMessage(object sender, NetMQSocketEventArgs args)
            {
                connectedSocket = (SubscriberSocket)args.Socket;
                poller.Stop();
            }

            var timeoutTimer = new NetMQTimer(_heartbeatTimeOut);

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

            foreach (var address in _addresses)
            {
                var socket = new SubscriberSocket();
                sockets.Add(socket);

                socket.ReceiveReady += HandleMessage;
                poller.Add(socket);

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

            poller.Run();

            // 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
                _subscriber = connectedSocket;

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

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

                // subscribe to all subscriptions
                foreach (var subscription in _subscriptions)
                {
                    _subscriber.Subscribe(subscription);
                }

                _subscriber.ReceiveReady -= HandleMessage;
                _subscriber.ReceiveReady += OnSubscriberMessage;

                _actor.ReceiveReady += OnActorMessage;

                _poller.Add(_actor);

                _poller.Add(_subscriber);

                _timeoutTimer.EnableAndReset();
            }
            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();
                }

                _reconnectTimer.EnableAndReset();
            }
        }
Example #5
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);
            }
        }