Thread portable class


Gate portable class


Timer class


Pool class


Terimber 2.0


About C++


Downloads Products & Services Support Clients Open Source About



Home / Open source / Terimber 2.0

msg_communicator Class Reference

kernel class in the messaging system communication dispatches incoming messages, creates and destroys connection objects makes a lot of dirty work: dynamic casting, msg allocation/destruction and more ... More...

#include <msg_comm.h>

Inheritance diagram for msg_communicator:

msg_queue_processor timer_callback terimber_log_helper msg_base msg_queue< 3 > terimber_thread_employer terimber_log

List of all members.

Public Member Functions

 msg_communicator ()
 constructor
 ~msg_communicator ()
 destructor
void init (const char *info_, const char *ini_key_)
 initializes internal structure due to file xml content generates unique initializing key for owner the same key owner has to use for uinit function NB!!! throw exception class object
void init (const void *info_, size_t len)
 initializes internal structure due to file xml content generates unique initializing key for owner the same key owner has to use for uinit function NB!!! throws exception class object
void init (xml_designer *nav)
 as previous but take already parsed document NB!!! throws exception class object
void uninit ()
 uinitializes message system closes all listeners and connection
void add_connection (msg_connection *connection)
 adds connection to the connection map
void change_connection_address (const guid_t &old_address, msg_connection *connection)
void shutdown_connection (msg_connection *connection)
 this function insert message into communicator queue gives the order to destroy connection so this function can be called the connection function itself
const guid_tget_address () const
 returns communicator address - this is module address
msg_cppconstruct_msg (size_t size)
 constructs msg with body size NB!!! throws exception class object
bool resize_msg (msg_cpp *msg, size_t size)
 reallocates message body, saves the old part of body, if any
msg_cppcopy_msg (msg_cpp *msg)
 copies message content
bool destroy_msg (msg_cpp *msg)
 destroys msg_cpp object, free memory
void get_msg_key (room_byte_t &crypt_private, const room_byte_t &crypt_external, const guid_t &session) const
 returns transport key
msg_connectionfind_connection (const guid_t &addr)
 tries to find connection with specified address
msg_listenerfind_listener (transport_type type)
 tries to find listener with specified address and type
bool validate_connection (const msg_connection *connection) const
 checks if specified connection is still alive and has not been removed from connection map
void comm_msg (msg_cpp *msg)
 put incoming message into the communicator communicator can redirect message or put message into queue NB!!! throws exception class object
msg_cppconstruct_handshake (const rsa *rsa)
 constructs handshake message as initiator NB!!! throws exception class object
msg_cppreply_handshake (const msg_cpp *msg, room_byte_t *symetric_private_key)
 analyzes incoming handshake message prepares outgoing message on incoming message NB!!! throws exception class object
void check_handshake (const guid_t &sessionid, const msg_cpp *reply, const rsa *rsa, room_byte_t &symetric_private_key)
 checks the reply from connection peer NB!!! throws exception class object
msg_cppconstruct_ping ()
 constructs ping message as initiator NB !!! throws exception class object
aiosockget_aiosock ()
 gets socket manager
threadpoolget_thread_manager ()
 gets thread manager
event_pool_tget_event_pool ()
 gets event pool
void add_connection_config (const conf_connection &atom)
 adds connection trows exception functions
void add_listener_config (const conf_listener &atom, bool start)
 adds listener
void remove_connection_config (const guid_t &address)
 removes connection
void remove_listener_config (transport_type type)
 removes listener
void doxray ()
 does xray
void log_msg (const msg_cpp *msg)
 outputs debug message

Static Public Member Functions

static msg_cppcast (msg_t *msg)
 casts C style message to CPP style message
static const msg_cppcast (const msg_t *msg)
 casts C style const message to CPP style const message
static bool set_receiver (msg_cpp *msg, const guid_t &receiver)
 sets receiver address for specified message
static bool get_sender (const msg_cpp *msg, guid_t &sender)
 returns the sender of specified message
static void parse_connection (xml_designer *nav, conf_connection &atom)
 parses xml into connection info
static void parse_listener (xml_designer *nav, conf_listener &atom)
 parses xml into listener info
static msg_communicatorloan_communicator (const guid_t &addr)
 loans the communicator
