0
0
mirror of https://github.com/zeromq/libzmq.git synced 2025-01-21 15:12:03 +08:00
libzmq/src/object.cpp

428 lines
8.9 KiB
C++
Raw Normal View History

2009-07-29 12:07:54 +02:00
/*
2014-01-02 12:00:57 +01:00
Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
2009-07-29 12:07:54 +02:00
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
2009-07-29 12:07:54 +02: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
GNU Lesser General Public License for more details.
2009-07-29 12:07:54 +02:00
You should have received a copy of the GNU Lesser General Public License
2009-07-29 12:07:54 +02:00
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdarg.h>
2009-07-29 12:07:54 +02:00
#include "object.hpp"
2010-05-05 14:24:54 +02:00
#include "ctx.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 "session_base.hpp"
#include "socket_base.hpp"
2009-07-29 12:07:54 +02:00
zmq::object_t::object_t (ctx_t *ctx_, uint32_t tid_) :
2010-05-05 14:24:54 +02:00
ctx (ctx_),
tid (tid_)
2009-07-29 12:07:54 +02:00
{
}
2009-08-03 11:30:13 +02:00
zmq::object_t::object_t (object_t *parent_) :
2010-05-05 14:24:54 +02:00
ctx (parent_->ctx),
tid (parent_->tid)
2009-07-29 12:07:54 +02:00
{
}
2009-08-03 11:30:13 +02:00
zmq::object_t::~object_t ()
2009-07-29 12:07:54 +02:00
{
}
uint32_t zmq::object_t::get_tid ()
2009-07-29 12:07:54 +02:00
{
return tid;
2009-07-29 12:07:54 +02:00
}
void zmq::object_t::set_tid(uint32_t id)
{
tid = id;
}
2010-05-05 14:24:54 +02:00
zmq::ctx_t *zmq::object_t::get_ctx ()
2009-09-04 16:02:41 +02:00
{
2010-05-05 14:24:54 +02:00
return ctx;
2009-09-04 16:02:41 +02:00
}
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::activate_read:
process_activate_read ();
2010-08-28 10:15:03 +02:00
break;
case command_t::activate_write:
process_activate_write (cmd_.args.activate_write.msgs_read);
2009-08-27 10:54:28 +02:00
break;
case command_t::stop:
process_stop ();
2009-07-29 12:07:54 +02:00
break;
case command_t::plug:
process_plug ();
2009-12-02 21:26:47 +01:00
process_seqnum ();
break;
2009-07-29 12:07:54 +02:00
case command_t::own:
process_own (cmd_.args.own.object);
process_seqnum ();
break;
2009-07-29 12:07:54 +02:00
case command_t::attach:
process_attach (cmd_.args.attach.engine);
2009-12-02 21:26:47 +01:00
process_seqnum ();
break;
2009-07-29 12:07:54 +02:00
case command_t::bind:
process_bind (cmd_.args.bind.pipe);
2009-12-02 21:26:47 +01:00
process_seqnum ();
break;
2009-07-29 12:07:54 +02:00
case command_t::hiccup:
process_hiccup (cmd_.args.hiccup.pipe);
break;
2009-08-28 16:51:46 +02:00
case command_t::pipe_term:
process_pipe_term ();
break;
2009-08-28 16:51:46 +02:00
case command_t::pipe_term_ack:
process_pipe_term_ack ();
break;
2009-08-28 16:51:46 +02:00
case command_t::term_req:
process_term_req (cmd_.args.term_req.object);
break;
case command_t::term:
process_term (cmd_.args.term.linger);
break;
2009-07-29 12:07:54 +02:00
case command_t::term_ack:
process_term_ack ();
break;
2009-07-29 12:07:54 +02:00
case command_t::reap:
process_reap (cmd_.args.reap.socket);
break;
case command_t::reaped:
process_reaped ();
break;
case command_t::inproc_connected:
process_seqnum ();
break;
2013-01-01 05:42:46 -05:00
case command_t::done:
2009-07-29 12:07:54 +02:00
default:
2009-08-03 11:30:13 +02:00
zmq_assert (false);
2009-07-29 12:07:54 +02:00
}
}
int zmq::object_t::register_endpoint (const char *addr_,
const endpoint_t &endpoint_)
2009-11-21 20:59:55 +01:00
{
return ctx->register_endpoint (addr_, endpoint_);
2009-11-21 20:59:55 +01:00
}
2014-07-09 09:57:28 +02:00
int zmq::object_t::unregister_endpoint (
const std::string &addr_, socket_base_t *socket_)
{
return ctx->unregister_endpoint (addr_, socket_);
}
2009-11-21 20:59:55 +01:00
void zmq::object_t::unregister_endpoints (socket_base_t *socket_)
{
2010-05-05 14:24:54 +02:00
return ctx->unregister_endpoints (socket_);
2009-11-21 20:59:55 +01:00
}
zmq::endpoint_t zmq::object_t::find_endpoint (const char *addr_)
2009-11-21 20:59:55 +01:00
{
2010-05-05 14:24:54 +02:00
return ctx->find_endpoint (addr_);
2009-11-21 20:59:55 +01:00
}
void zmq::object_t::pend_connection (const std::string &addr_,
const endpoint_t &endpoint_, pipe_t **pipes_)
{
ctx->pend_connection (addr_, endpoint_, pipes_);
}
void zmq::object_t::connect_pending (const char *addr_, zmq::socket_base_t *bind_socket_)
{
return ctx->connect_pending(addr_, bind_socket_);
}
void zmq::object_t::destroy_socket (socket_base_t *socket_)
{
ctx->destroy_socket (socket_);
}
zmq::io_thread_t *zmq::object_t::choose_io_thread (uint64_t affinity_)
2009-07-29 12:07:54 +02:00
{
return ctx->choose_io_thread (affinity_);
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
{
// 'stop' command goes always from administrative thread to
// the current object.
2009-07-29 12:07:54 +02:00
command_t cmd;
cmd.destination = this;
cmd.type = command_t::stop;
ctx->send_command (tid, cmd);
2009-07-29 12:07:54 +02:00
}
2010-08-11 14:09:56 +02:00
void zmq::object_t::send_plug (own_t *destination_, bool inc_seqnum_)
2009-07-29 12:07:54 +02:00
{
2009-12-02 21:26:47 +01:00
if (inc_seqnum_)
destination_->inc_seqnum ();
2009-07-29 12:07:54 +02:00
command_t cmd;
cmd.destination = destination_;
cmd.type = command_t::plug;
2009-07-29 12:07:54 +02:00
send_command (cmd);
}
2010-08-11 14:09:56 +02:00
void zmq::object_t::send_own (own_t *destination_, own_t *object_)
2009-07-29 12:07:54 +02:00
{
destination_->inc_seqnum ();
2009-07-29 12:07:54 +02:00
command_t cmd;
cmd.destination = destination_;
cmd.type = command_t::own;
cmd.args.own.object = object_;
2009-07-29 12:07:54 +02:00
send_command (cmd);
}
void zmq::object_t::send_attach (session_base_t *destination_,
i_engine *engine_, bool inc_seqnum_)
{
2009-12-02 21:26:47 +01:00
if (inc_seqnum_)
destination_->inc_seqnum ();
command_t cmd;
cmd.destination = destination_;
cmd.type = command_t::attach;
cmd.args.attach.engine = engine_;
send_command (cmd);
}
void zmq::object_t::send_bind (own_t *destination_, pipe_t *pipe_,
bool inc_seqnum_)
2009-07-29 12:07:54 +02:00
{
2009-12-02 21:26:47 +01:00
if (inc_seqnum_)
destination_->inc_seqnum ();
2009-07-29 12:07:54 +02:00
command_t cmd;
cmd.destination = destination_;
cmd.type = command_t::bind;
cmd.args.bind.pipe = pipe_;
2009-08-27 10:54:28 +02:00
send_command (cmd);
}
void zmq::object_t::send_activate_read (pipe_t *destination_)
2009-08-27 10:54:28 +02:00
{
command_t cmd;
cmd.destination = destination_;
cmd.type = command_t::activate_read;
2009-07-29 12:07:54 +02:00
send_command (cmd);
}
void zmq::object_t::send_activate_write (pipe_t *destination_,
uint64_t msgs_read_)
{
command_t cmd;
cmd.destination = destination_;
cmd.type = command_t::activate_write;
cmd.args.activate_write.msgs_read = msgs_read_;
send_command (cmd);
}
void zmq::object_t::send_hiccup (pipe_t *destination_, void *pipe_)
{
command_t cmd;
cmd.destination = destination_;
cmd.type = command_t::hiccup;
cmd.args.hiccup.pipe = pipe_;
send_command (cmd);
}
void zmq::object_t::send_pipe_term (pipe_t *destination_)
2009-08-28 16:51:46 +02:00
{
command_t cmd;
cmd.destination = destination_;
cmd.type = command_t::pipe_term;
send_command (cmd);
}
void zmq::object_t::send_pipe_term_ack (pipe_t *destination_)
2009-08-28 16:51:46 +02:00
{
command_t cmd;
cmd.destination = destination_;
cmd.type = command_t::pipe_term_ack;
send_command (cmd);
}
2010-08-11 14:09:56 +02:00
void zmq::object_t::send_term_req (own_t *destination_,
own_t *object_)
2009-07-29 12:07:54 +02:00
{
command_t cmd;
cmd.destination = destination_;
cmd.type = command_t::term_req;
cmd.args.term_req.object = object_;
2009-07-29 12:07:54 +02:00
send_command (cmd);
}
void zmq::object_t::send_term (own_t *destination_, int linger_)
2009-07-29 12:07:54 +02:00
{
command_t cmd;
cmd.destination = destination_;
cmd.type = command_t::term;
cmd.args.term.linger = linger_;
2009-07-29 12:07:54 +02:00
send_command (cmd);
}
2010-08-11 14:09:56 +02:00
void zmq::object_t::send_term_ack (own_t *destination_)
2009-07-29 12:07:54 +02:00
{
command_t cmd;
cmd.destination = destination_;
cmd.type = command_t::term_ack;
2009-07-29 12:07:54 +02:00
send_command (cmd);
}
void zmq::object_t::send_reap (class socket_base_t *socket_)
{
command_t cmd;
cmd.destination = ctx->get_reaper ();
cmd.type = command_t::reap;
cmd.args.reap.socket = socket_;
send_command (cmd);
}
void zmq::object_t::send_reaped ()
{
command_t cmd;
cmd.destination = ctx->get_reaper ();
cmd.type = command_t::reaped;
send_command (cmd);
}
void zmq::object_t::send_inproc_connected (zmq::socket_base_t *socket_)
{
command_t cmd;
cmd.destination = socket_;
cmd.type = command_t::inproc_connected;
send_command (cmd);
}
void zmq::object_t::send_done ()
{
command_t cmd;
cmd.destination = NULL;
cmd.type = command_t::done;
ctx->send_command (ctx_t::term_tid, 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
}
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
}
void zmq::object_t::process_own (own_t *)
{
zmq_assert (false);
}
void zmq::object_t::process_attach (i_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
}
void zmq::object_t::process_bind (pipe_t *)
2009-08-27 10:54:28 +02:00
{
zmq_assert (false);
}
void zmq::object_t::process_activate_read ()
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
}
void zmq::object_t::process_activate_write (uint64_t)
{
zmq_assert (false);
}
void zmq::object_t::process_hiccup (void *)
{
zmq_assert (false);
}
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);
}
void zmq::object_t::process_term_req (own_t *)
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
}
void zmq::object_t::process_term (int)
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
}
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
}
void zmq::object_t::process_reap (class socket_base_t *)
{
zmq_assert (false);
}
void zmq::object_t::process_reaped ()
{
zmq_assert (false);
}
2009-12-02 21:26:47 +01:00
void zmq::object_t::process_seqnum ()
{
zmq_assert (false);
}
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
{
ctx->send_command (cmd_.destination->get_tid (), cmd_);
2009-07-29 12:07:54 +02:00
}