::
service
¶The main class of the n3rv framework. Each node inside a n3rv infrastructure inherits the service class, and therefore benefits from its functionalities.
Subclassed by n3rv::httpservice
Public Functions
service
(const char *controller_host, int controller_port, logger *ll = nullptr)¶service class constructor.
controller_host
: the ip/hostname of the controller. controller_port
: the port number on which the controller is listening (ch1). ll
: (optional), logger object pointer to use for service logging. initialize
()¶Service class initializer. Empty for base service class, but inheriting classes can use it to initialize new connections and make attachements.
set_uid
(const char *namespace_, const char *service_class, const char *name)¶Sets UID for node. This method MUST be imperatively called before any bind() operation.
namespace_
: namespace choosen for the service. service_class
: service class (usually you can choose the service-inhereted class name) name
: Name of the node. set_uid
(const char *uid)¶Sets UID for node. This method MUST be imperatively called before any connect()/binding action.
uid
: Node UID, at the following format “namespace_.service_class.node_name” subscribe
(const char *binding_name, int port)¶Registers the service on the controller, for it to update its directory and advise others a new node is available. This method is called automatically by bind() on TCP sockets, except specific cases you don’t need to call it yourself.
binding_name
: Name of the binding to register. port
: port of the binding to register. unsubscribe
()¶Gracefuly Unregisters the service from the controller, in case the service must go down. Not very useful, because the ZMQ stack takes care of this.
run
()¶Starts the service. run() Basically manages all the established connections, plus eventual extra behaviour coded inside hkloop().
run_async
()¶Start the service asynchronously, by running it in its own dedicated thread.
hkloop
()¶hkloop is a “hook” function allowing to place some code inside a service’s main loop. Indeed, hkloop() is called inside the run() process, so if you need to run code inside the main loop, you can override hkloop() with your own code.
Important Note: NEVER PERFORM BLOCKING OPERATIONS inside hkloop(), since it will affect the rest of the service’s execution. If you really need to perform blocking, synchronous operations, please do them in a separate thread/process.
register_main
(const char *cbid, mlptr cb)¶Another, more elaborate mechanism to add extra code in the running loop. callbacks added with this function get executed for every loop iteration.
cbid
: callback identifier. cb
: callback function pointer. register_main
(const char *cbid, const char *cbstr)¶Alternate version of register_main, for preregistered callbacks with register_mlcb
cbid
: callback identifier cbstr
: callback preregistration key unregister_main
(const char *cbid)¶used to unregister a previously regestered main loop callback
cbid
: callback identifier. connect
(const char *lookup, int connection_type, qhandler *hdlref = nullptr)¶starts a new connection (zmq_socket) to a remote service available inside the directory.
name
: identifier of the remote service inside the directory. connection_type
: Type of connection to the remote host, directly related to ZMQ (ZMQ_REQ, ZMQ_SUB, etc..). hdlref
: optional, used for internal purpose when we have to reconnect using existing qhandler object. bind
(const char *bind_name, const char *ip, int bind_type, int port = 0)¶Binds A NEW ZMQ TCP Socket (main endpoint type supported by n3rv) Note: If port is set to 0, n3rv will try to find a randomly choosen, available port on the machine. (Not very firewall-friendly but can solve a few headaches).
bind_name
: Name of the binding. ip
: ip to listen on (0.0.0.0 to listen on all addr). bind_type
: ZMQ socket type to create (ZMQ_REP, ZMQ_PUB,..) port
: TCP port to listen on.zbind
(const char *bind_name, const char *endpoint, int bind_type)¶binds a new RAW ZMQ socket if the service needs a listening socket (ZMQ_REP, ZMQ_PUB, etc..)
bind_name
: name of the bound connection, to identify it. endpoint
: string, to tell on what parameters to bind the socket. type
: kind of ZMQ socket to bind: ZMQ_REP, ZMQ_PUB, etc.. zsockopt
(qhandler *hdl, int option_name, const void *option_value, size_t option_len = -1)¶Allows to deal with underlying ZeroMQ stack and set socket options for a given connection
hdl
: n3rv connection handler pointer option_name
: ZMQ option to set. option_value
: ZMQ value to give to option. option_len
: size of option value. attach
(qhandler *hdl, fctptr callback)¶Attaches a service connection to its message handler callback !
attach
(qhandler *hdl, std::string callback_name)¶Attaches a service connection to its message handler callback ! Warning: this method only works if service::cbmap was filled at map_callbacks() time.
connection_name
: Name of the connection to attach callback to. callback_name
: string name of the callback to atach to connection. load_topology
(std::string topology_file)¶Loads a topology file and automatically bind ports, connects to remote endpoints and attach callbacks
load_topology
(topology *topo)¶Uses a previously defined topology object to automatically bind ports, connects to remote endpoints and attach callbacks.
get_zsocket
(qhandler *hdl)¶Retrieves a RAW ZMQ socket from the internal connections list.
hdl
: n3rv connection handler. send
(qhandler *hdl, std::string &data, int flags)¶Conveniency function to send string data on a specified connection.
hdl
: n3rv connection handler to send data to. data
: string to send. flags
: ZMQ send flags. send
(qhandler *hdl, void *data, size_t size, int flags)¶Conveniency function to send raw data on a specified connection.
hdl
: n3rv connection handler to send data to. data
: memory pointer of data to send. size
: size of the memory data block. flags
: ZMQ send flags. send
(qhandler *hdl, message &msg, int flags)¶Conveniency function to send n3rv::mesage data on a specified connection.
hdl
: n3rv connection handler to send data to. msg
: n3rv::message to send. flags
: ZMQ send flags. send
(qhandler *hdl, zmq::message_t *zmsg, int flags)¶Conveniency function to send direct zmq::message_t data on a specified connection.
hdl
: n3rv connection handler to send data to. msg
: ZMQ message_t to send. flags
: ZMQ send flags. check_deferred
()¶Checks for deferred connections whose endpoint was not yet in directory, and tries to establish connection.
register_rcb
(const char *cbid, fctptr cb)¶Registers a Receive Callback for later use through topologies
cbid
: callback id. cb
: callback function pointer. Protected Functions