static void return_communicator (const guid_t &addr, msg_communicator *comm)
 returns back the communicator pointer

Protected Member Functions

virtual void v_do_job (size_t ident, void *user_data)
 base functionality - override virtual function
virtual void v_on ()
 turns on communicator to the working state turns on all listeners and established previous connections
virtual void v_off ()
 turns off communicator turns off all listeners and established previuosly connections
virtual void wakeup ()
 wakeup wokring thread
virtual void notify (size_t ident, size_t interval, size_t multiplier)
 timer_callback

Private Types

typedef list< msg_listener * > listener_list_t
 list of listener pointers
typedef map< guid_t,
msg_connection * > 
connection_map_t
 maps guid to message connection
typedef map< guid_t,
msg_communicator * > 
this_map_t
 maps guid to communicator connection

Private Member Functions

msg_connection_connect (const conf_connection &info_, ub8_t timeout)
 tries to establish the new connection according to info_ NB!!! throws exception class object
void _close_connection (const guid_t &addr)
 removes connection from the connection map
msg_cpp_generate_crypt_private_key (const rsa &rsa, room_byte_t &symetric_private_key)
 packs rsa
msg_cpp_pack_keys (const integer &n, const integer &e)
 adds encryption keys to message
void _extract_keys (const msg_cpp *msg, integer &n, integer &e)
 extracts encryption keys from message
void _decrypt_private_key (const msg_cpp *reply, const rsa &rsa, room_byte_t &symetric_private_key)
 extracts private symmetric key
void _turn_off_listeners ()
 turns off all listeners
void _turn_on_listeners ()
 turns on all listeners
void _destroy_listeners ()
 destroys all listeners
void _turn_off_connections ()
 turns off all connections
void _turn_on_connections ()
 turns on all connections
void _destroy_connections ()
 destroys all connections
void _create_listeners ()
 creates all listenrs
void _create_listener (const conf_listener &atom)
 creates a listener from info
void _register_this ()
 registers this pointer at the static map
void _revoke_this ()
 revokes this pointer from the static map

Private Attributes

guid_t _address
 internal communicator address
guid_t _debug_address
 debug information address
mutex _mtx_listeners
 multithreaded locker for listeners map
listener_list_t _listeners
 list of listeners
mutex _mtx_conn
 multithreaded locker for connection map
connection_map_t _connections
 connection map
mem_pool_t _manager
 memory allocation manager
threadpool _thread_manager
 thread pool
aiosock _aio_port
 socket port
timer _ping_timer
 ping timer
mutex _mtx_config
 multithreaded locker for config unit
conf_unit _config
 configuration information
event_pool_t _event_pool
 pool of events

Static Private Attributes

static keylocker _this_access
 to prevent static function calls without valid communicator instance
static this_map_t _this_map
 keeps the communicators map


Detailed Description

kernel class in the messaging system communication dispatches incoming messages, creates and destroys connection objects makes a lot of dirty work: dynamic casting, msg allocation/destruction and more ...

Definition at line 71 of file msg_comm.h.


Member Typedef Documentation

list of listener pointers

Definition at line 77 of file msg_comm.h.

maps guid to message connection

Definition at line 80 of file msg_comm.h.

maps guid to communicator connection

Definition at line 83 of file msg_comm.h.


Constructor & Destructor Documentation

msg_communicator::msg_communicator (  ) 

constructor

Definition at line 144 of file msg_comm.cpp.

msg_communicator::~msg_communicator (  ) 

destructor

Definition at line 151 of file msg_comm.cpp.

References uninit().


Member Function Documentation

void msg_communicator::init ( const char *  info_,
const char *  ini_key_ 
)

initializes internal structure due to file xml content generates unique initializing key for owner the same key owner has to use for uinit function NB!!! throw exception class object

Parameters:
info_  ini file
ini_key_  crypto key, optional

Definition at line 342 of file msg_comm.cpp.

References exception::_throw(), room_array< T >::copy(), msg_xml_dtd, room_array< T >::size(), and str_template::strlen().

Referenced by aiomsg::init(), and init().

void msg_communicator::init ( const void *  info_,
size_t  len 
)

initializes internal structure due to file xml content generates unique initializing key for owner the same key owner has to use for uinit function NB!!! throws exception class object

