sub.cpp 4.83 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2010 iMatix Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
    the terms of the Lesser GNU 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
    Lesser GNU General Public License for more details.

    You should have received a copy of the Lesser GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

20 21
#include <string.h>

22
#include "../include/zmq.h"
23 24 25 26 27

#include "sub.hpp"
#include "err.hpp"

zmq::sub_t::sub_t (class app_thread_t *parent_) :
28
    socket_base_t (parent_),
29
    has_message (false),
30
    more (false)
31
{
32 33
    options.requires_in = true;
    options.requires_out = false;
34
    zmq_msg_init (&message);
35 36 37 38
}

zmq::sub_t::~sub_t ()
{
39
    zmq_msg_close (&message);
40 41
}

42
void zmq::sub_t::xattach_pipes (class reader_t *inpipe_,
43
    class writer_t *outpipe_, const blob_t &peer_identity_)
44
{
45 46
    zmq_assert (inpipe_ && !outpipe_);
    fq.attach (inpipe_);
47 48 49 50
}

void zmq::sub_t::xdetach_inpipe (class reader_t *pipe_)
{
51 52
    zmq_assert (pipe_);
    fq.detach (pipe_);
53 54 55 56
}

void zmq::sub_t::xdetach_outpipe (class writer_t *pipe_)
{
57
    //  SUB socket is read-only thus there should be no outpipes.
58 59 60 61 62
    zmq_assert (false);
}

void zmq::sub_t::xkill (class reader_t *pipe_)
{
63
    fq.kill (pipe_);
64 65 66 67
}

void zmq::sub_t::xrevive (class reader_t *pipe_)
{
68
    fq.revive (pipe_);
69 70
}

Martin Hurton's avatar
Martin Hurton committed
71 72 73 74 75
void zmq::sub_t::xrevive (class writer_t *pipe_)
{
    zmq_assert (false);
}

76
int zmq::sub_t::xsetsockopt (int option_, const void *optval_,
77 78 79
    size_t optvallen_)
{
    if (option_ == ZMQ_SUBSCRIBE) {
80
        subscriptions.add ((unsigned char*) optval_, optvallen_);
81 82 83 84
        return 0;
    }
    
    if (option_ == ZMQ_UNSUBSCRIBE) {
85 86 87
        if (!subscriptions.rm ((unsigned char*) optval_, optvallen_)) {
            errno = EINVAL;
            return -1;
88 89 90 91
        }
        return 0;
    }

92 93
    errno = EINVAL;
    return -1;
94 95
}

96
int zmq::sub_t::xsend (zmq_msg_t *msg_, int flags_)
97
{
98
    errno = ENOTSUP;
99 100 101
    return -1;
}

102
int zmq::sub_t::xrecv (zmq_msg_t *msg_, int flags_)
103
{
104 105 106 107 108
    //  If there's already a message prepared by a previous call to zmq_poll,
    //  return it straight ahead.
    if (has_message) {
        zmq_msg_move (msg_, &message);
        has_message = false;
109
        more = msg_->flags & ZMQ_MSG_MORE;
110 111 112 113 114 115 116
        return 0;
    }

    //  TODO: This can result in infinite loop in the case of continuous
    //  stream of non-matching messages which breaks the non-blocking recv
    //  semantics.
    while (true) {
117

118 119
        //  Get a message using fair queueing algorithm.
        int rc = fq.recv (msg_, flags_);
120

121 122 123 124 125 126
        //  If there's no message available, return immediately.
        //  The same when error occurs.
        if (rc != 0)
            return -1;

        //  Check whether the message matches at least one subscription.
127
        //  Non-initial parts of the message are passed 
128 129
        if (more || match (msg_)) {
            more = msg_->flags & ZMQ_MSG_MORE;
130
            return 0;
131 132 133 134
        }

        //  Message doesn't match. Pop any remaining parts of the message
        //  from the pipe.
135
        while (msg_->flags & ZMQ_MSG_MORE) {
136 137 138
            rc = fq.recv (msg_, ZMQ_NOBLOCK);
            zmq_assert (rc == 0);
        }
139 140 141 142 143
    }
}

bool zmq::sub_t::xhas_in ()
{
144
    //  There are subsequent parts of the partly-read message available.
145
    if (more)
146 147
        return true;

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    //  If there's already a message prepared by a previous call to zmq_poll,
    //  return straight ahead.
    if (has_message)
        return true;

    //  TODO: This can result in infinite loop in the case of continuous
    //  stream of non-matching messages.
    while (true) {

        //  Get a message using fair queueing algorithm.
        int rc = fq.recv (&message, ZMQ_NOBLOCK);

        //  If there's no message available, return immediately.
        //  The same when error occurs.
        if (rc != 0) {
            zmq_assert (errno == EAGAIN);
            return false;
        }

        //  Check whether the message matches at least one subscription.
        if (match (&message)) {
            has_message = true;
            return true;
        }
172 173 174

        //  Message doesn't match. Pop any remaining parts of the message
        //  from the pipe.
175
        while (message.flags & ZMQ_MSG_MORE) {
176 177 178
            rc = fq.recv (&message, ZMQ_NOBLOCK);
            zmq_assert (rc == 0);
        }
179 180 181 182 183 184 185 186 187 188
    }
}

bool zmq::sub_t::xhas_out ()
{
    return false;
}

bool zmq::sub_t::match (zmq_msg_t *msg_)
{
189 190
    return subscriptions.check ((unsigned char*) zmq_msg_data (msg_),
        zmq_msg_size (msg_));
191
}