lb.cpp 4.56 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
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
29

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

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

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

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

55
void zmq::lb_t::pipe_terminated (pipe_t *pipe_)
56
{
57 58 59 60 61 62 63
    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;

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

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

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

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

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

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

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

111 112 113 114 115 116 117 118 119 120 121
        // If send fails for multi-part msg rollback other
        // parts sent earlier and return EAGAIN.
        // Application should handle this as suitable 
        if (more)
        {
            pipes [current]->rollback ();
            more = 0;
            errno = EAGAIN;
            return -1;
        }

Martin Hurton's avatar
Martin Hurton committed
122 123 124 125 126 127 128
        active--;
        if (current < active)
            pipes.swap (current, active);
        else
            current = 0;
    }

129
    //  If there are no pipes we cannot send the message.
Martin Hurton's avatar
Martin Hurton committed
130
    if (active == 0) {
131 132 133 134
        errno = EAGAIN;
        return -1;
    }

135 136
    //  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
137
    more = msg_->flags () & msg_t::more? true: false;
138
    if (!more) {
139 140 141
        pipes [current]->flush ();
        current = (current + 1) % active;
    }
142 143

    //  Detach the message from the data buffer.
144 145
    int rc = msg_->init ();
    errno_assert (rc == 0);
146 147 148 149 150 151

    return 0;
}

bool zmq::lb_t::has_out ()
{
152 153
    //  If one part of the message was already written we can definitely
    //  write the rest of the message.
154
    if (more)
155 156
        return true;

Martin Hurton's avatar
Martin Hurton committed
157
    while (active > 0) {
158

159 160
        //  Check whether a pipe has room for another message.
        if (pipes [current]->check_write ())
161
            return true;
Martin Hurton's avatar
Martin Hurton committed
162

163
        //  Deactivate the pipe.
Martin Hurton's avatar
Martin Hurton committed
164
        active--;
165 166
        pipes.swap (current, active);
        if (current == active)
167 168 169 170 171
            current = 0;
    }

    return false;
}