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 () :
37
    active (0),
38
    last_in (NULL),
39
    current (0),
40
    more (false)
41 42 43 44 45
{
}

zmq::fq_t::~fq_t ()
{
46
    zmq_assert (pipes.empty ());
47 48
}

49
void zmq::fq_t::attach (pipe_t *pipe_)
50 51 52 53 54 55
{
    pipes.push_back (pipe_);
    pipes.swap (active, pipes.size () - 1);
    active++;
}

56
void zmq::fq_t::pipe_terminated (pipe_t *pipe_)
57
{
58 59
    const pipes_t::size_type index = pipes.index (pipe_);

60 61
    //  Remove the pipe from the list; adjust number of active pipes
    //  accordingly.
62
    if (index < active) {
63
        active--;
64
        pipes.swap (index, active);
65 66 67
        if (current == active)
            current = 0;
    }
68
    pipes.erase (pipe_);
69 70 71 72 73

    if (last_in == pipe_) {
        saved_credential = last_in->get_credential ();
        last_in = NULL;
    }
74 75
}

76
void zmq::fq_t::activated (pipe_t *pipe_)
77 78 79 80 81 82
{
    //  Move the pipe to the list of active pipes.
    pipes.swap (pipes.index (pipe_), active);
    active++;
}

83
int zmq::fq_t::recv (msg_t *msg_)
84
{
85
    return recvpipe (msg_, NULL);
86 87
}

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

94
    //  Round-robin over the pipes to get the next message.
Martin Hurton's avatar
Martin Hurton committed
95
    while (active > 0) {
96

97 98
        //  Try to fetch new message. If we've already read part of the message
        //  subsequent part should be immediately available.
99
        bool fetched = pipes [current]->read (msg_);
100

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

115 116 117 118 119
        //  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
120 121 122 123
        active--;
        pipes.swap (current, active);
        if (current == active)
            current = 0;
124 125 126 127
    }

    //  No message is available. Initialise the output parameter
    //  to be a 0-byte message.
128 129
    rc = msg_->init ();
    errno_assert (rc == 0);
130 131 132 133 134 135
    errno = EAGAIN;
    return -1;
}

bool zmq::fq_t::has_in ()
{
136
    //  There are subsequent parts of the partly-read message available.
137
    if (more)
138 139
        return true;

140 141 142 143
    //  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
144
    while (active > 0) {
145 146
        if (pipes [current]->check_read ())
            return true;
147 148 149 150 151

        //  Deactivate the pipe.
        active--;
        pipes.swap (current, active);
        if (current == active)
152 153 154 155 156 157
            current = 0;
    }

    return false;
}

158 159 160 161 162 163
zmq::blob_t zmq::fq_t::get_credential () const
{
    return last_in?
        last_in->get_credential (): saved_credential;
}