Parameters:
info_  ini xml in memory
len  size of buffer

Definition at line 381 of file msg_comm.cpp.

References exception::_throw(), init(), msg_xml_dtd, and str_template::strlen().

void msg_communicator::uninit (  ) 

uinitializes message system closes all listeners and connection

Definition at line 564 of file msg_comm.cpp.

References _config, _destroy_connections(), _destroy_listeners(), conf_unit::clear(), msg_queue< 3 >::is_block(), msg_base::is_on(), and v_off().

Referenced by aiomsg::uninit(), and ~msg_communicator().

void msg_communicator::change_connection_address ( const guid_t old_address,
msg_connection connection 
)

void msg_communicator::shutdown_connection ( msg_connection connection  ) 

const guid_t& msg_communicator::get_address (  )  const [inline]

returns communicator address - this is module address

Definition at line 137 of file msg_comm.h.

References _address.

Referenced by aiomsg::get_port_address(), and aiomsg::start().

bool msg_communicator::resize_msg ( msg_cpp msg,
size_t  size 
)

reallocates message body, saves the old part of body, if any

Parameters:
msg  message pointer
size  message size

Definition at line 272 of file msg_comm.cpp.

References msg_cpp::resize().

Referenced by aiomsg::resize().

void msg_communicator::get_msg_key ( room_byte_t crypt_private,
const room_byte_t crypt_external,
const guid_t session 
) const

returns transport key

Parameters:
crypt_private  byte room for private key
crypt_external  bytes for external crypt key
session  session ident

Definition at line 280 of file msg_comm.cpp.

References room_array< T >::reserve(), and room_array< T >::size().

Referenced by msg_connection::msg_connection().

static msg_cpp* msg_communicator::cast ( msg_t msg  )  [inline, static]

casts C style message to CPP style message

Parameters:
msg  message pointer

Definition at line 169 of file msg_comm.h.

Referenced by aiomsg::destroy(), aiomsg::get_buffer(), aiomsg::get_sender(), aiomsg::get_size(), aiomsg::post(), aiomsg::resize(), aiomsg::send(), aiomsg::send_async(), aiomsg::set_receiver(), and aiomsg::write_buffer().

static const msg_cpp* msg_communicator::cast ( const msg_t msg  )  [inline, static]

casts C style const message to CPP style const message

Parameters:
msg  message pointer

Definition at line 185 of file msg_comm.h.

static bool msg_communicator::set_receiver ( msg_cpp msg,
const guid_t receiver 
) [inline, static]

sets receiver address for specified message

Parameters:
msg  message pointer
receiver  receiver address

Definition at line 201 of file msg_comm.h.

References msg_cpp::_receiver.

Referenced by aiomsg::set_receiver().

static bool msg_communicator::get_sender ( const msg_cpp msg,
guid_t sender 
) [inline, static]

returns the sender of specified message

Parameters:
msg  message pointer
sender  [out] sender address

Definition at line 214 of file msg_comm.h.

References msg_cpp::_sender.

Referenced by aiomsg::get_sender().

msg_connection * msg_communicator::find_connection ( const guid_t addr  ) 

tries to find connection with specified address

Parameters:
addr  input address

Definition at line 319 of file msg_comm.cpp.

References _connections, _mtx_conn, base_map< K, T, Pr, M >::end(), and base_map< K, T, Pr, M >::find().

Referenced by remove_connection_config().

msg_listener * msg_communicator::find_listener ( transport_type  type  ) 

tries to find listener with specified address and type

Parameters:
type  transport

Definition at line 330 of file msg_comm.cpp.

References _listeners, _mtx_listeners, base_list< T >::begin(), and base_list< T >::end().

bool msg_communicator::validate_connection ( const msg_connection connection  )  const

checks if specified connection is still alive and has not been removed from connection map

Parameters:
connection  pointer to connection

Definition at line 772 of file msg_comm.cpp.

References _connections, _mtx_conn, base_map< K, T, Pr, M >::begin(), and base_map< K, T, Pr, M >::end().

Referenced by aiomsg::check_ident().

void msg_communicator::comm_msg ( msg_cpp msg  ) 

