Exemple #1
0
        public void Polling()
        {
            using (var speaker = new NetMQBeacon())
                using (var listener = new NetMQBeacon())
                {
                    speaker.Configure(9999);

                    speaker.Publish("Hello", s_publishInterval);

                    var manualResetEvent = new ManualResetEvent(false);

                    listener.Configure(9999);
                    listener.Subscribe("H");

                    string message = "";

                    listener.ReceiveReady += (sender, args) =>
                    {
                        message = listener.Receive().String;
                        manualResetEvent.Set();
                    };

                    using (var poller = new NetMQPoller {
                        listener
                    })
                    {
                        poller.RunAsync();

                        manualResetEvent.WaitOne();

                        Assert.AreEqual("Hello", message);
                    }
                }
        }
Exemple #2
0
        private void RunActor(PairSocket shim)
        {
            _shim = shim;

            using (_subscriber = new SubscriberSocket())
                using (_publisher = new PublisherSocket())
                {
                    using (_beacon = new NetMQBeacon())
                    {
                        _shim.ReceiveReady += OnShimReady;

                        _subscriber.Subscribe(string.Empty);
                        _port = _subscriber.BindRandomPort("tcp://*");
                        _logger?.LogInformation($"{_id}: Peer bus is bound to {{BusPort}}", _port);
                        _subscriber.ReceiveReady += OnSubscriberReady;

                        _logger?.LogInformation($"{_id}: Peer is broadcasting UDP on port {{BeaconPort}}", _beaconPort);
                        _beacon.Configure(_beaconPort);
                        _beacon.Publish(_port.ToString(), _beaconInterval);
                        _beacon.Subscribe(string.Empty);
                        _beacon.ReceiveReady += OnBeaconReady;

                        var cleanupTimer = new NetMQTimer(_cleanupInterval);
                        cleanupTimer.Elapsed += Cleanup;

                        _poll = new NetMQPoller {
                            _shim, _subscriber, _beacon, cleanupTimer
                        };
                        _shim.SignalOK();
                        _poll.Run();
                    }
                }
        }
Exemple #3
0
        public void Unsubscribe()
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (NetMQBeacon speaker = new NetMQBeacon(context))
                {
                    speaker.Configure(9999);

                    using (NetMQBeacon listener = new NetMQBeacon(context))
                    {
                        listener.Configure(9999);
                        listener.Subscribe("H");

                        // this should send one broadcast message and stop
                        speaker.Publish("Hello");

                        string peerName;
                        string message = listener.ReceiveString(out peerName);

                        listener.Unsubscribe();

                        Assert.AreEqual("Hello", message);

                        ISocketPollable socket = listener;
                        socket.Socket.Options.ReceiveTimeout = TimeSpan.FromSeconds(2);

                        Assert.Throws <AgainException>(() =>
                        {
                            message = listener.ReceiveString(out peerName);
                        });
                    }
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Stop node discovery and interconnection
        /// </summary>
        private void Stop()
        {
            if (!_isRunning)
            {
                return;
            }
            _loggerDelegate?.Invoke($"Stopping {_name} {_uuid.ToShortString6()}. Publishing beacon on port 0. Removing _beacon and _inbox from poller.");

            // Stop broadcast/listen beacon by broadcasting port 0
            PublishBeacon(0);
            Thread.Sleep(1); // Allow 1 millisecond for beacon to go out
            _poller.Remove(_beacon);
            _beacon.ReceiveReady -= OnBeaconReady;
            _beacon.Unsubscribe();
            _beacon.Dispose();
            _beacon = null;

            // Stop polling on inbox
            _poller.Remove(_inbox);
            _inbox.ReceiveReady -= OnInboxReady;
            _inbox.Dispose();


            // Tell the application we are stopping
            var msg = new NetMQMessage(3);

            msg.Append("STOP");
            msg.Append(_uuid.ToByteArray());
            msg.Append(_name);
            _outbox.SendMultipartMessage(msg);
            _isRunning = false;
        }
Exemple #5
0
        public void SimplePublishSubscribe()
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (NetMQBeacon speaker = new NetMQBeacon(context))
                {
                    speaker.Configure(9999);
                    Console.WriteLine(speaker.Hostname);

                    speaker.Publish("Hello");

                    using (NetMQBeacon listener = new NetMQBeacon(context))
                    {
                        listener.Configure(9999);
                        listener.Subscribe("H");

                        string peerName;
                        string message = listener.ReceiveString(out peerName);

                        Console.WriteLine(peerName);

                        Assert.AreEqual("Hello", message);
                    }
                }
            }
        }
