2011-01-14 12:05:10 +01:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2011-01-14 12:05:10 +01:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2011-01-14 12:05:10 +01:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
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
|
2011-01-14 12:05:10 +01:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
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.
|
2011-01-14 12:05:10 +01:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2016-02-18 10:56:52 -06:00
|
|
|
#include "precompiled.hpp"
|
2011-01-14 12:05:10 +01:00
|
|
|
#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
|
|
|
|
2018-05-27 11:10:39 +02:00
|
|
|
zmq::dist_t::dist_t () :
|
|
|
|
_matching (0),
|
|
|
|
_active (0),
|
|
|
|
_eligible (0),
|
|
|
|
_more (false)
|
2011-01-14 12:05:10 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq::dist_t::~dist_t ()
|
|
|
|
{
|
2018-05-27 11:10:39 +02:00
|
|
|
zmq_assert (_pipes.empty ());
|
2011-01-14 12:05:10 +01:00
|
|
|
}
|
|
|
|
|
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.
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_more) {
|
|
|
|
_pipes.push_back (pipe_);
|
|
|
|
_pipes.swap (_eligible, _pipes.size () - 1);
|
|
|
|
_eligible++;
|
2018-02-01 11:46:09 +01:00
|
|
|
} else {
|
2018-05-27 11:10:39 +02:00
|
|
|
_pipes.push_back (pipe_);
|
|
|
|
_pipes.swap (_active, _pipes.size () - 1);
|
|
|
|
_active++;
|
|
|
|
_eligible++;
|
2011-04-30 06:48:18 +02:00
|
|
|
}
|
2011-01-14 12:05:10 +01:00
|
|
|
}
|
|
|
|
|
2011-06-11 20:29:56 +02:00
|
|
|
void zmq::dist_t::match (pipe_t *pipe_)
|
|
|
|
{
|
|
|
|
// If pipe is already matching do nothing.
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_pipes.index (pipe_) < _matching)
|
2011-06-11 20:29:56 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// If the pipe isn't eligible, ignore it.
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_pipes.index (pipe_) >= _eligible)
|
2011-06-11 20:29:56 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Mark the pipe as matching.
|
2018-05-27 11:10:39 +02:00
|
|
|
_pipes.swap (_pipes.index (pipe_), _matching);
|
|
|
|
_matching++;
|
2011-06-11 20:29:56 +02:00
|
|
|
}
|
|
|
|
|
2015-01-26 15:59:19 +01:00
|
|
|
void zmq::dist_t::reverse_match ()
|
|
|
|
{
|
2019-12-25 13:51:21 +01:00
|
|
|
const pipes_t::size_type prev_matching = _matching;
|
2015-01-26 15:59:19 +01:00
|
|
|
|
|
|
|
// Reset matching to 0
|
2018-02-01 11:46:09 +01:00
|
|
|
unmatch ();
|
2015-01-26 15:59:19 +01:00
|
|
|
|
|
|
|
// 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.
|
2018-05-27 11:10:39 +02:00
|
|
|
for (pipes_t::size_type i = prev_matching; i < _eligible; ++i) {
|
|
|
|
_pipes.swap (i, _matching++);
|
2015-01-26 15:59:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-12 10:19:21 +02:00
|
|
|
void zmq::dist_t::unmatch ()
|
|
|
|
{
|
2018-05-27 11:10:39 +02:00
|
|
|
_matching = 0;
|
2011-06-12 10:19:21 +02:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:49:24 +02:00
|
|
|
void zmq::dist_t::pipe_terminated (pipe_t *pipe_)
|
2011-01-14 12:05:10 +01:00
|
|
|
{
|
2011-06-11 20:29:56 +02:00
|
|
|
// Remove the pipe from the list; adjust number of matching, active and/or
|
2011-04-30 06:48:18 +02:00
|
|
|
// eligible pipes accordingly.
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_pipes.index (pipe_) < _matching) {
|
|
|
|
_pipes.swap (_pipes.index (pipe_), _matching - 1);
|
|
|
|
_matching--;
|
2013-07-01 15:57:06 +09:00
|
|
|
}
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_pipes.index (pipe_) < _active) {
|
|
|
|
_pipes.swap (_pipes.index (pipe_), _active - 1);
|
|
|
|
_active--;
|
2013-07-01 15:57:06 +09:00
|
|
|
}
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_pipes.index (pipe_) < _eligible) {
|
|
|
|
_pipes.swap (_pipes.index (pipe_), _eligible - 1);
|
|
|
|
_eligible--;
|
2013-07-01 15:57:06 +09:00
|
|
|
}
|
|
|
|
|
2018-05-27 11:10:39 +02:00
|
|
|
_pipes.erase (pipe_);
|
2011-01-14 12:05:10 +01:00
|
|
|
}
|
|
|
|
|
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.
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_eligible < _pipes.size ()) {
|
|
|
|
_pipes.swap (_pipes.index (pipe_), _eligible);
|
|
|
|
_eligible++;
|
2016-04-02 09:08:40 -07:00
|
|
|
}
|
2011-05-02 19:26:30 +02:00
|
|
|
|
|
|
|
// If there's no message being sent at the moment, move it to
|
|
|
|
// the active state.
|
2018-05-27 11:10:39 +02:00
|
|
|
if (!_more && _active < _pipes.size ()) {
|
|
|
|
_pipes.swap (_eligible - 1, _active);
|
|
|
|
_active++;
|
2011-04-30 06:48:18 +02:00
|
|
|
}
|
2011-01-14 12:05:10 +01:00
|
|
|
}
|
|
|
|
|
2012-11-09 17:08:03 +01:00
|
|
|
int zmq::dist_t::send_to_all (msg_t *msg_)
|
2011-06-11 20:29:56 +02:00
|
|
|
{
|
2018-05-27 11:10:39 +02:00
|
|
|
_matching = _active;
|
2012-11-09 17:08:03 +01:00
|
|
|
return send_to_matching (msg_);
|
2011-06-11 20:29:56 +02:00
|
|
|
}
|
|
|
|
|
2012-11-09 17:08:03 +01:00
|
|
|
int zmq::dist_t::send_to_matching (msg_t *msg_)
|
2011-03-20 11:50:51 +01:00
|
|
|
{
|
|
|
|
// Is this end of a multipart message?
|
2019-12-25 13:51:21 +01:00
|
|
|
const bool msg_more = (msg_->flags () & msg_t::more) != 0;
|
2011-03-20 11:50:51 +01:00
|
|
|
|
2011-06-11 20:29:56 +02:00
|
|
|
// Push the message to matching pipes.
|
2012-11-09 17:08:03 +01:00
|
|
|
distribute (msg_);
|
2011-03-20 11:50:51 +01:00
|
|
|
|
2015-09-06 18:46:32 +02:00
|
|
|
// If multipart message is fully sent, activate all the eligible pipes.
|
2011-04-30 06:48:18 +02:00
|
|
|
if (!msg_more)
|
2018-05-27 11:10:39 +02:00
|
|
|
_active = _eligible;
|
2011-03-20 11:50:51 +01:00
|
|
|
|
2018-05-27 11:10:39 +02:00
|
|
|
_more = msg_more;
|
2011-03-20 11:50:51 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-09 17:08:03 +01:00
|
|
|
void zmq::dist_t::distribute (msg_t *msg_)
|
2011-01-14 12:05:10 +01:00
|
|
|
{
|
2011-06-11 20:29:56 +02:00
|
|
|
// If there are no matching pipes available, simply drop the message.
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_matching == 0) {
|
2011-04-21 22:27:48 +02:00
|
|
|
int rc = msg_->close ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
rc = msg_->init ();
|
2012-05-28 23:13:09 +02:00
|
|
|
errno_assert (rc == 0);
|
2011-03-20 11:50:51 +01:00
|
|
|
return;
|
2011-01-14 12:05:10 +01:00
|
|
|
}
|
|
|
|
|
2011-10-27 13:59:54 +02:00
|
|
|
if (msg_->is_vsm ()) {
|
2019-12-23 12:02:32 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2018-02-01 11:46:09 +01:00
|
|
|
int rc = msg_->close ();
|
2011-10-27 13:59:54 +02:00
|
|
|
errno_assert (rc == 0);
|
|
|
|
rc = msg_->init ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-11 20:29:56 +02:00
|
|
|
// Add matching-1 references to the message. We already hold one reference,
|
2011-04-21 22:27:48 +02:00
|
|
|
// that's why -1.
|
2018-05-27 11:10:39 +02:00
|
|
|
msg_->add_refs (static_cast<int> (_matching) - 1);
|
2011-01-14 12:05:10 +01:00
|
|
|
|
2011-06-11 20:29:56 +02:00
|
|
|
// Push copy of the message to each matching pipe.
|
2011-09-14 15:16:48 +02:00
|
|
|
int failed = 0;
|
2019-12-23 12:02:32 +01:00
|
|
|
for (pipes_t::size_type i = 0; i < _matching;) {
|
2018-05-27 11:10:39 +02:00
|
|
|
if (!write (_pipes[i], msg_)) {
|
2011-09-14 15:16:48 +02:00
|
|
|
++failed;
|
2019-12-23 12:02:32 +01:00
|
|
|
// Use same index again because entry will have been removed.
|
|
|
|
} else {
|
|
|
|
++i;
|
2012-03-25 17:50:55 +01:00
|
|
|
}
|
2019-12-23 12:02:32 +01:00
|
|
|
}
|
2011-09-14 15:16:48 +02:00
|
|
|
if (unlikely (failed))
|
|
|
|
msg_->rm_refs (failed);
|
2011-01-14 12:05:10 +01:00
|
|
|
|
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.
|
2019-12-25 13:51:21 +01:00
|
|
|
const int rc = msg_->init ();
|
2011-04-21 22:27:48 +02:00
|
|
|
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_)) {
|
2018-05-27 11:10:39 +02:00
|
|
|
_pipes.swap (_pipes.index (pipe_), _matching - 1);
|
|
|
|
_matching--;
|
|
|
|
_pipes.swap (_pipes.index (pipe_), _active - 1);
|
|
|
|
_active--;
|
|
|
|
_pipes.swap (_active, _eligible - 1);
|
|
|
|
_eligible--;
|
2011-01-14 12:05:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
2011-11-01 13:39:54 +01:00
|
|
|
if (!(msg_->flags () & msg_t::more))
|
2011-01-14 12:05:10 +01:00
|
|
|
pipe_->flush ();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-08 19:36:00 +02:00
|
|
|
bool zmq::dist_t::check_hwm ()
|
|
|
|
{
|
2018-05-27 11:10:39 +02:00
|
|
|
for (pipes_t::size_type i = 0; i < _matching; ++i)
|
|
|
|
if (!_pipes[i]->check_hwm ())
|
2014-08-12 09:31:19 +02:00
|
|
|
return false;
|
2014-08-08 19:36:00 +02:00
|
|
|
|
2014-08-12 09:31:19 +02:00
|
|
|
return true;
|
2014-08-08 19:36:00 +02:00
|
|
|
}
|