S3FC project page S3FC home page

Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

s3_txport_tcp Class Template Reference

Bidirectional homogeneous TCP transport mechanism. More...

#include <s3_txport_tcp.h>

Inheritance diagram for s3_txport_tcp:

Inheritance graph
[legend]
Collaboration diagram for s3_txport_tcp:

Collaboration graph
[legend]
List of all members.

Public Methods

 s3_txport_tcp (int queue_size=4, std::string network="0.0.0.0", std::string mask="0.0.0.0")
 Constructor sets up a tcp transport or dies trying. More...

 ~s3_txport_tcp ()
 Cleans up like a good child, and will terminate the threads it spawned. More...

s3_fifo_queue< s3_txport_data<
T_Data > > & 
get_rx_queue ()
 Get a reference to the receive queue. More...

s3_fifo_queue< s3_txport_data<
T_Data > > & 
get_tx_queue ()
 Get a reference to the transmitter queue. More...

void send_metadata (s3_txport_client_id id, const std::string &mdata)
 Send metadata as required by s3_txport_base. More...

std::list< s3_txport_client_idget_client_id_list () const
 Returns a list of all currently connected clients. More...

bool is_connected_by_id (s3_txport_client_id id)
 Returns true if the client is still considered to be active. More...

std::string get_ip_address_by_id (s3_txport_client_id id)
 Returns the IP address (nnn.nnn.nnn.nnn) of the indicated client. More...

bool listen (const unsigned int port)
 Connect the server to the specified port. More...

s3_txport_client_id connect (const std::string &IP_addr, const unsigned int port)
 Connect the client to the target server at the IP address and port specified in the parameters. More...

bool disconnect_by_id (s3_txport_client_id id)
 Disconnect the indicated remote connection. More...

void disconnect_all ()
 Disconnect all clients, regardless, but keep the listener etc. alive. More...

void main_loop ()
 Thread that reads the incoming queue and pushes the data to the specified destination. More...

int num_connections () const
 Returns the number of connections associated with this txport instance. More...

T_Socket & get_socket ()
 Returns the socket associated with the transport. More...

bool listening () const
 Returns true if the server is listening for connections. More...

void stop ()
 Stop the server thread and release the socket, terminating all current connections from clients and the tx thread. More...

void restart ()
 Method to restart a stopped txport. More...


Protected Methods

void set_critical_by_id (s3_txport_client_id id)
 Sets a critical section for the indicated connection. More...

void clear_critical_by_id (s3_txport_client_id id)
 Clears a critical section for the indicated connection. More...

T_Socket * get_socket_by_id (s3_txport_client_id id)
 Returns the socket associated with the client ID. More...

void cleanup ()
 Code to clean the socket up if it is killed. More...

void reap ()
 Reap any threads associated with connections that have been closed. More...

s3_txport_client_id next_id ()
 Increment connection id (always non-zero for valid connections). More...

s3_txport_client_id spawn_handler (T_Socket *active_socket, std::string hostname)
 Helper function to set up the remote receive threads. More...

void set_terminate ()
 Post to the termination semaphore. More...


Private Types

typedef std::map< s3_txport_client_id,
s3_txport_tcp_rx_task< T_Data,
T_Socket > * > 
thread_map
 Map the client connection ID to the client's handler thread. More...


Private Attributes

unsigned int port
 Port to listen on. More...

s3_fifo_queue< s3_txport_data<
T_Data > > 
rx_queue
 Queue from which the data will be read. More...

s3_fifo_queue< s3_txport_data<
T_Data > > 
tx_queue
 Queue in which to write data to be sent to a client. More...

s3_txport_client_id connect_id
 Counter to ensure unique connection IDs. More...

T_Socket sock
 Socket through which we will be accepting connections. More...

s3_growable_fifo_queue< s3_txport_client_iddying_tasklets
 List of tasks that have expired and can be reaped. More...

thread_map connections
 A map of all the connection threads we currently have. More...

s3_txport_tcp_tx_task< T_Data,
T_Socket > 
tx_thread
 Local to remote transmission thread pointer used to stop tx thread. More...

s3_mutex connection_mutex
 Lock for active client map and other state variables. More...