Exemple #6
0
 public void NeverConfigured()
 {
     using (NetMQContext context = NetMQContext.Create())
     {
         using (NetMQBeacon speaker = new NetMQBeacon(context))
         {
         }
     }
 }
Exemple #7
0
        private void RunActor(PairSocket shim)
        {
            // save the shim to the class to use later
            m_shim = shim;

            // create all subscriber, publisher and beacon
            using (m_subscriber = new SubscriberSocket())
                using (m_publisher = new PublisherSocket())
                    using (m_beacon = new NetMQBeacon())
                    {
                        // listen to actor commands
                        m_shim.ReceiveReady += OnShimReady;

                        // subscribe to all messages
                        m_subscriber.Subscribe("");

                        // we bind to a random port, we will later publish this port
                        // using the beacon
                        m_randomPort = m_subscriber.BindRandomPort("tcp://*");
                        Console.WriteLine("Bus subscriber is bound to {0}", m_subscriber.Options.LastEndpoint);

                        // listen to incoming messages from other publishers, forward them to the shim
                        m_subscriber.ReceiveReady += OnSubscriberReady;

                        // configure the beacon to listen on the broadcast port
                        Console.WriteLine("Beacon is being configured to UDP port {0}", m_broadcastPort);
                        m_beacon.Configure(m_broadcastPort);

                        // publishing the random port to all other nodes
                        Console.WriteLine("Beacon is publishing the Bus subscriber port {0}", m_randomPort);
                        m_beacon.Publish(m_randomPort.ToString(), TimeSpan.FromSeconds(1));

                        // Subscribe to all beacon on the port
                        Console.WriteLine("Beacon is subscribing to all beacons on UDP port {0}", m_broadcastPort);
                        m_beacon.Subscribe("");

                        // listen to incoming beacons
                        m_beacon.ReceiveReady += OnBeaconReady;

                        // Create a timer to clear dead nodes
                        NetMQTimer timer = new NetMQTimer(TimeSpan.FromSeconds(1));
                        timer.Elapsed += ClearDeadNodes;

                        // Create and configure the poller with all sockets and the timer
                        m_poller = new NetMQPoller {
                            m_shim, m_subscriber, m_beacon, timer
                        };

                        // signal the actor that we finished with configuration and
                        // ready to work
                        m_shim.SignalOK();

                        // polling until cancelled
                        m_poller.Run();
                    }
        }
 public DiscoveryClient()
 {
     beaconList   = new VridgeServerBeaconList();
     beaconClient = new NetMQBeacon();
     beaconClient.ConfigureAllInterfaces(38219);
     beaconClient.Subscribe("");
     beaconClient.ReceiveReady += OnBeaconReceived;
     beaconPoller = new NetMQPoller()
     {
         beaconClient
     };
     beaconPoller.RunAsync();
 }
Exemple #9
0
        private void Initialize()
        {
            if (_beacon == null)
            {
                _beacon = new NetMQBeacon();
                _beacon.Configure(9999, GetAvalibleAddress());
                _beacon.Subscribe("");
                _beacon.ReceiveReady += OnBeaconReady;

                _selfNode.Address           = _beacon.BoundTo;
                _selfNode.HostName          = _beacon.HostName;
                _selfNode.ArgumentsChanged += OnBeaconArgumentsChanged;
            }
        }
