2011-01-14 12:05:10 +01:00
|
|
|
/*
|
2011-03-02 16:30:40 +01:00
|
|
|
Copyright (c) 2007-2011 iMatix Corporation
|
|
|
|
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
|
2011-01-14 12:05:10 +01:00
|
|
|
|
|
|
|
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"
|
2011-04-12 14:20:49 +02:00
|
|
|
#include "msg.hpp"
|
2011-04-30 06:48:18 +02:00
|
|
|
#include "likely.hpp"
|
2011-01-14 12:05:10 +01:00
|
|
|
|
2011-05-23 20:30:01 +02:00
|
|
|
zmq::dist_t::dist_t () :
|
2011-01-14 12:05:10 +01:00
|
|
|
active (0),
|
2011-04-30 06:48:18 +02:00
|
|
|
eligible (0),
|
2011-05-23 20:30:01 +02:00
|
|
|
more (false)
|
2011-01-14 12:05:10 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq::dist_t::~dist_t ()
|
|
|
|
{
|
|
|
|
zmq_assert (pipes.empty ());
|
|
|
|
}
|
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
void zmq::dist_t::attach (pipe_t *pipe_)
|
2011-01-14 12:05:10 +01:00
|
|
|
{
|
2011-04-30 06:48:18 +02:00
|
|
|
// 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++;
|
|
|
|
}
|
2011-01-14 12:05:10 +01:00
|
|
|
}
|
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
void zmq::dist_t::terminated (pipe_t *pipe_)
|
2011-01-14 12:05:10 +01:00
|
|
|
{
|
2011-04-30 06:48:18 +02:00
|
|
|
// Remove the pipe from the list; adjust number of active and/or
|
|
|
|
// eligible pipes accordingly.
|
2011-01-14 12:05:10 +01:00
|
|
|
if (pipes.index (pipe_) < active)
|
|
|
|
active--;
|
2011-04-30 06:48:18 +02:00
|
|
|
if (pipes.index (pipe_) < eligible)
|
|
|
|
eligible--;
|
2011-01-14 12:05:10 +01:00
|
|
|
pipes.erase (pipe_);
|
|
|
|
}
|
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
void zmq::dist_t::activated (pipe_t *pipe_)
|
2011-01-14 12:05:10 +01:00
|
|
|
{
|
2011-05-02 19:26:30 +02:00
|
|
|
// 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);
|
2011-04-30 06:48:18 +02:00
|
|
|
active++;
|
|
|
|
}
|
2011-01-14 12:05:10 +01:00
|
|
|
}
|
|
|
|
|
2011-04-21 22:27:48 +02:00
|
|
|
int zmq::dist_t::send (msg_t *msg_, int flags_)
|
2011-03-20 11:50:51 +01:00
|
|
|
{
|
|
|
|
// Is this end of a multipart message?
|
2011-04-21 22:27:48 +02:00
|
|
|
bool msg_more = msg_->flags () & msg_t::more;
|
2011-03-20 11:50:51 +01:00
|
|
|
|
|
|
|
// Push the message to active pipes.
|
|
|
|
distribute (msg_, flags_);
|
|
|
|
|
2011-04-30 06:48:18 +02:00
|
|
|
// If mutlipart message is fully sent, activate all the eligible pipes.
|
|
|
|
if (!msg_more)
|
|
|
|
active = eligible;
|
2011-03-20 11:50:51 +01:00
|
|
|
|
|
|
|
more = msg_more;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-21 22:27:48 +02:00
|
|
|
void zmq::dist_t::distribute (msg_t *msg_, int flags_)
|
2011-01-14 12:05:10 +01:00
|
|
|
{
|
|
|
|
// If there are no active pipes available, simply drop the message.
|
|
|
|
if (active == 0) {
|
2011-04-21 22:27:48 +02:00
|
|
|
int rc = msg_->close ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
rc = msg_->init ();
|
2011-01-14 12:05:10 +01:00
|
|
|
zmq_assert (rc == 0);
|
2011-03-20 11:50:51 +01:00
|
|
|
return;
|
2011-01-14 12:05:10 +01:00
|
|
|
}
|
|
|
|
|
2011-04-21 22:27:48 +02:00
|
|
|
// Add active-1 references to the message. We already hold one reference,
|
|
|
|
// that's why -1.
|
|
|
|
msg_->add_refs (active - 1);
|
2011-01-14 12:05:10 +01:00
|
|
|
|
2011-04-21 22:27:48 +02:00
|
|
|
// Push copy of the message to each active pipe.
|
2011-01-14 12:05:10 +01:00
|
|
|
for (pipes_t::size_type i = 0; i < active;) {
|
|
|
|
if (!write (pipes [i], msg_))
|
2011-04-21 22:27:48 +02:00
|
|
|
msg_->rm_refs (1);
|
2011-01-14 12:05:10 +01:00
|
|
|
else
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2011-04-21 22:27:48 +02:00
|
|
|
// 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);
|
2011-01-14 12:05:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool zmq::dist_t::has_out ()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
bool zmq::dist_t::write (pipe_t *pipe_, msg_t *msg_)
|
2011-01-14 12:05:10 +01:00
|
|
|
{
|
|
|
|
if (!pipe_->write (msg_)) {
|
2011-04-30 06:48:18 +02:00
|
|
|
pipes.swap (pipes.index (pipe_), active - 1);
|
2011-01-14 12:05:10 +01:00
|
|
|
active--;
|
2011-04-30 06:48:18 +02:00
|
|
|
pipes.swap (active, eligible - 1);
|
|
|
|
eligible--;
|
2011-01-14 12:05:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
2011-04-21 22:27:48 +02:00
|
|
|
if (!(msg_->flags () & msg_t::more))
|
2011-01-14 12:05:10 +01:00
|
|
|
pipe_->flush ();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|