ZIO
Python and C++ interface to ZeroMQ and Zyre
port.hpp
Go to the documentation of this file.
1 #ifndef ZIO_PORT_HPP_SEEN
2 #define ZIO_PORT_HPP_SEEN
3 
4 #include "zio/peer.hpp"
5 #include "zio/message.hpp"
6 #include "zio/interned.hpp"
7 
8 #include <memory>
9 #include <map>
10 
11 
12 namespace zio {
13 
27  class Port {
28  public:
29  typedef std::string address_t;
30  typedef std::string nodename_t;
31  typedef std::string portname_t;
32 
38  Port(const std::string& name, int stype,
39  const std::string& hostname = "127.0.0.1");
40  ~Port();
41 
43  void set_origin(origin_t origin) { m_origin = origin; }
44 
45  void set_verbose(bool verbose = true) { m_verbose = verbose; }
46 
48  const std::string& name() const { return m_name; }
49 
53  void bind();
54 
60  void bind(const std::string& hostname, int tcpportnum);
61 
65  void bind(const address_t& address);
66 
70  void connect(const address_t& address);
71 
77  void connect(const nodename_t& node, const portname_t& port);
78 
87  void subscribe(const std::string& prefix = "");
88 
93  void set_header(const std::string& leafname, const std::string& value);
94 
102 
108  void online(Peer& peer);
109 
113  void offline();
114 
118  void send(Message& msg);
119 
121  bool recv(Message& msg, int timeout=-1);
122 
126  zio::socket_t& socket() { return m_sock; }
127 
128  private:
129  const std::string m_name;
130  zio::context_t m_ctx;
131  zio::socket_t m_sock;
132  std::string m_hostname;
133  bool m_online;
134  std::map<std::string, std::string> m_headers;
135  origin_t m_origin;
136 
137  // functions which perform a bind() and return associated header
138  typedef std::function<address_t()> binder_t;
139  std::vector<binder_t> m_binders;
140 
141  std::vector<address_t> m_connect_addresses, m_connected, m_bound;
142  std::vector< std::pair<nodename_t, portname_t> > m_connect_nodeports;
143 
144  bool m_verbose{false};
145  };
146 
148  typedef std::shared_ptr<Port> portptr_t;
149 
150 }
151 #endif
zio::socket_t & socket()
Access the underlying cppzmq socket.
Definition: port.hpp:126
void set_verbose(bool verbose=true)
Definition: port.hpp:45
std::string portname_t
Definition: port.hpp:31
bool recv(Message &msg, int timeout=-1)
Recieve a message, return false if timeout occurred.
Definition: port.cpp:239
std::map< header_key_t, header_value_t > headerset_t
Definition: peer.hpp:22
void set_origin(origin_t origin)
Access the owning node&#39;s origin.
Definition: port.hpp:43
void online(Peer &peer)
Make any previously requested connections.
Definition: port.cpp:137
void offline()
Disconnect and unbind.
Definition: port.cpp:195
std::shared_ptr< Port > portptr_t
The context can&#39;t be copied and ports like to be shared.
Definition: port.hpp:148
std::string nodename_t
Definition: port.hpp:30
A port holds a socket in the context of a node.
Definition: port.hpp:27
const std::string & name() const
Access this port&#39;s name.
Definition: port.hpp:48
void subscribe(const std::string &prefix="")
Subscribe to a PUB topic.
Definition: port.cpp:100
void send(Message &msg)
Send a message.
Definition: port.cpp:220
void bind()
Request a default bind.
Definition: port.cpp:69
uint64_t origin_t
Definition: message.hpp:36
void connect(const address_t &address)
Request connect to fully qualified ZeroMQ address string.
Definition: port.cpp:90
std::string address_t
Definition: port.hpp:29
headerset_t do_binds()
Perform any requested binds.
Definition: port.cpp:113
a ZIO message
Definition: message.hpp:59
implementation of ZIO data flow protocol endpoints
Definition: actor.hpp:14
Port(const std::string &name, int stype, const std::string &hostname="127.0.0.1")
Create a port of given name and socket type.
Definition: port.cpp:56
Peer at the network to discover peers and advertise self.
Definition: peer.hpp:44
void set_header(const std::string &leafname, const std::string &value)
Set an extra port header.
Definition: port.cpp:107
~Port()
Definition: port.cpp:61