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 OnBeaconReady(object sender, NetMQBeaconEventArgs e) { // we got another beacon // let's check if we already know about the beacon string nodeName; int port = Convert.ToInt32(m_beacon.ReceiveString(out nodeName)); // remove the port from the peer name nodeName = nodeName.Replace(":" + m_broadcastPort, ""); NodeKey node = new NodeKey(nodeName, 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; } }
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 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 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(); string peerName; Assert.AreEqual("Hello", listener.ReceiveString(out peerName)); string message; Assert.IsFalse(listener.TryReceiveString(TimeSpan.FromMilliseconds(300), out peerName, out message)); } }
public void Polling() { using (var speaker = new NetMQBeacon()) using (var listener = new NetMQBeacon()) { 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 NetMQPoller { listener }) { poller.RunAsync(); manualResetEvent.WaitOne(); Console.WriteLine(peerName); Assert.AreEqual("Hello", 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 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 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 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); }); } }
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(); } } }