Exemple #10
0
        public void SubscribeToDifferentTopic()
        {
            using (var speaker = new NetMQBeacon())
                using (var listener = new NetMQBeacon())
                {
                    speaker.Configure(9999);

                    listener.Configure(9999);
                    listener.Subscribe("B");

                    // this should send one broadcast message and stop
                    speaker.Publish("Hello", s_publishInterval);

                    Assert.IsFalse(listener.TryReceive(TimeSpan.FromMilliseconds(300), out BeaconMessage message));
                }
        }
Exemple #11
0
        public void BothSpeakerAndListenerOverLoopback()
        {
            using (var beacon1 = new NetMQBeacon())
                using (var beacon2 = new NetMQBeacon())
                {
                    beacon1.Configure(9998, "loopback");
                    beacon1.Publish("H1", s_publishInterval);
                    beacon1.Subscribe("H");

                    beacon2.Configure(9998, "loopback");
                    beacon2.Publish("H2", s_publishInterval);
                    beacon2.Subscribe("H");

                    Assert.AreEqual("H2", beacon1.Receive().String);
                    Assert.AreEqual("H1", beacon2.Receive().String);
                }
        }
Exemple #12
0
        public void SimplePublishSubscribe()
        {
            using (var speaker = new NetMQBeacon())
                using (var listener = new NetMQBeacon())
                {
                    speaker.Configure(9999);

                    speaker.Publish("Hello", s_publishInterval);

                    listener.Configure(9999);
                    listener.Subscribe("H");

                    var message = listener.Receive();

                    Assert.AreEqual("Hello", message.String);
                }
        }
Exemple #13
0
        public void SubscribeToDifferentTopic()
        {
            using (var speaker = new NetMQBeacon())
            using (var listener = new NetMQBeacon())
            {
                speaker.Configure(9999);

                listener.Configure(9999);
                listener.Subscribe("B");

                // this should send one broadcast message and stop
                speaker.Publish("Hello", s_publishInterval);

                BeaconMessage message;
                Assert.IsFalse(listener.TryReceive(TimeSpan.FromMilliseconds(300), out message));
            }
        }
Exemple #14
0
        public void BothSpeakerAndListenerOverLoopback()
        {
            using (var beacon1 = new NetMQBeacon())
            using (var beacon2 = new NetMQBeacon())
            {
                beacon1.Configure(9998, "loopback");
                beacon1.Publish("H1", s_publishInterval);
                beacon1.Subscribe("H");

                beacon2.Configure(9998, "loopback");
                beacon2.Publish("H2", s_publishInterval);
                beacon2.Subscribe("H");

                Assert.AreEqual("H2", beacon1.Receive().String);
                Assert.AreEqual("H1", beacon2.Receive().String);
            }
        }
Exemple #15
0
        public void ConfigureTwice()
        {
            using (var speaker = new NetMQBeacon())
            using (var listener = new NetMQBeacon())
            {
                speaker.Configure(5555);
                speaker.Configure(9999);

                speaker.Publish("Hello", s_publishInterval);

                listener.Configure(9999);
                listener.Subscribe("H");

                string message = listener.Receive().String;

                Assert.AreEqual("Hello", message);
            }
        }
Exemple #16
0
        public void ConfigureTwice()
        {
            using (var speaker = new NetMQBeacon())
                using (var listener = new NetMQBeacon())
                {
                    speaker.Configure(5555);
                    speaker.Configure(9999);

                    speaker.Publish("Hello", s_publishInterval);

                    listener.Configure(9999);
                    listener.Subscribe("H");

                    string message = listener.Receive().String;

                    Assert.AreEqual("Hello", message);
                }
        }
Exemple #17
0
        public void SubscribeToDifferentTopic()
        {
            using (var context = NetMQContext.Create())
                using (var speaker = new NetMQBeacon(context))
                    using (var listener = new NetMQBeacon(context))
                    {
                        speaker.Configure(9999);

                        listener.Configure(9999);
                        listener.Subscribe("B");

                        // this should send one broadcast message and stop
                        speaker.Publish("Hello", s_publishInterval);

                        string peerName;
                        string message;
                        Assert.IsFalse(listener.TryReceiveString(TimeSpan.FromMilliseconds(300), out peerName, out message));
                    }
        }
