ZIO
Python and C++ interface to ZeroMQ and Zyre
node.cpp
Go to the documentation of this file.
1 #include "zio/node.hpp"
2 #include "zio/logging.hpp"
3 
4 static
5 std::string get_hostname()
6 {
7  zactor_t *beacon = zactor_new (zbeacon, NULL);
8  assert (beacon);
9  zsock_send (beacon, "si", "CONFIGURE", 31415);
10  char *tmp = zstr_recv (beacon);
11  std::string ret = tmp;
12  zstr_free (&tmp);
13  zactor_destroy (&beacon);
14  return ret;
15 }
16 
17 
18 
20  const std::string& hostname)
21  : m_nick(nick)
22  , m_origin(origin)
23  , m_hostname(hostname)
24  , m_peer(nullptr)
25 {
26  if (m_hostname.empty()) {
27  m_hostname = get_hostname();
28  }
29 }
30 
32 {
33  offline();
34  m_ports.clear();
35 }
36 
37 zio::portptr_t zio::Node::port(const std::string& name, int stype)
38 {
39  zio::portptr_t ret = port(name);
40  if (ret) { return ret; }
41  ret = std::make_shared<Port>(name, stype, m_hostname);
42  ret->set_origin(m_origin);
43  ret->set_verbose(m_verbose);
44  m_ports[name] = ret;
45  m_portnames.push_back(name);
46  return ret;
47 }
48 
49 zio::portptr_t zio::Node::port(const std::string& name)
50 {
51  auto it = m_ports.find(name);
52  if (it == m_ports.end()) {
53  return nullptr;
54  }
55  return it->second;
56 }
57 
58 
59 void zio::Node::online(const headerset_t& extra_headers)
60 {
61  if (m_peer) { return; }
62 
63  headerset_t headers = extra_headers;
64  for (auto& np : m_ports) {
65  headerset_t hs = np.second->do_binds();
66  headers.insert(hs.begin(), hs.end());
67  }
68  zio::debug("[node {}] going online with:", m_nick.c_str());
69  for (const auto& hh : headers) {
70  zio::debug("\t{} = {}", hh.first.c_str(), hh.second.c_str());
71  }
72  m_peer = new Peer(m_nick, headers, m_verbose);
73  for (auto& np : m_ports) {
74  np.second->online(*m_peer);
75  }
76 }
77 
79 {
80  if (!m_peer) { return ; }
81  for (auto& np : m_ports) {
82  np.second->offline();
83  }
84  delete m_peer;
85  m_peer = nullptr;
86 }
87 
88 
90 {
91  m_origin = origin;
92  for (auto& np : m_ports) {
93  np.second->set_origin(origin);
94  }
95 }
97 {
98  m_verbose = verbose;
99  if (m_peer)
100  m_peer->set_verbose(verbose);
101 }
bool verbose() const
Definition: node.hpp:64
void online(const headerset_t &extra_headers={})
Bring the node online.
Definition: node.cpp:59
void set_verbose(bool verbose=true)
Set verbose for underlying Zyre and internal debug messages.
Definition: node.cpp:96
portptr_t port(const std::string &name, int stype)
Create a named port with the given socket type.
Definition: node.cpp:37
Node(nickname_t nick="", origin_t origin=0, const std::string &hostname="")
Create a node.
Definition: node.cpp:19
name
Definition: setup.py:4
std::map< header_key_t, header_value_t > headerset_t
Definition: peer.hpp:22
origin_t origin() const
Return a previously set node origin.
Definition: node.hpp:50
void set_origin(origin_t origin)
Set the node origin.
Definition: node.cpp:89
std::shared_ptr< Port > portptr_t
The context can&#39;t be copied and ports like to be shared.
Definition: port.hpp:148
void set_verbose(bool verbose=true)
Turn on verbose debugging of the underlying Zyre actor.
Definition: peer.cpp:206
uint64_t origin_t
Definition: message.hpp:36
std::string nickname_t
A peer asserts a nickname.
Definition: peer.hpp:12
void offline()
Bring the node offline.
Definition: node.cpp:78
Peer at the network to discover peers and advertise self.
Definition: peer.hpp:44
~Node()
Definition: node.cpp:31