s3_mutex reap_mutex
 Mutex to make reap() thread safe as it must be re-entrant. More...

s3_growable_fifo_queue< metadata_typemeta_queue
 Queue through which metadata is communicated to the TCP subsystem. More...

s3_semaphore terminate
 Set to true if we must terminate the thread. More...

unsigned int network_mask
 Network mask anded with the accepted connection. More...

unsigned int network_number
 Network number. More...


Friends

class s3_txport_tcp_tx_task< T_Data, T_Socket >
class s3_txport_tcp_rx_task< T_Data, T_Socket >

Detailed Description

template<class T_Data, class T_Socket = s3_socket_tcp>
class s3_txport_tcp< T_Data, T_Socket >

Bidirectional homogeneous TCP transport mechanism.

This class provides a high-level bidirectional communication endpoint that accepts connections from one or more s3_txport_tcp<T_Data> instances, and is capable of making connections to one or more instances of a s3_txport_tcp<T_Data> using the TCP/IP protocol. It is able to listen at one port, and accept unlimited connections on this port, as well as make unlimited connections to arbitrary IP/port address combinations.

This is used for homogeneous data transport, i.e. the local and remote instances employ the same data types, that dispatches and received data packets of type T_Data. The data type should support the interface required by s3_streamable (i.e. it should have an s3_streamable::pack() operator specialised for this data type). Data is converted to and from a bitstream via the overloaded pack and unpack members.

Incoming data of type T_Data, received from remote connections, are internally queued in the RX queue and can be sequentially extracted (in the same order that it was received). Each connection has a unique identifier, of type s3_txport_client_id, associated with it. This is used to distinguish between the different remote txport instances connected to this instance of the transport.

Data is extracted from the RX queue as an s3_txport_data<T_Data> struct. This contains the actual data, as received from the remote instance, as well as the identifier of the originating client. This identifier is used to identify the client when sending data to it.

Outgoing data, of type T_Data, is internally queued in the TX queue and dispatched to connected instances. Each queued packet of T_Data is dispatched to a single destination.

The presence of the TX and RX queues decouples the physical data transport from the application level. The behaviour of the txport upon the TX or RX queue filling up is to block until the queues empty.

The system reports various conditions through the s3_event mechanism, including all connect, disconnect, error, informational and metadata events. All supported events are enumerated in s3_txport_event. Events are often dispatched deep inside loops that read data, thus it is the responsibility of the programmer not to make the handler routines too complex, as this will slow comms down.

Metadata can be send to any destination using the send_metadata() method, and the payload is always a string. The payload is delivered by the generation of an event via s3_event_dispatcher, and can be distinguised from other events by the type s3_txport_event::metadata.

Incoming connections can be restricted to particular sets of IP addresses using the network and mask parameters in the constructor. When a connection is requested, it is first ANDed with the mask and then tested for equivalence with the network number. If the mask is "255.255.255.0" and the network is "192.168.1.0" then all connections with IP addresses "192.168.1.x" are accepted and all others rejected. By default, all connections are accepted.

Definition at line 118 of file s3_txport_tcp.h.


Member Typedef Documentation

template<class T_Data, class T_Socket = s3_socket_tcp>
typedef std::map<s3_txport_client_id, s3_txport_tcp_rx_task<T_Data, T_Socket>*> s3_txport_tcp::thread_map [private]
 

Map the client connection ID to the client's handler thread.

Definition at line 146 of file s3_txport_tcp.h.


Constructor & Destructor Documentation

template<class T_Data, class T_Socket>
s3_txport_tcp< T_Data, T_Socket >::s3_txport_tcp int    queue_size = 4,
std::string    network = "0.0.0.0",
std::string    mask = "0.0.0.0"
 

Constructor sets up a tcp transport or dies trying.

Parameters:
queue_size  The size of the local queue
network  The network number to allow connections from
mask  The network mask.

Definition at line 481 of file s3_txport_tcp.tcc.

References INVALID_SOCKET, network_mask, network_number, and tx_thread.

template<class T_Data, class T_Socket>
s3_txport_tcp< T_Data, T_Socket >::~s3_txport_tcp  
 

