2012-05-04 02:32:46 +01:00
|
|
|
/*
|
2013-03-12 17:04:51 +01:00
|
|
|
Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file
|
2012-05-04 02:32:46 +01:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2013-03-08 13:48:18 +01:00
|
|
|
#include <string>
|
2012-05-04 02:32:46 +01:00
|
|
|
#include "../include/zmq.h"
|
2012-09-21 12:53:31 +01:00
|
|
|
#include <pthread.h>
|
2012-08-23 13:40:30 -07:00
|
|
|
#include <string.h>
|
|
|
|
#include "testutil.hpp"
|
2012-05-04 02:32:46 +01:00
|
|
|
|
2012-09-21 12:53:31 +01:00
|
|
|
// REQ socket events handled
|
|
|
|
static int req_socket_events;
|
|
|
|
// 2nd REQ socket events handled
|
|
|
|
static int req2_socket_events;
|
|
|
|
// REP socket events handled
|
|
|
|
static int rep_socket_events;
|
2012-05-21 20:47:11 +01:00
|
|
|
|
2013-03-08 13:48:18 +01:00
|
|
|
std::string addr ;
|
|
|
|
|
|
|
|
static bool read_msg(void* s, zmq_event_t& event, std::string& ep)
|
|
|
|
{
|
|
|
|
int rc ;
|
|
|
|
zmq_msg_t msg1; // binary part
|
|
|
|
zmq_msg_init (&msg1);
|
|
|
|
zmq_msg_t msg2; // address part
|
|
|
|
zmq_msg_init (&msg2);
|
|
|
|
rc = zmq_msg_recv (&msg1, s, 0);
|
|
|
|
if (rc == -1 && zmq_errno() == ETERM)
|
2013-06-22 10:58:16 +01:00
|
|
|
return true ;
|
|
|
|
|
2013-03-08 13:48:18 +01:00
|
|
|
assert (rc != -1);
|
|
|
|
assert (zmq_msg_more(&msg1) != 0);
|
|
|
|
rc = zmq_msg_recv (&msg2, s, 0);
|
|
|
|
if (rc == -1 && zmq_errno() == ETERM)
|
2013-06-22 10:58:16 +01:00
|
|
|
return true;
|
|
|
|
|
2013-03-08 13:48:18 +01:00
|
|
|
assert (rc != -1);
|
|
|
|
assert (zmq_msg_more(&msg2) == 0);
|
|
|
|
// copy binary data to event struct
|
|
|
|
const char* data = (char*)zmq_msg_data(&msg1);
|
|
|
|
memcpy(&event.event, data, sizeof(event.event));
|
|
|
|
memcpy(&event.value, data+sizeof(event.event), sizeof(event.value));
|
|
|
|
// copy address part
|
|
|
|
ep = std::string((char*)zmq_msg_data(&msg2), zmq_msg_size(&msg2));
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
2012-08-27 16:06:51 -07:00
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// REQ socket monitor thread
|
|
|
|
static void *req_socket_monitor (void *ctx)
|
2012-05-04 02:32:46 +01:00
|
|
|
{
|
2013-01-31 19:46:22 +01:00
|
|
|
zmq_event_t event;
|
2013-03-08 13:48:18 +01:00
|
|
|
std::string ep ;
|
2013-01-31 19:46:22 +01:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
void *s = zmq_socket (ctx, ZMQ_PAIR);
|
|
|
|
assert (s);
|
|
|
|
|
|
|
|
rc = zmq_connect (s, "inproc://monitor.req");
|
|
|
|
assert (rc == 0);
|
2013-03-08 13:48:18 +01:00
|
|
|
while (!read_msg(s, event, ep)) {
|
2013-06-22 10:58:16 +01:00
|
|
|
assert (ep == addr);
|
2013-01-31 19:46:22 +01:00
|
|
|
switch (event.event) {
|
2012-09-21 12:53:31 +01:00
|
|
|
case ZMQ_EVENT_CONNECTED:
|
2013-03-08 10:34:25 +01:00
|
|
|
assert (event.value > 0);
|
2012-09-21 12:53:31 +01:00
|
|
|
req_socket_events |= ZMQ_EVENT_CONNECTED;
|
|
|
|
req2_socket_events |= ZMQ_EVENT_CONNECTED;
|
|
|
|
break;
|
|
|
|
case ZMQ_EVENT_CONNECT_DELAYED:
|
2013-03-08 10:34:25 +01:00
|
|
|
assert (event.value != 0);
|
2012-09-21 12:53:31 +01:00
|
|
|
req_socket_events |= ZMQ_EVENT_CONNECT_DELAYED;
|
|
|
|
break;
|
|
|
|
case ZMQ_EVENT_CLOSE_FAILED:
|
2013-03-08 10:34:25 +01:00
|
|
|
assert (event.value != 0);
|
2012-09-21 12:53:31 +01:00
|
|
|
req_socket_events |= ZMQ_EVENT_CLOSE_FAILED;
|
|
|
|
break;
|
|
|
|
case ZMQ_EVENT_CLOSED:
|
2013-03-08 10:34:25 +01:00
|
|
|
assert (event.value != 0);
|
2012-09-21 12:53:31 +01:00
|
|
|
req_socket_events |= ZMQ_EVENT_CLOSED;
|
|
|
|
break;
|
|
|
|
case ZMQ_EVENT_DISCONNECTED:
|
2013-03-08 10:34:25 +01:00
|
|
|
assert (event.value != 0);
|
2012-09-21 12:53:31 +01:00
|
|
|
req_socket_events |= ZMQ_EVENT_DISCONNECTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-01-31 19:46:22 +01:00
|
|
|
zmq_close (s);
|
|
|
|
return NULL;
|
2012-09-21 12:53:31 +01:00
|
|
|
}
|
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// 2nd REQ socket monitor thread
|
|
|
|
static void *req2_socket_monitor (void *ctx)
|
2012-09-21 12:53:31 +01:00
|
|
|
{
|
2013-01-31 19:46:22 +01:00
|
|
|
zmq_event_t event;
|
2013-03-08 13:48:18 +01:00
|
|
|
std::string ep ;
|
2013-01-31 19:46:22 +01:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
void *s = zmq_socket (ctx, ZMQ_PAIR);
|
|
|
|
assert (s);
|
|
|
|
|
|
|
|
rc = zmq_connect (s, "inproc://monitor.req2");
|
|
|
|
assert (rc == 0);
|
2013-03-08 13:48:18 +01:00
|
|
|
while (!read_msg(s, event, ep)) {
|
2013-06-22 10:58:16 +01:00
|
|
|
assert (ep == addr);
|
2013-01-31 19:46:22 +01:00
|
|
|
switch (event.event) {
|
2012-09-21 12:53:31 +01:00
|
|
|
case ZMQ_EVENT_CONNECTED:
|
2013-03-08 10:34:25 +01:00
|
|
|
assert (event.value > 0);
|
2012-09-21 12:53:31 +01:00
|
|
|
req2_socket_events |= ZMQ_EVENT_CONNECTED;
|
|
|
|
break;
|
|
|
|
case ZMQ_EVENT_CLOSED:
|
2013-03-08 10:34:25 +01:00
|
|
|
assert (event.value != 0);
|
2012-09-21 12:53:31 +01:00
|
|
|
req2_socket_events |= ZMQ_EVENT_CLOSED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-01-31 19:46:22 +01:00
|
|
|
zmq_close (s);
|
|
|
|
return NULL;
|
2012-09-21 12:53:31 +01:00
|
|
|
}
|
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// REP socket monitor thread
|
|
|
|
static void *rep_socket_monitor (void *ctx)
|
2012-09-21 12:53:31 +01:00
|
|
|
{
|
2013-01-31 19:46:22 +01:00
|
|
|
zmq_event_t event;
|
2013-03-08 13:48:18 +01:00
|
|
|
std::string ep ;
|
2013-01-31 19:46:22 +01:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
void *s = zmq_socket (ctx, ZMQ_PAIR);
|
|
|
|
assert (s);
|
|
|
|
|
|
|
|
rc = zmq_connect (s, "inproc://monitor.rep");
|
|
|
|
assert (rc == 0);
|
2013-03-08 13:48:18 +01:00
|
|
|
while (!read_msg(s, event, ep)) {
|
2013-06-22 10:58:16 +01:00
|
|
|
assert (ep == addr);
|
2013-01-31 19:46:22 +01:00
|
|
|
switch (event.event) {
|
2012-09-21 12:53:31 +01:00
|
|
|
case ZMQ_EVENT_LISTENING:
|
2013-03-08 10:34:25 +01:00
|
|
|
assert (event.value > 0);
|
2012-09-21 12:53:31 +01:00
|
|
|
rep_socket_events |= ZMQ_EVENT_LISTENING;
|
|
|
|
break;
|
|
|
|
case ZMQ_EVENT_ACCEPTED:
|
2013-03-08 10:34:25 +01:00
|
|
|
assert (event.value > 0);
|
2012-09-21 12:53:31 +01:00
|
|
|
rep_socket_events |= ZMQ_EVENT_ACCEPTED;
|
|
|
|
break;
|
|
|
|
case ZMQ_EVENT_CLOSE_FAILED:
|
2013-03-08 10:34:25 +01:00
|
|
|
assert (event.value != 0);
|
2012-09-21 12:53:31 +01:00
|
|
|
rep_socket_events |= ZMQ_EVENT_CLOSE_FAILED;
|
|
|
|
break;
|
|
|
|
case ZMQ_EVENT_CLOSED:
|
2013-03-08 10:34:25 +01:00
|
|
|
assert (event.value != 0);
|
2012-09-21 12:53:31 +01:00
|
|
|
rep_socket_events |= ZMQ_EVENT_CLOSED;
|
|
|
|
break;
|
|
|
|
case ZMQ_EVENT_DISCONNECTED:
|
2013-03-08 10:34:25 +01:00
|
|
|
assert (event.value != 0);
|
2012-09-21 12:53:31 +01:00
|
|
|
rep_socket_events |= ZMQ_EVENT_DISCONNECTED;
|
|
|
|
break;
|
|
|
|
}
|
2012-05-04 02:32:46 +01:00
|
|
|
}
|
2013-01-31 19:46:22 +01:00
|
|
|
zmq_close (s);
|
|
|
|
return NULL;
|
2012-05-04 02:32:46 +01:00
|
|
|
}
|
|
|
|
|
2012-08-27 16:06:51 -07:00
|
|
|
int main (void)
|
2012-05-04 02:32:46 +01:00
|
|
|
{
|
|
|
|
int rc;
|
2012-09-21 12:53:31 +01:00
|
|
|
void *req;
|
|
|
|
void *req2;
|
|
|
|
void *rep;
|
|
|
|
pthread_t threads [3];
|
|
|
|
|
|
|
|
addr = "tcp://127.0.0.1:5560";
|
2012-05-04 02:32:46 +01:00
|
|
|
|
|
|
|
// Create the infrastructure
|
2013-01-31 09:10:49 +01:00
|
|
|
void *ctx = zmq_ctx_new ();
|
2012-05-04 02:32:46 +01:00
|
|
|
assert (ctx);
|
2012-09-21 12:53:31 +01:00
|
|
|
|
|
|
|
// REP socket
|
2012-08-27 16:06:51 -07:00
|
|
|
rep = zmq_socket (ctx, ZMQ_REP);
|
2012-05-04 02:32:46 +01:00
|
|
|
assert (rep);
|
|
|
|
|
2012-09-21 12:53:31 +01:00
|
|
|
// Assert supported protocols
|
2013-03-08 13:48:18 +01:00
|
|
|
rc = zmq_socket_monitor (rep, addr.c_str(), 0);
|
2012-09-21 12:53:31 +01:00
|
|
|
assert (rc == -1);
|
|
|
|
assert (zmq_errno() == EPROTONOSUPPORT);
|
|
|
|
|
|
|
|
// Deregister monitor
|
|
|
|
rc = zmq_socket_monitor (rep, NULL, 0);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
|
|
|
// REP socket monitor, all events
|
|
|
|
rc = zmq_socket_monitor (rep, "inproc://monitor.rep", ZMQ_EVENT_ALL);
|
|
|
|
assert (rc == 0);
|
|
|
|
rc = pthread_create (&threads [0], NULL, rep_socket_monitor, ctx);
|
|
|
|
assert (rc == 0);
|
2013-06-22 10:58:16 +01:00
|
|
|
|
2012-09-21 12:53:31 +01:00
|
|
|
// REQ socket
|
2012-08-27 16:06:51 -07:00
|
|
|
req = zmq_socket (ctx, ZMQ_REQ);
|
2012-05-04 02:32:46 +01:00
|
|
|
assert (req);
|
|
|
|
|
2012-09-21 12:53:31 +01:00
|
|
|
// REQ socket monitor, all events
|
|
|
|
rc = zmq_socket_monitor (req, "inproc://monitor.req", ZMQ_EVENT_ALL);
|
|
|
|
assert (rc == 0);
|
|
|
|
rc = pthread_create (&threads [1], NULL, req_socket_monitor, ctx);
|
|
|
|
assert (rc == 0);
|
2013-06-22 10:58:16 +01:00
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
// Bind REQ and REP
|
|
|
|
rc = zmq_bind (rep, addr.c_str());
|
|
|
|
assert (rc == 0);
|
2012-09-21 12:53:31 +01:00
|
|
|
|
2013-03-08 13:48:18 +01:00
|
|
|
rc = zmq_connect (req, addr.c_str());
|
2012-09-21 12:53:31 +01:00
|
|
|
assert (rc == 0);
|
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
bounce (rep, req);
|
|
|
|
|
2012-09-21 12:53:31 +01:00
|
|
|
// 2nd REQ socket
|
|
|
|
req2 = zmq_socket (ctx, ZMQ_REQ);
|
|
|
|
assert (req2);
|
|
|
|
|
|
|
|
// 2nd REQ socket monitor, connected event only
|
|
|
|
rc = zmq_socket_monitor (req2, "inproc://monitor.req2", ZMQ_EVENT_CONNECTED);
|
|
|
|
assert (rc == 0);
|
|
|
|
rc = pthread_create (&threads [2], NULL, req2_socket_monitor, ctx);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
2013-03-08 13:48:18 +01:00
|
|
|
rc = zmq_connect (req2, addr.c_str());
|
2012-05-04 02:32:46 +01:00
|
|
|
assert (rc == 0);
|
|
|
|
|
2012-09-21 12:53:31 +01:00
|
|
|
// Close the REP socket
|
|
|
|
rc = zmq_close (rep);
|
2012-05-04 02:32:46 +01:00
|
|
|
assert (rc == 0);
|
|
|
|
|
2012-09-21 12:53:31 +01:00
|
|
|
// Allow some time for detecting error states
|
2013-01-31 19:46:22 +01:00
|
|
|
struct timespec t = { 0, 250 * 1000000 };
|
|
|
|
nanosleep (&t, NULL);
|
2012-05-04 02:32:46 +01:00
|
|
|
|
2012-09-21 12:53:31 +01:00
|
|
|
// Close the REQ socket
|
|
|
|
rc = zmq_close (req);
|
2012-05-04 02:32:46 +01:00
|
|
|
assert (rc == 0);
|
|
|
|
|
2012-09-21 12:53:31 +01:00
|
|
|
// Close the 2nd REQ socket
|
|
|
|
rc = zmq_close (req2);
|
|
|
|
assert (rc == 0);
|
|
|
|
|
2013-01-31 09:10:49 +01:00
|
|
|
zmq_ctx_term (ctx);
|
2012-05-21 20:47:11 +01:00
|
|
|
|
2012-09-21 12:53:31 +01:00
|
|
|
// Expected REP socket events
|
|
|
|
assert (rep_socket_events & ZMQ_EVENT_LISTENING);
|
|
|
|
assert (rep_socket_events & ZMQ_EVENT_ACCEPTED);
|
|
|
|
assert (rep_socket_events & ZMQ_EVENT_CLOSED);
|
|
|
|
|
|
|
|
// Expected REQ socket events
|
|
|
|
assert (req_socket_events & ZMQ_EVENT_CONNECTED);
|
|
|
|
assert (req_socket_events & ZMQ_EVENT_DISCONNECTED);
|
|
|
|
assert (req_socket_events & ZMQ_EVENT_CLOSED);
|
|
|
|
|
|
|
|
// Expected 2nd REQ socket events
|
|
|
|
assert (req2_socket_events & ZMQ_EVENT_CONNECTED);
|
|
|
|
assert (!(req2_socket_events & ZMQ_EVENT_CLOSED));
|
|
|
|
|
|
|
|
pthread_exit (NULL);
|
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
|
|
|
}
|