ZIO
Python and C++ interface to ZeroMQ and Zyre
test_peer.cpp
Go to the documentation of this file.
1 // basic peer discovery and presence
2 
3 #include "zio/peer.hpp"
4 
5 #include <iostream>
6 using namespace std;
7 
8 void dump_peer(zio::Peer& peer) {
9  cerr << "peer '" << peer.nickname() << "' sees:" << endl;
10 
11  for (auto pi : peer.peers()) {
12  cerr << "\t" << pi.first << " is " << pi.second.nick << endl;
13  for (auto h : pi.second.headers) {
14  cerr << "\t\t" << h.first << " = " << h.second << endl;
15  }
16  }
17 }
18 
19 
20 int main(int argc, char*argv[])
21 {
22  bool verbose = true;
23 
24  zio::Peer peer1("peer1",{{"Color","blue"},{"Pet","cat"}}, verbose);
25 
26  std::string uuid2="";
27  {
28  zio::Peer peer2("peer2",{{"Color","yellow"},{"Pet","dog"}}, verbose);
29  auto pids1 = peer2.waitfor("peer1");
30  assert(pids1.size());
31  auto pids2 = peer1.waitfor("peer2");
32  assert(pids2.size());
33  uuid2 = pids2[0];
34  assert(uuid2.size());
35  bool found = peer1.isknown(uuid2);
36  assert(found);
37  dump_peer(peer1);
38  dump_peer(peer2);
39  }
40 
41  int countdown = 3;
42  bool found;
43  while (countdown) {
44  cerr << "check for death of " << uuid2 << " " << countdown << endl;
45  --countdown;
46  peer1.poll(1000);
47  found = peer1.isknown(uuid2);
48  if (!found) { break; }
49  }
50  assert(!found);
51 
52  return 0;
53 }
const peerset_t & peers()
Return known peers as map from UUID to nickname.
Definition: peer.cpp:189
const nickname_t nickname()
Get our nickname.
Definition: peer.hpp:60
void dump_peer(zio::Peer &peer)
Definition: test_peer.cpp:8
int main(int argc, char *argv[])
Definition: test_peer.cpp:20
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