lb.cpp 3.56 KB
Newer Older
1
/*
2 3
    Copyright (c) 2010-2011 250bpm s.r.o.
    Copyright (c) 2007-2009 iMatix Corporation
4
    Copyright (c) 2011 VMware, Inc.
5
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
6 7 8 9

    This file is part of 0MQ.

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

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

#include "lb.hpp"
#include "pipe.hpp"
#include "err.hpp"
26
#include "msg.hpp"
27

28
zmq::lb_t::lb_t () :
29
    active (0),
30
    current (0),
31
    more (false),
32
    dropping (false)
33 34 35 36 37
{
}

zmq::lb_t::~lb_t ()
{
38
    zmq_assert (pipes.empty ());
39 40
}

41
void zmq::lb_t::attach (pipe_t *pipe_)
42 43
{
    pipes.push_back (pipe_);
44
    activated (pipe_);
45 46
}

47
void zmq::lb_t::terminated (pipe_t *pipe_)
48
{
49 50 51 52 53 54 55
    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;

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
}

67
void zmq::lb_t::activated (pipe_t *pipe_)
68 69 70 71 72 73
{
    //  Move the pipe to the list of active pipes.
    pipes.swap (pipes.index (pipe_), active);
    active++;
}

74
int zmq::lb_t::send (msg_t *msg_)
75
{
76 77 78 79
    //  Drop the message if required. If we are at the end of the message
    //  switch back to non-dropping mode.
    if (dropping) {

80
        more = msg_->flags () & msg_t::more ? true : false;
Martin Hurton's avatar
Martin Hurton committed
81
        dropping = more;
82

83
        int rc = msg_->close ();
84
        errno_assert (rc == 0);
85
        rc = msg_->init ();
86
        errno_assert (rc == 0);
87 88 89
        return 0;
    }

Martin Hurton's avatar
Martin Hurton committed
90
    while (active > 0) {
Martin Hurton's avatar
Martin Hurton committed
91
        if (pipes [current]->write (msg_))
Martin Hurton's avatar
Martin Hurton committed
92 93
            break;

94
        zmq_assert (!more);
Martin Hurton's avatar
Martin Hurton committed
95 96 97 98 99 100 101
        active--;
        if (current < active)
            pipes.swap (current, active);
        else
            current = 0;
    }

102
    //  If there are no pipes we cannot send the message.
Martin Hurton's avatar
Martin Hurton committed
103
    if (active == 0) {
104 105 106 107
        errno = EAGAIN;
        return -1;
    }

108 109
    //  If it's final part of the message we can fluch it downstream and
    //  continue round-robinning (load balance).
Martin Hurton's avatar
Martin Hurton committed
110
    more = msg_->flags () & msg_t::more? true: false;
111
    if (!more) {
112 113 114
        pipes [current]->flush ();
        current = (current + 1) % active;
    }
115 116

    //  Detach the message from the data buffer.
117 118
    int rc = msg_->init ();
    errno_assert (rc == 0);
119 120 121 122 123 124

    return 0;
}

bool zmq::lb_t::has_out ()
{
125 126
    //  If one part of the message was already written we can definitely
    //  write the rest of the message.
127
    if (more)
128 129
        return true;

Martin Hurton's avatar
Martin Hurton committed
130
    while (active > 0) {
131

132 133
        //  Check whether a pipe has room for another message.
        if (pipes [current]->check_write ())
134
            return true;
Martin Hurton's avatar
Martin Hurton committed
135

136
        //  Deactivate the pipe.
Martin Hurton's avatar
Martin Hurton committed
137
        active--;
138 139
        pipes.swap (current, active);
        if (current == active)
140 141 142 143 144 145
            current = 0;
    }

    return false;
}