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); } } }
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(); } } }
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); } } } }
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); }); } } } }
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(); }
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; } }
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)); } }
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); } }
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); } }
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); } }
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); } }
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)); } }
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); } } } }
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(); } } }
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)); } }
public void Start() { Initialize(); if (IsRunning) { return; } _beacon.Subscribe(""); _beacon.Publish(_selfNode.Serialize(), TimeSpan.FromSeconds(1)); timer = new NetMQTimer(TimeSpan.FromSeconds(1)); timer.Elapsed += ClearDeadNodes; _poller = new NetMQPoller { _beacon, timer }; _poller.RunAsync(); }
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); } }
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)); } }
/// <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); }
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); } }
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)); } }
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); } }
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); } }
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); } }
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(); } } }
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)); } }
/// <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; }
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); }); } }
/// <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; }