Exemple #18
0
        public void SimplePublishSubscribe()
        {
            using (var speaker = new NetMQBeacon())
            using (var listener = new NetMQBeacon())
            {
                speaker.Configure(9999);

                speaker.Publish("Hello", s_publishInterval);

                listener.Configure(9999);
                listener.Subscribe("H");

                var message = listener.Receive();

                Console.WriteLine(message.PeerAddress);

                Assert.AreEqual("Hello", message.String);
            }
        }
Exemple #19
0
        public void Polling()
        {
            using (var context = NetMQContext.Create())
                using (var speaker = new NetMQBeacon(context))
                    using (var listener = new NetMQBeacon(context))
                    {
                        speaker.Configure(9999);
                        Console.WriteLine(speaker.Hostname);

                        speaker.Publish("Hello", s_publishInterval);

                        var manualResetEvent = new ManualResetEvent(false);

                        listener.Configure(9999);
                        listener.Subscribe("H");

                        string peerName = "";
                        string message  = "";

                        listener.ReceiveReady += (sender, args) =>
                        {
                            message = listener.ReceiveString(out peerName);
                            manualResetEvent.Set();
                        };

                        using (var poller = new Poller(listener)
                        {
                            PollTimeout = 10
                        })
                        {
                            poller.PollTillCancelledNonBlocking();

                            manualResetEvent.WaitOne();

                            Console.WriteLine(peerName);

                            Assert.AreEqual("Hello", message);

                            poller.CancelAndJoin();
                        }
                    }
        }
Exemple #20
0
        public void Polling()
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (NetMQBeacon speaker = new NetMQBeacon(context))
                {
                    speaker.Configure(9999);
                    Console.WriteLine(speaker.Hostname);

                    speaker.Publish("Hello");

                    using (NetMQBeacon listener = new NetMQBeacon(context))
                    {
                        ManualResetEvent manualResetEvent = new ManualResetEvent(false);

                        listener.Configure(9999);
                        listener.Subscribe("H");

                        string peerName = "";
                        string message  = "";

                        listener.ReceiveReady += (sender, args) =>
                        {
                            message = listener.ReceiveString(out peerName);
                            manualResetEvent.Set();
                        };

                        Poller poller = new Poller(listener);

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

                        manualResetEvent.WaitOne();

                        Console.WriteLine(peerName);

                        Assert.AreEqual("Hello", message);

                        poller.Stop(true);
                    }
                }
            }
        }
Exemple #21
0
        public void Unsubscribe()
        {
            using (var speaker = new NetMQBeacon())
                using (var listener = new NetMQBeacon())
                {
                    speaker.Configure(9999);

                    listener.Configure(9999);
                    listener.Subscribe("H");

                    // this should send one broadcast message and stop
                    speaker.Publish("Hello", s_publishInterval);
                    Thread.Sleep(10);
                    listener.Unsubscribe();

                    Assert.AreEqual("Hello", listener.Receive().String);

                    Assert.IsFalse(listener.TryReceive(TimeSpan.FromMilliseconds(300), out BeaconMessage message));
                }
        }
Exemple #22
0
        public void SimplePublishSubscribe()
        {
            using (var speaker = new NetMQBeacon())
            using (var listener = new NetMQBeacon())
            {
                speaker.Configure(9999);
                Console.WriteLine(speaker.Hostname);

                speaker.Publish("Hello", s_publishInterval);

                listener.Configure(9999);
                listener.Subscribe("H");

                string peerName;
                string message = listener.ReceiveString(out peerName);

                Console.WriteLine(peerName);

                Assert.AreEqual("Hello", message);
            }
        }
Exemple #23
0
        public void Unsubscribe()
        {
            using (var speaker = new NetMQBeacon())
            using (var listener = new NetMQBeacon())
            {
                speaker.Configure(9999);

                listener.Configure(9999);
                listener.Subscribe("H");

                // this should send one broadcast message and stop
                speaker.Publish("Hello", s_publishInterval);
                Thread.Sleep(10);
                listener.Unsubscribe();

                Assert.AreEqual("Hello", listener.Receive().String);

                BeaconMessage message;
                Assert.IsFalse(listener.TryReceive(TimeSpan.FromMilliseconds(300), out message));
            }
        }