Cleans up like a good child, and will terminate the threads it spawned.

Definition at line 546 of file s3_txport_tcp.tcc.

References stop.


Member Function Documentation

template<class T_Data, class T_Socket>
void s3_txport_tcp< T_Data, T_Socket >::cleanup   [protected, virtual]
 

Code to clean the socket up if it is killed.

Reimplemented from s3_thread_base.

Definition at line 805 of file s3_txport_tcp.tcc.

References sock.

template<class T_Data, class T_Socket>
void s3_txport_tcp< T_Data, T_Socket >::clear_critical_by_id s3_txport_client_id    id [protected]
 

Clears a critical section for the indicated connection.

Parameters:
id  The id of the client

Definition at line 677 of file s3_txport_tcp.tcc.

References connection_mutex, connections, s3_mutex::lock, s3_txport_client_id, and s3_mutex::unlock.

template<class T_Data, class T_Socket>
s3_txport_client_id s3_txport_tcp< T_Data, T_Socket >::connect const std::string &    IP_addr,
const unsigned int    port
 

Connect the client to the target server at the IP address and port specified in the parameters.

If a connecion already exists and it is different from the target specified, the connection is closed, and a new connection is re-opened to the specified target. If the target is the same, and it is connected, no action is taken. If the target is the same and it is not connected, the connection is made.

Returns:
The id of the new connection, or 0 if no connection was made.

Definition at line 867 of file s3_txport_tcp.tcc.

References s3_event_dispatcher::dispatch_event, s3_txport_event::error, port, S3FC_DBG, S3FC_DBG2_, sock, and spawn_handler.

template<class T_Data, class T_Socket>
void s3_txport_tcp< T_Data, T_Socket >::disconnect_all   [virtual]
 

Disconnect all clients, regardless, but keep the listener etc. alive.

Implements s3_txport_base.

Definition at line 600 of file s3_txport_tcp.tcc.

References disconnect_by_id, dying_tasklets, get_client_id_list, num_connections, reap, S3FC_DBG, and s3_growable_fifo_queue< s3_txport_client_id >::size.

Referenced by stop.

template<class T_Data, class T_Socket>
bool s3_txport_tcp< T_Data, T_Socket >::disconnect_by_id s3_txport_client_id    id [virtual]
 

Disconnect the indicated remote connection.

Parameters:
The  ID of the remote to be disconnected.

Implements s3_txport_base.

Definition at line 572 of file s3_txport_tcp.tcc.

References connection_mutex, connections, s3_mutex::lock, s3_txport_client_id, S3FC_DBG, and s3_mutex::unlock.

Referenced by disconnect_all, and s3_txport_tcp_tx_task< s3_message, s3_socket_tcp >::handle_failure.

template<class T_Data, class T_Socket>
std::list< s3_txport_client_id > s3_txport_tcp< T_Data, T_Socket >::get_client_id_list   const [virtual]
 

Returns a list of all currently connected clients.

Note that this list reflects the situation at the time of the call, and there is no guarantee that all the clients in the list are connected.

Implements s3_txport_base.

Definition at line 736 of file s3_txport_tcp.tcc.

References connection_mutex, connections, s3_mutex::lock, and s3_mutex::unlock.

Referenced by disconnect_all.

template<class T_Data, class T_Socket>
std::string s3_txport_tcp< T_Data, T_Socket >::get_ip_address_by_id s3_txport_client_id    id
 

Returns the IP address (nnn.nnn.nnn.nnn) of the indicated client.

If the indicated client is no longer active, an empty string is returned.

Definition at line 720 of file s3_txport_tcp.tcc.

References connection_mutex, connections, s3_mutex::lock, s3_txport_client_id, and s3_mutex::unlock.

template<class T_Data, class T_Socket = s3_socket_tcp>
s3_fifo_queue<s3_txport_data<T_Data> >& s3_txport_tcp< T_Data, T_Socket >::get_rx_queue   [inline, virtual]
 

Get a reference to the receive queue.

Implements s3_txport_base.

Definition at line 243 of file s3_txport_tcp.h.

