~ubuntu-branches/ubuntu/saucy/zeromq3/saucy

« back to all changes in this revision

Viewing changes to doc/zmq_socket_monitor.3

  • Committer: Package Import Robot
  • Author(s): Alessandro Ghedini
  • Date: 2012-10-16 19:49:30 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20121016194930-98r0bi746eoaa4iv
Tags: 3.2.1~rc2+dfsg-1
* New upstream RC release (Closes: #690704)
* Bump Standards-Version to 3.9.4 (no changes needed)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'\" t
 
2
.\"     Title: zmq_ctx_socket_monitor
 
3
.\"    Author: [see the "AUTHORS" section]
 
4
.\" Generator: DocBook XSL Stylesheets v1.76.1 <http://docbook.sf.net/>
 
5
.\"      Date: 10/15/2012
 
6
.\"    Manual: 0MQ Manual
 
7
.\"    Source: 0MQ 3.2.1
 
8
.\"  Language: English
 
9
.\"
 
10
.TH "ZMQ_CTX_SOCKET_MONIT" "3" "10/15/2012" "0MQ 3\&.2\&.1" "0MQ Manual"
 
11
.\" -----------------------------------------------------------------
 
12
.\" * Define some portability stuff
 
13
.\" -----------------------------------------------------------------
 
14
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
15
.\" http://bugs.debian.org/507673
 
16
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
 
17
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
18
.ie \n(.g .ds Aq \(aq
 
19
.el       .ds Aq '
 
20
.\" -----------------------------------------------------------------
 
21
.\" * set default formatting
 
22
.\" -----------------------------------------------------------------
 
23
.\" disable hyphenation
 
24
.nh
 
25
.\" disable justification (adjust text to left margin only)
 
26
.ad l
 
27
.\" -----------------------------------------------------------------
 
28
.\" * MAIN CONTENT STARTS HERE *
 
29
.\" -----------------------------------------------------------------
 
30
.SH "NAME"
 
31
zmq_socket_monitor \- register a monitoring callback
 
32
.SH "SYNOPSIS"
 
33
.sp
 
34
\fBint zmq_socket_monitor (void \fR\fB\fI*socket\fR\fR\fB, char * \fR\fB\fI*addr\fR\fR\fB, int \fR\fB\fIevents\fR\fR\fB);\fR
 
35
.SH "DESCRIPTION"
 
36
.sp
 
37
The \fIzmq_socket_monitor()\fR function shall spawn a \fIPAIR\fR socket that publishes socket state changes (events) over the inproc:// transport to a given endpoint\&. Messages are \fIzmq_event_t\fR structs\&. It\(cqs recommended to connect via a \fIPAIR\fR socket in another application thread and handle monitoring events there\&. It\(cqs possible to also supply a bitmask (\fIZMQ_EVENT_ALL\fR or any combination of the \fIZMQ_EVENT_*\fR constants) of the events you\(cqre interested in\&.
 
38
.sp
 
39
.if n \{\
 
40
.RS 4
 
41
.\}
 
42
.nf
 
43
// monitoring thread
 
44
static void *req_socket_monitor (void *ctx)
 
45
{
 
46
    zmq_event_t event;
 
47
    int rc;
 
48
 
 
49
    void *s = zmq_socket (ctx, ZMQ_PAIR);
 
50
    assert (s);
 
51
 
 
52
    rc = zmq_connect (s, "inproc://monitor\&.req");
 
53
    assert (rc == 0);
 
54
    while (true) {
 
55
        zmq_msg_t msg;
 
56
        zmq_msg_init (&msg);
 
57
        rc = zmq_recvmsg (s, &msg, 0);
 
58
        if (rc == \-1 && zmq_errno() == ETERM) break;
 
59
        assert (rc != \-1);
 
60
        memcpy (&event, zmq_msg_data (&msg), sizeof (event));
 
61
        switch (event\&.event) {
 
62
        case ZMQ_EVENT_CONNECTED:
 
63
            // handle socket connected event
 
64
            break;
 
65
        case ZMQ_EVENT_CLOSED:
 
66
            // handle socket closed event
 
67
            break;
 
68
        }
 
69
    }
 
70
    zmq_close (s);
 
71
    return NULL;
 
72
}
 
73
 
 
74
// register a monitor endpoint for all socket events
 
75
rc = zmq_socket_monitor (req, "inproc://monitor\&.req", ZMQ_EVENT_ALL);
 
76
assert (rc == 0);
 
77
 
 
78
// spawn a monitoring thread
 
79
rc = pthread_create (&threads [0], NULL, req_socket_monitor, ctx);
 
80
assert (rc == 0);
 
81
.fi
 
82
.if n \{\
 
83
.RE
 
84
.\}
 
85
.sp
 
86
Only connection oriented (tcp and ipc) transports are supported in this initial implementation\&.
 
87
.sp
 
88
Supported events are:
 
89
.SS "ZMQ_EVENT_CONNECTED: connection established"
 
90
.sp
 
91
The \fIZMQ_EVENT_CONNECTED\fR event triggers when a connection has been established to a remote peer\&. This can happen either synchronous or asynchronous\&.
 
92
.PP
 
93
\fBEvent metadata:\fR. 
 
94
.sp
 
95
.if n \{\
 
96
.RS 4
 
97
.\}
 
98
.nf
 
99
data\&.connected\&.addr // peer address
 
100
data\&.connected\&.fd // socket descriptor
 
101
.fi
 
102
.if n \{\
 
103
.RE
 
104
.\}
 
105
.sp
 
106
.SS "ZMQ_EVENT_CONNECT_DELAYED: synchronous connect failed, it\(cqs being polled"
 
107
.sp
 
108
The \fIZMQ_EVENT_CONNECT_DELAYED\fR event triggers when an immediate connection attempt is delayed and it\(cqs completion\(cqs being polled for\&.
 
109
.PP
 
110
\fBEvent metadata:\fR. 
 
111
.sp
 
112
.if n \{\
 
113
.RS 4
 
114
.\}
 
115
.nf
 
116
data\&.connect_delayed\&.addr // peer address
 
117
data\&.connect_delayed\&.err // errno value
 
118
.fi
 
119
.if n \{\
 
120
.RE
 
121
.\}
 
122
.sp
 
123
.SS "ZMQ_EVENT_CONNECT_RETRIED: asynchronous connect / reconnection attempt"
 
124
.sp
 
125
The \fIZMQ_EVENT_CONNECT_RETRIED\fR event triggers when a connection attempt is being handled by reconnect timer\&. The reconnect interval\(cqs recomputed for each attempt\&.
 
126
.PP
 
127
\fBEvent metadata:\fR. 
 
128
.sp
 
129
.if n \{\
 
130
.RS 4
 
131
.\}
 
132
.nf
 
133
data\&.connect_retried\&.addr // peer address
 
134
data\&.connect_retried\&.interval // computed reconnect interval
 
135
.fi
 
136
.if n \{\
 
137
.RE
 
138
.\}
 
139
.sp
 
140
.SS "ZMQ_EVENT_LISTENING: socket bound to an address, ready to accept connections"
 
141
.sp
 
142
The \fIZMQ_EVENT_LISTENING\fR event triggers when a socket\(cqs successfully bound to a an interface\&.
 
143
.PP
 
144
\fBEvent metadata:\fR. 
 
145
.sp
 
146
.if n \{\
 
147
.RS 4
 
148
.\}
 
149
.nf
 
150
data\&.listening\&.addr //  listen address
 
151
data\&.listening\&.fd // socket descriptor
 
152
.fi
 
153
.if n \{\
 
154
.RE
 
155
.\}
 
156
.sp
 
157
.SS "ZMQ_EVENT_BIND_FAILED: socket could not bind to an address"
 
158
.sp
 
159
The \fIZMQ_EVENT_BIND_FAILED\fR event triggers when a socket could not bind to a given interface\&.
 
160
.PP
 
161
\fBEvent metadata:\fR. 
 
162
.sp
 
163
.if n \{\
 
164
.RS 4
 
165
.\}
 
166
.nf
 
167
data\&.bind_failed\&.addr // listen address
 
168
data\&.bind_failed\&.err // errno value
 
169
.fi
 
170
.if n \{\
 
171
.RE
 
172
.\}
 
173
.sp
 
174
.SS "ZMQ_EVENT_ACCEPTED: connection accepted to bound interface"
 
175
.sp
 
176
The \fIZMQ_EVENT_ACCEPTED\fR event triggers when a connection from a remote peer has been established with a socket\(cqs listen address\&.
 
177
.PP
 
178
\fBEvent metadata:\fR. 
 
179
.sp
 
180
.if n \{\
 
181
.RS 4
 
182
.\}
 
183
.nf
 
184
data\&.accepted\&.addr // listen address
 
185
data\&.accepted\&.fd // socket descriptor
 
186
.fi
 
187
.if n \{\
 
188
.RE
 
189
.\}
 
190
.sp
 
191
.SS "ZMQ_EVENT_ACCEPT_FAILED: could not accept client connection"
 
192
.sp
 
193
The \fIZMQ_EVENT_ACCEPT_FAILED\fR event triggers when a connection attempt to a socket\(cqs bound address fails\&.
 
194
.PP
 
195
\fBEvent metadata:\fR. 
 
196
.sp
 
197
.if n \{\
 
198
.RS 4
 
199
.\}
 
200
.nf
 
201
data\&.accept_failed\&.addr // listen address
 
202
data\&.accept_failed\&.err // errno value
 
203
.fi
 
204
.if n \{\
 
205
.RE
 
206
.\}
 
207
.sp
 
208
.SS "ZMQ_EVENT_CLOSED: connection closed"
 
209
.sp
 
210
The \fIZMQ_EVENT_CLOSED\fR event triggers when a connection\(cqs underlying descriptor has been closed\&.
 
211
.PP
 
212
\fBEvent metadata:\fR. 
 
213
.sp
 
214
.if n \{\
 
215
.RS 4
 
216
.\}
 
217
.nf
 
218
data\&.closed\&.addr // address
 
219
data\&.closed\&.fd // socket descriptor
 
220
.fi
 
221
.if n \{\
 
222
.RE
 
223
.\}
 
224
.sp
 
225
.SS "ZMQ_EVENT_CLOSE_FAILED: connection couldn\(cqt be closed"
 
226
.sp
 
227
The \fIZMQ_EVENT_CLOSE_FAILED\fR event triggers when a descriptor could not be released back to the OS\&.
 
228
.PP
 
229
\fBEvent metadata:\fR. 
 
230
.sp
 
231
.if n \{\
 
232
.RS 4
 
233
.\}
 
234
.nf
 
235
data\&.close_failed\&.addr // address
 
236
data\&.close_failed\&.err // errno value
 
237
.fi
 
238
.if n \{\
 
239
.RE
 
240
.\}
 
241
.sp
 
242
.SS "ZMQ_EVENT_DISCONNECTED: broken session"
 
243
.sp
 
244
The \fIZMQ_EVENT_DISCONNECTED\fR event triggers when the stream engine (tcp and ipc specific) detects a corrupted / broken session\&.
 
245
.PP
 
246
\fBEvent metadata:\fR. 
 
247
.sp
 
248
.if n \{\
 
249
.RS 4
 
250
.\}
 
251
.nf
 
252
data\&.disconnected\&.addr // address
 
253
data\&.disconnected\&.fd // socket descriptor
 
254
.fi
 
255
.if n \{\
 
256
.RE
 
257
.\}
 
258
.sp
 
259
.SH "RETURN VALUE"
 
260
.sp
 
261
The \fIzmq_socket_monitor()\fR function returns a value of 0 or greater if successful\&. Otherwise it returns \-1 and sets \fIerrno\fR to one of the values defined below\&.
 
262
.SH "ERRORS"
 
263
.PP
 
264
\fBETERM\fR
 
265
.RS 4
 
266
The 0MQ
 
267
\fIcontext\fR
 
268
associated with the specified
 
269
\fIsocket\fR
 
270
was terminated\&.
 
271
.RE
 
272
.PP
 
273
\fBEPROTONOSUPPORT\fR
 
274
.RS 4
 
275
The requested
 
276
\fItransport\fR
 
277
protocol is not supported\&. Monitor sockets are required to use the inproc:// transport\&.
 
278
.RE
 
279
.PP
 
280
\fBEINVAL\fR
 
281
.RS 4
 
282
The endpoint supplied is invalid\&.
 
283
.RE
 
284
.SH "EXAMPLE"
 
285
.PP
 
286
\fBObserving a REP socket\(cqs connection state\fR. 
 
287
.sp
 
288
.if n \{\
 
289
.RS 4
 
290
.\}
 
291
.nf
 
292
// REP socket monitor thread
 
293
static void *rep_socket_monitor (void *ctx)
 
294
{
 
295
    zmq_event_t event;
 
296
    int rc;
 
297
 
 
298
    void *s = zmq_socket (ctx, ZMQ_PAIR);
 
299
    assert (s);
 
300
 
 
301
    rc = zmq_connect (s, "inproc://monitor\&.rep");
 
302
    assert (rc == 0);
 
303
    while (true) {
 
304
        zmq_msg_t msg;
 
305
        zmq_msg_init (&msg);
 
306
        rc = zmq_recvmsg (s, &msg, 0);
 
307
        if (rc == \-1 && zmq_errno() == ETERM) break;
 
308
        assert (rc != \-1);
 
309
        memcpy (&event, zmq_msg_data (&msg), sizeof (event));
 
310
        switch (event\&.event) {
 
311
        case ZMQ_EVENT_LISTENING:
 
312
            printf ("listening socket descriptor %d\en", event\&.data\&.listening\&.fd);
 
313
            printf ("listening socket address %s\en", event\&.data\&.listening\&.addr);
 
314
            break;
 
315
        case ZMQ_EVENT_ACCEPTED:
 
316
            printf ("accepted socket descriptor %d\en", event\&.data\&.accepted\&.fd);
 
317
            printf ("accepted socket address %s\en", event\&.data\&.accepted\&.addr);
 
318
            break;
 
319
        case ZMQ_EVENT_CLOSE_FAILED:
 
320
            printf ("socket close failure error code %d\en", event\&.data\&.close_failed\&.err);
 
321
            printf ("socket address %s\en", event\&.data\&.close_failed\&.addr);
 
322
            break;
 
323
        case ZMQ_EVENT_CLOSED:
 
324
            printf ("closed socket descriptor %d\en", event\&.data\&.closed\&.fd);
 
325
            printf ("closed socket address %s\en", event\&.data\&.closed\&.addr);
 
326
            break;
 
327
        case ZMQ_EVENT_DISCONNECTED:
 
328
            printf ("disconnected socket descriptor %d\en", event\&.data\&.disconnected\&.fd);
 
329
            printf ("disconnected socket address %s\en", event\&.data\&.disconnected\&.addr);
 
330
            break;
 
331
        }
 
332
        zmq_msg_close (&msg);
 
333
    }
 
334
    zmq_close (s);
 
335
    return NULL;
 
336
}
 
337
 
 
338
 
 
339
//  Create the infrastructure
 
340
void *ctx = zmq_init (1);
 
341
assert (ctx);
 
342
 
 
343
// REP socket
 
344
rep = zmq_socket (ctx, ZMQ_REP);
 
345
assert (rep);
 
346
 
 
347
// REP socket monitor, all events
 
348
rc = zmq_socket_monitor (rep, "inproc://monitor\&.rep", ZMQ_EVENT_ALL);
 
349
assert (rc == 0);
 
350
rc = pthread_create (&threads [0], NULL, rep_socket_monitor, ctx);
 
351
assert (rc == 0);
 
352
 
 
353
rc = zmq_bind (rep, addr);
 
354
assert (rc == 0);
 
355
 
 
356
// Allow some time for event detection
 
357
zmq_sleep (1);
 
358
 
 
359
// Close the REP socket
 
360
rc = zmq_close (rep);
 
361
assert (rc == 0);
 
362
 
 
363
zmq_term (ctx);
 
364
.fi
 
365
.if n \{\
 
366
.RE
 
367
.\}
 
368
.sp
 
369
.SH "SEE ALSO"
 
370
.sp
 
371
\fBzmq\fR(7)
 
372
.SH "AUTHORS"
 
373
.sp
 
374
This 0MQ manual page was written by Lourens Naud\('e <\m[blue]\fBlourens@methodmissing\&.com\fR\m[]\&\s-2\u[1]\d\s+2>
 
375
.SH "NOTES"
 
376
.IP " 1." 4
 
377
lourens@methodmissing.com
 
378
.RS 4
 
379
\%mailto:lourens@methodmissing.com
 
380
.RE