msg_cpp * msg_communicator::construct_handshake ( const rsa *  rsa  ) 

constructs handshake message as initiator NB!!! throws exception class object

Parameters:
rsa  RSA object

Definition at line 1055 of file msg_comm.cpp.

References _address, _pack_keys(), msg_cpp::_sender, msg_cpp::_sessionid, msg_cpp::_type, construct_msg(), handshake_default_timeout, handshake_type, msg_id_handshake_request, MSG_PRIORITY_SYSTEM, msg_t::msgid, msg_t::priority, msg_t::timeout, and uuid_gen().

Referenced by msg_connection::prepare_handshake_msg().

msg_cpp * msg_communicator::reply_handshake ( const msg_cpp msg,
room_byte_t symetric_private_key 
)

analyzes incoming handshake message prepares outgoing message on incoming message NB!!! throws exception class object

Parameters:
msg  message pointer
symetric_private_key  private symmetric key, optional

Definition at line 1069 of file msg_comm.cpp.

References _address, _extract_keys(), _generate_crypt_private_key(), msg_cpp::_receiver, msg_cpp::_sender, msg_cpp::_sessionid, exception::_throw(), msg_cpp::_type, construct_msg(), handshake_default_timeout, handshake_type, msg_id_handshake_reply, msg_id_handshake_request, MSG_PRIORITY_SYSTEM, MSG_RESULT_ACCESS_DENIED, msg_t::msgid, msg_t::priority, and msg_t::timeout.

Referenced by msg_connection::prepare_handshake_reply(), and msg_connection::process_incoming_message().

void msg_communicator::check_handshake ( const guid_t sessionid,
const msg_cpp reply,
const rsa *  rsa,
room_byte_t symetric_private_key 
)

checks the reply from connection peer NB!!! throws exception class object

Parameters:
sessionid  session ident
reply  message pointer
rsa  RSA object
symetric_private_key  private symmetric key

Definition at line 1104 of file msg_comm.cpp.

References _address, _decrypt_private_key(), msg_cpp::_receiver, msg_cpp::_sessionid, exception::_throw(), msg_cpp::_type, handshake_type, msg_id_handshake_reply, MSG_RESULT_ACCESS_DENIED, and msg_t::msgid.

Referenced by msg_connection::process_incoming_message(), and msg_connection::validate_handshake_reply().

msg_cpp * msg_communicator::construct_ping (  ) 

constructs ping message as initiator NB !!! throws exception class object

Definition at line 1163 of file msg_comm.cpp.

References _address, msg_cpp::_sender, msg_cpp::_type, construct_msg(), msg_id_ping, msg_t::msgid, and system_type.

Referenced by msg_connection::ping_notify().

event_pool_t & msg_communicator::get_event_pool (  ) 

gets event pool

Definition at line 1189 of file msg_comm.cpp.

References _event_pool.

Referenced by msg_user_connection::send().

void msg_communicator::add_connection_config ( const conf_connection atom  ) 

adds connection trows exception functions

dynamic configuration

Parameters:
atom  connection info

Definition at line 484 of file msg_comm.cpp.

References conf_connection::_address, _config, conf_unit::_connections, _mtx_config, exception::_throw(), base_list< T >::begin(), base_list< T >::end(), and list< T >::push_back().

Referenced by aiomsg::add_connection(), and init().

void msg_communicator::add_listener_config ( const conf_listener atom,
bool  start 
)

adds listener

Parameters:
atom  listener info
start  start listener

Definition at line 497 of file msg_comm.cpp.

References _config, _create_listener(), conf_unit::_listeners, _mtx_config, exception::_throw(), conf_listener::_type, base_list< T >::begin(), base_list< T >::end(), and list< T >::push_back().

Referenced by aiomsg::add_listener(), and init().

void msg_communicator::remove_connection_config ( const guid_t address  ) 

removes connection

Parameters:
address  connection address

Definition at line 513 of file msg_comm.cpp.

References _config, conf_unit::_connections, _mtx_config, base_list< T >::begin(), base_list< T >::end(), list< T >::erase(), find_connection(), shutdown_connection(), and mutex_keeper::unlock().

Referenced by aiomsg::remove_connection().

void msg_communicator::remove_listener_config ( transport_type  type  ) 

