Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes
FIX::ThreadedSocketInitiator Class Reference

Threaded Socket implementation of Initiator. More...

#include <ThreadedSocketInitiator.h>

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

List of all members.

Public Member Functions

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

Private Types

typedef std::map< int, thread_idSocketToThread
typedef std::map< SessionID, int > SessionToHostNum
typedef std::pair
< ThreadedSocketInitiator
*, ThreadedSocketConnection * > 
ThreadPair

Private Member Functions

void onConfigure (const SessionSettings &) throw ( ConfigError )
 Implemented to configure acceptor.
void onInitialize (const SessionSettings &) throw ( RuntimeError )
 Implemented to initialize initiator.
void onStart ()
 Implemented to start connecting to targets.
bool onPoll (double timeout)
 Implemented to connect and poll for events.
void onStop ()
 Implemented to stop a running initiator.
void doConnect (const SessionID &s, const Dictionary &d)
 Implemented to connect a session to its target.
void addThread (int s, thread_id t)
void removeThread (int s)
void lock ()
void getHost (const SessionID &, const Dictionary &, std::string &, short &)

Static Private Member Functions

static THREAD_PROC socketThread (void *p)

Private Attributes

SessionSettings m_settings
SessionToHostNum m_sessionToHostNum
time_t m_lastConnect
int m_reconnectInterval
bool m_noDelay
int m_sendBufSize
int m_rcvBufSize
SocketToThread m_threads
Mutex m_mutex

Detailed Description

Threaded Socket implementation of Initiator.

Definition at line 39 of file ThreadedSocketInitiator.h.


Member Typedef Documentation

typedef std::map< SessionID, int > FIX::ThreadedSocketInitiator::SessionToHostNum [private]

Definition at line 52 of file ThreadedSocketInitiator.h.

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

Definition at line 51 of file ThreadedSocketInitiator.h.

Definition at line 53 of file ThreadedSocketInitiator.h.


Constructor & Destructor Documentation

Definition at line 32 of file ThreadedSocketInitiator.cpp.

References FIX::socket_init().

: Initiator( application, factory, settings ),
  m_lastConnect( 0 ), m_reconnectInterval( 30 ), m_noDelay( false ), 
  m_sendBufSize( 0 ), m_rcvBufSize( 0 ) 
{ 
  socket_init(); 
}
FIX::ThreadedSocketInitiator::ThreadedSocketInitiator ( Application application,
MessageStoreFactory factory,
const SessionSettings settings,
LogFactory logFactory 
) throw ( ConfigError )

Definition at line 43 of file ThreadedSocketInitiator.cpp.

References FIX::socket_init().

: Initiator( application, factory, settings, logFactory ),
  m_lastConnect( 0 ), m_reconnectInterval( 30 ), m_noDelay( false ), 
  m_sendBufSize( 0 ), m_rcvBufSize( 0 ) 
{ 
  socket_init(); 
}

Definition at line 55 of file ThreadedSocketInitiator.cpp.

References FIX::socket_term().

{ 
  socket_term(); 
}

Member Function Documentation

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

Definition at line 181 of file ThreadedSocketInitiator.cpp.

References m_mutex, and m_threads.

Referenced by doConnect().

{
  Locker l(m_mutex);

  m_threads[ s ] = t;
}
void FIX::ThreadedSocketInitiator::doConnect ( const SessionID ,
const Dictionary  
) [private, virtual]

Implemented to connect a session to its target.

Implements FIX::Initiator.

Definition at line 133 of file ThreadedSocketInitiator.cpp.

References addThread(), FIX::IntConvertor::convert(), FIX::ThreadedSocketConnection::disconnect(), getHost(), FIX::Initiator::getLog(), FIX::Session::getLog(), FIX::Session::isSessionTime(), FIX::Session::lookupSession(), m_mutex, m_noDelay, m_rcvBufSize, m_sendBufSize, FIX::Log::onEvent(), FIX::Initiator::setDisconnected(), FIX::Initiator::setPending(), FIX::socket_createConnector(), FIX::socket_setsockopt(), socketThread(), and FIX::thread_spawn().

