dist.cpp 4.94 KB
Newer Older
1
/*
2
    Copyright (c) 2011 250bpm s.r.o.
3
    Copyright (c) 2011 VMware, Inc.
4
    Copyright (c) 2011 Other contributors as noted in the AUTHORS file
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

    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"
25
#include "msg.hpp"
26
#include "likely.hpp"
27

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

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

41
void zmq::dist_t::attach (pipe_t *pipe_)
42
{
43 44 45 46 47 48 49 50 51 52 53 54 55 56
    //  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++;
    }
57 58
}

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

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

79
void zmq::dist_t::terminated (pipe_t *pipe_)
80
{
81
    //  Remove the pipe from the list; adjust number of matching, active and/or
82
    //  eligible pipes accordingly.
83 84
    if (pipes.index (pipe_) < matching)
        matching--;
85 86
    if (pipes.index (pipe_) < active)
        active--;
87 88
    if (pipes.index (pipe_) < eligible)
        eligible--;
89 90 91
    pipes.erase (pipe_);
}

92
void zmq::dist_t::activated (pipe_t *pipe_)
93
{
94 95 96 97 98 99 100 101
    //  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);
102 103
        active++;
    }
104 105
}

106 107 108 109 110 111 112
int zmq::dist_t::send_to_all (msg_t *msg_, int flags_)
{
    matching = active;
    return send_to_matching (msg_, flags_);
}

int zmq::dist_t::send_to_matching (msg_t *msg_, int flags_)
113 114
{
    //  Is this end of a multipart message?
115
    bool msg_more = msg_->flags () & msg_t::more ? true : false;
116

117
    //  Push the message to matching pipes.
118 119
    distribute (msg_, flags_);

120 121 122
    //  If mutlipart message is fully sent, activate all the eligible pipes.
    if (!msg_more)
        active = eligible;
123 124 125 126 127 128

    more = msg_more;

    return 0;
}

129
void zmq::dist_t::distribute (msg_t *msg_, int flags_)
130
{
131 132
    //  If there are no matching pipes available, simply drop the message.
    if (matching == 0) {
133 134 135
        int rc = msg_->close ();
        errno_assert (rc == 0);
        rc = msg_->init ();
136
        zmq_assert (rc == 0);
137
        return;
138 139
    }

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

151
    //  Add matching-1 references to the message. We already hold one reference,
152
    //  that's why -1.
Martin Sustrik's avatar
Martin Sustrik committed
153
    msg_->add_refs ((int) matching - 1);
154

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

165 166 167 168
    //  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);
169 170 171 172 173 174 175
}

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

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