2012-05-04 02:32:46 +01:00
|
|
|
/*
|
|
|
|
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>
|
2012-08-04 11:41:33 +01:00
|
|
|
#include "testutil.hpp"
|
2012-05-04 02:32:46 +01:00
|
|
|
|
|
|
|
#include "../include/zmq.h"
|
|
|
|
#include "../include/zmq_utils.h"
|
|
|
|
|
2012-05-21 20:47:11 +01:00
|
|
|
static int events;
|
|
|
|
|
|
|
|
void socket_monitor (void *s, int event_, zmq_event_data_t *data_)
|
2012-05-04 02:32:46 +01:00
|
|
|
{
|
|
|
|
const char *addr = "tcp://127.0.0.1:5560";
|
|
|
|
// Only some of the exceptional events could fire
|
|
|
|
switch (event_) {
|
2012-05-21 20:47:11 +01:00
|
|
|
// listener specific
|
2012-05-04 02:32:46 +01:00
|
|
|
case ZMQ_EVENT_LISTENING:
|
|
|
|
assert (data_->listening.fd > 0);
|
2012-08-23 12:56:00 -07:00
|
|
|
assert (!strcmp (data_->listening.addr, addr));
|
2012-05-21 20:47:11 +01:00
|
|
|
events |= ZMQ_EVENT_LISTENING;
|
2012-05-04 02:32:46 +01:00
|
|
|
break;
|
|
|
|
case ZMQ_EVENT_ACCEPTED:
|
|
|
|
assert (data_->accepted.fd > 0);
|
2012-08-23 12:56:00 -07:00
|
|
|
assert (!strcmp (data_->accepted.addr, addr));
|
2012-05-21 20:47:11 +01:00
|
|
|
events |= ZMQ_EVENT_ACCEPTED;
|
2012-05-04 02:32:46 +01:00
|
|
|
break;
|
2012-05-21 20:47:11 +01:00
|
|
|
// connecter specific
|
2012-05-04 02:32:46 +01:00
|
|
|
case ZMQ_EVENT_CONNECTED:
|
|
|
|
assert (data_->connected.fd > 0);
|
2012-08-23 12:56:00 -07:00
|
|
|
assert (!strcmp (data_->connected.addr, addr));
|
2012-05-21 20:47:11 +01:00
|
|
|
events |= ZMQ_EVENT_CONNECTED;
|
2012-05-04 02:32:46 +01:00
|
|
|
break;
|
|
|
|
case ZMQ_EVENT_CONNECT_DELAYED:
|
|
|
|
assert (data_->connect_delayed.err != 0);
|
2012-08-23 12:56:00 -07:00
|
|
|
assert (!strcmp (data_->connect_delayed.addr, addr));
|
2012-05-21 20:47:11 +01:00
|
|
|
events |= ZMQ_EVENT_CONNECT_DELAYED;
|
2012-05-04 02:32:46 +01:00
|
|
|
break;
|
2012-05-21 20:47:11 +01:00
|
|
|
// generic - either end of the socket
|
2012-05-04 02:32:46 +01:00
|
|
|
case ZMQ_EVENT_CLOSE_FAILED:
|
|
|
|
assert (data_->close_failed.err != 0);
|
2012-08-23 12:56:00 -07:00
|
|
|
assert (!strcmp (data_->close_failed.addr, addr));
|
2012-05-21 20:47:11 +01:00
|
|
|
events |= ZMQ_EVENT_CLOSE_FAILED;
|
2012-05-04 02:32:46 +01:00
|
|
|
break;
|
|
|
|
case ZMQ_EVENT_CLOSED:
|
|
|
|
assert (data_->closed.fd != 0);
|
2012-08-23 12:56:00 -07:00
|
|
|
assert (!strcmp (data_->closed.addr, addr));
|
2012-05-21 20:47:11 +01:00
|
|
|
events |= ZMQ_EVENT_CLOSED;
|
2012-05-04 02:32:46 +01:00
|
|
|
break;
|
|
|
|
case ZMQ_EVENT_DISCONNECTED:
|
|
|
|
assert (data_->disconnected.fd != 0);
|
2012-08-23 12:56:00 -07:00
|
|
|
assert (!strcmp (data_->disconnected.addr, addr));
|
2012-05-21 20:47:11 +01:00
|
|
|
events |= ZMQ_EVENT_DISCONNECTED;
|
2012-05-04 02:32:46 +01:00
|
|
|
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);
|
2012-05-21 20:47:11 +01:00
|
|
|
// set socket monitor
|
2012-05-22 20:15:18 +01:00
|
|
|
rc = zmq_ctx_set_monitor (ctx, socket_monitor);
|
2012-05-21 20:47:11 +01:00
|
|
|
assert (rc == 0);
|
2012-05-04 02:32:46 +01:00
|
|
|
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);
|
|
|
|
|
2012-08-04 11:41:33 +01:00
|
|
|
bounce (rep, req);
|
|
|
|
|
2012-05-04 02:32:46 +01:00
|
|
|
// 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);
|
2012-05-21 20:47:11 +01:00
|
|
|
|
|
|
|
// 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);
|
2012-08-04 11:41:33 +01:00
|
|
|
assert (events & ZMQ_EVENT_DISCONNECTED);
|
2012-05-21 20:47:11 +01:00
|
|
|
|
2012-05-04 02:32:46 +01:00
|
|
|
return 0 ;
|
2012-06-05 09:22:22 -05:00
|
|
|
}
|
|
|
|
|