test_radio_dish.cpp 5.3 KB
Newer Older
somdoron's avatar
somdoron committed
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
somdoron's avatar
somdoron committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

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

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

    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.

    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"

somdoron's avatar
somdoron committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
int msg_send (zmq_msg_t *msg_, void *s_, const char* group_, const char* body_)
{
    int rc = zmq_msg_init_size (msg_, strlen (body_));
    if (rc != 0)
        return rc;

    memcpy (zmq_msg_data (msg_), body_, strlen (body_));

    rc = zmq_msg_set_group (msg_, group_);
    if (rc != 0) {
        zmq_msg_close (msg_);
        return rc;
    }

    rc = zmq_msg_send (msg_, s_, 0);

    zmq_msg_close (msg_);

    return rc;
}

int msg_recv_cmp (zmq_msg_t *msg_, void *s_, const char* group_, const char* body_)
{
    int rc = zmq_msg_init (msg_);
    if (rc != 0)
        return -1;

    int recv_rc = zmq_msg_recv (msg_, s_, 0);
    if (recv_rc == -1)
        return -1;

63
    if (strcmp (zmq_msg_group (msg_), group_) != 0)
somdoron's avatar
somdoron committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    {
        zmq_msg_close (msg_);
        return -1;
    }

    char * body = (char*) malloc (sizeof(char) * (zmq_msg_size (msg_) + 1));
    memcpy (body, zmq_msg_data (msg_), zmq_msg_size (msg_));
    body [zmq_msg_size (msg_)] = '\0';

    if (strcmp (body, body_) != 0)
    {
        zmq_msg_close (msg_);
        return -1;
    }

    zmq_msg_close (msg_);
80
    free(body);
somdoron's avatar
somdoron committed
81 82 83
    return recv_rc;
}

somdoron's avatar
somdoron committed
84 85
int main (void)
{
86 87
    size_t len = MAX_SOCKET_STRING;
    char my_endpoint[MAX_SOCKET_STRING];
somdoron's avatar
somdoron committed
88 89 90 91 92 93 94
    setup_test_environment ();
    void *ctx = zmq_ctx_new ();
    assert (ctx);

    void *radio = zmq_socket (ctx, ZMQ_RADIO);
    void *dish = zmq_socket (ctx, ZMQ_DISH);

95 96 97
    int rc = zmq_bind (radio, "tcp://127.0.0.1:*");
    assert (rc == 0);
    rc = zmq_getsockopt (radio, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
somdoron's avatar
somdoron committed
98 99 100
    assert (rc == 0);

    //  Leaving a group which we didn't join
somdoron's avatar
somdoron committed
101
    rc = zmq_leave (dish, "Movies");
somdoron's avatar
somdoron committed
102 103 104 105 106 107 108 109 110 111 112
    assert (rc == -1);

    //  Joining too long group
    char too_long_group[ZMQ_GROUP_MAX_LENGTH + 2];
    for (int index = 0; index < ZMQ_GROUP_MAX_LENGTH + 2; index++)
        too_long_group[index] = 'A';
    too_long_group[ZMQ_GROUP_MAX_LENGTH + 1] = '\0';
    rc = zmq_join (dish, too_long_group);
    assert (rc == -1);

    // Joining
somdoron's avatar
somdoron committed
113
    rc = zmq_join (dish, "Movies");
somdoron's avatar
somdoron committed
114 115 116
    assert (rc == 0);

    // Duplicate Joining
somdoron's avatar
somdoron committed
117
    rc = zmq_join (dish, "Movies");
somdoron's avatar
somdoron committed
118 119 120
    assert (rc == -1);

    // Connecting
121
    rc = zmq_connect (dish, my_endpoint);
somdoron's avatar
somdoron committed
122 123
    assert (rc == 0);

124
    msleep (SETTLE_TIME);
somdoron's avatar
somdoron committed
125

somdoron's avatar
somdoron committed
126
    zmq_msg_t msg;
somdoron's avatar
somdoron committed
127

somdoron's avatar
somdoron committed
128 129 130
    //  This is not going to be sent as dish only subscribe to "Movies"
    rc = msg_send (&msg, radio, "TV", "Friends");
    assert (rc == 7);
somdoron's avatar
somdoron committed
131

somdoron's avatar
somdoron committed
132 133 134
    //  This is going to be sent to the dish
    rc = msg_send (&msg, radio, "Movies", "Godfather");
    assert (rc == 9);
somdoron's avatar
somdoron committed
135

somdoron's avatar
somdoron committed
136 137 138
    //  Check the correct message arrived
    rc = msg_recv_cmp (&msg, dish, "Movies", "Godfather");
    assert (rc == 9);
somdoron's avatar
somdoron committed
139 140

    //  Join group during connection optvallen
somdoron's avatar
somdoron committed
141
    rc = zmq_join (dish, "TV");
somdoron's avatar
somdoron committed
142 143 144 145 146
    assert (rc == 0);

    zmq_sleep (1);

    //  This should arrive now as we joined the group
somdoron's avatar
somdoron committed
147 148
    rc = msg_send (&msg, radio, "TV", "Friends");
    assert (rc == 7);
somdoron's avatar
somdoron committed
149

somdoron's avatar
somdoron committed
150 151 152
    //  Check the correct message arrived
    rc = msg_recv_cmp (&msg, dish, "TV", "Friends");
    assert (rc == 7);
somdoron's avatar
somdoron committed
153

somdoron's avatar
somdoron committed
154 155
    //  Leaving groupr
    rc = zmq_leave (dish, "TV");
somdoron's avatar
somdoron committed
156 157 158 159
    assert (rc == 0);

    zmq_sleep (1);

somdoron's avatar
somdoron committed
160 161 162
    //  This is not going to be sent as dish only subscribe to "Movies"
    rc = msg_send (&msg, radio, "TV", "Friends");
    assert (rc == 7);
somdoron's avatar
somdoron committed
163 164

    //  This is going to be sent to the dish
somdoron's avatar
somdoron committed
165 166
    rc = msg_send (&msg, radio, "Movies", "Godfather");
    assert (rc == 9);
somdoron's avatar
somdoron committed
167

168 169 170 171 172 173
    // test zmq_poll with dish
    zmq_pollitem_t items [] = {
        { radio, 0, ZMQ_POLLIN, 0 }, // read publications
        { dish, 0, ZMQ_POLLIN, 0 }, // read subscriptions
    };
    rc = zmq_poll(items, 2, 2000);
174
    assert (rc == 1);
175 176
    assert (items[1].revents == ZMQ_POLLIN);

somdoron's avatar
somdoron committed
177 178 179
    //  Check the correct message arrived
    rc = msg_recv_cmp (&msg, dish, "Movies", "Godfather");
    assert (rc == 9);
somdoron's avatar
somdoron committed
180 181 182 183 184 185 186 187 188 189 190 191

    rc = zmq_close (dish);
    assert (rc == 0);

    rc = zmq_close (radio);
    assert (rc == 0);

    rc = zmq_ctx_term (ctx);
    assert (rc == 0);

    return 0 ;
}