removes listener

Parameters:
type  listener type

Definition at line 534 of file msg_comm.cpp.

References _config, _listeners, conf_unit::_listeners, _mtx_config, _mtx_listeners, base_list< T >::begin(), base_list< T >::end(), list< T >::erase(), and mutex_keeper::unlock().

Referenced by aiomsg::remove_listener().

msg_communicator * msg_communicator::loan_communicator ( const guid_t addr  )  [static]

void msg_communicator::return_communicator ( const guid_t addr,
msg_communicator comm 
) [static]

void msg_communicator::log_msg ( const msg_cpp msg  ) 

void msg_communicator::v_on (  )  [protected, virtual]

turns on communicator to the working state turns on all listeners and established previous connections

Reimplemented from msg_queue_processor.

Definition at line 584 of file msg_comm.cpp.

References _aio_port, _ping_timer, _register_this(), _thread_manager, _turn_on_connections(), _turn_on_listeners(), timer::activate(), en_log_info, terimber_log::format_logging(), msg_base::is_on(), terimber_log_helper::log_on(), aiosock::on(), threadpool::on(), msg_queue< 3 >::unblock(), and msg_queue_processor::v_on().

Referenced by init().

void msg_communicator::v_off (  )  [protected, virtual]

void msg_communicator::wakeup (  )  [protected, virtual]

wakeup wokring thread

Implements msg_queue< 3 >.

Definition at line 766 of file msg_comm.cpp.

References _thread_manager, threadpool::borrow_thread(), and stay_on_alert_time.

void msg_communicator::notify ( size_t  ident,
size_t  interval,
size_t  multiplier 
) [protected, virtual]

timer_callback

Parameters:
ident  timer ident
interval  fire interval
multiplier  multiplier

Implements timer_callback.

Definition at line 755 of file msg_comm.cpp.

References _connections, _mtx_conn, base_map< K, T, Pr, M >::begin(), and base_map< K, T, Pr, M >::end().

msg_connection * msg_communicator::_connect ( const conf_connection info_,
ub8_t  timeout 
) [private]

tries to establish the new connection according to info_ NB!!! throws exception class object

Parameters:
info_  connection info
timeout  timeout in milliseconds

Definition at line 298 of file msg_comm.cpp.

References msg_base::_communicator, conf_connection::_type, msg_sock_connection::connect(), msg_rpc_connection::connect(), rpc, and sock.

Referenced by v_do_job().

void msg_communicator::_close_connection ( const guid_t addr  )  [private]

removes connection from the connection map

Parameters:
addr  connection address

Definition at line 862 of file msg_comm.cpp.

References _connections, _mtx_conn, base_map< K, T, Pr, M >::end(), map< K, T, Pr, M >::erase(), and base_map< K, T, Pr, M >::find().

Referenced by v_do_job().

msg_cpp * msg_communicator::_generate_crypt_private_key ( const rsa &  rsa,
room_byte_t symetric_private_key 
) [private]

packs rsa

Parameters:
rsa  RSA object
symetric_private_key  private symmetric key

Definition at line 1118 of file msg_comm.cpp.

References construct_msg(), msg_cpp::get_body(), room_array< T >::reserve(), and rsa_key_size.

Referenced by reply_handshake().

msg_cpp * msg_communicator::_pack_keys ( const integer &  n,
const integer &  e 
) [private]

adds encryption keys to message

Parameters:
n  neutral PKI key
e  encryption PKI key

Definition at line 990 of file msg_comm.cpp.

References construct_msg(), and msg_cpp::get_body().

Referenced by construct_handshake().

void msg_communicator::_extract_keys ( const msg_cpp msg,
integer &  n,
integer &  e 
) [private]

extracts encryption keys from message

Parameters:
msg  message pointer
n  neutral PKI key
e  encryption PKI key

Definition at line 1018 of file msg_comm.cpp.

References exception::_throw(), msg_cpp::get_body(), msg_cpp::get_size(), and MSG_RESULT_ACCESS_DENIED.

Referenced by reply_handshake().

void msg_communicator::_decrypt_private_key ( const msg_cpp reply,
const rsa &  rsa,
room_byte_t symetric_private_key 
) [private]

extracts private symmetric key

