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

21
#include "../include/zmq.h"
22 23 24 25

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

28
zmq::lb_t::lb_t (own_t *sink_) :
29
    active (0),
30
    current (0),
31
    more (false),
32
    dropping (false),
33 34
    sink (sink_),
    terminating (false)
35 36 37 38 39
{
}

zmq::lb_t::~lb_t ()
{
40
    zmq_assert (pipes.empty ());
41 42 43 44
}

void zmq::lb_t::attach (writer_t *pipe_)
{
45 46
    pipe_->set_event_sink (this);

47 48 49
    pipes.push_back (pipe_);
    pipes.swap (active, pipes.size () - 1);
    active++;
50

51 52
    if (terminating) {
        sink->register_term_acks (1);
53
        pipe_->terminate ();
54
    }
55 56
}

57
void zmq::lb_t::terminate ()
58
{
59
    zmq_assert (!terminating);
60 61
    terminating = true;

62
    sink->register_term_acks (pipes.size ());
63 64 65 66 67 68
    for (pipes_t::size_type i = 0; i != pipes.size (); i++)
        pipes [i]->terminate ();
}

void zmq::lb_t::terminated (writer_t *pipe_)
{
69 70 71 72 73 74 75
    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;

76 77
    //  Remove the pipe from the list; adjust number of active pipes
    //  accordingly.
78
    if (index < active) {
79
        active--;
80 81 82
        if (current == active)
            current = 0;
    }
83 84
    pipes.erase (pipe_);

85 86
    if (terminating)
        sink->unregister_term_ack ();
87 88 89
}

void zmq::lb_t::activated (writer_t *pipe_)
90 91 92 93 94 95 96 97
{
    //  Move the pipe to the list of active pipes.
    pipes.swap (pipes.index (pipe_), active);
    active++;
}

int zmq::lb_t::send (zmq_msg_t *msg_, int flags_)
{
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    //  Drop the message if required. If we are at the end of the message
    //  switch back to non-dropping mode.
    if (dropping) {

        more = msg_->flags & ZMQ_MSG_MORE;
        if (!more)
            dropping = false;

        int rc = zmq_msg_close (msg_);
        errno_assert (rc == 0);
        rc = zmq_msg_init (msg_);
        zmq_assert (rc == 0);
        return 0;
    }

Martin Hurton's avatar
Martin Hurton committed
113
    while (active > 0) {
114
        if (pipes [current]->write (msg_)) {
115
            more = msg_->flags & ZMQ_MSG_MORE;
Martin Hurton's avatar
Martin Hurton committed
116
            break;
117
        }
Martin Hurton's avatar
Martin Hurton committed
118

119
        zmq_assert (!more);
Martin Hurton's avatar
Martin Hurton committed
120 121 122 123 124 125 126
        active--;
        if (current < active)
            pipes.swap (current, active);
        else
            current = 0;
    }

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

133 134
    //  If it's final part of the message we can fluch it downstream and
    //  continue round-robinning (load balance).
135
    if (!more) {
136 137 138
        pipes [current]->flush ();
        current = (current + 1) % active;
    }
139 140 141 142 143 144 145 146 147 148

    //  Detach the message from the data buffer.
    int rc = zmq_msg_init (msg_);
    zmq_assert (rc == 0);

    return 0;
}

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

Martin Hurton's avatar
Martin Hurton committed
154
    while (active > 0) {
155 156 157 158 159 160

        //  Check whether zero-sized message can be written to the pipe.
        zmq_msg_t msg;
        zmq_msg_init (&msg);
        if (pipes [current]->check_write (&msg)) {
            zmq_msg_close (&msg);
161
            return true;
162 163
        }
        zmq_msg_close (&msg);
Martin Hurton's avatar
Martin Hurton committed
164

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

    return false;
}