test_monitor.cpp 3.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
    Copyright (c) 2007-2012 iMatix Corporation
    Copyright (c) 2011 250bpm s.r.o.
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <assert.h>
#include <string.h>

#include "../include/zmq.h"
#include "../include/zmq_utils.h"

28 29 30
static int events;

void socket_monitor (void *s, int event_, zmq_event_data_t *data_)
31 32 33 34
{
    const char *addr = "tcp://127.0.0.1:5560";
    // Only some of the exceptional events could fire
    switch (event_) {
35
    // listener specific
36 37 38
    case ZMQ_EVENT_LISTENING:
        assert (data_->listening.fd > 0);
        assert (memcmp (data_->listening.addr, addr, 22));
39
        events |= ZMQ_EVENT_LISTENING;
40 41 42 43
        break;
    case ZMQ_EVENT_ACCEPTED:
        assert (data_->accepted.fd > 0);
        assert (memcmp (data_->accepted.addr, addr, 22));
44
        events |= ZMQ_EVENT_ACCEPTED;
45
        break;
46
    // connecter specific
47 48 49
    case ZMQ_EVENT_CONNECTED:
        assert (data_->connected.fd > 0);
        assert (memcmp (data_->connected.addr, addr, 22));
50
        events |= ZMQ_EVENT_CONNECTED;
51 52 53 54
        break;
    case ZMQ_EVENT_CONNECT_DELAYED:
        assert (data_->connect_delayed.err != 0);
        assert (memcmp (data_->connect_delayed.addr, addr, 22));
55
        events |= ZMQ_EVENT_CONNECT_DELAYED;
56
        break;
57
    // generic - either end of the socket
58 59 60
    case ZMQ_EVENT_CLOSE_FAILED:
        assert (data_->close_failed.err != 0);
        assert (memcmp (data_->close_failed.addr, addr, 22));
61
        events |= ZMQ_EVENT_CLOSE_FAILED;
62 63 64 65
        break;
    case ZMQ_EVENT_CLOSED:
        assert (data_->closed.fd != 0);
        assert (memcmp (data_->closed.addr, addr, 22));
66
        events |= ZMQ_EVENT_CLOSED;
67 68 69 70
        break;
    case ZMQ_EVENT_DISCONNECTED:
        assert (data_->disconnected.fd != 0);
        assert (memcmp (data_->disconnected.addr, addr, 22));
71
        events |= ZMQ_EVENT_DISCONNECTED;
72 73 74 75 76 77 78 79 80 81 82 83 84 85
        break;
    default:
        // out of band / unexpected event
        assert (0);
    }
}

int main (int argc, char *argv [])
{
    int rc;

    //  Create the infrastructure
    void *ctx = zmq_init (1);
    assert (ctx);
86
    // set socket monitor
87
    rc = zmq_ctx_set_monitor (ctx, socket_monitor);
88
    assert (rc == 0);
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
    void *rep = zmq_socket (ctx, ZMQ_REP);
    assert (rep);

    rc = zmq_bind (rep, "tcp://127.0.0.1:5560");
    assert (rc == 0);

    void *req = zmq_socket (ctx, ZMQ_REQ);
    assert (req);

    rc = zmq_connect (req, "tcp://127.0.0.1:5560");
    assert (rc == 0);

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

    //  Deallocate the infrastructure.
    rc = zmq_close (req);
    assert (rc == 0);

    // Allow for closed or disconnected events to bubble up
    zmq_sleep (1);

    rc = zmq_close (rep);
    assert (rc == 0);

    zmq_sleep (1);

    zmq_term (ctx);
117 118 119 120 121 122 123

    // We expect to at least observe these events
    assert (events & ZMQ_EVENT_LISTENING);
    assert (events & ZMQ_EVENT_ACCEPTED);
    assert (events & ZMQ_EVENT_CONNECTED);
    assert (events & ZMQ_EVENT_CLOSED);

124
    return 0 ;
AJ Lewis's avatar
AJ Lewis committed
125 126
}