lb.cpp 3.61 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 44 45
{
    pipes.push_back (pipe_);
    pipes.swap (active, pipes.size () - 1);
    active++;
46 47
}

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

57 58
    //  Remove the pipe from the list; adjust number of active pipes
    //  accordingly.
59
    if (index < active) {
60
        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_, int flags_)
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;
81 82 83
        if (!more)
            dropping = false;

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

Martin Hurton's avatar
Martin Hurton committed
91
    while (active > 0) {
92
        if (pipes [current]->write (msg_)) {
93
            more = msg_->flags () & msg_t::more ? true : false;
Martin Hurton's avatar
Martin Hurton committed
94
            break;
95
        }
Martin Hurton's avatar
Martin Hurton committed
96

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

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

111 112
    //  If it's final part of the message we can fluch it downstream and
    //  continue round-robinning (load balance).
113
    if (!more) {
114 115 116
        pipes [current]->flush ();
        current = (current + 1) % active;
    }
117 118

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

    return 0;
}

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

Martin Hurton's avatar
Martin Hurton committed
132
    while (active > 0) {
133

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

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

    return false;
}