test_pub_invert_matching.cpp 4.4 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 30 31 32 33

    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"

int main (void)
{
34
    setup_test_environment ();
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
    void *ctx = zmq_ctx_new ();
    assert (ctx);

    //  Create a publisher
    void *pub = zmq_socket (ctx, ZMQ_PUB);
    assert (pub);
    int rc = zmq_bind (pub, "inproc://soname");
    assert (rc == 0);

    //  Create two subscribers
    void *sub1 = zmq_socket (ctx, ZMQ_SUB);
    assert (sub1);
    rc = zmq_connect (sub1, "inproc://soname");
    assert (rc == 0);

    void *sub2 = zmq_socket (ctx, ZMQ_SUB);
    assert (sub2);
    rc = zmq_connect (sub2, "inproc://soname");
    assert (rc == 0);

    //  Subscribe pub1 to one prefix
    //  and pub2 to another prefix.
    const char PREFIX1[] = "prefix1";
    const char PREFIX2[] = "p2";

60
    rc = zmq_setsockopt (sub1, ZMQ_SUBSCRIBE, PREFIX1, sizeof (PREFIX1));
61 62
    assert (rc == 0);

63
    rc = zmq_setsockopt (sub2, ZMQ_SUBSCRIBE, PREFIX2, sizeof (PREFIX2));
64 65 66
    assert (rc == 0);

    //  Send a message with the first prefix
67 68
    rc = zmq_send_const (pub, PREFIX1, sizeof (PREFIX1), 0);
    assert (rc == sizeof (PREFIX1));
69 70 71

    //  sub1 should receive it, but not sub2
    rc = zmq_recv (sub1, NULL, 0, ZMQ_DONTWAIT);
72
    assert (rc == sizeof (PREFIX1));
73 74 75 76 77 78

    rc = zmq_recv (sub2, NULL, 0, ZMQ_DONTWAIT);
    assert (rc == -1);
    assert (errno == EAGAIN);

    //  Send a message with the second prefix
79 80
    rc = zmq_send_const (pub, PREFIX2, sizeof (PREFIX2), 0);
    assert (rc == sizeof (PREFIX2));
81 82 83

    //  sub2 should receive it, but not sub1
    rc = zmq_recv (sub2, NULL, 0, ZMQ_DONTWAIT);
84
    assert (rc == sizeof (PREFIX2));
85 86 87 88 89 90 91

    rc = zmq_recv (sub1, NULL, 0, ZMQ_DONTWAIT);
    assert (rc == -1);
    assert (errno == EAGAIN);

    //  Now invert the matching
    int invert = 1;
92
    rc = zmq_setsockopt (pub, ZMQ_INVERT_MATCHING, &invert, sizeof (invert));
93 94 95
    assert (rc == 0);

    //  ... on both sides, otherwise the SUB socket will filter the messages out
96 97
    rc = zmq_setsockopt (sub1, ZMQ_INVERT_MATCHING, &invert, sizeof (invert));
    rc = zmq_setsockopt (sub2, ZMQ_INVERT_MATCHING, &invert, sizeof (invert));
98 99 100
    assert (rc == 0);

    //  Send a message with the first prefix
101 102
    rc = zmq_send_const (pub, PREFIX1, sizeof (PREFIX1), 0);
    assert (rc == sizeof (PREFIX1));
103 104 105

    //  sub2 should receive it, but not sub1
    rc = zmq_recv (sub2, NULL, 0, ZMQ_DONTWAIT);
106
    assert (rc == sizeof (PREFIX1));
107 108 109 110 111 112

    rc = zmq_recv (sub1, NULL, 0, ZMQ_DONTWAIT);
    assert (rc == -1);
    assert (errno == EAGAIN);

    //  Send a message with the second prefix
113 114
    rc = zmq_send_const (pub, PREFIX2, sizeof (PREFIX2), 0);
    assert (rc == sizeof (PREFIX2));
115 116 117

    //  sub1 should receive it, but not sub2
    rc = zmq_recv (sub1, NULL, 0, ZMQ_DONTWAIT);
118
    assert (rc == sizeof (PREFIX2));
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

    rc = zmq_recv (sub2, NULL, 0, ZMQ_DONTWAIT);
    assert (rc == -1);
    assert (errno == EAGAIN);


    //  Clean up.
    rc = zmq_close (pub);
    assert (rc == 0);
    rc = zmq_close (sub1);
    assert (rc == 0);
    rc = zmq_close (sub2);
    assert (rc == 0);
    rc = zmq_ctx_term (ctx);
    assert (rc == 0);

135
    return 0;
136
}