template<class T_Data, class T_Socket = s3_socket_tcp>
T_Socket& s3_txport_tcp< T_Data, T_Socket >::get_socket   [inline]
 

Returns the socket associated with the transport.

Definition at line 321 of file s3_txport_tcp.h.

template<class T_Data, class T_Socket>
T_Socket * s3_txport_tcp< T_Data, T_Socket >::get_socket_by_id s3_txport_client_id    id [protected]
 

Returns the socket associated with the client ID.

Definition at line 688 of file s3_txport_tcp.tcc.

References connection_mutex, connections, s3_mutex::lock, s3_txport_client_id, and s3_mutex::unlock.

template<class T_Data, class T_Socket = s3_socket_tcp>
s3_fifo_queue<s3_txport_data<T_Data> >& s3_txport_tcp< T_Data, T_Socket >::get_tx_queue   [inline, virtual]
 

Get a reference to the transmitter queue.

Implements s3_txport_base.

Definition at line 249 of file s3_txport_tcp.h.

template<class T_Data, class T_Socket>
bool s3_txport_tcp< T_Data, T_Socket >::is_connected_by_id s3_txport_client_id    id [virtual]
 

Returns true if the client is still considered to be active.

Implements s3_txport_base.

Definition at line 704 of file s3_txport_tcp.tcc.

References connection_mutex, connections, s3_mutex::lock, s3_txport_client_id, and s3_mutex::unlock.

template<class T_Data, class T_Socket>
bool s3_txport_tcp< T_Data, T_Socket >::listen const unsigned int    port
 

Connect the server to the specified port.

If the port specified in the parameter is the same as the port specified in the constructor, the call will start the server thread if it was stopped, else it will do nothing. If the ports are not the same, it will stop the server thread, thereby closing any current connections, and start a new server and listen on the specified port. If the port is 0, the server will be closed.

Parameters:
port  The port to connect the server to.
Returns:
true if the connection was succesful or if the port to which the connection was requested is already connected.

Definition at line 759 of file s3_txport_tcp.tcc.

References s3_event_dispatcher::dispatch_event, s3_txport_event::error, s3_thread_base::is_running, port, S3FC_DBG2_, sock, and s3_thread_base::start.

template<class T_Data, class T_Socket = s3_socket_tcp>
bool s3_txport_tcp< T_Data, T_Socket >::listening   const [inline, virtual]
 

Returns true if the server is listening for connections.

Implements s3_txport_base.

Definition at line 327 of file s3_txport_tcp.h.

template<class T_Data, class T_Socket>
void s3_txport_tcp< T_Data, T_Socket >::main_loop   [virtual]
 

Thread that reads the incoming queue and pushes the data to the specified destination.

This should not ever be called.

Implements s3_thread_base.

Definition at line 901 of file s3_txport_tcp.tcc.

References s3_event_dispatcher::dispatch_event, s3_txport_event::error, network_mask, network_number, s3_txport_event::reject, S3FC_DBG, S3FC_DBG2_, sock, spawn_handler, terminate, and s3_semaphore::try_wait.

template<class T_Data, class T_Socket = s3_socket_tcp>
s3_txport_client_id s3_txport_tcp< T_Data, T_Socket >::next_id   [inline, protected]
 

Increment connection id (always non-zero for valid connections).

Definition at line 206 of file s3_txport_tcp.h.

Referenced by spawn_handler.

template<class T_Data, class T_Socket>
int s3_txport_tcp< T_Data, T_Socket >::num_connections   const
 

Returns the number of connections associated with this txport instance.

Definition at line 981 of file s3_txport_tcp.tcc.

References connection_mutex, connections, s3_mutex::lock, and s3_mutex::unlock.

Referenced by disconnect_all, and stop.

template<class T_Data, class T_Socket>
void s3_txport_tcp< T_Data, T_Socket >::reap   [protected]
 

Reap any threads associated with connections that have been closed.

Definition at line 812 of file s3_txport_tcp.tcc.

