Public Member Functions | Private Types | Private Member Functions | Private Attributes | Friends
FIX::SocketAcceptor Class Reference

Socket implementation of Acceptor. More...

#include <SocketAcceptor.h>

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

List of all members.

Public Member Functions

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

Private Types

typedef std::set< SessionIDSessions
typedef std::map< int, SessionsPortToSessions
typedef std::map< int,
SocketConnection * > 
SocketConnections

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 onConnect (SocketServer &, int, int)
void onWrite (SocketServer &, int)
bool onData (SocketServer &, int)
void onDisconnect (SocketServer &, int)
void onError (SocketServer &)
void onTimeout (SocketServer &)

Private Attributes

SocketServerm_pServer
PortToSessions m_portToSessions
SocketConnections m_connections

Friends

class SocketConnection

Detailed Description

Socket implementation of Acceptor.

Definition at line 36 of file SocketAcceptor.h.


Member Typedef Documentation

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

Definition at line 51 of file SocketAcceptor.h.

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

Reimplemented from FIX::Acceptor.

Definition at line 50 of file SocketAcceptor.h.

typedef std::map< int, SocketConnection* > FIX::SocketAcceptor::SocketConnections [private]

Definition at line 52 of file SocketAcceptor.h.


Constructor & Destructor Documentation

FIX::SocketAcceptor::SocketAcceptor ( Application application,
MessageStoreFactory factory,
const SessionSettings settings 
) throw ( ConfigError )

Definition at line 34 of file SocketAcceptor.cpp.

: Acceptor( application, factory, settings ),
  m_pServer( 0 ) {}
FIX::SocketAcceptor::SocketAcceptor ( Application application,
MessageStoreFactory factory,
const SessionSettings settings,
LogFactory logFactory 
) throw ( ConfigError )

Definition at line 40 of file SocketAcceptor.cpp.

: Acceptor( application, factory, settings, logFactory ),
  m_pServer( 0 ) 
{
}

Definition at line 49 of file SocketAcceptor.cpp.

References m_connections.

{
  SocketConnections::iterator iter;
  for ( iter = m_connections.begin(); iter != m_connections.end(); ++iter )
    delete iter->second;
}

Member Function Documentation

void FIX::SocketAcceptor::onConfigure ( const SessionSettings ) throw ( ConfigError ) [private, virtual]

Implemented to configure acceptor.

Reimplemented from FIX::Acceptor.

Definition at line 56 of file SocketAcceptor.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::SocketAcceptor::onConnect ( SocketServer server,
int  a,
int  s 
) [private, virtual]

Implements FIX::SocketServer::Strategy.

Definition at line 166 of file SocketAcceptor.cpp.

References FIX::Acceptor::getLog(), FIX::SocketServer::getMonitor(), m_connections, m_portToSessions, FIX::Log::onEvent(), FIX::socket_isValid(), FIX::socket_peername(), SocketConnection, and FIX::SocketServer::socketToPort().

{
  if ( !socket_isValid( s ) ) return;
  SocketConnections::iterator i = m_connections.find( s );
  if ( i != m_connections.end() ) return;
  int port = server.socketToPort( a );
  Sessions sessions = m_portToSessions[port];
  m_connections[ s ] = new SocketConnection( s, sessions, &server.getMonitor() );

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

  if( getLog() )
    getLog()->onEvent( stream.str() );
}
bool FIX::SocketAcceptor::onData ( SocketServer server,
int  s 
) [private, virtual]

Implements FIX::SocketServer::Strategy.

Definition at line 191 of file SocketAcceptor.cpp.

References m_connections, and FIX::SocketConnection::read().

{
  SocketConnections::iterator i = m_connections.find( s );
  if ( i == m_connections.end() ) return false;
  SocketConnection* pSocketConnection = i->second;
  return pSocketConnection->read( *this, server );
}
void FIX::SocketAcceptor::onDisconnect ( SocketServer ,
int  s 
) [private, virtual]

Implements FIX::SocketServer::Strategy.

Definition at line 199 of file SocketAcceptor.cpp.

References FIX::Session::disconnect(), FIX::SocketConnection::getSession(), and m_connections.

{
  SocketConnections::iterator i = m_connections.find( s );
  if ( i == m_connections.end() ) return ;
  SocketConnection* pSocketConnection = i->second;

  Session* pSession = pSocketConnection->getSession();
  if ( pSession ) pSession->disconnect();

  delete pSocketConnection;
  m_connections.erase( s );
}
void FIX::SocketAcceptor::onError ( SocketServer ) [private, virtual]

