dist.cpp 5.18 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

    This file is part of 0MQ.

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

    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/>.
*/

#include "dist.hpp"
#include "pipe.hpp"
#include "err.hpp"
23
#include "msg.hpp"
24
#include "likely.hpp"
25

26
zmq::dist_t::dist_t () :
27
    matching (0),
28
    active (0),
29
    eligible (0),
30
    more (false)
31 32 33 34 35 36 37 38
{
}

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

39
void zmq::dist_t::attach (pipe_t *pipe_)
40
{
41 42 43 44 45 46 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++;
    }
    else {
        pipes.push_back (pipe_);
        pipes.swap (active, pipes.size () - 1);
        active++;
        eligible++;
    }
55 56
}

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
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);
    matching++;    
}

72 73 74 75 76
void zmq::dist_t::unmatch ()
{
    matching = 0;
}

77
void zmq::dist_t::pipe_terminated (pipe_t *pipe_)
78
{
79
    //  Remove the pipe from the list; adjust number of matching, active and/or
80
    //  eligible pipes accordingly.
ganesh.vr's avatar
ganesh.vr committed
81 82
    if (pipes.index (pipe_) < matching) {
        pipes.swap (pipes.index (pipe_), matching - 1);
83
        matching--;
ganesh.vr's avatar
ganesh.vr committed
84 85 86
    }
    if (pipes.index (pipe_) < active) {
        pipes.swap (pipes.index (pipe_), active - 1);
87
        active--;
ganesh.vr's avatar
ganesh.vr committed
88 89 90
    }
    if (pipes.index (pipe_) < eligible) {
        pipes.swap (pipes.index (pipe_), eligible - 1);
91
        eligible--;
ganesh.vr's avatar
ganesh.vr committed
92 93
    }

94 95 96
    pipes.erase (pipe_);
}

97
void zmq::dist_t::activated (pipe_t *pipe_)
98
{
99 100 101 102 103 104 105 106
    //  Move the pipe from passive to eligible state.
    pipes.swap (pipes.index (pipe_), eligible);
    eligible++;

    //  If there's no message being sent at the moment, move it to
    //  the active state.
    if (!more) {
        pipes.swap (eligible - 1, active);
107 108
        active++;
    }
109 110
}

111
int zmq::dist_t::send_to_all (msg_t *msg_)
112 113
{
    matching = active;
114
    return send_to_matching (msg_);
115 116
}

117
int zmq::dist_t::send_to_matching (msg_t *msg_)
118 119
{
    //  Is this end of a multipart message?
120
    bool msg_more = msg_->flags () & msg_t::more ? true : false;
121

122
    //  Push the message to matching pipes.
123
    distribute (msg_);
124

125 126 127
    //  If mutlipart message is fully sent, activate all the eligible pipes.
    if (!msg_more)
        active = eligible;
128 129 130 131 132 133

    more = msg_more;

    return 0;
}

134
void zmq::dist_t::distribute (msg_t *msg_)
135
{
136 137
    //  If there are no matching pipes available, simply drop the message.
    if (matching == 0) {
138 139 140
        int rc = msg_->close ();
        errno_assert (rc == 0);
        rc = msg_->init ();
141
        errno_assert (rc == 0);
142
        return;
143 144
    }

145 146
    if (msg_->is_vsm ()) {
        for (pipes_t::size_type i = 0; i < matching; ++i)
Douglas Young's avatar
Douglas Young committed
147 148
            if(!write (pipes [i], msg_))
                --i; //  Retry last write because index will have been swapped
149 150 151 152 153 154 155
        int rc = msg_->close();
        errno_assert (rc == 0);
        rc = msg_->init ();
        errno_assert (rc == 0);
        return;
    }

156
    //  Add matching-1 references to the message. We already hold one reference,
157
    //  that's why -1.
Martin Sustrik's avatar
Martin Sustrik committed
158
    msg_->add_refs ((int) matching - 1);
159

160
    //  Push copy of the message to each matching pipe.
161 162
    int failed = 0;
    for (pipes_t::size_type i = 0; i < matching; ++i)
Douglas Young's avatar
Douglas Young committed
163
        if (!write (pipes [i], msg_)) {
164
            ++failed;
Douglas Young's avatar
Douglas Young committed
165 166
            --i; //  Retry last write because index will have been swapped
        }
167 168
    if (unlikely (failed))
        msg_->rm_refs (failed);
169

170 171 172 173
    //  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);
174 175 176 177 178 179 180
}

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

181
bool zmq::dist_t::write (pipe_t *pipe_, msg_t *msg_)
182 183
{
    if (!pipe_->write (msg_)) {
184 185
        pipes.swap (pipes.index (pipe_), matching - 1);
        matching--;
186
        pipes.swap (pipes.index (pipe_), active - 1);
187
        active--;
188 189
        pipes.swap (active, eligible - 1);
        eligible--;
190 191
        return false;
    }
192
    if (!(msg_->flags () & msg_t::more))
193 194 195 196
        pipe_->flush ();
    return true;
}

197 198
bool zmq::dist_t::check_hwm ()
{
Martin Hurton's avatar
Martin Hurton committed
199 200 201
    for (pipes_t::size_type i = 0; i < matching; ++i)
        if (!pipes [i]->check_hwm ())
            return false;
202

Martin Hurton's avatar
Martin Hurton committed
203
    return true;
204 205 206
}