lb.cpp 3.81 KB
Newer Older
1
/*
2 3
    Copyright (c) 2007-2011 iMatix Corporation
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
4 5 6 7

    This file is part of 0MQ.

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

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

#include "lb.hpp"
#include "pipe.hpp"
#include "err.hpp"
24
#include "msg.hpp"
25

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

zmq::lb_t::~lb_t ()
{
36
    zmq_assert (pipes.empty ());
37 38
}

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

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

55 56
    //  Remove the pipe from the list; adjust number of active pipes
    //  accordingly.
57
    if (index < active) {
58
        active--;
59 60 61
        if (current == active)
            current = 0;
    }
62
    pipes.erase (pipe_);
63 64
}

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

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

Martin Sustrik's avatar
Martin Sustrik committed
78
        more = msg_->flags () & (msg_t::more | msg_t::label) ? true : false;
79 80 81
        if (!more)
            dropping = false;

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

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

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

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

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

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

    return 0;
}

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

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

        //  Check whether zero-sized message can be written to the pipe.
134 135 136
        msg_t msg;
        int rc = msg.init ();
        errno_assert (rc == 0);
137
        if (pipes [current]->check_write (&msg)) {
138 139
            rc = msg.close ();
            errno_assert (rc == 0);
140
            return true;
141
        }
142 143
        rc = msg.close ();
        errno_assert (rc == 0);
Martin Hurton's avatar
Martin Hurton committed
144

145
        //  Deactivate the pipe.
Martin Hurton's avatar
Martin Hurton committed
146
        active--;
147 148
        pipes.swap (current, active);
        if (current == active)
149 150 151 152 153 154
            current = 0;
    }

    return false;
}