{
  try
  {
    Session* session = Session::lookupSession( s );
    if( !session->isSessionTime(UtcTimeStamp()) ) return;

    Log* log = session->getLog();

    std::string address;
    short port = 0;
    getHost( s, d, address, port );

    int socket = socket_createConnector();
    if( m_noDelay )
      socket_setsockopt( socket, TCP_NODELAY );
    if( m_sendBufSize )
      socket_setsockopt( socket, SO_SNDBUF, m_sendBufSize );
    if( m_rcvBufSize )
      socket_setsockopt( socket, SO_RCVBUF, m_rcvBufSize );

    setPending( s );
    log->onEvent( "Connecting to " + address + " on port " + IntConvertor::convert((unsigned short)port) );

    ThreadedSocketConnection* pConnection =
      new ThreadedSocketConnection( s, socket, address, port, getLog() );

    ThreadPair* pair = new ThreadPair( this, pConnection );

    {
      Locker l( m_mutex );
      thread_id thread;
      if ( thread_spawn( &socketThread, pair, thread ) )
      {
        addThread( socket, thread );
      }
      else
      {
        delete pair;
        pConnection->disconnect();
        delete pConnection;
        setDisconnected( s );
      }
    }
  }
  catch ( std::exception& ) {}
}
void FIX::ThreadedSocketInitiator::getHost ( const SessionID s,
const Dictionary d,
std::string &  address,
short &  port 
) [private]

Definition at line 238 of file ThreadedSocketInitiator.cpp.

References FIX::Dictionary::getInt(), FIX::Dictionary::getString(), FIX::Dictionary::has(), m_sessionToHostNum, FIX::SOCKET_CONNECT_HOST, and FIX::SOCKET_CONNECT_PORT.

Referenced by doConnect().

{
  int num = 0;
  SessionToHostNum::iterator i = m_sessionToHostNum.find( s );
  if ( i != m_sessionToHostNum.end() ) num = i->second;

  std::stringstream hostStream;
  hostStream << SOCKET_CONNECT_HOST << num;
  std::string hostString = hostStream.str();

  std::stringstream portStream;
  portStream << SOCKET_CONNECT_PORT << num;
  std::string portString = portStream.str();

  if( d.has(hostString) && d.has(portString) )
  {
    address = d.getString( hostString );
    port = ( short ) d.getInt( portString );
  }
  else
  {
    num = 0;
    address = d.getString( SOCKET_CONNECT_HOST );
    port = ( short ) d.getInt( SOCKET_CONNECT_PORT );
  }

  m_sessionToHostNum[ s ] = ++num;
}
void FIX::ThreadedSocketInitiator::lock ( ) [inline, private]

Definition at line 66 of file ThreadedSocketInitiator.h.

References m_mutex.

{ Locker l(m_mutex); }
void FIX::ThreadedSocketInitiator::onConfigure ( const SessionSettings ) throw ( ConfigError ) [private, virtual]

Implemented to configure acceptor.

Reimplemented from FIX::Initiator.

Definition at line 60 of file ThreadedSocketInitiator.cpp.

References FIX::Dictionary::getBool(), FIX::Dictionary::getInt(), FIX::Dictionary::has(), FIX::RECONNECT_INTERVAL, FIX::SOCKET_NODELAY, FIX::SOCKET_RECEIVE_BUFFER_SIZE, and FIX::SOCKET_SEND_BUFFER_SIZE.

{
  const Dictionary& dict = s.get();

  if( dict.has( RECONNECT_INTERVAL ) )
    m_reconnectInterval = dict.getInt( RECONNECT_INTERVAL );
  if( dict.has( SOCKET_NODELAY ) )
    m_noDelay = dict.getBool( SOCKET_NODELAY );
  if( dict.has( SOCKET_SEND_BUFFER_SIZE ) )
    m_sendBufSize = dict.getInt( SOCKET_SEND_BUFFER_SIZE );
  if( dict.has( SOCKET_RECEIVE_BUFFER_SIZE ) )
    m_rcvBufSize = dict.getInt( SOCKET_RECEIVE_BUFFER_SIZE );
}
void FIX::ThreadedSocketInitiator::onInitialize ( const SessionSettings ) throw ( RuntimeError ) [private, virtual]

