fq.cpp 3.73 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file
3 4 5 6

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
7
    the terms of the GNU Lesser General Public License as published by
8 9 10 11 12 13
    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
14
    GNU Lesser General Public License for more details.
15

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

#include "fq.hpp"
#include "pipe.hpp"
#include "err.hpp"
23
#include "msg.hpp"
24

25
zmq::fq_t::fq_t () :
26
    active (0),
27
    current (0),
28
    more (false)
29 30 31 32 33
{
}

zmq::fq_t::~fq_t ()
{
34
    zmq_assert (pipes.empty ());
35 36
}

37
void zmq::fq_t::attach (pipe_t *pipe_)
38 39 40 41 42 43
{
    pipes.push_back (pipe_);
    pipes.swap (active, pipes.size () - 1);
    active++;
}

44
void zmq::fq_t::pipe_terminated (pipe_t *pipe_)
45
{
46 47
    const pipes_t::size_type index = pipes.index (pipe_);

48 49
    //  Remove the pipe from the list; adjust number of active pipes
    //  accordingly.
50
    if (index < active) {
51
        active--;
52
        pipes.swap (index, active);
53 54 55
        if (current == active)
            current = 0;
    }
56 57 58
    pipes.erase (pipe_);
}

59
void zmq::fq_t::activated (pipe_t *pipe_)
60 61 62 63 64 65
{
    //  Move the pipe to the list of active pipes.
    pipes.swap (pipes.index (pipe_), active);
    active++;
}

66
int zmq::fq_t::recv (msg_t *msg_)
67
{
68
    return recvpipe (msg_, NULL);
69 70
}

71
int zmq::fq_t::recvpipe (msg_t *msg_, pipe_t **pipe_)
72 73
{
    //  Deallocate old content of the message.
74 75
    int rc = msg_->close ();
    errno_assert (rc == 0);
76

77
    //  Round-robin over the pipes to get the next message.
Martin Hurton's avatar
Martin Hurton committed
78
    while (active > 0) {
79 80 81

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

84 85
        //  Note that when message is not fetched, current pipe is deactivated
        //  and replaced by another active pipe. Thus we don't have to increase
86 87
        //  the 'current' pointer.
        if (fetched) {
88 89
            if (pipe_)
                *pipe_ = pipes [current];
Martin Hurton's avatar
Martin Hurton committed
90 91 92
            more = msg_->flags () & msg_t::more? true: false;
            if (!more)
                current = (current + 1) % active;
93
            return 0;
94
        }
Martin Hurton's avatar
Martin Hurton committed
95

96 97 98 99 100
        //  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
101 102 103 104
        active--;
        pipes.swap (current, active);
        if (current == active)
            current = 0;
105 106 107 108
    }

    //  No message is available. Initialise the output parameter
    //  to be a 0-byte message.
109 110
    rc = msg_->init ();
    errno_assert (rc == 0);
111 112 113 114 115 116
    errno = EAGAIN;
    return -1;
}

bool zmq::fq_t::has_in ()
{
117
    //  There are subsequent parts of the partly-read message available.
118
    if (more)
119 120
        return true;

121 122 123 124
    //  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
125
    while (active > 0) {
126 127
        if (pipes [current]->check_read ())
            return true;
128 129 130 131 132

        //  Deactivate the pipe.
        active--;
        pipes.swap (current, active);
        if (current == active)
133 134 135 136 137 138
            current = 0;
    }

    return false;
}