lb.cpp 3.61 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
3 4 5 6

    This file is part of 0MQ.

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

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

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

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

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

38
void zmq::lb_t::attach (pipe_t *pipe_)
39 40
{
    pipes.push_back (pipe_);
41
    activated (pipe_);
42 43
}

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

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

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

71
int zmq::lb_t::send (msg_t *msg_)
72 73 74 75 76
{
    return sendpipe (msg_, NULL);
}

int zmq::lb_t::sendpipe (msg_t *msg_, pipe_t **pipe_)
77
{
78 79 80 81
    //  Drop the message if required. If we are at the end of the message
    //  switch back to non-dropping mode.
    if (dropping) {

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

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

Martin Hurton's avatar
Martin Hurton committed
92
    while (active > 0) {
Martin Hurton's avatar
Martin Hurton committed
93
        if (pipes [current]->write (msg_))
94 95 96
        {
            if (pipe_)
                *pipe_ = pipes [current];
Martin Hurton's avatar
Martin Hurton committed
97
            break;
98
        }
Martin Hurton's avatar
Martin Hurton committed
99

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

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

114 115
    //  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
116
    more = msg_->flags () & msg_t::more? true: false;
117
    if (!more) {
118 119 120
        pipes [current]->flush ();
        current = (current + 1) % active;
    }
121 122

    //  Detach the message from the data buffer.
123 124
    int rc = msg_->init ();
    errno_assert (rc == 0);
125 126 127 128 129 130

    return 0;
}

bool zmq::lb_t::has_out ()
{
131 132
    //  If one part of the message was already written we can definitely
    //  write the rest of the message.
133
    if (more)
134 135
        return true;

Martin Hurton's avatar
Martin Hurton committed
136
    while (active > 0) {
137

138 139
        //  Check whether a pipe has room for another message.
        if (pipes [current]->check_write ())
140
            return true;
Martin Hurton's avatar
Martin Hurton committed
141

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

    return false;
}