Exemple #24
0
        public void SimplePublishSubscribe()
        {
            using (var speaker = new NetMQBeacon())
                using (var listener = new NetMQBeacon())
                {
                    speaker.Configure(9999);
                    Console.WriteLine(speaker.Hostname);

                    speaker.Publish("Hello", s_publishInterval);

                    listener.Configure(9999);
                    listener.Subscribe("H");

                    string peerName;
                    string message = listener.ReceiveString(out peerName);

                    Console.WriteLine(peerName);

                    Assert.AreEqual("Hello", message);
                }
        }
Exemple #25
0
        public void Stop()
        {
            if (!IsRunning)
            {
                return;
            }

            this._nodes.Clear();

            _poller.Stop();
            _beacon.Unsubscribe();
            _poller.Remove(_beacon);
            _poller.Remove(timer);

            _poller.Dispose();
            _poller = null;

            _beacon.Dispose();
            _beacon = null;

            timer = null;
        }
Exemple #26
0
        /// <summary>
        /// Start node. Use beacon discovery.
        /// We get a new _inbox (RouterSocket listening to peers) and a new _beacon on every Start().
        /// Also a new _port and _endpoint at each Start()
        /// </summary>
        /// <param name="interfaceName">A particular name, or null </param>
        /// <returns>true if OK, false if not possible or if already running</returns>
        private bool Start(string interfaceName)
        {
            if (_isRunning)
            {
                return(false);
            }

            // Create the _beacon and bind the _inbox
            _beacon = new NetMQBeacon();
            _beacon.ReceiveReady += OnBeaconReady;
            _beacon.Configure(_beaconPort, interfaceName);

            // Bind our router port to the host. Our hostName is provided by the beacon.
            var address = $"tcp://{_beacon.BoundTo}";

            _inbox = new RouterSocket();
            _inbox.ReceiveReady += OnInboxReady;
            _port = _inbox.BindRandomPort(address);
            if (_port <= 0)
            {
                // Die on bad interface or port exhaustion
                return(false);
            }
            _endpoint = $"{address}:{_port}";
            _loggerDelegate?.Invoke($"The _inbox RouterSocket for node {_uuid.ToShortString6()} is bound to _endpoint={_endpoint}");

            _loggerDelegate?.Invoke($"Starting {_name} {_uuid.ToShortString6()}. Publishing beacon on port {_port}. Adding _beacon and _inbox to poller.");

            // Start polling on _inbox and _beacon
            _poller.Add(_inbox);
            _poller.Add(_beacon);

            //  Set broadcast/listen beacon
            PublishBeacon(_port);
            _beacon.Subscribe("ZRE");

            _isRunning = true;
            return(true);
        }
Exemple #27
0
        public void Polling()
        {
            using (var context = NetMQContext.Create())
            using (var speaker = new NetMQBeacon(context))
            using (var listener = new NetMQBeacon(context))
            {
                speaker.Configure(9999);
                Console.WriteLine(speaker.Hostname);

                speaker.Publish("Hello", s_publishInterval);

                var manualResetEvent = new ManualResetEvent(false);

                listener.Configure(9999);
                listener.Subscribe("H");

                string peerName = "";
                string message = "";

                listener.ReceiveReady += (sender, args) =>
                {
                    message = listener.ReceiveString(out peerName);
                    manualResetEvent.Set();
                };

                using (var poller = new Poller(listener) { PollTimeout = 10})
                {
                    poller.PollTillCancelledNonBlocking();

                    manualResetEvent.WaitOne();

                    Console.WriteLine(peerName);

                    Assert.AreEqual("Hello", message);

                    poller.CancelAndJoin();
                }
            }
        }
