test_connect_rid.cpp 9.34 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
5

6 7 8
    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
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    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.
25 26 27 28 29 30 31 32

    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"


33 34
void test_stream_2_stream ()
{
35 36 37 38
    void *rbind, *rconn1;
    int ret;
    char buff[256];
    char msg[] = "hi 1";
39
    const char *bindip = "tcp://127.0.0.1:*";
40
    int disabled = 0;
41
    int zero = 0;
42 43
    size_t len = MAX_SOCKET_STRING;
    char my_endpoint[MAX_SOCKET_STRING];
44
    void *ctx = zmq_ctx_new ();
45

46
    //  Set up listener STREAM.
47
    rbind = zmq_socket (ctx, ZMQ_STREAM);
48
    assert (rbind);
49 50
    ret =
      zmq_setsockopt (rbind, ZMQ_STREAM_NOTIFY, &disabled, sizeof (disabled));
51
    assert (ret == 0);
52 53 54
    ret = zmq_setsockopt (rbind, ZMQ_LINGER, &zero, sizeof (zero));
    assert (0 == ret);
    ret = zmq_bind (rbind, bindip);
55
    assert (0 == ret);
56 57
    ret = zmq_getsockopt (rbind, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
    assert (0 == ret);
58

59
    //  Set up connection stream.
60
    rconn1 = zmq_socket (ctx, ZMQ_STREAM);
61 62 63
    assert (rconn1);
    ret = zmq_setsockopt (rconn1, ZMQ_LINGER, &zero, sizeof (zero));
    assert (0 == ret);
64

65
    //  Do the connection.
66
    ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_ROUTING_ID, "conn1", 6);
67
    assert (0 == ret);
68
    ret = zmq_connect (rconn1, my_endpoint);
69

70
    /*  Uncomment to test assert on duplicate routing id.
71
    //  Test duplicate connect attempt.
72
    ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_ROUTING_ID, "conn1", 6);
73 74 75
    assert (0 == ret);
    ret = zmq_connect (rconn1, bindip);
    assert (0 == ret);
76
*/
77 78 79 80 81 82 83 84 85 86
    //  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]);
87
    ret = zmq_recv (rbind, buff + 128, 128, 0);
88
    assert (5 == ret);
89 90
    assert ('h' == buff[128]);

91
    // Handle close of the socket.
92
    ret = zmq_unbind (rbind, my_endpoint);
93
    assert (0 == ret);
94
    ret = zmq_close (rbind);
95
    assert (0 == ret);
96
    ret = zmq_close (rconn1);
97
    assert (0 == ret);
98 99

    zmq_ctx_destroy (ctx);
100
}
101

