dist.cpp 6.51 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
5

6 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq 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 GNU Lesser General Public
    License for more details.
25 26 27 28 29

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

30
#include "precompiled.hpp"
31 32 33
#include "dist.hpp"
#include "pipe.hpp"
#include "err.hpp"
34
#include "msg.hpp"
35
#include "likely.hpp"
36

37 38 39 40 41
zmq::dist_t::dist_t () :
    _matching (0),
    _active (0),
    _eligible (0),
    _more (false)
42 43 44 45 46
{
}

zmq::dist_t::~dist_t ()
{
47
    zmq_assert (_pipes.empty ());
48 49
}

50
void zmq::dist_t::attach (pipe_t *pipe_)
51
{
52 53 54
    //  If we are in the middle of sending a message, we'll add new pipe
    //  into the list of eligible pipes. Otherwise we add it to the list
    //  of active pipes.
55 56 57 58
    if (_more) {
        _pipes.push_back (pipe_);
        _pipes.swap (_eligible, _pipes.size () - 1);
        _eligible++;
59
    } else {
60 61 62 63
        _pipes.push_back (pipe_);
        _pipes.swap (_active, _pipes.size () - 1);
        _active++;
        _eligible++;
64
    }
65 66
}

67 68 69
void zmq::dist_t::match (pipe_t *pipe_)
{
    //  If pipe is already matching do nothing.
70
    if (_pipes.index (pipe_) < _matching)
71 72 73
        return;

    //  If the pipe isn't eligible, ignore it.
74
    if (_pipes.index (pipe_) >= _eligible)
75 76 77
        return;

    //  Mark the pipe as matching.
78 79
    _pipes.swap (_pipes.index (pipe_), _matching);
    _matching++;
80 81
}

82 83
void zmq::dist_t::reverse_match ()
{
84
    const pipes_t::size_type prev_matching = _matching;
85 86

    // Reset matching to 0
87
    unmatch ();
88 89 90 91 92

    // Mark all matching pipes as not matching and vice-versa.
    // To do this, push all pipes that are eligible but not
    // matched - i.e. between "matching" and "eligible" -
    // to the beginning of the queue.
93 94
    for (pipes_t::size_type i = prev_matching; i < _eligible; ++i) {
        _pipes.swap (i, _matching++);
95 96 97
    }
}

98 99
void zmq::dist_t::unmatch ()
{
100
    _matching = 0;
101 102
}

103
void zmq::dist_t::pipe_terminated (pipe_t *pipe_)
104
{
105
    //  Remove the pipe from the list; adjust number of matching, active and/or
106
    //  eligible pipes accordingly.
107 108 109
    if (_pipes.index (pipe_) < _matching) {
        _pipes.swap (_pipes.index (pipe_), _matching - 1);
        _matching--;
ganesh.vr's avatar
ganesh.vr committed
110
    }
111 112 113
    if (_pipes.index (pipe_) < _active) {
        _pipes.swap (_pipes.index (pipe_), _active - 1);
        _active--;
ganesh.vr's avatar
ganesh.vr committed
114
    }
115 116 117
    if (_pipes.index (pipe_) < _eligible) {
        _pipes.swap (_pipes.index (pipe_), _eligible - 1);
        _eligible--;
ganesh.vr's avatar
ganesh.vr committed
118 119
    }

120
    _pipes.erase (pipe_);
121 122
}

123
void zmq::dist_t::activated (pipe_t *pipe_)
124
{
125
    //  Move the pipe from passive to eligible state.
126 127 128
    if (_eligible < _pipes.size ()) {
        _pipes.swap (_pipes.index (pipe_), _eligible);
        _eligible++;
129
    }
130 131 132

    //  If there's no message being sent at the moment, move it to
    //  the active state.
133 134 135
    if (!_more && _active < _pipes.size ()) {
        _pipes.swap (_eligible - 1, _active);
        _active++;
136
    }
137 138
}

139
int zmq::dist_t::send_to_all (msg_t *msg_)
140
{
141
    _matching = _active;
142
    return send_to_matching (msg_);
143 144
}

145
int zmq::dist_t::send_to_matching (msg_t *msg_)
146 147
{
    //  Is this end of a multipart message?
148
    const bool msg_more = (msg_->flags () & msg_t::more) != 0;
149

150
    //  Push the message to matching pipes.
151
    distribute (msg_);
152

153
    //  If multipart message is fully sent, activate all the eligible pipes.
154
    if (!msg_more)
155
        _active = _eligible;
156

157
    _more = msg_more;
158 159 160 161

    return 0;
}

162
void zmq::dist_t::distribute (msg_t *msg_)
163
{
164
    //  If there are no matching pipes available, simply drop the message.
165
    if (_matching == 0) {
166 167 168
        int rc = msg_->close ();
        errno_assert (rc == 0);
        rc = msg_->init ();
169
        errno_assert (rc == 0);
170
        return;
171 172
    }

173
    if (msg_->is_vsm ()) {
174 175 176 177 178 179 180
        for (pipes_t::size_type i = 0; i < _matching;) {
            if (!write (_pipes[i], msg_)) {
                //  Use same index again because entry will have been removed.
            } else {
                ++i;
            }
        }
181
        int rc = msg_->init ();
182 183 184 185
        errno_assert (rc == 0);
        return;
    }

186
    //  Add matching-1 references to the message. We already hold one reference,
187
    //  that's why -1.
188
    msg_->add_refs (static_cast<int> (_matching) - 1);
189

190
    //  Push copy of the message to each matching pipe.
191
    int failed = 0;
192
    for (pipes_t::size_type i = 0; i < _matching;) {
193
        if (!write (_pipes[i], msg_)) {
194
            ++failed;
195 196 197
            //  Use same index again because entry will have been removed.
        } else {
            ++i;
Douglas Young's avatar
Douglas Young committed
198
        }
199
    }
200 201
    if (unlikely (failed))
        msg_->rm_refs (failed);
202

203 204
    //  Detach the original message from the data buffer. Note that we don't
    //  close the message. That's because we've already used all the references.
205
    const int rc = msg_->init ();
206
    errno_assert (rc == 0);
207 208 209 210 211 212 213
}

bool zmq::dist_t::has_out ()
{
    return true;
}

214
bool zmq::dist_t::write (pipe_t *pipe_, msg_t *msg_)
215 216
{
    if (!pipe_->write (msg_)) {
217 218 219 220 221 222
        _pipes.swap (_pipes.index (pipe_), _matching - 1);
        _matching--;
        _pipes.swap (_pipes.index (pipe_), _active - 1);
        _active--;
        _pipes.swap (_active, _eligible - 1);
        _eligible--;
223 224
        return false;
    }
225
    if (!(msg_->flags () & msg_t::more))
226 227 228 229
        pipe_->flush ();
    return true;
}

230 231
bool zmq::dist_t::check_hwm ()
{
232 233
    for (pipes_t::size_type i = 0; i < _matching; ++i)
        if (!_pipes[i]->check_hwm ())
Martin Hurton's avatar
Martin Hurton committed
234
            return false;
235

Martin Hurton's avatar
Martin Hurton committed
236
    return true;
237
}