ZIO
Python and C++ interface to ZeroMQ and Zyre
test_peer_link.cpp
Go to the documentation of this file.
1 // basic peer discovery and presence with linking up
2 
3 #include "zio/peer.hpp"
4 #include "zio/interned.hpp"
5 
6 #include <iostream>
7 using namespace std;
8 
9 int main(int argc, char* argv[])
10 {
11  bool verbose=false;
12  if (argc>1)
13  verbose=true;
14 
16 
17  // in a publisher application
18  zio::socket_t pub(ctx, ZMQ_PUB);
19  const std::string addr = "inproc://testpeerlink";
20  pub.bind(addr);
21  zio::Peer pubpeer("pub",{{"Feed",addr}}, verbose);
22 
23  // in a subscriber application
24  zio::Peer subpeer("sub", {}, verbose);
25  auto uuids = subpeer.waitfor("pub");
26  assert (uuids.size() == 1);
27  if (verbose)
28  cerr << "pub uuid is " << uuids[0] << endl;
29  auto pubinfo = subpeer.peer_info(uuids[0]);
30  if (verbose)
31  cerr << "pub nick is " << pubinfo.nick << endl;
32  assert (pubinfo.nick == "pub");
33  assert (pubinfo.headers.size() == 1);
34  auto feeds = pubinfo.branch("Feed");
35  if (verbose)
36  cerr << "got " << feeds.size() << ": " << feeds[""] << endl;
37  assert(feeds.size() == 1);
38  zio::socket_t sub(ctx, ZMQ_SUB);
39  std::string prefix = "";
40  sub.setsockopt(ZMQ_SUBSCRIBE, prefix.c_str(), prefix.size());
41 
42  sub.connect(feeds[""]);
43 
44  // zmq wart: SSS, give time for pub to process any subscriptions.
45  zclock_sleep(100);
46 
47 
48  std::string hw = "Hello World!";
49  // back in publisher
50  pub.send(zio::buffer(hw.data(), hw.size()));
51  if (verbose)
52  cerr << "send "<<hw<<"\n";
53 
54 
55  // back in subscriber
56  zio::message_t msg;
57  auto res = sub.recv(msg);
58  assert(res);
59  std::string hw2(static_cast<char*>(msg.data()), msg.size());
60  assert(hw2 == hw);
61  if (verbose)
62  cerr << "recv "<<hw2<<"\n";
63 
64 
65 
66  return 0;
67 }
const char * addr
inproc hangs. no messages ever get received by server. tcp/ipc okay.
Definition: test_tcs.cpp:16
size_t send(const void *buf_, size_t len_, int flags_=0)
Definition: zmq.hpp:1345
void * data() ZMQ_NOTHROW
Definition: zmq.hpp:481
size_t size() const ZMQ_NOTHROW
Definition: zmq.hpp:488
void bind(std::string const &addr)
Definition: zmq.hpp:1306
Peer at the network to discover peers and advertise self.
Definition: peer.hpp:44
std::vector< uuid_t > waitfor(const nickname_t &nickname, timeout_t timeout=-1)
Wait for a peer of a given nickname to be discovered.
Definition: peer.cpp:148