dist.cpp 6.34 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
zmq::dist_t::dist_t () : matching (0), active (0), eligible (0), more (false)
38 39 40 41 42 43 44 45
{
}

zmq::dist_t::~dist_t ()
{
    zmq_assert (pipes.empty ());
}

46
void zmq::dist_t::attach (pipe_t *pipe_)
47
{
48 49 50 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.
    if (more) {
        pipes.push_back (pipe_);
        pipes.swap (eligible, pipes.size () - 1);
        eligible++;
55
    } else {
56 57 58 59 60
        pipes.push_back (pipe_);
        pipes.swap (active, pipes.size () - 1);
        active++;
        eligible++;
    }
61 62
}

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

    //  If the pipe isn't eligible, ignore it.
    if (pipes.index (pipe_) >= eligible)
        return;

    //  Mark the pipe as matching.
    pipes.swap (pipes.index (pipe_), matching);
75
    matching++;
76 77
}

78 79 80 81 82
void zmq::dist_t::reverse_match ()
{
    pipes_t::size_type prev_matching = matching;

    // Reset matching to 0
83
    unmatch ();
84 85 86 87 88 89

    // 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.
    for (pipes_t::size_type i = prev_matching; i < eligible; ++i) {
90
        pipes.swap (i, matching++);
91 92 93
    }
}

94 95 96 97 98
void zmq::dist_t::unmatch ()
{
    matching = 0;
}

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

116 117 118
    pipes.erase (pipe_);
}

119
void zmq::dist_t::activated (pipe_t *pipe_)
120
{
121
    //  Move the pipe from passive to eligible state.
122 123 124 125
    if (eligible < pipes.size ()) {
        pipes.swap (pipes.index (pipe_), eligible);
        eligible++;
    }
126 127 128

    //  If there's no message being sent at the moment, move it to
    //  the active state.
129
    if (!more && active < pipes.size ()) {
130
        pipes.swap (eligible - 1, active);
131 132
        active++;
    }
133 134
}

135
int zmq::dist_t::send_to_all (msg_t *msg_)
136 137
{
    matching = active;
138
    return send_to_matching (msg_);
139 140
}

141
int zmq::dist_t::send_to_matching (msg_t *msg_)
142 143
{
    //  Is this end of a multipart message?
144
    bool msg_more = msg_->flags () & msg_t::more ? true : false;
145

146
    //  Push the message to matching pipes.
147
    distribute (msg_);
148

149
    //  If multipart message is fully sent, activate all the eligible pipes.
150 151
    if (!msg_more)
        active = eligible;
152 153 154 155 156 157

    more = msg_more;

    return 0;
}

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

169 170
    if (msg_->is_vsm ()) {
        for (pipes_t::size_type i = 0; i < matching; ++i)
171
            if (!write (pipes[i], msg_))
Douglas Young's avatar
Douglas Young committed
172
                --i; //  Retry last write because index will have been swapped
173
        int rc = msg_->close ();
174 175 176 177 178 179
        errno_assert (rc == 0);
        rc = msg_->init ();
        errno_assert (rc == 0);
        return;
    }

180
    //  Add matching-1 references to the message. We already hold one reference,
181
    //  that's why -1.
Martin Sustrik's avatar
Martin Sustrik committed
182
    msg_->add_refs ((int) matching - 1);
183

184
    //  Push copy of the message to each matching pipe.
185 186
    int failed = 0;
    for (pipes_t::size_type i = 0; i < matching; ++i)
187
        if (!write (pipes[i], msg_)) {
188
            ++failed;
Douglas Young's avatar
Douglas Young committed
189 190
            --i; //  Retry last write because index will have been swapped
        }
191 192
    if (unlikely (failed))
        msg_->rm_refs (failed);
193

194 195 196 197
    //  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.
    int rc = msg_->init ();
    errno_assert (rc == 0);
198 199 200 201 202 203 204
}

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

205
bool zmq::dist_t::write (pipe_t *pipe_, msg_t *msg_)
206 207
{
    if (!pipe_->write (msg_)) {
208 209
        pipes.swap (pipes.index (pipe_), matching - 1);
        matching--;
210
        pipes.swap (pipes.index (pipe_), active - 1);
211
        active--;
212 213
        pipes.swap (active, eligible - 1);
        eligible--;
214 215
        return false;
    }
216
    if (!(msg_->flags () & msg_t::more))
217 218 219 220
        pipe_->flush ();
    return true;
}

221 222
bool zmq::dist_t::check_hwm ()
{
Martin Hurton's avatar
Martin Hurton committed
223
    for (pipes_t::size_type i = 0; i < matching; ++i)
224
        if (!pipes[i]->check_hwm ())
Martin Hurton's avatar
Martin Hurton committed
225
            return false;
226

Martin Hurton's avatar
Martin Hurton committed
227
    return true;
228
}