Classes | Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Friends
FIX::ThreadedSocketAcceptor Class Reference

Threaded Socket implementation of Acceptor. More...

#include <ThreadedSocketAcceptor.h>

Inheritance diagram for FIX::ThreadedSocketAcceptor:
Inheritance graph
[legend]
Collaboration diagram for FIX::ThreadedSocketAcceptor:
Collaboration graph
[legend]

List of all members.

Classes

struct  AcceptorThreadInfo
struct  ConnectionThreadInfo

Public Member Functions

 ThreadedSocketAcceptor (Application &, MessageStoreFactory &, const SessionSettings &) throw ( ConfigError )
 ThreadedSocketAcceptor (Application &, MessageStoreFactory &, const SessionSettings &, LogFactory &) throw ( ConfigError )
virtual ~ThreadedSocketAcceptor ()

Private Types

typedef std::set< int > Sockets
typedef std::set< SessionIDSessions
typedef std::map< int, SessionsPortToSessions
typedef std::map< int, int > SocketToPort
typedef std::map< int, thread_idSocketToThread

Private Member Functions

bool readSettings (const SessionSettings &)
void onConfigure (const SessionSettings &) throw ( ConfigError )
 Implemented to configure acceptor.
void onInitialize (const SessionSettings &) throw ( RuntimeError )
 Implemented to initialize acceptor.
void onStart ()
 Implemented to start listening for connections.
bool onPoll (double timeout)
 Implemented to connect and poll for events.
void onStop ()
 Implemented to stop a running acceptor.
void addThread (int s, thread_id t)
void removeThread (int s)

Static Private Member Functions

static THREAD_PROC socketAcceptorThread (void *p)
static THREAD_PROC socketConnectionThread (void *p)

Private Attributes

Sockets m_sockets
PortToSessions m_portToSessions
SocketToPort m_socketToPort
SocketToThread m_threads
Mutex m_mutex

Friends

class SocketConnection

Detailed Description

Threaded Socket implementation of Acceptor.

Definition at line 36 of file ThreadedSocketAcceptor.h.


Member Typedef Documentation

typedef std::map< int, Sessions > FIX::ThreadedSocketAcceptor::PortToSessions [private]

Definition at line 73 of file ThreadedSocketAcceptor.h.

typedef std::set< SessionID > FIX::ThreadedSocketAcceptor::Sessions [private]

Reimplemented from FIX::Acceptor.

Definition at line 72 of file ThreadedSocketAcceptor.h.

typedef std::set< int > FIX::ThreadedSocketAcceptor::Sockets [private]

Definition at line 71 of file ThreadedSocketAcceptor.h.

typedef std::map< int, int > FIX::ThreadedSocketAcceptor::SocketToPort [private]

Definition at line 74 of file ThreadedSocketAcceptor.h.

typedef std::map< int, thread_id > FIX::ThreadedSocketAcceptor::SocketToThread [private]

Definition at line 75 of file ThreadedSocketAcceptor.h.


Constructor & Destructor Documentation

Definition at line 32 of file ThreadedSocketAcceptor.cpp.

References FIX::socket_init().

: Acceptor( application, factory, settings )
{ socket_init(); }
FIX::ThreadedSocketAcceptor::ThreadedSocketAcceptor ( Application application,
MessageStoreFactory factory,
const SessionSettings settings,
LogFactory logFactory 
) throw ( ConfigError )

Definition at line 39 of file ThreadedSocketAcceptor.cpp.

References FIX::socket_init().

: Acceptor( application, factory, settings, logFactory )
{ 
  socket_init(); 
}

Definition at line 49 of file ThreadedSocketAcceptor.cpp.

References FIX::socket_term().

{ 
  socket_term(); 
}

Member Function Documentation

void FIX::ThreadedSocketAcceptor::addThread ( int  s,
thread_id  t 
) [private]

Definition at line 168 of file ThreadedSocketAcceptor.cpp.

References m_mutex, and m_threads.

Referenced by onStart().

{
  Locker l(m_mutex);

  m_threads[ s ] = t;
}
void FIX::ThreadedSocketAcceptor::onConfigure ( const SessionSettings ) throw ( ConfigError ) [private, virtual]

Implemented to configure acceptor.

Reimplemented from FIX::Acceptor.

Definition at line 54 of file ThreadedSocketAcceptor.cpp.

References FIX::Dictionary::getBool(), FIX::Dictionary::getInt(), FIX::Dictionary::has(), FIX::SOCKET_ACCEPT_PORT, FIX::SOCKET_NODELAY, and FIX::SOCKET_REUSE_ADDRESS.