Parameters:
reply  message pointer
rsa  RSA object
symetric_private_key  private symmetric key

Definition at line 1140 of file msg_comm.cpp.

References exception::_throw(), msg_cpp::get_body(), msg_cpp::get_size(), MSG_RESULT_ACCESS_DENIED, and room_array< T >::reserve().

Referenced by check_handshake().

void msg_communicator::_turn_off_listeners (  )  [private]

turns off all listeners

Definition at line 654 of file msg_comm.cpp.

References _listeners, _mtx_listeners, base_list< T >::begin(), and base_list< T >::end().

Referenced by v_off().

void msg_communicator::_turn_on_listeners (  )  [private]

turns on all listeners

Definition at line 666 of file msg_comm.cpp.

References _listeners, _mtx_listeners, base_list< T >::begin(), and base_list< T >::end().

Referenced by v_on().

void msg_communicator::_destroy_listeners (  )  [private]

destroys all listeners

Definition at line 678 of file msg_comm.cpp.

References _listeners, _mtx_listeners, base_list< T >::begin(), list< T >::clear(), and base_list< T >::end().

Referenced by uninit().

void msg_communicator::_turn_off_connections (  )  [private]

turns off all connections

Definition at line 719 of file msg_comm.cpp.

References _connections, _mtx_conn, base_map< K, T, Pr, M >::begin(), and base_map< K, T, Pr, M >::end().

Referenced by v_off().

void msg_communicator::_turn_on_connections (  )  [private]

turns on all connections

Definition at line 731 of file msg_comm.cpp.

References _connections, _mtx_conn, base_map< K, T, Pr, M >::begin(), and base_map< K, T, Pr, M >::end().

Referenced by v_on().

void msg_communicator::_destroy_connections (  )  [private]

destroys all connections

Definition at line 743 of file msg_comm.cpp.

References _connections, _mtx_conn, base_map< K, T, Pr, M >::begin(), map< K, T, Pr, M >::clear(), and base_map< K, T, Pr, M >::end().

Referenced by uninit().

void msg_communicator::_create_listeners (  )  [private]

creates all listenrs

Definition at line 689 of file msg_comm.cpp.

References _config, _create_listener(), conf_unit::_listeners, _mtx_config, base_list< T >::begin(), and base_list< T >::end().

Referenced by init().

void msg_communicator::_create_listener ( const conf_listener atom  )  [private]

creates a listener from info

Parameters:
atom  listener info

Definition at line 698 of file msg_comm.cpp.

References msg_base::_communicator, _listeners, _mtx_listeners, conf_listener::_type, list< T >::push_back(), rpc, and sock.

Referenced by _create_listeners(), and add_listener_config().

void msg_communicator::_register_this (  )  [private]

registers this pointer at the static map

Definition at line 166 of file msg_comm.cpp.

References _address, _this_access, _this_map, exception::_throw(), base_map< K, T, Pr, M >::end(), base_map< K, T, Pr, M >::find(), and map< K, T, Pr, M >::insert().

Referenced by v_on().

void msg_communicator::_revoke_this (  )  [private]

revokes this pointer from the static map

Definition at line 181 of file msg_comm.cpp.

References _address, _this_access, _this_map, and map< K, T, Pr, M >::erase().


Member Data Documentation

debug information address

Definition at line 419 of file msg_comm.h.

memory allocation manager

Definition at line 424 of file msg_comm.h.

Referenced by construct_msg(), destroy_msg(), and doxray().

socket port

Definition at line 426 of file msg_comm.h.

Referenced by doxray(), get_aiosock(), v_off(), and v_on().

ping timer

Definition at line 427 of file msg_comm.h.

Referenced by v_off(), and v_on().

pool of events

Definition at line 430 of file msg_comm.h.

Referenced by doxray(), and get_event_pool().

to prevent static function calls without valid communicator instance

locker for "this" communicator instance

Definition at line 431 of file msg_comm.h.

Referenced by _register_this(), _revoke_this(), loan_communicator(), and return_communicator().

keeps the communicators map

Definition at line 433 of file msg_comm.h.

Referenced by _register_this(), _revoke_this(), loan_communicator(), and return_communicator().


The documentation for this class was generated from the following files:


© Copyright Terimber 2003-.