test_disconnect_inproc.cpp 5.84 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2016 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

    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/>.
*/

30
#include "testutil.hpp"
31 32

/// Initialize a zeromq message with a given null-terminated string
33 34 35 36 37 38 39 40 41
#define ZMQ_PREPARE_STRING(msg, data, size)                                    \
                                                                               \
    zmq_msg_init (&msg)                                                        \
      && printf ("zmq_msg_init: %s\n", zmq_strerror (errno));                  \
                                                                               \
    zmq_msg_init_size (&msg, size + 1)                                         \
      && printf ("zmq_msg_init_size: %s\n", zmq_strerror (errno));             \
                                                                               \
    memcpy (zmq_msg_data (&msg), data, size + 1);
42

43 44 45 46
//  TODO: this code fails to meet our style guidelines, and needs rewriting

static int publicationsReceived = 0;
static bool isSubscribed = false;
47

48 49 50 51
int main (int, char **)
{
    setup_test_environment ();
    void *context = zmq_ctx_new ();
52 53
    void *pub_socket;
    void *sub_socket;
54

55
    (pub_socket = zmq_socket (context, ZMQ_XPUB))
56
      || printf ("zmq_socket: %s\n", zmq_strerror (errno));
57
    (sub_socket = zmq_socket (context, ZMQ_SUB))
58
      || printf ("zmq_socket: %s\n", zmq_strerror (errno));
59
    zmq_setsockopt (sub_socket, ZMQ_SUBSCRIBE, "foo", 3)
60 61
      && printf ("zmq_setsockopt: %s\n", zmq_strerror (errno));

62
    zmq_bind (pub_socket, "inproc://someInProcDescriptor")
63
      && printf ("zmq_bind: %s\n", zmq_strerror (errno));
64
    //zmq_bind(pubSocket, "tcp://127.0.0.1:30010") && printf("zmq_bind: %s\n", zmq_strerror(errno));
65

66
    int more;
67
    size_t more_size = sizeof (more);
68
    int iteration = 0;
69

70
    while (1) {
71
        zmq_pollitem_t items[] = {
72 73
          {sub_socket, 0, ZMQ_POLLIN, 0}, // read publications
          {pub_socket, 0, ZMQ_POLLIN, 0}, // read subscriptions
74
        };
75
        int rc = zmq_poll (items, 2, 100);
76 77

        if (items[1].revents & ZMQ_POLLIN) {
78 79 80
            while (1) {
                zmq_msg_t msg;
                zmq_msg_init (&msg);
81
                zmq_msg_recv (&msg, pub_socket, 0);
82
                char *buffer = (char *) zmq_msg_data (&msg);
83 84

                if (buffer[0] == 0) {
85
                    assert (isSubscribed);
86
                    isSubscribed = false;
87 88
                } else {
                    assert (!isSubscribed);
89 90 91
                    isSubscribed = true;
                }

92
                zmq_getsockopt (pub_socket, ZMQ_RCVMORE, &more, &more_size);
93 94 95
                zmq_msg_close (&msg);

                if (!more)
96
                    break; //  Last message part
97 98 99 100 101 102 103
            }
        }

        if (items[0].revents & ZMQ_POLLIN) {
            while (1) {
                zmq_msg_t msg;
                zmq_msg_init (&msg);
104 105
                zmq_msg_recv (&msg, sub_socket, 0);
                zmq_getsockopt (sub_socket, ZMQ_RCVMORE, &more, &more_size);
106
                zmq_msg_close (&msg);
107

108 109
                if (!more) {
                    publicationsReceived++;
110
                    break; //  Last message part
111 112 113 114
                }
            }
        }
        if (iteration == 1) {
115
            zmq_connect (sub_socket, "inproc://someInProcDescriptor")
116
              && printf ("zmq_connect: %s\n", zmq_strerror (errno));
117
            msleep (SETTLE_TIME);
118 119
        }
        if (iteration == 4) {
120
            zmq_disconnect (sub_socket, "inproc://someInProcDescriptor")
121 122
              && printf ("zmq_disconnect(%d): %s\n", errno,
                         zmq_strerror (errno));
123
        }
124
        if (iteration > 4 && rc == 0)
125
            break;
126

127 128 129
        zmq_msg_t channel_envlp;
        ZMQ_PREPARE_STRING (channel_envlp, "foo", 3);
        zmq_msg_send (&channel_envlp, pub_socket, ZMQ_SNDMORE) >= 0
130
          || printf ("zmq_msg_send: %s\n", zmq_strerror (errno));
131
        zmq_msg_close (&channel_envlp)
132
          && printf ("zmq_msg_close: %s\n", zmq_strerror (errno));
133 134

        zmq_msg_t message;
135
        ZMQ_PREPARE_STRING (message, "this is foo!", 12);
136
        zmq_msg_send (&message, pub_socket, 0) >= 0
137 138 139
          || printf ("zmq_msg_send: %s\n", zmq_strerror (errno));
        zmq_msg_close (&message)
          && printf ("zmq_msg_close: %s\n", zmq_strerror (errno));
140 141 142

        iteration++;
    }
143 144 145
    assert (publicationsReceived == 3);
    assert (!isSubscribed);

146 147
    zmq_close (pub_socket) && printf ("zmq_close: %s", zmq_strerror (errno));
    zmq_close (sub_socket) && printf ("zmq_close: %s", zmq_strerror (errno));
148

149
    zmq_ctx_term (context);
150
    return 0;
151
}