Implements FIX::SocketServer::Strategy.

Definition at line 212 of file SocketAcceptor.cpp.

{
}
void FIX::SocketAcceptor::onInitialize ( const SessionSettings ) throw ( RuntimeError ) [private, virtual]

Implemented to initialize acceptor.

Reimplemented from FIX::Acceptor.

Definition at line 72 of file SocketAcceptor.cpp.

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

{
  short port = 0;

  try
  {
    m_pServer = new SocketServer( 1 );

    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 );

      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;

      m_portToSessions[port].insert( *i );
      m_pServer->add( port, reuseAddress, noDelay, sendBufSize, rcvBufSize );      
    }    
  }
  catch( SocketException& e )
  {
    throw RuntimeError( "Unable to create, bind, or listen to port "
                       + IntConvertor::convert( (unsigned short)port ) + " (" + e.what() + ")" );
  }
}
bool FIX::SocketAcceptor::onPoll ( double  second) [private, virtual]

Implemented to connect and poll for events.

Implements FIX::Acceptor.

Definition at line 134 of file SocketAcceptor.cpp.

References FIX::SocketServer::block(), FIX::Acceptor::isLoggedOn(), FIX::Acceptor::isStopped(), m_pServer, and FIX::Acceptor::start().

{
  if( !m_pServer )
    return false;

  time_t start = 0;
  time_t now = 0;

  if( isStopped() )
  {
    if( start == 0 )
      ::time( &start );
    if( !isLoggedOn() )
    {
      start = 0;
      return false;
    }
    if( ::time(&now) - 5 >= start )
    {
      start = 0;
      return false;
    }
  }

  m_pServer->block( *this, true, timeout );
  return true;
}
void FIX::SocketAcceptor::onStart ( ) [private, virtual]

Implemented to start listening for connections.

Implements FIX::Acceptor.

Definition at line 111 of file SocketAcceptor.cpp.

References FIX::SocketServer::block(), FIX::SocketServer::close(), FIX::Acceptor::isLoggedOn(), FIX::Acceptor::isStopped(), m_pServer, and FIX::Acceptor::start().

{
  while ( !isStopped() && m_pServer && m_pServer->block( *this ) ) {}

  if( !m_pServer )
    return;

  time_t start = 0;
  time_t now = 0;

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

  m_pServer->close();
  delete m_pServer;
  m_pServer = 0;
}
void FIX::SocketAcceptor::onStop ( ) [private, virtual]

Implemented to stop a running acceptor.

Implements FIX::Acceptor.

Definition at line 162 of file SocketAcceptor.cpp.

{
}
void FIX::SocketAcceptor::onTimeout ( SocketServer ) [private, virtual]

Reimplemented from FIX::SocketServer::Strategy.

Definition at line 216 of file SocketAcceptor.cpp.

References m_connections.

{
  SocketConnections::iterator i;
  for ( i = m_connections.begin(); i != m_connections.end(); ++i )
    i->second->onTimeout();
}
void FIX::SocketAcceptor::onWrite ( SocketServer server,
int  s 
) [private, virtual]

Implements FIX::SocketServer::Strategy.

Definition at line 182 of file SocketAcceptor.cpp.

References m_connections, FIX::SocketConnection::processQueue(), and FIX::SocketConnection::unsignal().

{
  SocketConnections::iterator i = m_connections.find( s );
  if ( i == m_connections.end() ) return ;
  SocketConnection* pSocketConnection = i->second;
  if( pSocketConnection->processQueue() )
    pSocketConnection->unsignal();
}
bool FIX::SocketAcceptor::readSettings ( const SessionSettings ) [private]

Friends And Related Function Documentation

friend class SocketConnection [friend]

Definition at line 38 of file SocketAcceptor.h.

Referenced by onConnect().


Member Data Documentation

Definition at line 70 of file SocketAcceptor.h.

Referenced by onConnect(), onData(), onDisconnect(), onTimeout(), onWrite(), and ~SocketAcceptor().

Definition at line 69 of file SocketAcceptor.h.

Referenced by onConnect().

Definition at line 68 of file SocketAcceptor.h.

Referenced by onPoll(), and onStart().


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

Generated on Mon Jun 23 2014 23:49:39 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001