References connection_mutex, connections, s3_txport_event::disconnect, s3_event_dispatcher::dispatch_event, dying_tasklets, s3_growable_fifo_queue< s3_txport_client_id >::empty, s3_mutex::lock, s3_growable_fifo_queue< s3_txport_client_id >::pop, s3_growable_fifo_queue< s3_txport_client_id >::push, reap_mutex, s3_txport_client_id, S3FC_DBG, and s3_mutex::unlock.

Referenced by disconnect_all.

template<class T_Data, class T_Socket>
void s3_txport_tcp< T_Data, T_Socket >::restart  
 

Method to restart a stopped txport.

Returns:
True if if the system was stopped, else false.

Definition at line 563 of file s3_txport_tcp.tcc.

References tx_thread.

template<class T_Data, class T_Socket>
void s3_txport_tcp< T_Data, T_Socket >::send_metadata s3_txport_client_id    id,
const std::string &    mdata
[virtual]
 

Send metadata as required by s3_txport_base.

Implements s3_txport_base.

Definition at line 753 of file s3_txport_tcp.tcc.

References meta_queue, s3_growable_fifo_queue< metadata_type >::push, and s3_txport_client_id.

template<class T_Data, class T_Socket>
void s3_txport_tcp< T_Data, T_Socket >::set_critical_by_id s3_txport_client_id    id [protected]
 

Sets a critical section for the indicated connection.

Parameters:
id  The id of the client

Definition at line 664 of file s3_txport_tcp.tcc.

References connection_mutex, connections, s3_mutex::lock, s3_txport_client_id, and s3_mutex::unlock.

template<class T_Data, class T_Socket = s3_socket_tcp>
void s3_txport_tcp< T_Data, T_Socket >::set_terminate   [inline, protected]
 

Post to the termination semaphore.

Definition at line 224 of file s3_txport_tcp.h.

Referenced by stop.

template<class T_Data, class T_Socket>
s3_txport_client_id s3_txport_tcp< T_Data, T_Socket >::spawn_handler T_Socket *    active_socket,
std::string    hostname
[protected]
 

Helper function to set up the remote receive threads.

Definition at line 961 of file s3_txport_tcp.tcc.

References s3_txport_event::connect, connect_id, connection_mutex, connections, s3_event_dispatcher::dispatch_event, s3_mutex::lock, next_id, s3_txport_client_id, S3FC_DBG, and s3_mutex::unlock.

Referenced by connect, and main_loop.

template<class T_Data, class T_Socket>
void s3_txport_tcp< T_Data, T_Socket >::stop  
 

Stop the server thread and release the socket, terminating all current connections from clients and the tx thread.

Reimplemented from s3_thread_base.

Definition at line 634 of file s3_txport_tcp.tcc.

References disconnect_all, dying_tasklets, s3_thread_base::get_thread_id, s3_thread_base::is_running, num_connections, S3FC_DBG, set_terminate, s3_growable_fifo_queue< s3_txport_client_id >::size, tx_thread, and s3_thread_base::wait_on_exit.

Referenced by ~s3_txport_tcp.


Friends And Related Function Documentation

template<class T_Data, class T_Socket = s3_socket_tcp>
friend class s3_txport_tcp_rx_task< T_Data, T_Socket > [friend]
 

Definition at line 346 of file s3_txport_tcp.h.

template<class T_Data, class T_Socket = s3_socket_tcp>
friend class s3_txport_tcp_tx_task< T_Data, T_Socket > [friend]
 

Definition at line 345 of file s3_txport_tcp.h.


Member Data Documentation

template<class T_Data, class T_Socket = s3_socket_tcp>
s3_txport_client_id s3_txport_tcp::connect_id [private]
 

Counter to ensure unique connection IDs.

Definition at line 130 of file s3_txport_tcp.h.

Referenced by s3_txport_tcp< s3_message >::next_id, and spawn_handler.

template<class T_Data, class T_Socket = s3_socket_tcp>
s3_mutex s3_txport_tcp::connection_mutex [private]
 

Lock for active client map and other state variables.

Definition at line 155 of file s3_txport_tcp.h.

Referenced by clear_critical_by_id, disconnect_by_id, get_client_id_list, get_ip_address_by_id, get_socket_by_id, is_connected_by_id, num_connections, reap, set_critical_by_id, and spawn_handler.

