fq.cpp 4.71 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
    You should have received a copy of the GNU Lesser General Public License
27 28 29
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

30
#include "precompiled.hpp"
31 32 33
#include "fq.hpp"
#include "pipe.hpp"
#include "err.hpp"
34
#include "msg.hpp"
35

36
zmq::fq_t::fq_t () : active (0), last_in (NULL), current (0), more (false)
37 38 39 40 41
{
}

zmq::fq_t::~fq_t ()
{
42
    zmq_assert (pipes.empty ());
43 44
}

45
void zmq::fq_t::attach (pipe_t *pipe_)
46 47 48 49 50 51
{
    pipes.push_back (pipe_);
    pipes.swap (active, pipes.size () - 1);
    active++;
}

52
void zmq::fq_t::pipe_terminated (pipe_t *pipe_)
53
{
54 55
    const pipes_t::size_type index = pipes.index (pipe_);

56 57
    //  Remove the pipe from the list; adjust number of active pipes
    //  accordingly.
58
    if (index < active) {
59
        active--;
60
        pipes.swap (index, active);
61 62 63
        if (current == active)
            current = 0;
    }
64
    pipes.erase (pipe_);
65 66

    if (last_in == pipe_) {
67
        saved_credential.set_deep_copy (last_in->get_credential ());
68 69
        last_in = NULL;
    }
70 71
}

72
void zmq::fq_t::activated (pipe_t *pipe_)
73 74 75 76 77 78
{
    //  Move the pipe to the list of active pipes.
    pipes.swap (pipes.index (pipe_), active);
    active++;
}

79
int zmq::fq_t::recv (msg_t *msg_)
80
{
81
    return recvpipe (msg_, NULL);
82 83
}

84
int zmq::fq_t::recvpipe (msg_t *msg_, pipe_t **pipe_)
85 86
{
    //  Deallocate old content of the message.
87 88
    int rc = msg_->close ();
    errno_assert (rc == 0);
89

90
    //  Round-robin over the pipes to get the next message.
Martin Hurton's avatar
Martin Hurton committed
91
    while (active > 0) {
92 93
        //  Try to fetch new message. If we've already read part of the message
        //  subsequent part should be immediately available.
94
        bool fetched = pipes[current]->read (msg_);
95

96 97
        //  Note that when message is not fetched, current pipe is deactivated
        //  and replaced by another active pipe. Thus we don't have to increase
98 99
        //  the 'current' pointer.
        if (fetched) {
100
            if (pipe_)
101 102
                *pipe_ = pipes[current];
            more = msg_->flags () & msg_t::more ? true : false;
103
            if (!more) {
104
                last_in = pipes[current];
Martin Hurton's avatar
Martin Hurton committed
105
                current = (current + 1) % active;
106
            }
107
            return 0;
108
        }
Martin Hurton's avatar
Martin Hurton committed
109

110 111 112 113 114
        //  Check the atomicity of the message.
        //  If we've already received the first part of the message
        //  we should get the remaining parts without blocking.
        zmq_assert (!more);

Martin Hurton's avatar
Martin Hurton committed
115 116 117 118
        active--;
        pipes.swap (current, active);
        if (current == active)
            current = 0;
119 120 121 122
    }

    //  No message is available. Initialise the output parameter
    //  to be a 0-byte message.
123 124
    rc = msg_->init ();
    errno_assert (rc == 0);
125 126 127 128 129 130
    errno = EAGAIN;
    return -1;
}

bool zmq::fq_t::has_in ()
{
131
    //  There are subsequent parts of the partly-read message available.
132
    if (more)
133 134
        return true;

135 136 137 138
    //  Note that messing with current doesn't break the fairness of fair
    //  queueing algorithm. If there are no messages available current will
    //  get back to its original value. Otherwise it'll point to the first
    //  pipe holding messages, skipping only pipes with no messages available.
Martin Hurton's avatar
Martin Hurton committed
139
    while (active > 0) {
140
        if (pipes[current]->check_read ())
141
            return true;
142 143 144 145 146

        //  Deactivate the pipe.
        active--;
        pipes.swap (current, active);
        if (current == active)
147 148 149 150 151 152
            current = 0;
    }

    return false;
}

153
const zmq::blob_t &zmq::fq_t::get_credential () const
154
{
155
    return last_in ? last_in->get_credential () : saved_credential;
156
}