n3rv::service

class n3rv::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

n3rv::serviceservice(const char *controller_host, int controller_port, logger *ll = nullptr)

service class constructor.

Parameters
  • 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.

n3rv::service~service()

service class destructor, closes all open zmq sockets and zmq context.

int n3rv::serviceinitialize()

Service class initializer. Empty for base service class, but inheriting classes can use it to initialize new connections and make attachements.

void n3rv::serviceset_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.

Parameters
  • namespace_: namespace choosen for the service.
  • service_class: service class (usually you can choose the service-inhereted class name)
  • name: Name of the node.

void n3rv::serviceset_uid(const char *uid)

Sets UID for node. This method MUST be imperatively called before any connect()/binding action.

Parameters
  • uid: Node UID, at the following format “namespace_.service_class.node_name”

int n3rv::servicesubscribe(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.

Parameters
  • binding_name: Name of the binding to register.
  • port: port of the binding to register.

int n3rv::serviceunsubscribe()

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.

int n3rv::servicerun()

Starts the service. run() Basically manages all the established connections, plus eventual extra behaviour coded inside hkloop().

void n3rv::servicestop()

Stops the main running loop.

std::thread *n3rv::servicerun_async()

Start the service asynchronously, by running it in its own dedicated thread.

int n3rv::serviceterminate()

Closes all sockets and destroys ZMQ context

void n3rv::servicehkloop()

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.

void n3rv::serviceregister_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.

Parameters
  • cbid: callback identifier.
  • cb: callback function pointer.

void n3rv::serviceregister_main(const char *cbid, const char *cbstr)

Alternate version of register_main, for preregistered callbacks with register_mlcb

Parameters
  • cbid: callback identifier
  • cbstr: callback preregistration key

int n3rv::serviceunregister_main(const char *cbid)

used to unregister a previously regestered main loop callback

Return
0 if unregister was succesful, >0 otherwise.
Parameters
  • cbid: callback identifier.

qhandler *n3rv::serviceconnect(const char *lookup, int connection_type, qhandler *hdlref = nullptr)

starts a new connection (zmq_socket) to a remote service available inside the directory.

Return
n3rv connection handler.
Parameters
  • 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.

qhandler *n3rv::servicebind(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).

Parameters
  • 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.

Return
n3rv connection handler.

qhandler *n3rv::servicezbind(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..)

Return
n3rv connection handler.
Parameters
  • 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..

void n3rv::servicezsockopt(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

Parameters
  • 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.

int n3rv::serviceattach(qhandler *hdl, fctptr callback)

Attaches a service connection to its message handler callback !

Return
0 if attach went correctly, >0 otherwise.
Parameters
  • hdl: n3rv connection handler, created with connect(), bind() or zbind().
  • connection_name: Name of the connection to attach callback to. callback function.

int n3rv::serviceattach(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.

Return
0 if attach went correctly, >0 otherwise.
Parameters
  • connection_name: Name of the connection to attach callback to.
  • callback_name: string name of the callback to atach to connection.

std::map<std::string, qhandler *> n3rv::serviceload_topology(std::string topology_file)

Loads a topology file and automatically bind ports, connects to remote endpoints and attach callbacks

Return
a dictionary of uid/qhandler* key/values

std::map<std::string, qhandler *> n3rv::serviceload_topology(topology *topo)

Uses a previously defined topology object to automatically bind ports, connects to remote endpoints and attach callbacks.

Return
a dictionary of uid/qhandler* key/values

zmq::socket_t *n3rv::serviceget_zsocket(qhandler *hdl)

Retrieves a RAW ZMQ socket from the internal connections list.

Return
the related connection object.
Parameters
  • hdl: n3rv connection handler.

int n3rv::servicesend(qhandler *hdl, std::string &data, int flags)

Conveniency function to send string data on a specified connection.

Parameters
  • hdl: n3rv connection handler to send data to.
  • data: string to send.
  • flags: ZMQ send flags.

int n3rv::servicesend(qhandler *hdl, void *data, size_t size, int flags)

Conveniency function to send raw data on a specified connection.

Parameters
  • 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.

int n3rv::servicesend(qhandler *hdl, message &msg, int flags)

Conveniency function to send n3rv::mesage data on a specified connection.

Parameters
  • hdl: n3rv connection handler to send data to.
  • msg: n3rv::message to send.
  • flags: ZMQ send flags.

int n3rv::servicesend(qhandler *hdl, zmq::message_t *zmsg, int flags)

Conveniency function to send direct zmq::message_t data on a specified connection.

Parameters
  • hdl: n3rv connection handler to send data to.
  • msg: ZMQ message_t to send.
  • flags: ZMQ send flags.

int n3rv::servicecheck_deferred()

Checks for deferred connections whose endpoint was not yet in directory, and tries to establish connection.

void n3rv::serviceregister_rcb(const char *cbid, fctptr cb)

Registers a Receive Callback for later use through topologies

Parameters
  • cbid: callback id.
  • cb: callback function pointer.

void n3rv::serviceregister_mlcb(const char *cbid, mlptr cb)

Registers a Main Loop Callback for later use through topologies

Parameters
  • cbid: callback id.
  • cb: callback function pointer.

void n3rv::serviceset_poll_timeout(int poll_timeout)

Sets the main ZMQ polling timeout, if required

Parameters
  • poll_timeout: time (in ms) the poller must wait before returning.

Protected Functions

int n3rv::serviceconnect_controller()

Controller connection procedure

std::string n3rv::serviceadd_scope(const char *name)

prepends an uid with missing scope parts

Protected Attributes

std::map<std::string, mlptr> n3rv::serviceml_chmap

Main loop callbacks dictionary

std::map<std::string, fctptr> n3rv::servicercb_map

Available receive callbacks list (mainly for topologies)

std::map<std::string, mlptr> n3rv::servicemlcb_map

Available main loop callbacks list (mainly for topologies)

Protected Static Functions

void *n3rv::servicedirectory_update(void *objref, zmq::message_t *dirmsg)

Directory updates message handling callback