test_connect_rid.cpp 5.2 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
/*
    Copyright (c) 2007-2014 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 "testutil.hpp"


23
void test_stream_2_stream(){
24 25 26 27
    void *rbind, *rconn1;
    int ret;
    char buff[256];
    char msg[] = "hi 1";
28 29
    const char *bindip = "tcp://127.0.0.1:5556";
    int zero = 0;
30
    void *ctx = zmq_ctx_new ();
31

32
    //  Set up listener STREAM.
33
    rbind = zmq_socket (ctx, ZMQ_STREAM);
34 35 36 37
    assert (rbind);
    ret = zmq_setsockopt (rbind, ZMQ_LINGER, &zero, sizeof (zero));
    assert (0 == ret);
    ret = zmq_bind (rbind, bindip);
38 39
    assert(0 == ret);

40
    //  Set up connection stream.
41
    rconn1 = zmq_socket (ctx, ZMQ_STREAM);
42 43 44 45 46 47 48 49 50
    assert (rconn1);
    ret = zmq_setsockopt (rconn1, ZMQ_LINGER, &zero, sizeof (zero));
    assert (0 == ret);
    
    //  Do the connection.
    ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
    assert (0 == ret);
    ret = zmq_connect (rconn1, bindip);

51
/*  Uncomment to test assert on duplicate rid.
52 53 54 55 56
    //  Test duplicate connect attempt.
    ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
    assert (0 == ret);
    ret = zmq_connect (rconn1, bindip);
    assert (0 == ret);
57
*/   
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    //  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 && 0 == buff[0]);
    assert (0 == buff[0]);
    ret = zmq_recv (rbind, buff, 256, 0);
    assert (0 == ret);

    // Handle close of the socket.
    ret = zmq_recv (rbind, buff, 256, 0);
    assert (ret);
    assert (0 == buff[0]);
    ret = zmq_recv (rbind, buff+128, 128, 0);
    assert (5 == ret); 
    assert ('h' == buff[128]);

79 80 81 82 83 84
    ret = zmq_unbind (rbind, bindip);
    assert(0 == ret);
    ret = zmq_close (rbind);
    assert(0 == ret);
    ret = zmq_close (rconn1);
    assert(0 == ret);
85 86

    zmq_ctx_destroy (ctx);
87
}
88

89
void test_router_2_router(bool named){
90 91 92 93
    void *rbind, *rconn1;
    int ret;
    char buff[256];
    char msg[] = "hi 1";
94 95
    const char *bindip = "tcp://127.0.0.1:5556";
    int zero = 0;
96
    void *ctx = zmq_ctx_new ();
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

    //  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);
    assert (rconn1); 
    ret = zmq_setsockopt (rconn1, ZMQ_LINGER, &zero, sizeof (zero));
    assert (0 == ret);

    //  If we're in named mode, set some identities.
    if (named) {
114 115 116
        ret = zmq_setsockopt (rbind, ZMQ_IDENTITY, "X", 1);
        ret = zmq_setsockopt (rconn1, ZMQ_IDENTITY, "Y", 1);
    }
117 118 119 120 121 122

    //  Make call to connect using a connect_rid. 
    ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
    assert (0 == ret);
    ret = zmq_connect (rconn1, bindip);
    assert (0 == ret);
123
/*  Uncomment to test assert on duplicate rid 
124 125 126 127 128
    //  Test duplicate connect attempt.
    ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
    assert (0 == ret);
    ret = zmq_connect (rconn1, bindip);
    assert (0 == ret);
129
*/
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    //  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);
    if (named) 
        assert (ret && 'Y' == buff[0]);
    else 
        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);
151 152
    }
    else {
153 154
        ret = zmq_send (rbind, buff, 5, ZMQ_SNDMORE);
        assert (5 == ret);
155
    }
156 157
    ret = zmq_send_const (rbind, "ok", 3, 0);
    assert (3 == ret);
158
    
159 160 161 162 163 164 165 166 167 168 169 170
    //  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);
171 172

    zmq_ctx_destroy (ctx);
173 174 175 176
}

int main (void)
{
177
    setup_test_environment ();
178 179 180 181 182

	test_stream_2_stream ();
    test_router_2_router (false);
    test_router_2_router (true);

183 184
    return 0;
}