zmq_ctx_set_monitor.txt 6.4 KB
Newer Older
1 2
zmq_ctx_set_monitor(3)
======================
3 4 5 6 7


NAME
----

8
zmq_ctx_set_monitor - register a monitoring callback
9 10 11 12


SYNOPSIS
--------
13
*int zmq_ctx_set_monitor (void '*context', zmq_monitor_fn '*monitor');*
14 15 16 17


DESCRIPTION
-----------
18
The _zmq_ctx_set_monitor()_ function shall register a callback function specified by
19 20 21
the 'monitor' argument. This is an event sink for changes in per socket
connection and mailbox (work in progress) states.

22
.The _zmq_ctx_set_monitor()_ callback function is expected to have this prototype:
23 24 25 26 27 28 29 30
----
typedef void (zmq_monitor_fn) (void *s, int event, zmq_event_data_t *data);
----

The callback is global (per context), with the socket that triggered the event
passed to the handler as well. Each event also populates a 'zmq_event_data_t'
union with additional metadata which can be used for correlation.

31
CAUTION: _zmq_ctx_set_monitor()_ is intended for monitoring infrastructure / operations
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
concerns only - NOT BUSINESS LOGIC. An event is a representation of something
that happened - you cannot change the past, but only react to them. The
implementation is also only concerned with a single session. No state of peers,
other sessions etc. are tracked - this will only pollute internals and is the
responsibility of application authors to either implement or correlate in
another datastore. Monitor events are exceptional conditions and are thus not
directly in the messaging critical path. However, still be careful with what
you're doing in the callback function as excess time spent in the handler will
block the socket's application thread.

Only tcp and ipc specific transport events are supported in this initial
implementation.

Supported events are:


ZMQ_EVENT_CONNECTED: connection established
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The 'ZMQ_EVENT_CONNECTED' event triggers when a connection has been established
to a remote peer. This can happen either synchronous or asynchronous.

.Callback metadata:
----
data.connected.addr // peer address
data.connected.fd // socket descriptor
----

ZMQ_EVENT_CONNECT_DELAYED: synchronous connect failed, it's being polled
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The 'ZMQ_EVENT_CONNECT_DELAYED' event triggers when an immediate connection
attempt is delayed and it's completion's being polled for.

.Callback metadata:
----
data.connect_delayed.addr // peer address
data.connect_delayed.err // errno
----

ZMQ_EVENT_CONNECT_RETRIED: asynchronous connect / reconnection attempt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The 'ZMQ_EVENT_CONNECT_RETRIED' event triggers when a connection attempt
is being handled by reconnect timer. The reconnect interval's recomputed
for each attempt.

.Callback metadata:
----
data.connect_retried.addr // peer address
data.connect_retried.interval // computed reconnect interval
----

ZMQ_EVENT_LISTENING: socket bound to an address, ready to accept connections
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The 'ZMQ_EVENT_LISTENING' event triggers when a socket's successfully bound
to a an interface.

.Callback metadata:
----
data.listening.addr //  listen address
data.listening.fd // socket descriptor
----

ZMQ_EVENT_BIND_FAILED: socket could not bind to an address
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The 'ZMQ_EVENT_BIND_FAILED' event triggers when a socket could not bind to
a given interface.

.Callback metadata:
----
data.bind_failed.addr // listen address
data.bind_failed.err // errno
----

ZMQ_EVENT_ACCEPTED: connection accepted to bound interface
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The 'ZMQ_EVENT_ACCEPTED' event triggers when a connection from a remote peer
has been established with a socket's listen address.

.Callback metadata:
----
data.accepted.addr // listen address
data.accepted.fd // socket descriptor
----

ZMQ_EVENT_ACCEPT_FAILED: could not accept client connection
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The 'ZMQ_EVENT_ACCEPT_FAILED' event triggers when a connection attempt to
a socket's bound address fails.

.Callback metadata:
----
data.accept_failed.addr // listen address
data.accept_failed.err // errno
----

ZMQ_EVENT_CLOSED: connection closed
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The 'ZMQ_EVENT_CLOSED' event triggers when a connection's underlying descriptor
has been closed. 

.Callback metadata:
----
data.closed.addr // address
data.closed.fd // socket descriptor
----

ZMQ_EVENT_CLOSE_FAILED: connection couldn't be closed
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The 'ZMQ_EVENT_CLOSE_FAILED' event triggers when a descriptor could not be
released back to the OS.

.Callback metadata:
----
data.close_failed.addr // address
data.close_failed.err // errno
----

ZMQ_EVENT_DISCONNECTED: broken session
149
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
150 151 152 153 154 155 156 157 158 159 160
The 'ZMQ_EVENT_DISCONNECTED' event triggers when the stream engine (tcp and ipc
specific) detects a corrupted / broken session.

.Callback metadata:
----
data.disconnected.addr // address
data.disconnected.fd // socket descriptor
----

RETURN VALUE
------------
161
The _zmq_ctx_set_monitor()_ function returns a value of 0 or greater if successful.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
Otherwise it returns `-1` and sets 'errno' to one of the values defined
below.


ERRORS
------
*EINVAL*::
The requested callback function _monitor_ is invalid.


EXAMPLE
-------
.Observing a 'PUB' socket's connection state 
----
void socket_monitor (void *s, int event_, zmq_event_data_t *data_)
{
    switch (event_) {
    case ZMQ_EVENT_LISTENING:
        printf ("Socket bound to %s, socket descriptor is %d\n",
                 data.listening.addr, data.listening.fd);
        break;
    case ZMQ_EVENT_ACCEPTED:
        printf ("Accepted connection to %s, socket descriptor is %d\n",
                data.accepted.addr, data.accepted.fd);
        break;
    }
}

void *context = zmq_ctx_new ();
191
int rc = zmq_ctx_set_monitor (context, socket_monitor);
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
assert (rc == 0);
void *pub = zmq_socket (context, ZMQ_PUB);
assert (pub);
void *sub = zmq_socket (context, ZMQ_SUB);
assert (pub);
rc = zmq_bind (pub, "tcp://127.0.0.1:5560");
assert (rc == 0);
rc = zmq_connect (sub, "tcp://127.0.0.1:5560");
assert (rc == 0);

// Allow a window for socket events as connect can be async
zmq_sleep (1);

rc = zmq_close (pub);
assert (rc == 0);
rc = zmq_close (sub);
assert (rc == 0);

zmq_term (context);
----


SEE ALSO
--------
linkzmq:zmq[7]


AUTHORS
-------
This 0MQ manual page was written by Lourens Naudé <lourens@methodmissing.com>