{
  std::set<SessionID> sessions = s.getSessions();
  std::set<SessionID>::iterator i;
  for( i = sessions.begin(); i != sessions.end(); ++i )
  {
    const Dictionary& settings = s.get( *i );
    settings.getInt( SOCKET_ACCEPT_PORT );
    if( settings.has(SOCKET_REUSE_ADDRESS) )
      settings.getBool( SOCKET_REUSE_ADDRESS );
    if( settings.has(SOCKET_NODELAY) )
      settings.getBool( SOCKET_NODELAY );
  }
}
void FIX::ThreadedSocketAcceptor::onInitialize ( const SessionSettings ) throw ( RuntimeError ) [private, virtual]

Implemented to initialize acceptor.

Reimplemented from FIX::Acceptor.

Definition at line 70 of file ThreadedSocketAcceptor.cpp.

References FIX::IntConvertor::convert(), FIX::Dictionary::getBool(), FIX::Dictionary::getInt(), FIX::Dictionary::has(), FIX::SOCKET_ACCEPT_PORT, FIX::socket_close(), FIX::socket_createAcceptor(), FIX::SOCKET_NODELAY, FIX::SOCKET_RECEIVE_BUFFER_SIZE, FIX::SOCKET_REUSE_ADDRESS, FIX::SOCKET_SEND_BUFFER_SIZE, and FIX::socket_setsockopt().

{
  short port = 0;
  std::set<int> ports;

  std::set<SessionID> sessions = s.getSessions();
  std::set<SessionID>::iterator i = sessions.begin();
  for( ; i != sessions.end(); ++i )
  {
    const Dictionary& settings = s.get( *i );
    port = (short)settings.getInt( SOCKET_ACCEPT_PORT );

    m_portToSessions[port].insert( *i );

    if( ports.find(port) != ports.end() )
      continue;
    ports.insert( port );

    const bool reuseAddress = settings.has( SOCKET_REUSE_ADDRESS ) ? 
      settings.getBool( SOCKET_REUSE_ADDRESS ) : true;

    const bool noDelay = settings.has( SOCKET_NODELAY ) ? 
      settings.getBool( SOCKET_NODELAY ) : false;

    const int sendBufSize = settings.has( SOCKET_SEND_BUFFER_SIZE ) ?
      settings.getInt( SOCKET_SEND_BUFFER_SIZE ) : 0;

    const int rcvBufSize = settings.has( SOCKET_RECEIVE_BUFFER_SIZE ) ?
      settings.getInt( SOCKET_RECEIVE_BUFFER_SIZE ) : 0;

    int socket = socket_createAcceptor( port, reuseAddress );
    if( socket < 0 )
    {
      SocketException e;
      socket_close( socket );
      throw RuntimeError( "Unable to create, bind, or listen to port " 
                         + IntConvertor::convert( (unsigned short)port ) + " (" + e.what() + ")" );
    }
    if( noDelay )
      socket_setsockopt( socket, TCP_NODELAY );
    if( sendBufSize )
      socket_setsockopt( socket, SO_SNDBUF, sendBufSize );
    if( rcvBufSize )
      socket_setsockopt( socket, SO_RCVBUF, rcvBufSize );

    m_socketToPort[socket] = port;
    m_sockets.insert( socket );
  }    
}
bool FIX::ThreadedSocketAcceptor::onPoll ( double  second) [private, virtual]

Implemented to connect and poll for events.

Implements FIX::Acceptor.

Definition at line 135 of file ThreadedSocketAcceptor.cpp.

{
  return false;
}
void FIX::ThreadedSocketAcceptor::onStart ( ) [private, virtual]

Implemented to start listening for connections.

Implements FIX::Acceptor.

Definition at line 121 of file ThreadedSocketAcceptor.cpp.

References addThread(), m_mutex, m_sockets, m_socketToPort, socketAcceptorThread(), and FIX::thread_spawn().

{
  Sockets::iterator i;
  for( i = m_sockets.begin(); i != m_sockets.end(); ++i )
  {
    Locker l( m_mutex );
    int port = m_socketToPort[*i];
    AcceptorThreadInfo* info = new AcceptorThreadInfo( this, *i, port );
    thread_id thread;
    thread_spawn( &socketAcceptorThread, info, thread );
    addThread( *i, thread );
  }
}
void FIX::ThreadedSocketAcceptor::onStop ( ) [private, virtual]

Implemented to stop a running acceptor.

Implements FIX::Acceptor.

Definition at line 140 of file ThreadedSocketAcceptor.cpp.

References FIX::Acceptor::isLoggedOn(), m_mutex, m_threads, FIX::socket_close(), FIX::Acceptor::start(), and FIX::thread_join().

