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-08 16:01:58 +02:00
|
|
|
#include "dispatcher.hpp"
|
2009-07-29 12:07:54 +02:00
|
|
|
#include "err.hpp"
|
2009-08-27 10:54:28 +02:00
|
|
|
#include "pipe.hpp"
|
2009-07-29 12:07:54 +02:00
|
|
|
#include "io_thread.hpp"
|
|
|
|
#include "simple_semaphore.hpp"
|
2009-08-20 11:32:23 +02:00
|
|
|
#include "owned.hpp"
|
|
|
|
#include "session.hpp"
|
|
|
|
#include "socket_base.hpp"
|
|
|
|
#include "zmq_engine.hpp" // TODO: remove this line
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
zmq::object_t::object_t (dispatcher_t *dispatcher_, int thread_slot_) :
|
|
|
|
dispatcher (dispatcher_),
|
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-08 16:01:58 +02:00
|
|
|
dispatcher (parent_->dispatcher),
|
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-08 16:01:58 +02:00
|
|
|
return dispatcher->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) {
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
case command_t::revive:
|
|
|
|
process_revive ();
|
|
|
|
break;
|
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
case command_t::stop:
|
|
|
|
process_stop ();
|
2009-07-29 12:07:54 +02:00
|
|
|
break;
|
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
case command_t::plug:
|
|
|
|
process_plug ();
|
|
|
|
return;
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
case command_t::own:
|
|
|
|
process_own (cmd_.args.own.object);
|
|
|
|
return;
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-08-20 11:32:23 +02:00
|
|
|
case command_t::attach:
|
|
|
|
process_attach (cmd_.args.attach.engine);
|
|
|
|
return;
|
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
case command_t::bind:
|
2009-08-27 10:54:28 +02:00
|
|
|
process_bind (cmd_.args.bind.session,
|
|
|
|
cmd_.args.bind.in_pipe, cmd_.args.bind.out_pipe);
|
2009-08-08 16:01:58 +02:00
|
|
|
return;
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
case command_t::pipe_term:
|
|
|
|
process_pipe_term ();
|
|
|
|
return;
|
|
|
|
|
|
|
|
case command_t::pipe_term_ack:
|
|
|
|
process_pipe_term_ack ();
|
|
|
|
return;
|
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
case command_t::term_req:
|
|
|
|
process_term_req (cmd_.args.term_req.object);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case command_t::term:
|
|
|
|
process_term ();
|
|
|
|
return;
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
case command_t::term_ack:
|
|
|
|
process_term_ack ();
|
2009-07-29 12:07:54 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_assert (false);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
void zmq::object_t::register_pipe (class pipe_t *pipe_)
|
|
|
|
{
|
|
|
|
dispatcher->register_pipe (pipe_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::object_t::unregister_pipe (class pipe_t *pipe_)
|
|
|
|
{
|
|
|
|
dispatcher->unregister_pipe (pipe_);
|
|
|
|
}
|
|
|
|
|
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-08 16:01:58 +02:00
|
|
|
return dispatcher->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-08 16:01:58 +02:00
|
|
|
dispatcher->write (thread_slot, thread_slot, cmd);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-20 11:32:23 +02:00
|
|
|
void zmq::object_t::send_plug (owned_t *destination_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-20 11:32:23 +02:00
|
|
|
// Let the object know that it cannot shut down till it gets this command.
|
|
|
|
destination_->inc_seqnum ();
|
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
2009-08-08 16:01:58 +02:00
|
|
|
cmd.type = command_t::plug;
|
2009-07-29 12:07:54 +02:00
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-20 11:32:23 +02:00
|
|
|
void zmq::object_t::send_own (socket_base_t *destination_, owned_t *object_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
2009-08-08 16:01:58 +02:00
|
|
|
cmd.type = command_t::own;
|
|
|
|
cmd.args.own.object = object_;
|
2009-07-29 12:07:54 +02:00
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-20 11:32:23 +02:00
|
|
|
void zmq::object_t::send_attach (session_t *destination_, zmq_engine_t *engine_)
|
|
|
|
{
|
2009-08-21 14:29:22 +02:00
|
|
|
// The assumption here is that command sequence number of the destination
|
|
|
|
// object was already incremented in find_session function.
|
2009-08-20 11:32:23 +02:00
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
|
|
|
cmd.type = command_t::attach;
|
|
|
|
cmd.args.attach.engine = engine_;
|
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
void zmq::object_t::send_bind (object_t *destination_, owned_t *session_,
|
|
|
|
reader_t *in_pipe_, writer_t *out_pipe_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
2009-08-08 16:01:58 +02:00
|
|
|
cmd.type = command_t::bind;
|
2009-08-27 10:54:28 +02:00
|
|
|
cmd.args.bind.session = session_;
|
|
|
|
cmd.args.bind.in_pipe = in_pipe_;
|
|
|
|
cmd.args.bind.out_pipe = out_pipe_;
|
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::object_t::send_revive (object_t *destination_)
|
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
|
|
|
cmd.type = command_t::revive;
|
2009-07-29 12:07:54 +02:00
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-28 16:51:46 +02:00
|
|
|
void zmq::object_t::send_pipe_term (writer_t *destination_)
|
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
|
|
|
cmd.type = command_t::pipe_term;
|
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::object_t::send_pipe_term_ack (reader_t *destination_)
|
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
|
|
|
cmd.type = command_t::pipe_term_ack;
|
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-20 11:32:23 +02:00
|
|
|
void zmq::object_t::send_term_req (socket_base_t *destination_,
|
|
|
|
owned_t *object_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
2009-08-08 16:01:58 +02:00
|
|
|
cmd.type = command_t::term_req;
|
|
|
|
cmd.args.term_req.object = object_;
|
2009-07-29 12:07:54 +02:00
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-20 11:32:23 +02:00
|
|
|
void zmq::object_t::send_term (owned_t *destination_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
2009-08-08 16:01:58 +02:00
|
|
|
cmd.type = command_t::term;
|
2009-07-29 12:07:54 +02:00
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-20 11:32:23 +02:00
|
|
|
void zmq::object_t::send_term_ack (socket_base_t *destination_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
command_t cmd;
|
|
|
|
cmd.destination = destination_;
|
2009-08-08 16:01:58 +02:00
|
|
|
cmd.type = command_t::term_ack;
|
2009-07-29 12:07:54 +02:00
|
|
|
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-08 16:01:58 +02:00
|
|
|
void zmq::object_t::process_plug ()
|
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-20 11:32:23 +02:00
|
|
|
void zmq::object_t::process_own (owned_t *object_)
|
|
|
|
{
|
|
|
|
zmq_assert (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::object_t::process_attach (zmq_engine_t *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-27 10:54:28 +02:00
|
|
|
void zmq::object_t::process_bind (owned_t *session_,
|
|
|
|
reader_t *in_pipe_, writer_t *out_pipe_)
|
|
|
|
{
|
|
|
|
zmq_assert (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::object_t::process_revive ()
|
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-28 16:51:46 +02:00
|
|
|
void zmq::object_t::process_pipe_term ()
|
|
|
|
{
|
|
|
|
zmq_assert (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::object_t::process_pipe_term_ack ()
|
|
|
|
{
|
|
|
|
zmq_assert (false);
|
|
|
|
}
|
|
|
|
|
2009-08-20 11:32:23 +02:00
|
|
|
void zmq::object_t::process_term_req (owned_t *object_)
|
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-08 16:01:58 +02:00
|
|
|
void zmq::object_t::process_term ()
|
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-08 16:01:58 +02:00
|
|
|
void zmq::object_t::process_term_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-08 16:01:58 +02:00
|
|
|
dispatcher->write (thread_slot, destination_thread_slot, cmd_);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|