2009-07-29 12:07:54 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2007-2009 FastMQ Inc.
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "object.hpp"
|
2009-08-06 10:47:34 +02:00
|
|
|
#include "context.hpp"
|
2009-07-29 12:07:54 +02:00
|
|
|
#include "err.hpp"
|
|
|
|
#include "io_thread.hpp"
|
|
|
|
#include "simple_semaphore.hpp"
|
|
|
|
|
2009-08-06 10:47:34 +02:00
|
|
|
zmq::object_t::object_t (context_t *context_, int thread_slot_) :
|
|
|
|
context (context_),
|
2009-07-29 12:07:54 +02:00
|
|
|
thread_slot (thread_slot_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq::object_t::object_t (object_t *parent_) :
|
2009-08-06 10:47:34 +02:00
|
|
|
context (parent_->context),
|
2009-07-29 12:07:54 +02:00
|
|
|
thread_slot (parent_->thread_slot)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq::object_t::~object_t ()
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq::object_t::thread_slot_count ()
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-06 10:47:34 +02:00
|
|
|
return context->thread_slot_count ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq::object_t::get_thread_slot ()
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
return thread_slot;
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::process_command (command_t &cmd_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
switch (cmd_.type) {
|
|
|
|
|
|
|
|
case command_t::head:
|
|
|
|
process_head (cmd_.args.head.bytes);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case command_t::tail:
|
|
|
|
process_tail (cmd_.args.tail.bytes);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case command_t::engine:
|
|
|
|
process_engine (cmd_.args.engine.engine);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case command_t::bind:
|
|
|
|
process_bind (cmd_.args.bind.reader, cmd_.args.bind.peer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case command_t::reg:
|
|
|
|
process_reg (cmd_.args.reg.smph);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case command_t::reg_and_bind:
|
|
|
|
process_reg_and_bind (cmd_.args.reg_and_bind.peer,
|
|
|
|
cmd_.args.reg_and_bind.flow_in, cmd_.args.reg_and_bind.flow_out);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case command_t::unreg:
|
|
|
|
process_unreg (cmd_.args.unreg.smph);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case command_t::terminate:
|
|
|
|
process_terminate ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case command_t::terminate_ack:
|
|
|
|
process_terminate_ack ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case command_t::stop:
|
|
|
|
process_stop ();
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_assert (false);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq::io_thread_t *zmq::object_t::choose_io_thread (uint64_t taskset_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-06 10:47:34 +02:00
|
|
|
return context->choose_io_thread (taskset_);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::send_stop ()
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
// Send command goes always to the current object. To-self pipe is
|
|
|
|
// used exclusively for sending this command.
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = this;
|
|
|
|
cmd.type = command_t::stop;
|
2009-08-06 10:47:34 +02:00
|
|
|
context->write (thread_slot, thread_slot, cmd);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::send_bind (object_t *destination_, pipe_reader_t *reader_,
|
2009-07-29 12:07:54 +02:00
|
|
|
session_t *peer_)
|
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
|
|
|
cmd.type = command_t::bind;
|
|
|
|
cmd.args.bind.reader = reader_;
|
|
|
|
cmd.args.bind.peer = peer_;
|
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::send_head (object_t *destination_, uint64_t bytes_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
|
|
|
cmd.type = command_t::head;
|
|
|
|
cmd.args.head.bytes = bytes_;
|
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::send_tail (object_t *destination_, uint64_t bytes_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
|
|
|
cmd.type = command_t::tail;
|
|
|
|
cmd.args.tail.bytes = bytes_;
|
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::send_reg (object_t *destination_, simple_semaphore_t *smph_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
|
|
|
cmd.type = command_t::reg;
|
|
|
|
cmd.args.reg.smph = smph_;
|
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::send_reg_and_bind (object_t *destination_,
|
2009-07-29 12:07:54 +02:00
|
|
|
session_t *peer_, bool flow_in_, bool flow_out_)
|
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
|
|
|
cmd.type = command_t::reg_and_bind;
|
|
|
|
cmd.args.reg_and_bind.peer = peer_;
|
|
|
|
cmd.args.reg_and_bind.flow_in = flow_in_;
|
|
|
|
cmd.args.reg_and_bind.flow_out = flow_out_;
|
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::send_unreg (object_t *destination_,
|
2009-07-29 12:07:54 +02:00
|
|
|
simple_semaphore_t *smph_)
|
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
|
|
|
cmd.type = command_t::unreg;
|
|
|
|
cmd.args.unreg.smph = smph_;
|
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::send_engine (object_t *destination_, i_engine *engine_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
|
|
|
cmd.type = command_t::engine;
|
|
|
|
cmd.args.engine.engine = engine_;
|
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::send_terminate (object_t *destination_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
|
|
|
cmd.type = command_t::terminate;
|
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::send_terminate_ack (object_t *destination_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
|
|
|
cmd.type = command_t::terminate_ack;
|
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::process_stop ()
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_assert (false);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::process_bind (pipe_reader_t *reader_, session_t *peer_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_assert (false);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::process_head (uint64_t bytes_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_assert (false);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::process_tail (uint64_t bytes_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_assert (false);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::process_reg (simple_semaphore_t *smph_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_assert (false);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::process_reg_and_bind (session_t *session_,
|
2009-07-29 12:07:54 +02:00
|
|
|
bool flow_in_, bool flow_out_)
|
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_assert (false);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::process_unreg (simple_semaphore_t *smph_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_assert (false);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::process_engine (i_engine *engine_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_assert (false);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::process_terminate ()
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_assert (false);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::process_terminate_ack ()
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_assert (false);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
void zmq::object_t::send_command (command_t &cmd_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
int destination_thread_slot = cmd_.destination->get_thread_slot ();
|
|
|
|
if (destination_thread_slot == thread_slot)
|
|
|
|
cmd_.destination->process_command (cmd_);
|
|
|
|
else
|
2009-08-06 10:47:34 +02:00
|
|
|
context->write (thread_slot, destination_thread_slot, cmd_);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|