Exemple #28
0
        public void Silence()
        {
            using (var context = NetMQContext.Create())
                using (var speaker = new NetMQBeacon(context))
                    using (var listener = new NetMQBeacon(context))
                    {
                        speaker.Configure(9999);

                        listener.Configure(9999);
                        listener.Subscribe("H");

                        // this should send one broadcast message and stop
                        speaker.Publish("Hello", s_publishInterval);
                        Thread.Sleep(10);
                        speaker.Silence();

                        string peerName;
                        Assert.AreEqual("Hello", listener.ReceiveString(out peerName));

                        string message;
                        Assert.IsFalse(listener.TryReceiveString(TimeSpan.FromMilliseconds(300), out peerName, out message));
                    }
        }
Exemple #29
0
        public void BothSpeakerAndListenerOverLoopback()
        {
            using (var beacon1 = new NetMQBeacon())
                using (var beacon2 = new NetMQBeacon())
                {
                    beacon1.Configure("loopback", 9998);
                    beacon1.Publish("H1", s_publishInterval);
                    beacon1.Subscribe("H");

                    beacon2.Configure("loopback", 9998);
                    beacon2.Publish("H2", s_publishInterval);
                    beacon2.Subscribe("H");

                    string peerName;
                    string message = beacon1.ReceiveString(out peerName);

                    Assert.AreEqual("H2", message);

                    message = beacon2.ReceiveString(out peerName);

                    Assert.AreEqual("H1", message);
                }
        }
Exemple #30
0
        public void ConfigureTwice()
        {
            using (var context = NetMQContext.Create())
            using (var speaker = new NetMQBeacon(context))
            using (var listener = new NetMQBeacon(context))
            {
                speaker.Configure(5555);
                speaker.Configure(9999);
                Console.WriteLine(speaker.Hostname);

                speaker.Publish("Hello");

                listener.Configure(9999);
                listener.Subscribe("H");

                string peerName;
                string message = listener.ReceiveString(out peerName);

                Console.WriteLine(peerName);

                Assert.AreEqual("Hello", message);
            }
        }
Exemple #31
0
        public void BothSpeakerAndListener()
        {
            using (NetMQContext context = NetMQContext.Create())
                using (NetMQBeacon beacon1 = new NetMQBeacon(context))
                    using (NetMQBeacon beacon2 = new NetMQBeacon(context))
                    {
                        beacon1.Configure(9999);
                        beacon1.Publish("H1");
                        beacon1.Subscribe("H");

                        beacon2.Configure(9999);
                        beacon2.Publish("H2");
                        beacon2.Subscribe("H");

                        string peerName;
                        string message = beacon1.ReceiveString(out peerName);

                        Assert.AreEqual("H2", message);

                        message = beacon2.ReceiveString(out peerName);

                        Assert.AreEqual("H1", message);
                    }
        }
Exemple #32
0
        public void BothSpeakerAndListener()
        {
            using (var context = NetMQContext.Create())
            using (var beacon1 = new NetMQBeacon(context))
            using (var beacon2 = new NetMQBeacon(context))
            {
                beacon1.Configure(9999);
                beacon1.Publish("H1", s_publishInterval);
                beacon1.Subscribe("H");

                beacon2.Configure(9999);
                beacon2.Publish("H2", s_publishInterval);
                beacon2.Subscribe("H");

                string peerName;
                string message = beacon1.ReceiveString(out peerName);

                Assert.AreEqual("H2", message);

                message = beacon2.ReceiveString(out peerName);

                Assert.AreEqual("H1", message);
            }
        }
Exemple #33
0
        public void Silence()
        {
            using (var context = NetMQContext.Create())
            using (var speaker = new NetMQBeacon(context))
            using (var listener = new NetMQBeacon(context))
            {
                speaker.Configure(9999);

                listener.Configure(9999);
                listener.Subscribe("H");

                // this should send one broadcast message and stop
                speaker.Publish("Hello", s_publishInterval);
                Thread.Sleep(10);
                speaker.Silence();

                string peerName;
                Assert.AreEqual("Hello", listener.ReceiveString(out peerName));

                string message;
                Assert.IsFalse(listener.TryReceiveString(TimeSpan.FromMilliseconds(300), out peerName, out message));
            }
        }