102
void test_router_2_router (bool named_)
103
{
104 105 106 107
    void *rbind, *rconn1;
    int ret;
    char buff[256];
    char msg[] = "hi 1";
108
    const char *bindip = "tcp://127.0.0.1:*";
109
    int zero = 0;
110 111
    size_t len = MAX_SOCKET_STRING;
    char my_endpoint[MAX_SOCKET_STRING];
112
    void *ctx = zmq_ctx_new ();
113 114 115 116 117 118 119 120

    //  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);
121 122
    ret = zmq_getsockopt (rbind, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
    assert (0 == ret);
123 124 125

    //  Create connection socket.
    rconn1 = zmq_socket (ctx, ZMQ_ROUTER);
126
    assert (rconn1);
127 128 129 130
    ret = zmq_setsockopt (rconn1, ZMQ_LINGER, &zero, sizeof (zero));
    assert (0 == ret);

    //  If we're in named mode, set some identities.
131
    if (named_) {
132 133
        ret = zmq_setsockopt (rbind, ZMQ_ROUTING_ID, "X", 1);
        ret = zmq_setsockopt (rconn1, ZMQ_ROUTING_ID, "Y", 1);
134
    }
135

136 137
    //  Make call to connect using a connect_routing_id.
    ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_ROUTING_ID, "conn1", 6);
138
    assert (0 == ret);
139
    ret = zmq_connect (rconn1, my_endpoint);
140
    assert (0 == ret);
141
    /*  Uncomment to test assert on duplicate routing id
142
    //  Test duplicate connect attempt.
143
    ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_ROUTING_ID, "conn1", 6);
144 145 146
    assert (0 == ret);
    ret = zmq_connect (rconn1, bindip);
    assert (0 == ret);
147
*/
148 149 150 151 152 153 154 155
    //  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);
156
    if (named_)
157
        assert (ret && 'Y' == buff[0]);
158
    else
159 160 161
        assert (ret && 0 == buff[0]);

    //  Receive the data.
162 163
    ret = zmq_recv (rbind, buff + 128, 128, 0);
    assert (5 == ret && 'h' == buff[128]);
164 165

    //  Send some data back.
166
    if (named_) {
167 168
        ret = zmq_send (rbind, buff, 1, ZMQ_SNDMORE);
        assert (1 == ret);
169
    } else {
170 171
        ret = zmq_send (rbind, buff, 5, ZMQ_SNDMORE);
        assert (5 == ret);
172
    }
173 174
    ret = zmq_send_const (rbind, "ok", 3, 0);
    assert (3 == ret);
175

176 177 178
    //  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);
179
    ret = zmq_recv (rconn1, buff + 128, 128, 0);
180 181
    assert (3 == ret && 'o' == buff[128]);

182
    ret = zmq_unbind (rbind, my_endpoint);
183
    assert (0 == ret);
184
    ret = zmq_close (rbind);
185
    assert (0 == ret);
186
    ret = zmq_close (rconn1);
187
    assert (0 == ret);
188 189

    zmq_ctx_destroy (ctx);
190 191
}

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
void test_router_2_router_while_receiving ()
{
    void *xbind, *zbind, *yconn;
    int ret;
    char buff[256];
    char msg[] = "hi 1";
    const char *wildcard_bind = "tcp://127.0.0.1:*";
    int zero = 0;
    size_t len = MAX_SOCKET_STRING;
    char x_endpoint[MAX_SOCKET_STRING];
    char z_endpoint[MAX_SOCKET_STRING];
    void *ctx = zmq_ctx_new ();

    //  Create xbind socket.
    xbind = zmq_socket (ctx, ZMQ_ROUTER);
    assert (xbind);
    ret = zmq_setsockopt (xbind, ZMQ_LINGER, &zero, sizeof (zero));
    assert (0 == ret);
    ret = zmq_bind (xbind, wildcard_bind);
    assert (0 == ret);
    ret = zmq_getsockopt (xbind, ZMQ_LAST_ENDPOINT, x_endpoint, &len);
    assert (0 == ret);

    //  Create zbind socket.
    zbind = zmq_socket (ctx, ZMQ_ROUTER);
    assert (zbind);
    ret = zmq_setsockopt (zbind, ZMQ_LINGER, &zero, sizeof (zero));
    assert (0 == ret);
    ret = zmq_bind (zbind, wildcard_bind);
    assert (0 == ret);
    ret = zmq_getsockopt (zbind, ZMQ_LAST_ENDPOINT, z_endpoint, &len);
    assert (0 == ret);

    //  Create connection socket.
    yconn = zmq_socket (ctx, ZMQ_ROUTER);
    assert (yconn);
    ret = zmq_setsockopt (yconn, ZMQ_LINGER, &zero, sizeof (zero));
    assert (0 == ret);

    // set identites for each socket
    ret = zmq_setsockopt (xbind, ZMQ_ROUTING_ID, "X", 2);
    ret = zmq_setsockopt (yconn, ZMQ_ROUTING_ID, "Y", 2);
    ret = zmq_setsockopt (zbind, ZMQ_ROUTING_ID, "Z", 2);

    //  Connect Y to X using a routing id
    ret = zmq_setsockopt (yconn, ZMQ_CONNECT_ROUTING_ID, "X", 2);
    assert (0 == ret);
    ret = zmq_connect (yconn, x_endpoint);
    assert (0 == ret);

    //  Send some data from Y to X.
    ret = zmq_send (yconn, "X", 2, ZMQ_SNDMORE);
    assert (2 == ret);
    ret = zmq_send (yconn, msg, 5, 0);
    assert (5 == ret);

    // wait for the Y->X message to be received
    msleep (SETTLE_TIME);

    // Now X tries to connect to Z and send a message
    ret = zmq_setsockopt (xbind, ZMQ_CONNECT_ROUTING_ID, "Z", 2);
    assert (0 == ret);
    ret = zmq_connect (xbind, z_endpoint);
    assert (0 == ret);

    //  Try to send some data from X to Z.
    ret = zmq_send (xbind, "Z", 2, ZMQ_SNDMORE);
    assert (2 == ret);
    ret = zmq_send (xbind, msg, 5, 0);
    assert (5 == ret);

    // wait for the X->Z message to be received (so that our non-blocking check will actually
    // fail if the message is routed to Y)
    msleep (SETTLE_TIME);

    // nothing should have been received on the Y socket
    ret = zmq_recv (yconn, buff, 256, ZMQ_DONTWAIT);
    assert (ret == -1);
    assert (zmq_errno () == EAGAIN);

    // the message should have been received on the Z socket
    ret = zmq_recv (zbind, buff, 256, 0);
    assert (ret && 'X' == buff[0]);
    ret = zmq_recv (zbind, buff + 128, 128, 0);
    assert (5 == ret && 'h' == buff[128]);

    ret = zmq_unbind (xbind, x_endpoint);
    assert (0 == ret);
    ret = zmq_unbind (zbind, z_endpoint);
    assert (0 == ret);
    ret = zmq_close (yconn);
    assert (0 == ret);
    ret = zmq_close (xbind);
    assert (0 == ret);
    ret = zmq_close (zbind);
    assert (0 == ret);

    zmq_ctx_destroy (ctx);
}

292 293
int main (void)
{
294
    setup_test_environment ();
295

296
    test_stream_2_stream ();
297 298
    test_router_2_router (false);
    test_router_2_router (true);
299
    test_router_2_router_while_receiving ();
300

301 302
    return 0;
}