lb.cpp 4.25 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2015 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
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
29

30 31 32
#include "lb.hpp"
#include "pipe.hpp"
#include "err.hpp"
33
#include "msg.hpp"
34

35
zmq::lb_t::lb_t () :
36
    active (0),
37
    current (0),
38
    more (false),
39
    dropping (false)
40 41 42 43 44
{
}

zmq::lb_t::~lb_t ()
{
45
    zmq_assert (pipes.empty ());
46 47
}

48
void zmq::lb_t::attach (pipe_t *pipe_)
49 50
{
    pipes.push_back (pipe_);
51
    activated (pipe_);
52 53
}

54
void zmq::lb_t::pipe_terminated (pipe_t *pipe_)
55
{
56 57 58 59 60 61 62
    pipes_t::size_type index = pipes.index (pipe_);

    //  If we are in the middle of multipart message and current pipe
    //  have disconnected, we have to drop the remainder of the message.
    if (index == current && more)
        dropping = true;

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

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

81
int zmq::lb_t::send (msg_t *msg_)
82 83 84 85 86
{
    return sendpipe (msg_, NULL);
}

int zmq::lb_t::sendpipe (msg_t *msg_, pipe_t **pipe_)
87
{
88 89 90 91
    //  Drop the message if required. If we are at the end of the message
    //  switch back to non-dropping mode.
    if (dropping) {

92
        more = msg_->flags () & msg_t::more ? true : false;
Martin Hurton's avatar
Martin Hurton committed
93
        dropping = more;
94

95
        int rc = msg_->close ();
96
        errno_assert (rc == 0);
97
        rc = msg_->init ();
98
        errno_assert (rc == 0);
99 100 101
        return 0;
    }

Martin Hurton's avatar
Martin Hurton committed
102
    while (active > 0) {
103
        if (pipes [current]->write (msg_))
104 105 106
        {
            if (pipe_)
                *pipe_ = pipes [current];
Martin Hurton's avatar
Martin Hurton committed
107
            break;
108
        }
109

110
        zmq_assert (!more);
Martin Hurton's avatar
Martin Hurton committed
111 112 113 114 115 116 117
        active--;
        if (current < active)
            pipes.swap (current, active);
        else
            current = 0;
    }

118
    //  If there are no pipes we cannot send the message.
Martin Hurton's avatar
Martin Hurton committed
119
    if (active == 0) {
120 121 122 123
        errno = EAGAIN;
        return -1;
    }

124 125
    //  If it's final part of the message we can flush it downstream and
    //  continue round-robining (load balance).
Martin Hurton's avatar
Martin Hurton committed
126
    more = msg_->flags () & msg_t::more? true: false;
127
    if (!more) {
128 129 130
        pipes [current]->flush ();
        current = (current + 1) % active;
    }
131 132

    //  Detach the message from the data buffer.
133 134
    int rc = msg_->init ();
    errno_assert (rc == 0);
135 136 137 138 139 140

    return 0;
}

bool zmq::lb_t::has_out ()
{
141 142
    //  If one part of the message was already written we can definitely
    //  write the rest of the message.
143
    if (more)
144 145
        return true;

Martin Hurton's avatar
Martin Hurton committed
146
    while (active > 0) {
147

148 149
        //  Check whether a pipe has room for another message.
        if (pipes [current]->check_write ())
150
            return true;
Martin Hurton's avatar
Martin Hurton committed
151

152
        //  Deactivate the pipe.
Martin Hurton's avatar
Martin Hurton committed
153
        active--;
154 155
        pipes.swap (current, active);
        if (current == active)
156 157 158 159 160
            current = 0;
    }

    return false;
}