2009-12-15 09:09:19 +01:00
|
|
|
/*
|
2010-01-05 08:29:35 +01:00
|
|
|
Copyright (c) 2007-2010 iMatix Corporation
|
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
|
|
|
|
the terms of the Lesser GNU 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
|
|
|
|
Lesser GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the Lesser GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-03-11 20:33:27 +01:00
|
|
|
#include "../include/zmq.h"
|
2009-12-15 09:09:19 +01:00
|
|
|
|
|
|
|
#include "lb.hpp"
|
|
|
|
#include "pipe.hpp"
|
|
|
|
#include "err.hpp"
|
|
|
|
|
|
|
|
zmq::lb_t::lb_t () :
|
|
|
|
active (0),
|
|
|
|
current (0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq::lb_t::~lb_t ()
|
|
|
|
{
|
|
|
|
for (pipes_t::size_type i = 0; i != pipes.size (); i++)
|
|
|
|
pipes [i]->term ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::lb_t::attach (writer_t *pipe_)
|
|
|
|
{
|
|
|
|
pipes.push_back (pipe_);
|
|
|
|
pipes.swap (active, pipes.size () - 1);
|
|
|
|
active++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::lb_t::detach (writer_t *pipe_)
|
|
|
|
{
|
|
|
|
// Remove the pipe from the list; adjust number of active pipes
|
|
|
|
// accordingly.
|
2010-02-10 12:48:04 +01:00
|
|
|
if (pipes.index (pipe_) < active) {
|
2009-12-15 09:09:19 +01:00
|
|
|
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_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::lb_t::revive (writer_t *pipe_)
|
|
|
|
{
|
|
|
|
// Move the pipe to the list of active pipes.
|
|
|
|
pipes.swap (pipes.index (pipe_), active);
|
|
|
|
active++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq::lb_t::send (zmq_msg_t *msg_, int flags_)
|
|
|
|
{
|
2010-03-01 10:13:26 +01:00
|
|
|
while (active > 0) {
|
|
|
|
if (pipes [current]->write (msg_))
|
|
|
|
break;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-03-13 14:40:10 +01:00
|
|
|
pipes [current]->flush ();
|
2009-12-15 09:09:19 +01:00
|
|
|
|
|
|
|
// Detach the message from the data buffer.
|
|
|
|
int rc = zmq_msg_init (msg_);
|
|
|
|
zmq_assert (rc == 0);
|
|
|
|
|
2010-02-10 12:48:04 +01:00
|
|
|
// Move to the next pipe (load-balancing).
|
2010-03-01 10:13:26 +01:00
|
|
|
current = (current + 1) % active;
|
2010-02-10 12:48:04 +01:00
|
|
|
|
2009-12-15 09:09:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool zmq::lb_t::has_out ()
|
|
|
|
{
|
2010-03-01 10:13:26 +01:00
|
|
|
while (active > 0) {
|
|
|
|
if (pipes [current]->check_write ())
|
2009-12-15 09:09:19 +01:00
|
|
|
return true;
|
2010-03-01 10:13:26 +01:00
|
|
|
|
|
|
|
active--;
|
|
|
|
if (current < active)
|
|
|
|
pipes.swap (current, active);
|
|
|
|
else
|
2009-12-15 09:09:19 +01:00
|
|
|
current = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|