2014-01-20 21:18:04 +01:00
|
|
|
/*
|
2015-01-22 10:32:06 +01:00
|
|
|
Copyright (c) 2007-2015 Contributors as noted in the AUTHORS file
|
2014-01-20 21:18:04 +01:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2014-01-20 21:18:04 +01:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
libzmq is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License (LGPL) as published
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or
|
2014-01-20 21:18:04 +01:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
As a special exception, the Contributors give you permission to link
|
|
|
|
this library with independent modules to produce an executable,
|
|
|
|
regardless of the license terms of these independent modules, and to
|
|
|
|
copy and distribute the resulting executable under terms of your choice,
|
|
|
|
provided that you also meet, for each linked independent module, the
|
|
|
|
terms and conditions of the license of that module. An independent
|
|
|
|
module is a module which is not derived from or based on this library.
|
|
|
|
If you modify this library, you must extend this exception to your
|
|
|
|
version of the library.
|
|
|
|
|
|
|
|
libzmq 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.
|
2014-01-20 21:18:04 +01:00
|
|
|
|
|
|
|
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 "testutil.hpp"
|
|
|
|
|
|
|
|
|
2014-03-17 14:41:02 +01:00
|
|
|
void test_stream_2_stream(){
|
2014-01-20 21:18:04 +01:00
|
|
|
void *rbind, *rconn1;
|
|
|
|
int ret;
|
|
|
|
char buff[256];
|
|
|
|
char msg[] = "hi 1";
|
2014-01-21 20:24:39 +01:00
|
|
|
const char *bindip = "tcp://127.0.0.1:5556";
|
2015-07-23 23:39:47 +02:00
|
|
|
int disabled = 0;
|
2014-01-21 20:24:39 +01:00
|
|
|
int zero = 0;
|
2014-03-17 14:41:02 +01:00
|
|
|
void *ctx = zmq_ctx_new ();
|
2014-01-20 21:18:04 +01:00
|
|
|
|
2014-01-21 20:24:39 +01:00
|
|
|
// Set up listener STREAM.
|
2014-03-17 14:41:02 +01:00
|
|
|
rbind = zmq_socket (ctx, ZMQ_STREAM);
|
2014-01-21 20:24:39 +01:00
|
|
|
assert (rbind);
|
2015-07-23 23:39:47 +02:00
|
|
|
ret = zmq_setsockopt (rbind, ZMQ_STREAM_NOTIFY, &disabled, sizeof (disabled));
|
|
|
|
assert (ret == 0);
|
2014-01-21 20:24:39 +01:00
|
|
|
ret = zmq_setsockopt (rbind, ZMQ_LINGER, &zero, sizeof (zero));
|
|
|
|
assert (0 == ret);
|
|
|
|
ret = zmq_bind (rbind, bindip);
|
2014-01-20 21:18:04 +01:00
|
|
|
assert(0 == ret);
|
|
|
|
|
2014-01-21 20:24:39 +01:00
|
|
|
// Set up connection stream.
|
2014-03-17 14:41:02 +01:00
|
|
|
rconn1 = zmq_socket (ctx, ZMQ_STREAM);
|
2014-01-21 20:24:39 +01:00
|
|
|
assert (rconn1);
|
|
|
|
ret = zmq_setsockopt (rconn1, ZMQ_LINGER, &zero, sizeof (zero));
|
|
|
|
assert (0 == ret);
|
2015-08-20 16:46:34 +02:00
|
|
|
|
2014-01-21 20:24:39 +01:00
|
|
|
// Do the connection.
|
|
|
|
ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
|
|
|
|
assert (0 == ret);
|
|
|
|
ret = zmq_connect (rconn1, bindip);
|
|
|
|
|
2014-01-21 20:43:34 +01:00
|
|
|
/* Uncomment to test assert on duplicate rid.
|
2014-01-21 20:24:39 +01:00
|
|
|
// Test duplicate connect attempt.
|
|
|
|
ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
|
|
|
|
assert (0 == ret);
|
|
|
|
ret = zmq_connect (rconn1, bindip);
|
|
|
|
assert (0 == ret);
|
2015-08-20 16:46:34 +02:00
|
|
|
*/
|
2014-01-21 20:24:39 +01:00
|
|
|
// Send data to the bound stream.
|
|
|
|
ret = zmq_send (rconn1, "conn1", 6, ZMQ_SNDMORE);
|
|
|
|
assert (6 == ret);
|
|
|
|
ret = zmq_send (rconn1, msg, 5, 0);
|
|
|
|
assert (5 == ret);
|
|
|
|
|
|
|
|
// Accept data on the bound stream.
|
|
|
|
ret = zmq_recv (rbind, buff, 256, 0);
|
|
|
|
assert (ret);
|
|
|
|
assert (0 == buff[0]);
|
|
|
|
ret = zmq_recv (rbind, buff+128, 128, 0);
|
2015-01-23 15:25:40 +01:00
|
|
|
assert (5 == ret);
|
2014-01-21 20:24:39 +01:00
|
|
|
assert ('h' == buff[128]);
|
|
|
|
|
2015-01-23 15:25:40 +01:00
|
|
|
// Handle close of the socket.
|
2014-01-21 20:49:41 +01:00
|
|
|
ret = zmq_unbind (rbind, bindip);
|
|
|
|
assert(0 == ret);
|
|
|
|
ret = zmq_close (rbind);
|
|
|
|
assert(0 == ret);
|
|
|
|
ret = zmq_close (rconn1);
|
|
|
|
assert(0 == ret);
|
2014-03-17 14:41:02 +01:00
|
|
|
|
|
|
|
zmq_ctx_destroy (ctx);
|
2014-01-20 21:18:04 +01:00
|
|
|
}
|
2014-01-21 20:24:39 +01:00
|
|
|
|
2014-03-17 14:41:02 +01:00
|
|
|
void test_router_2_router(bool named){
|
2014-01-20 21:18:04 +01:00
|
|
|
void *rbind, *rconn1;
|
|
|
|
int ret;
|
|
|
|
char buff[256];
|
|
|
|
char msg[] = "hi 1";
|
2014-01-21 20:24:39 +01:00
|
|
|
const char *bindip = "tcp://127.0.0.1:5556";
|
|
|
|
int zero = 0;
|
2014-03-17 14:41:02 +01:00
|
|
|
void *ctx = zmq_ctx_new ();
|
2014-01-21 20:24:39 +01:00
|
|
|
|
|
|
|
// Create bind socket.
|
|
|
|
rbind = zmq_socket (ctx, ZMQ_ROUTER);
|
|
|
|
assert (rbind);
|
|
|
|
ret = zmq_setsockopt (rbind, ZMQ_LINGER, &zero, sizeof (zero));
|
|
|
|
assert (0 == ret);
|
|
|
|
ret = zmq_bind (rbind, bindip);
|
|
|
|
assert (0 == ret);
|
|
|
|
|
|
|
|
// Create connection socket.
|
|
|
|
rconn1 = zmq_socket (ctx, ZMQ_ROUTER);
|
2015-08-20 16:46:34 +02:00
|
|
|
assert (rconn1);
|
2014-01-21 20:24:39 +01:00
|
|
|
ret = zmq_setsockopt (rconn1, ZMQ_LINGER, &zero, sizeof (zero));
|
|
|
|
assert (0 == ret);
|
|
|
|
|
|
|
|
// If we're in named mode, set some identities.
|
|
|
|
if (named) {
|
2014-01-20 21:18:04 +01:00
|
|
|
ret = zmq_setsockopt (rbind, ZMQ_IDENTITY, "X", 1);
|
|
|
|
ret = zmq_setsockopt (rconn1, ZMQ_IDENTITY, "Y", 1);
|
|
|
|
}
|
2014-01-21 20:24:39 +01:00
|
|
|
|
2015-08-20 16:46:34 +02:00
|
|
|
// Make call to connect using a connect_rid.
|
2014-01-21 20:24:39 +01:00
|
|
|
ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
|
|
|
|
assert (0 == ret);
|
|
|
|
ret = zmq_connect (rconn1, bindip);
|
|
|
|
assert (0 == ret);
|
2015-08-20 16:46:34 +02:00
|
|
|
/* Uncomment to test assert on duplicate rid
|
2014-01-21 20:24:39 +01:00
|
|
|
// Test duplicate connect attempt.
|
|
|
|
ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
|
|
|
|
assert (0 == ret);
|
|
|
|
ret = zmq_connect (rconn1, bindip);
|
|
|
|
assert (0 == ret);
|
2014-01-21 20:43:34 +01:00
|
|
|
*/
|
2014-01-21 20:24:39 +01:00
|
|
|
// Send some data.
|
|
|
|
ret = zmq_send (rconn1, "conn1", 6, ZMQ_SNDMORE);
|
|
|
|
assert (6 == ret);
|
|
|
|
ret = zmq_send (rconn1, msg, 5, 0);
|
|
|
|
assert (5 == ret);
|
|
|
|
|
|
|
|
// Receive the name.
|
|
|
|
ret = zmq_recv (rbind, buff, 256, 0);
|
2015-08-20 16:46:34 +02:00
|
|
|
if (named)
|
2014-01-21 20:24:39 +01:00
|
|
|
assert (ret && 'Y' == buff[0]);
|
2015-08-20 16:46:34 +02:00
|
|
|
else
|
2014-01-21 20:24:39 +01:00
|
|
|
assert (ret && 0 == buff[0]);
|
|
|
|
|
|
|
|
// Receive the data.
|
|
|
|
ret = zmq_recv (rbind, buff+128, 128, 0);
|
|
|
|
assert(5 == ret && 'h' == buff[128]);
|
|
|
|
|
|
|
|
// Send some data back.
|
|
|
|
if (named) {
|
|
|
|
ret = zmq_send (rbind, buff, 1, ZMQ_SNDMORE);
|
|
|
|
assert (1 == ret);
|
2014-01-20 21:18:04 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-01-21 20:24:39 +01:00
|
|
|
ret = zmq_send (rbind, buff, 5, ZMQ_SNDMORE);
|
|
|
|
assert (5 == ret);
|
2014-01-20 21:18:04 +01:00
|
|
|
}
|
2014-01-21 20:24:39 +01:00
|
|
|
ret = zmq_send_const (rbind, "ok", 3, 0);
|
|
|
|
assert (3 == ret);
|
2015-08-20 16:46:34 +02:00
|
|
|
|
2014-01-21 20:24:39 +01:00
|
|
|
// If bound socket identity naming a problem, we'll likely see something funky here.
|
|
|
|
ret = zmq_recv (rconn1, buff, 256, 0);
|
|
|
|
assert ('c' == buff[0] && 6 == ret);
|
|
|
|
ret = zmq_recv (rconn1, buff+128, 128, 0);
|
|
|
|
assert (3 == ret && 'o' == buff[128]);
|
|
|
|
|
|
|
|
ret = zmq_unbind (rbind, bindip);
|
|
|
|
assert(0 == ret);
|
|
|
|
ret = zmq_close (rbind);
|
|
|
|
assert(0 == ret);
|
|
|
|
ret = zmq_close (rconn1);
|
|
|
|
assert(0 == ret);
|
2014-03-17 14:41:02 +01:00
|
|
|
|
|
|
|
zmq_ctx_destroy (ctx);
|
2014-01-20 21:18:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main (void)
|
|
|
|
{
|
2014-01-21 20:24:39 +01:00
|
|
|
setup_test_environment ();
|
2014-03-17 14:41:02 +01:00
|
|
|
|
2015-08-20 16:46:34 +02:00
|
|
|
test_stream_2_stream ();
|
2014-03-17 14:41:02 +01:00
|
|
|
test_router_2_router (false);
|
|
|
|
test_router_2_router (true);
|
|
|
|
|
2014-01-20 21:18:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|