template<class T_Data, class T_Socket = s3_socket_tcp>
thread_map s3_txport_tcp::connections [private]
 

A map of all the connection threads we currently have.

Definition at line 149 of file s3_txport_tcp.h.

Referenced by clear_critical_by_id, disconnect_by_id, get_client_id_list, get_ip_address_by_id, get_socket_by_id, is_connected_by_id, num_connections, reap, set_critical_by_id, and spawn_handler.

template<class T_Data, class T_Socket = s3_socket_tcp>
s3_growable_fifo_queue<s3_txport_client_id> s3_txport_tcp::dying_tasklets [private]
 

List of tasks that have expired and can be reaped.

The rx tasks push the client ID of the connection into this queue as it expires, which allows the removal of the ancillary structures and data associated with the connection.

Definition at line 141 of file s3_txport_tcp.h.

Referenced by s3_txport_tcp_rx_task::cleanup, disconnect_all, reap, and stop.

template<class T_Data, class T_Socket = s3_socket_tcp>
s3_growable_fifo_queue<metadata_type> s3_txport_tcp::meta_queue [private]
 

Queue through which metadata is communicated to the TCP subsystem.

Definition at line 171 of file s3_txport_tcp.h.

Referenced by send_metadata.

template<class T_Data, class T_Socket = s3_socket_tcp>
unsigned int s3_txport_tcp::network_mask [private]
 

Network mask anded with the accepted connection.

Definition at line 177 of file s3_txport_tcp.h.

Referenced by main_loop, and s3_txport_tcp.

template<class T_Data, class T_Socket = s3_socket_tcp>
unsigned int s3_txport_tcp::network_number [private]
 

Network number.

The accepted connection's ip is ANDed with the nework mask and tested for equivalence with the network number.

Definition at line 183 of file s3_txport_tcp.h.

Referenced by main_loop, and s3_txport_tcp.

template<class T_Data, class T_Socket = s3_socket_tcp>
unsigned int s3_txport_tcp::port [private]
 

Port to listen on.

Definition at line 121 of file s3_txport_tcp.h.

Referenced by connect, and listen.

template<class T_Data, class T_Socket = s3_socket_tcp>
s3_mutex s3_txport_tcp::reap_mutex [private]
 

Mutex to make reap() thread safe as it must be re-entrant.

Definition at line 158 of file s3_txport_tcp.h.

Referenced by reap.

template<class T_Data, class T_Socket = s3_socket_tcp>
s3_fifo_queue<s3_txport_data<T_Data> > s3_txport_tcp::rx_queue [private]
 

Queue from which the data will be read.

Definition at line 124 of file s3_txport_tcp.h.

Referenced by s3_txport_tcp_rx_task::cleanup, and s3_txport_tcp_rx_task::main_loop.

template<class T_Data, class T_Socket = s3_socket_tcp>
T_Socket s3_txport_tcp::sock [private]
 

Socket through which we will be accepting connections.

Definition at line 133 of file s3_txport_tcp.h.

Referenced by cleanup, connect, s3_txport_tcp< s3_message >::get_socket, listen, and main_loop.

template<class T_Data, class T_Socket = s3_socket_tcp>
s3_semaphore s3_txport_tcp::terminate [private]
 

Set to true if we must terminate the thread.

Definition at line 174 of file s3_txport_tcp.h.

Referenced by main_loop.

template<class T_Data, class T_Socket = s3_socket_tcp>
s3_fifo_queue<s3_txport_data<T_Data> > s3_txport_tcp::tx_queue [private]
 

Queue in which to write data to be sent to a client.

Definition at line 127 of file s3_txport_tcp.h.

template<class T_Data, class T_Socket = s3_socket_tcp>
s3_txport_tcp_tx_task<T_Data, T_Socket> s3_txport_tcp::tx_thread [private]
 

Local to remote transmission thread pointer used to stop tx thread.

Definition at line 152 of file s3_txport_tcp.h.

Referenced by restart, s3_txport_tcp, and stop.


The documentation for this class was generated from the following files:
Send comments to: s3fc@stonethree.com SourceForge Logo