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"
|
|
|
|
#include "io_thread.hpp"
|
|
|
|
#include "simple_semaphore.hpp"
|
|
|
|
|
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-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
|
|
|
|
|
|
|
case command_t::bind:
|
2009-08-08 16:01:58 +02:00
|
|
|
process_bind ();
|
|
|
|
return;
|
2009-07-29 12:07:54 +02:00
|
|
|
|
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-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-08 16:01:58 +02:00
|
|
|
void zmq::object_t::send_plug (object_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::plug;
|
2009-07-29 12:07:54 +02:00
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
void zmq::object_t::send_own (object_t *destination_, object_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-08 16:01:58 +02:00
|
|
|
void zmq::object_t::send_bind (object_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::bind;
|
2009-07-29 12:07:54 +02:00
|
|
|
send_command (cmd);
|
|
|
|
}
|
|
|
|
|
2009-08-08 16:01:58 +02:00
|
|
|
void zmq::object_t::send_term_req (object_t *destination_, object_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-08 16:01:58 +02:00
|
|
|
void zmq::object_t::send_term (object_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-08 16:01:58 +02:00
|
|
|
void zmq::object_t::send_term_ack (object_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-08 16:01:58 +02:00
|
|
|
void zmq::object_t::process_own (object_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_bind ()
|
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_req (object_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
|
|
|
}
|
|
|
|
|