2009-12-15 09:09:19 +01:00
|
|
|
/*
|
2015-01-22 10:32:06 +01:00
|
|
|
Copyright (c) 2007-2015 Contributors as noted in the AUTHORS file
|
2009-12-15 09:09:19 +01:00
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
2010-10-30 15:08:28 +02:00
|
|
|
the terms of the GNU Lesser General Public License as published by
|
2009-12-15 09:09:19 +01:00
|
|
|
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
|
2010-10-30 15:08:28 +02:00
|
|
|
GNU Lesser General Public License for more details.
|
2009-12-15 09:09:19 +01:00
|
|
|
|
2010-10-30 15:08:28 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2009-12-15 09:09:19 +01:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2014-05-02 16:50:16 +02:00
|
|
|
|
2009-12-15 09:09:19 +01:00
|
|
|
#include "lb.hpp"
|
|
|
|
#include "pipe.hpp"
|
|
|
|
#include "err.hpp"
|
2011-04-21 22:27:48 +02:00
|
|
|
#include "msg.hpp"
|
2009-12-15 09:09:19 +01:00
|
|
|
|
2011-05-23 20:30:01 +02:00
|
|
|
zmq::lb_t::lb_t () :
|
2014-05-02 16:50:16 +02:00
|
|
|
active (0),
|
2010-03-27 09:43:49 +01:00
|
|
|
current (0),
|
2010-08-11 14:09:56 +02:00
|
|
|
more (false),
|
2011-05-23 20:30:01 +02:00
|
|
|
dropping (false)
|
2009-12-15 09:09:19 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq::lb_t::~lb_t ()
|
|
|
|
{
|
2010-08-06 17:49:37 +02:00
|
|
|
zmq_assert (pipes.empty ());
|
2009-12-15 09:09:19 +01:00
|
|
|
}
|
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
void zmq::lb_t::attach (pipe_t *pipe_)
|
2009-12-15 09:09:19 +01:00
|
|
|
{
|
|
|
|
pipes.push_back (pipe_);
|
2012-06-12 15:34:48 +01:00
|
|
|
activated (pipe_);
|
2010-08-06 17:49:37 +02:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:49:24 +02:00
|
|
|
void zmq::lb_t::pipe_terminated (pipe_t *pipe_)
|
2010-08-06 17:49:37 +02:00
|
|
|
{
|
2011-03-20 20:52:54 +01:00
|
|
|
pipes_t::size_type index = pipes.index (pipe_);
|
|
|
|
|
|
|
|
// If we are in the middle of multipart message and current pipe
|
|
|
|
// have disconnected, we have to drop the remainder of the message.
|
|
|
|
if (index == current && more)
|
|
|
|
dropping = true;
|
|
|
|
|
2009-12-15 09:09:19 +01:00
|
|
|
// Remove the pipe from the list; adjust number of active pipes
|
|
|
|
// accordingly.
|
2011-03-20 20:52:54 +01:00
|
|
|
if (index < active) {
|
2009-12-15 09:09:19 +01:00
|
|
|
active--;
|
2012-05-31 15:34:30 +02:00
|
|
|
pipes.swap (index, active);
|
2010-02-10 12:48:04 +01:00
|
|
|
if (current == active)
|
|
|
|
current = 0;
|
|
|
|
}
|
2009-12-15 09:09:19 +01:00
|
|
|
pipes.erase (pipe_);
|
2010-08-06 17:49:37 +02:00
|
|
|
}
|
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
void zmq::lb_t::activated (pipe_t *pipe_)
|
2009-12-15 09:09:19 +01:00
|
|
|
{
|
|
|
|
// Move the pipe to the list of active pipes.
|
|
|
|
pipes.swap (pipes.index (pipe_), active);
|
|
|
|
active++;
|
|
|
|
}
|
|
|
|
|
2012-11-09 17:08:03 +01:00
|
|
|
int zmq::lb_t::send (msg_t *msg_)
|
2013-07-02 20:05:20 +02:00
|
|
|
{
|
|
|
|
return sendpipe (msg_, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::lb_t::sendpipe (msg_t *msg_, pipe_t **pipe_)
|
2009-12-15 09:09:19 +01:00
|
|
|
{
|
2011-03-20 20:52:54 +01:00
|
|
|
// Drop the message if required. If we are at the end of the message
|
|
|
|
// switch back to non-dropping mode.
|
|
|
|
if (dropping) {
|
|
|
|
|
2011-11-01 13:39:54 +01:00
|
|
|
more = msg_->flags () & msg_t::more ? true : false;
|
2012-05-31 15:59:59 +02:00
|
|
|
dropping = more;
|
2011-03-20 20:52:54 +01:00
|
|
|
|
2011-04-21 22:27:48 +02:00
|
|
|
int rc = msg_->close ();
|
2011-03-20 20:52:54 +01:00
|
|
|
errno_assert (rc == 0);
|
2011-04-21 22:27:48 +02:00
|
|
|
rc = msg_->init ();
|
2012-05-28 23:13:09 +02:00
|
|
|
errno_assert (rc == 0);
|
2011-03-20 20:52:54 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-01 10:13:26 +01:00
|
|
|
while (active > 0) {
|
2014-05-02 16:50:16 +02:00
|
|
|
if (pipes [current]->write (msg_))
|
2013-07-02 20:05:20 +02:00
|
|
|
{
|
|
|
|
if (pipe_)
|
|
|
|
*pipe_ = pipes [current];
|
2010-03-01 10:13:26 +01:00
|
|
|
break;
|
2013-07-02 20:05:20 +02:00
|
|
|
}
|
2014-05-02 16:50:16 +02:00
|
|
|
|
2010-03-27 21:25:40 +01:00
|
|
|
zmq_assert (!more);
|
2010-03-01 10:13:26 +01:00
|
|
|
active--;
|
|
|
|
if (current < active)
|
|
|
|
pipes.swap (current, active);
|
|
|
|
else
|
|
|
|
current = 0;
|
|
|
|
}
|
|
|
|
|
2009-12-15 09:09:19 +01:00
|
|
|
// If there are no pipes we cannot send the message.
|
2010-03-01 10:13:26 +01:00
|
|
|
if (active == 0) {
|
2009-12-15 09:09:19 +01:00
|
|
|
errno = EAGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-22 13:39:20 +02:00
|
|
|
// If it's final part of the message we can flush it downstream and
|
|
|
|
// continue round-robining (load balance).
|
2012-05-31 15:59:59 +02:00
|
|
|
more = msg_->flags () & msg_t::more? true: false;
|
2010-03-27 21:25:40 +01:00
|
|
|
if (!more) {
|
2010-03-27 09:43:49 +01:00
|
|
|
pipes [current]->flush ();
|
|
|
|
current = (current + 1) % active;
|
|
|
|
}
|
2009-12-15 09:09:19 +01:00
|
|
|
|
|
|
|
// Detach the message from the data buffer.
|
2011-04-21 22:27:48 +02:00
|
|
|
int rc = msg_->init ();
|
|
|
|
errno_assert (rc == 0);
|
2009-12-15 09:09:19 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool zmq::lb_t::has_out ()
|
|
|
|
{
|
2010-03-27 09:43:49 +01:00
|
|
|
// If one part of the message was already written we can definitely
|
|
|
|
// write the rest of the message.
|
2010-03-27 21:25:40 +01:00
|
|
|
if (more)
|
2010-03-27 09:43:49 +01:00
|
|
|
return true;
|
|
|
|
|
2010-03-01 10:13:26 +01:00
|
|
|
while (active > 0) {
|
2010-12-15 20:10:27 +01:00
|
|
|
|
2012-03-28 06:38:25 +02:00
|
|
|
// Check whether a pipe has room for another message.
|
|
|
|
if (pipes [current]->check_write ())
|
2009-12-15 09:09:19 +01:00
|
|
|
return true;
|
2010-03-01 10:13:26 +01:00
|
|
|
|
2010-09-07 15:49:54 +02:00
|
|
|
// Deactivate the pipe.
|
2010-03-01 10:13:26 +01:00
|
|
|
active--;
|
2010-09-07 15:49:54 +02:00
|
|
|
pipes.swap (current, active);
|
|
|
|
if (current == active)
|
2009-12-15 09:09:19 +01:00
|
|
|
current = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|