zsock_monitor(3)

zsock_monitor(3)

CZMQ Manual - CZMQ/3.0.0

Name

zsock_monitor - socket event monitor

Synopsis

//  Create a new socket monitor
CZMQ_EXPORT zsock_monitor_t *
    zsock_monitor_new (zsock_t *sock);

//  Destroy a socket monitor
CZMQ_EXPORT void
    zsock_monitor_destroy (zsock_monitor_t **self_p);

//  Receive a status message from the monitor; if no message arrives within
//  500 msec, or the call was interrupted, returns NULL.
CZMQ_EXPORT zmsg_t *
    zsock_monitor_recv (zsock_monitor_t *self);

//  Enable verbose tracing of commands and activity
CZMQ_EXPORT void
    zsock_monitor_set_verbose (zsock_monitor_t *self, bool verbose);

//  Listen to a specified event
CZMQ_EXPORT void
    zsock_monitor_listen (zsock_monitor_t *self, int event);

//  Start the monitor; once started, new calls to zsock_monitor_event() will
//  have no effect.
CZMQ_EXPORT void
    zsock_monitor_start (zsock_monitor_t *self);

//  Return the status pipe, to allow polling for monitor data.
CZMQ_EXPORT zsock_t *
    zsock_monitor_handle (zsock_monitor_t *self);

// Self test of this class
CZMQ_EXPORT void 
    zsock_monitor_test (bool verbose);

Description

The zsock_monitor class provides an API for obtaining socket events such as connected, listen, disconnected, etc. Socket events are only available for sockets connecting or bound to ipc:// and tcp:// endpoints.

This class wraps the ZMQ socket monitor API, see zmq_socket_monitor for details. Currently this class requires libzmq v4.0 or later and is empty on older versions of libzmq.

Example

From zsock_monitor_test method

#if defined (ZMQ_EVENT_ALL)
    zsock_t *sink = zsock_new (ZMQ_PULL);
    zsock_monitor_t *sinkmon = zsock_monitor_new (sink);
    zsock_monitor_set_verbose (sinkmon, verbose);
    zsock_monitor_listen (sinkmon, ZMQ_EVENT_LISTENING);
    zsock_monitor_listen (sinkmon, ZMQ_EVENT_ACCEPTED);
    zsock_monitor_start (sinkmon);

    zsock_t *source = zsock_new (ZMQ_PUSH);
    zsock_monitor_t *sourcemon = zsock_monitor_new (source);
    zsock_monitor_set_verbose (sourcemon, verbose);
    zsock_monitor_listen (sourcemon, ZMQ_EVENT_CONNECTED);
    zsock_monitor_listen (sourcemon, ZMQ_EVENT_DISCONNECTED);
    zsock_monitor_start (sourcemon);

    //  Allow a brief time for the message to get there...
    zmq_poll (NULL, 0, 200);

    //  Check sink is now listening
    int port_nbr = zsock_bind (sink, "tcp://127.0.0.1:*");
    assert (port_nbr != -1);
    bool result = s_check_event (sinkmon, ZMQ_EVENT_LISTENING);
    assert (result);

    //  Check source connected to sink
    zsock_connect (source, "tcp://127.0.0.1:%d", port_nbr);
    result = s_check_event (sourcemon, ZMQ_EVENT_CONNECTED);
    assert (result);

    //  Check sink accepted connection
    result = s_check_event (sinkmon, ZMQ_EVENT_ACCEPTED);
    assert (result);

    zsock_monitor_destroy (&sinkmon);
    zsock_monitor_destroy (&sourcemon);
    zsock_destroy (&sink);
    zsock_destroy (&source); 
#endif

See also

czmq(7)

Authors

The CZMQ manual was written by Pieter Hintjens<moc.xitami|hp#moc.xitami|hp>.

Resources

Main web site: http://czmq.zeromq.org/

Report bugs to the ØMQ development mailing list: <gro.qmorez.stsil|ved-qmorez#gro.qmorez.stsil|ved-qmorez>

Copyright

Copyright (c) the Contributors as noted in the AUTHORS file. This file is part of CZMQ, the high-level C binding for ØMQ: http://czmq.zeromq.org. This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.