2009-12-23 19:37:56 +01:00
|
|
|
/*
|
2010-01-05 08:29:35 +01:00
|
|
|
Copyright (c) 2007-2010 iMatix Corporation
|
2009-12-23 19:37:56 +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-02-19 15:24:43 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2009-12-23 19:37:56 +01:00
|
|
|
#include "zmq_init.hpp"
|
2010-08-11 14:09:56 +02:00
|
|
|
#include "transient_session.hpp"
|
|
|
|
#include "named_session.hpp"
|
|
|
|
#include "socket_base.hpp"
|
2009-12-23 19:37:56 +01:00
|
|
|
#include "zmq_engine.hpp"
|
|
|
|
#include "io_thread.hpp"
|
|
|
|
#include "session.hpp"
|
2010-02-19 15:24:43 +01:00
|
|
|
#include "uuid.hpp"
|
2010-08-11 14:09:56 +02:00
|
|
|
#include "blob.hpp"
|
2009-12-23 19:37:56 +01:00
|
|
|
#include "err.hpp"
|
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
zmq::zmq_init_t::zmq_init_t (io_thread_t *io_thread_,
|
|
|
|
socket_base_t *socket_, session_t *session_, fd_t fd_,
|
|
|
|
const options_t &options_) :
|
|
|
|
own_t (io_thread_),
|
2009-12-23 19:37:56 +01:00
|
|
|
sent (false),
|
|
|
|
received (false),
|
2010-08-11 14:09:56 +02:00
|
|
|
socket (socket_),
|
|
|
|
session (session_),
|
|
|
|
options (options_),
|
|
|
|
io_thread (io_thread_)
|
2009-12-23 19:37:56 +01:00
|
|
|
{
|
|
|
|
// Create the engine object for this connection.
|
2010-08-11 14:09:56 +02:00
|
|
|
engine = new (std::nothrow) zmq_engine_t (fd_, options);
|
2009-12-23 19:37:56 +01:00
|
|
|
zmq_assert (engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq::zmq_init_t::~zmq_init_t ()
|
|
|
|
{
|
|
|
|
if (engine)
|
2010-09-19 22:45:48 +02:00
|
|
|
engine->terminate ();
|
2009-12-23 19:37:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool zmq::zmq_init_t::read (::zmq_msg_t *msg_)
|
|
|
|
{
|
|
|
|
// If the identity was already sent, do nothing.
|
|
|
|
if (sent)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Send the identity.
|
|
|
|
int rc = zmq_msg_init_size (msg_, options.identity.size ());
|
|
|
|
zmq_assert (rc == 0);
|
|
|
|
memcpy (zmq_msg_data (msg_), options.identity.c_str (),
|
|
|
|
options.identity.size ());
|
|
|
|
sent = true;
|
|
|
|
|
|
|
|
// If initialisation is done, pass the engine to the session and
|
|
|
|
// destroy the init object.
|
2010-08-11 14:09:56 +02:00
|
|
|
finalise_initialisation ();
|
2009-12-23 19:37:56 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool zmq::zmq_init_t::write (::zmq_msg_t *msg_)
|
|
|
|
{
|
|
|
|
// If identity was already received, we are not interested
|
|
|
|
// in subsequent messages.
|
|
|
|
if (received)
|
|
|
|
return false;
|
|
|
|
|
2010-02-19 15:24:43 +01:00
|
|
|
// Retreieve the remote identity. If it's empty, generate a unique name.
|
|
|
|
if (!zmq_msg_size (msg_)) {
|
2010-02-24 13:02:29 +01:00
|
|
|
unsigned char identity [uuid_t::uuid_blob_len + 1];
|
2010-02-19 15:24:43 +01:00
|
|
|
identity [0] = 0;
|
2010-02-24 13:02:29 +01:00
|
|
|
memcpy (identity + 1, uuid_t ().to_blob (), uuid_t::uuid_blob_len);
|
|
|
|
peer_identity.assign (identity, uuid_t::uuid_blob_len + 1);
|
2010-02-19 15:24:43 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
peer_identity.assign ((const unsigned char*) zmq_msg_data (msg_),
|
|
|
|
zmq_msg_size (msg_));
|
|
|
|
}
|
2010-03-20 10:58:59 +01:00
|
|
|
|
2009-12-23 19:37:56 +01:00
|
|
|
received = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::zmq_init_t::flush ()
|
|
|
|
{
|
|
|
|
// Check if there's anything to flush.
|
|
|
|
if (!received)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If initialisation is done, pass the engine to the session and
|
|
|
|
// destroy the init object.
|
2010-08-11 14:09:56 +02:00
|
|
|
finalise_initialisation ();
|
2009-12-23 19:37:56 +01:00
|
|
|
}
|
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
void zmq::zmq_init_t::detach ()
|
2009-12-23 19:37:56 +01:00
|
|
|
{
|
|
|
|
// This function is called by engine when disconnection occurs.
|
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
// If there is an associated session, send it a null engine to let it know
|
|
|
|
// that connection process was unsuccesful.
|
|
|
|
if (session)
|
|
|
|
send_attach (session, NULL, blob_t (), true);
|
2009-12-23 19:37:56 +01:00
|
|
|
|
|
|
|
// The engine will destroy itself, so let's just drop the pointer here and
|
|
|
|
// start termination of the init object.
|
|
|
|
engine = NULL;
|
2010-08-11 14:09:56 +02:00
|
|
|
terminate ();
|
2009-12-23 19:37:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::zmq_init_t::process_plug ()
|
|
|
|
{
|
|
|
|
zmq_assert (engine);
|
2010-08-11 14:09:56 +02:00
|
|
|
engine->plug (io_thread, this);
|
2009-12-23 19:37:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::zmq_init_t::process_unplug ()
|
|
|
|
{
|
|
|
|
if (engine)
|
|
|
|
engine->unplug ();
|
|
|
|
}
|
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
void zmq::zmq_init_t::finalise_initialisation ()
|
2009-12-23 19:37:56 +01:00
|
|
|
{
|
|
|
|
if (sent && received) {
|
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
// If we know what session we belong to, it's easy, just send the
|
2010-09-19 21:40:26 +02:00
|
|
|
// engine to that session and destroy the init object. Note that we
|
|
|
|
// know about the session only if this object is owned by it. Thus,
|
|
|
|
// lifetime of this object in contained in the lifetime of the session
|
|
|
|
// so the pointer cannot become invalid without notice.
|
2010-08-11 14:09:56 +02:00
|
|
|
if (session) {
|
|
|
|
engine->unplug ();
|
|
|
|
send_attach (session, engine, peer_identity, true);
|
|
|
|
engine = NULL;
|
|
|
|
terminate ();
|
|
|
|
return;
|
|
|
|
}
|
2009-12-23 19:37:56 +01:00
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
// All the cases below are listener-based. Therefore we need the socket
|
|
|
|
// reference so that new sessions can bind to that socket.
|
|
|
|
zmq_assert (socket);
|
|
|
|
|
|
|
|
// We have no associated session. If the peer has no identity we'll
|
2010-09-19 20:43:14 +02:00
|
|
|
// create a transient session for the connection. Note that
|
|
|
|
// seqnum is incremented to account for attach command before the
|
|
|
|
// session is launched. That way we are sure it won't terminate before
|
|
|
|
// being attached.
|
2010-08-11 14:09:56 +02:00
|
|
|
if (peer_identity [0] == 0) {
|
|
|
|
session = new (std::nothrow) transient_session_t (io_thread,
|
|
|
|
socket, options);
|
|
|
|
zmq_assert (session);
|
2010-09-19 20:43:14 +02:00
|
|
|
session->inc_seqnum ();
|
2010-08-11 14:09:56 +02:00
|
|
|
launch_sibling (session);
|
|
|
|
engine->unplug ();
|
2010-09-19 20:43:14 +02:00
|
|
|
send_attach (session, engine, peer_identity, false);
|
2010-08-11 14:09:56 +02:00
|
|
|
engine = NULL;
|
|
|
|
terminate ();
|
|
|
|
return;
|
2009-12-23 19:37:56 +01:00
|
|
|
}
|
2010-08-11 14:09:56 +02:00
|
|
|
|
|
|
|
// Try to find the session corresponding to the peer's identity.
|
|
|
|
// If found, send the engine to that session and destroy this object.
|
|
|
|
// Note that session's seqnum is incremented by find_session rather
|
|
|
|
// than by send_attach.
|
|
|
|
session = socket->find_session (peer_identity);
|
|
|
|
if (session) {
|
|
|
|
engine->unplug ();
|
|
|
|
send_attach (session, engine, peer_identity, false);
|
|
|
|
engine = NULL;
|
|
|
|
terminate ();
|
|
|
|
return;
|
2009-12-23 19:37:56 +01:00
|
|
|
}
|
|
|
|
|
2010-09-19 20:43:14 +02:00
|
|
|
// There's no such named session. We have to create one. Note that
|
|
|
|
// seqnum is incremented to account for attach command before the
|
|
|
|
// session is launched. That way we are sure it won't terminate before
|
|
|
|
// being attached.
|
2010-08-24 11:19:22 +02:00
|
|
|
session = new (std::nothrow) named_session_t (io_thread, socket,
|
|
|
|
options, peer_identity);
|
2010-08-11 14:09:56 +02:00
|
|
|
zmq_assert (session);
|
2010-09-19 20:43:14 +02:00
|
|
|
session->inc_seqnum ();
|
2010-08-11 14:09:56 +02:00
|
|
|
launch_sibling (session);
|
|
|
|
engine->unplug ();
|
2010-09-19 20:43:14 +02:00
|
|
|
send_attach (session, engine, peer_identity, false);
|
2009-12-23 19:37:56 +01:00
|
|
|
engine = NULL;
|
2010-08-11 14:09:56 +02:00
|
|
|
terminate ();
|
|
|
|
return;
|
2009-12-23 19:37:56 +01:00
|
|
|
}
|
|
|
|
}
|