Exemple #34
0
        /// <summary>
        /// Start node. Use beacon discovery.
        /// We get a new _inbox (RouterSocket listening to peers) and a new _beacon on every Start().
        /// Also a new _port and _endpoint at each Start()
        /// </summary>
        /// <param name="interfaceName">A particular name, or null </param>
        /// <returns>true if OK, false if not possible or if already running</returns>
        private bool Start(string interfaceName)
        {
            if (_isRunning)
            {
                return false;
            }

            // Create the _beacon and bind the _inbox
            _beacon = new NetMQBeacon();
            _beacon.ReceiveReady += OnBeaconReady;
            _beacon.Configure(_beaconPort, interfaceName);

            // Bind our router port to the host. Our hostName is provided by the beacon.
            var address = $"tcp://{_beacon.BoundTo}";
            _inbox = new RouterSocket();
            _inbox.ReceiveReady += OnInboxReady;
            _port = _inbox.BindRandomPort(address);
            if (_port <= 0)
            {
                // Die on bad interface or port exhaustion
                return false;
            }
            _endpoint = $"{address}:{_port}";
            _loggerDelegate?.Invoke($"The _inbox RouterSocket for node {_uuid.ToShortString6()} is bound to _endpoint={_endpoint}");

            _loggerDelegate?.Invoke($"Starting {_name} {_uuid.ToShortString6()}. Publishing beacon on port {_port}. Adding _beacon and _inbox to poller.");

            // Start polling on _inbox and _beacon
            _poller.Add(_inbox);
            _poller.Add(_beacon);

            //  Set broadcast/listen beacon
            PublishBeacon(_port);
            _beacon.Subscribe("ZRE");

            _isRunning = true;
            return true;
        }
Exemple #35
0
        /// <summary>
        /// Stop node discovery and interconnection
        /// </summary>
        private void Stop()
        {
            if (!_isRunning)
            {
                return;
            }
            _loggerDelegate?.Invoke($"Stopping {_name} {_uuid.ToShortString6()}. Publishing beacon on port 0. Removing _beacon and _inbox from poller.");

            // Stop broadcast/listen beacon by broadcasting port 0
            PublishBeacon(0);
            Thread.Sleep(1); // Allow 1 millisecond for beacon to go out
            _poller.Remove(_beacon);
            _beacon.ReceiveReady -= OnBeaconReady;
            _beacon.Unsubscribe();
            _beacon.Dispose();
            _beacon = null;

            // Stop polling on inbox
            _poller.Remove(_inbox);
            _inbox.ReceiveReady -= OnInboxReady;
            _inbox.Dispose();


            // Tell the application we are stopping
            var msg = new NetMQMessage(3);
            msg.Append("STOP");
            msg.Append(_uuid.ToByteArray());
            msg.Append(_name);
            _outbox.SendMultipartMessage(msg);
            _isRunning = false;
        }
Exemple #36
0
        public void BothSpeakerAndListenerOverLoopback()
        {
            using (var beacon1 = new NetMQBeacon())
            using (var beacon2 = new NetMQBeacon())
            {
                beacon1.Configure("loopback", 9998);
                beacon1.Publish("H1", s_publishInterval);
                beacon1.Subscribe("H");

                beacon2.Configure("loopback", 9998);
                beacon2.Publish("H2", s_publishInterval);
                beacon2.Subscribe("H");

                string peerName;
                string message = beacon1.ReceiveString(out peerName);

                Assert.AreEqual("H2", message);

                message = beacon2.ReceiveString(out peerName);

                Assert.AreEqual("H1", message);
            }
        }
Exemple #37
0
        public void Polling()
        {
            using (var speaker = new NetMQBeacon())
            using (var listener = new NetMQBeacon())
            {
                speaker.Configure(9999);

                speaker.Publish("Hello", s_publishInterval);

                var manualResetEvent = new ManualResetEvent(false);

                listener.Configure(9999);
                listener.Subscribe("H");

                string message = "";

                listener.ReceiveReady += (sender, args) =>
                {
                    message = listener.Receive().String;
                    manualResetEvent.Set();
                };

                using (var poller = new NetMQPoller { listener })
                {
                    poller.RunAsync();

                    manualResetEvent.WaitOne();

                    Assert.AreEqual("Hello", message);
                }
            }
        }