{ 
  SocketToThread threads;
  SocketToThread::iterator i;

  {
    Locker l(m_mutex);

    time_t start = 0;
    time_t now = 0;

    ::time( &start );
    while ( isLoggedOn() )
    {
      if( ::time(&now) -5 >= start )
        break;
    }

    threads = m_threads;
    m_threads.clear();
  }

  for ( i = threads.begin(); i != threads.end(); ++i )
    socket_close( i->first );
  for ( i = threads.begin(); i != threads.end(); ++i )
    thread_join( i->second );
}
void FIX::ThreadedSocketAcceptor::removeThread ( int  s) [private]

Definition at line 175 of file ThreadedSocketAcceptor.cpp.

References m_mutex, m_threads, and FIX::thread_detach().

{
  Locker l(m_mutex);
  SocketToThread::iterator i = m_threads.find( s );
  if ( i != m_threads.end() )
  {
    thread_detach( i->second );
    m_threads.erase( i );
  }
}

Definition at line 186 of file ThreadedSocketAcceptor.cpp.

References FIX::ThreadedSocketAcceptor::AcceptorThreadInfo::m_pAcceptor, FIX::ThreadedSocketAcceptor::AcceptorThreadInfo::m_port, FIX::ThreadedSocketAcceptor::AcceptorThreadInfo::m_socket, FIX::socket_accept(), FIX::socket_getsockopt(), FIX::socket_peername(), FIX::socket_setsockopt(), socketConnectionThread(), and FIX::thread_spawn().

Referenced by onStart().

{
  AcceptorThreadInfo * info = reinterpret_cast < AcceptorThreadInfo* > ( p );

  ThreadedSocketAcceptor* pAcceptor = info->m_pAcceptor;
  int s = info->m_socket;
  int port = info->m_port;
  delete info;

  int noDelay = 0;
  int sendBufSize = 0;
  int rcvBufSize = 0;
  socket_getsockopt( s, TCP_NODELAY, noDelay );
  socket_getsockopt( s, SO_SNDBUF, sendBufSize );
  socket_getsockopt( s, SO_RCVBUF, rcvBufSize );

  int socket = 0;
  while ( ( !pAcceptor->isStopped() && ( socket = socket_accept( s ) ) >= 0 ) )
  {
    if( noDelay )
      socket_setsockopt( socket, TCP_NODELAY );
    if( sendBufSize )
      socket_setsockopt( socket, SO_SNDBUF, sendBufSize );
    if( rcvBufSize )
      socket_setsockopt( socket, SO_RCVBUF, rcvBufSize );

    Sessions sessions = pAcceptor->m_portToSessions[port];

    ThreadedSocketConnection * pConnection =
      new ThreadedSocketConnection
        ( socket, sessions, pAcceptor->getLog() );

    ConnectionThreadInfo* info = new ConnectionThreadInfo( pAcceptor, pConnection );

    {
      Locker l( pAcceptor->m_mutex );

      std::stringstream stream;
      stream << "Accepted connection from " << socket_peername( socket ) << " on port " << port;

      if( pAcceptor->getLog() )
        pAcceptor->getLog()->onEvent( stream.str() );

      thread_id thread;
      if ( !thread_spawn( &socketConnectionThread, info, thread ) )
        delete info;
      pAcceptor->addThread( socket, thread );
    }
  }

  if( !pAcceptor->isStopped() )
    pAcceptor->removeThread( s );

  return 0;
}

Definition at line 242 of file ThreadedSocketAcceptor.cpp.

References FIX::ThreadedSocketConnection::getSocket(), FIX::ThreadedSocketAcceptor::ConnectionThreadInfo::m_pAcceptor, FIX::ThreadedSocketAcceptor::ConnectionThreadInfo::m_pConnection, and FIX::ThreadedSocketConnection::read().

Referenced by socketAcceptorThread().

{
  ConnectionThreadInfo * info = reinterpret_cast < ConnectionThreadInfo* > ( p );

  ThreadedSocketAcceptor* pAcceptor = info->m_pAcceptor;
  ThreadedSocketConnection* pConnection = info->m_pConnection;
  delete info;

  int socket = pConnection->getSocket();

  while ( pConnection->read() ) {}
  delete pConnection;
  if( !pAcceptor->isStopped() )
    pAcceptor->removeThread( socket );
  return 0;
}

Friends And Related Function Documentation

friend class SocketConnection [friend]

Definition at line 38 of file ThreadedSocketAcceptor.h.


Member Data Documentation

Definition at line 93 of file ThreadedSocketAcceptor.h.

Referenced by addThread(), onStart(), onStop(), and removeThread().

Definition at line 90 of file ThreadedSocketAcceptor.h.

Definition at line 89 of file ThreadedSocketAcceptor.h.

Referenced by onStart().

Definition at line 91 of file ThreadedSocketAcceptor.h.

Referenced by onStart().

Definition at line 92 of file ThreadedSocketAcceptor.h.

Referenced by addThread(), onStop(), and removeThread().


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

Generated on Mon Sep 15 2014 01:23:57 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001