Implemented to initialize initiator.

Reimplemented from FIX::Initiator.

Definition at line 75 of file ThreadedSocketInitiator.cpp.

{
}
bool FIX::ThreadedSocketInitiator::onPoll ( double  timeout) [private, virtual]

Implemented to connect and poll for events.

Implements FIX::Initiator.

Definition at line 98 of file ThreadedSocketInitiator.cpp.

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

Implemented to start connecting to targets.

Implements FIX::Initiator.

Definition at line 80 of file ThreadedSocketInitiator.cpp.

References FIX::Initiator::connect(), FIX::Initiator::isStopped(), m_lastConnect, m_mutex, m_reconnectInterval, and FIX::process_sleep().

{
  while ( !isStopped() )
  {
    time_t now;
    ::time( &now );

    if ( (now - m_lastConnect) >= m_reconnectInterval )
    {
      Locker l( m_mutex );
      connect();
      m_lastConnect = now;
    }

    process_sleep( 1 );
  }
}
void FIX::ThreadedSocketInitiator::onStop ( ) [private, virtual]

Implemented to stop a running initiator.

Implements FIX::Initiator.

Definition at line 103 of file ThreadedSocketInitiator.cpp.

References FIX::Initiator::isLoggedOn(), m_mutex, m_threads, FIX::socket_close(), FIX::Initiator::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 );
  threads.clear();
}
void FIX::ThreadedSocketInitiator::removeThread ( int  s) [private]

Definition at line 188 of file ThreadedSocketInitiator.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 200 of file ThreadedSocketInitiator.cpp.

References FIX::ThreadedSocketConnection::connect(), FIX::ThreadedSocketConnection::disconnect(), FIX::ThreadedSocketConnection::getSession(), FIX::Session::getSessionID(), FIX::ThreadedSocketConnection::getSocket(), FIX::Session::lookupSession(), FIX::Session::next(), and FIX::ThreadedSocketConnection::read().

Referenced by doConnect().

{
  ThreadPair * pair = reinterpret_cast < ThreadPair* > ( p );

  ThreadedSocketInitiator* pInitiator = pair->first;
  ThreadedSocketConnection* pConnection = pair->second;
  FIX::SessionID sessionID = pConnection->getSession()->getSessionID();
  FIX::Session* pSession = FIX::Session::lookupSession( sessionID );
  int socket = pConnection->getSocket();
  delete pair;

  pInitiator->lock();

  if( !pConnection->connect() )
  {
    pInitiator->getLog()->onEvent( "Connection failed" );
    pConnection->disconnect();
    delete pConnection;
    pInitiator->removeThread( socket );
    pInitiator->setDisconnected( sessionID );
    return 0;
  }

  pInitiator->setConnected( sessionID );
  pInitiator->getLog()->onEvent( "Connection succeeded" );

  pSession->next();

  while ( pConnection->read() ) {}

  delete pConnection;
  if( !pInitiator->isStopped() )
    pInitiator->removeThread( socket );
  
  pInitiator->setDisconnected( sessionID );
  return 0;
}

Member Data Documentation

Definition at line 73 of file ThreadedSocketInitiator.h.

Referenced by onStart().

Reimplemented from FIX::Initiator.

Definition at line 79 of file ThreadedSocketInitiator.h.

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

Definition at line 75 of file ThreadedSocketInitiator.h.

Referenced by doConnect().

Definition at line 77 of file ThreadedSocketInitiator.h.

Referenced by doConnect().

Definition at line 74 of file ThreadedSocketInitiator.h.

Referenced by onStart().

Definition at line 76 of file ThreadedSocketInitiator.h.

Referenced by doConnect().

Definition at line 72 of file ThreadedSocketInitiator.h.

Referenced by getHost().

Reimplemented from FIX::Initiator.

Definition at line 71 of file ThreadedSocketInitiator.h.

Definition at line 78 of file ThreadedSocketInitiator.h.

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


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

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