Exemple #38
0
        /// <summary>
        /// Release any contained resources.
        /// </summary>
        /// <param name="disposing">true if managed resources are to be released</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposing)
                return;

            if (_outbox != null)
            {
                _outbox.Dispose();
                _outbox = null;
            }
            if (_poller != null)
            {
                _poller.Stop();
                _poller.Dispose();
                _poller = null;
            }
            if (_beacon != null)
            {
                _beacon.Dispose();
                _beacon = null;
            }
            if (_inbox != null)
            {
                _inbox.Dispose();
                _inbox = null;
            }
            foreach (var peer in _peers.Values)
            {
                peer.Destroy();
            }
            foreach (var group in _peerGroups.Values)
            {
                group.Dispose();
            }
            foreach (var group in _ownGroups.Values)
            {
                group.Dispose();
            }
        }
Exemple #39
0
        /// <summary>
        /// Stop node discovery and interconnection
        /// </summary>
        /// <returns></returns>
        public bool Stop()
        {
            if (!IsRunning)
            {
                throw new InvalidOperationException("ZreNode is not running");
            }
            // Stop broadcast/listen beacon
            PublishBeacon(0);
            Thread.Sleep(1); // Allow 1 msec for beacon to go out
            _beacon.ReceiveReady -= OnBeaconReady;
            _poller.Remove(_beacon);
            _beacon = null;

            // Stop polling on inbox
            _inbox.ReceiveReady -= OnInboxReady;
            _poller.Remove(_inbox);

            // Tell the application we are stopping
            var msg = new NetMQMessage(3);
            msg.Append("STOP");
            msg.Append(_uuid.ToString());
            msg.Append(_name);
            _outbox.TrySendMultipartMessage(TimeSpan.Zero, msg);
            IsRunning = false;
            return true;
        }
Exemple #40
0
        /// <summary>
        /// Start node. Use beacon discovery
        /// </summary>
        /// <returns>true if OK, false if not possible</returns>
        public bool Start()
        {
            if (IsRunning)
            {
                throw new InvalidOperationException("ZreNode is already running");
            }
            Debug.Assert(_beacon == null);
            _beacon = new NetMQBeacon();

            _beacon.Configure(ZreDiscoveryPort);

            // listen to incoming beacons
            _beacon.ReceiveReady += OnBeaconReady;


            IPAddress bindTo = null; // TODO change this once this property comes thru from NuGet = _beacon.BoundTo;
            var interfaceCollection = new InterfaceCollection();
            foreach (var @interface in interfaceCollection)
            {
                    bindTo = @interface.Address;
                    break;
            }

            // Bind our router port to the host
            var address = string.Format("tcp://{0}", bindTo);
            _port = _inbox.BindRandomPort(address);
            if (_port <= 0)
            {
                // Die on bad interface or port exhaustion
                return false;
            }
            _endpoint = _inbox.Options.LastEndpoint;

            //  Set broadcast/listen beacon
            PublishBeacon(_port);
            _beacon.Subscribe("ZRE");
            _poller.Add(_beacon);

            // Start polling on inbox
            _inbox.ReceiveReady += OnInboxReady;
            _poller.Add(_inbox);
            IsRunning = true;
            return true;
        }
Exemple #41
0
        public void Unsubscribe()
        {
            using (var context = NetMQContext.Create())
            using (var speaker = new NetMQBeacon(context))
            using (var listener = new NetMQBeacon(context))
            {
                speaker.Configure(9999);

                listener.Configure(9999);
                listener.Subscribe("H");

                // this should send one broadcast message and stop
                speaker.Publish("Hello");

                string peerName;
                string message = listener.ReceiveString(out peerName);

                listener.Unsubscribe();

                Assert.AreEqual("Hello", message);

                ISocketPollable socket = listener;
                socket.Socket.Options.ReceiveTimeout = TimeSpan.FromSeconds(2);

                Assert.Throws<AgainException>(() => { message = listener.ReceiveString(out peerName); });
            }
        }