Esempio n. 1
0
        /// <summary>
        /// Handle beacon data
        /// </summary>
        private void ReceiveBeacon()
        {
            // Get IP address and beacon of peer
            var beaconMessage = _beacon.Receive();

            // Ignore anything that isn't a valid beacon
            int  port;
            Guid uuid;

            if (!IsValidBeacon(beaconMessage.Bytes, out uuid, out port))
            {
                return;
            }
            var      endpoint = $"tcp://{beaconMessage.PeerHost}:{port}";
            ZyrePeer peer;

            if (port > 0)
            {
                //_loggerDelegate?.Invoke($"TMP {nameof(ReceiveBeacon)} is doing RequirePeer() for endpoint={endpoint}");
                peer = RequirePeer(uuid, endpoint);
                //_loggerDelegate?.Invoke($"TMP {nameof(ReceiveBeacon)} DID RequirePeer() for endpoint={endpoint}");
                peer.Refresh();
            }
            else
            {
                // Zero port means peer is going away; remove it if we had any knowledge of it already
                if (_peers.TryGetValue(uuid, out peer))
                {
                    _loggerDelegate?.Invoke($"Removing peer {uuid.ToShortString6()} due to zero port received from {endpoint}");
                    RemovePeer(peer);
                }
            }
        }
Esempio n. 2
0
        private void OnBeaconReady(object sender, NetMQBeaconEventArgs e)
        {
            // we got another beacon
            // let's check if we already know about the beacon
            var message = m_beacon.Receive();
            int port;

            int.TryParse(message.String, out port);

            NodeKey node = new NodeKey(message.PeerHost, port);

            // check if node already exist
            if (!m_nodes.ContainsKey(node))
            {
                // we have a new node, let's add it and connect to subscriber
                m_nodes.Add(node, DateTime.Now);
                m_publisher.Connect(node.Address);
                m_shim.SendMoreFrame(AddedNodeCommand).SendFrame(node.Address);
            }
            else
            {
                //Console.WriteLine("Node {0} is not a new beacon.", node);
                m_nodes[node] = DateTime.Now;
            }
        }
Esempio n. 3
0
        private void OnBeaconReady(object sender, NetMQBeaconEventArgs e)
        {
            var readyTime = DateTimeOffset.Now;

            var message = _beacon.Receive();

            int.TryParse(message.String, out var port);

            var peer = new PeerInfo(message.PeerHost, port);

            if (!_info.ContainsKey(peer))
            {
                _info.Add(peer, readyTime);
                _publisher.Connect(peer.Address);
                _shim.SendMoreFrame("A").SendFrame(peer.Address);

                _logger?.LogInformation($"{_id}: Added new peer from '{{PeerAddress}}'", peer.Address);
                _hub?.Clients.All.SendAsync("ReceiveMessage", "success", $"Adding new peer from '{peer.Address}'");
            }
            else
            {
                _logger?.LogDebug($"{_id}: Updating keep-alive for peer '{{PeerAddress}}'", peer.Address);
                _info[peer] = readyTime;
            }
        }
Esempio n. 4
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);
                    }
                }
        }
Esempio n. 5
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);
            }
        }
Esempio n. 6
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);
                }
        }
Esempio n. 7
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);
                }
        }
Esempio n. 8
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);
            }
        }
Esempio n. 9
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);
                }
        }
Esempio n. 10
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);
            }
        }
Esempio n. 11
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));
                }
        }
Esempio n. 12
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));
            }
        }
Esempio n. 13
0
        private void OnBeaconReady(object sender, NetMQBeaconEventArgs e)
        {
            // we got another beacon
            // let's check if we already know about the beacon
            var message = _beacon.Receive();
            var port    = int.Parse(message.String);
            var node    = new NodeKey(message.PeerHost, port);

            // check if node already exist
            if (!_nodes.ContainsKey(node))
            {
                // we have a new node, let's add it and connect to subscriber
                _nodes.Add(node, DateTime.UtcNow);
                _publisher.Connect(node.Address);
                _shim.SendMoreFrame(TaskSchedulerBusCommands.AddedNode.ToString()).SendFrame(node.Address);
            }
            else
            {
                _nodes[node] = DateTime.UtcNow; //heartbeat
            }
        }
Esempio n. 14
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);
                }
            }
        }