QuickFIX: FIX::SocketInitiator Class Reference

Index Source Files Annotated Class List Alphabetical Class List Class Hierarchy Graphical Class Hierarchy

# FIX::SocketInitiator Class Reference

Socket implementation of Initiator. More...

`#include <SocketInitiator.h>`

Inheritance diagram for FIX::SocketInitiator:
[legend]
Collaboration diagram for FIX::SocketInitiator:
[legend]

List of all members.

## Public Member Functions
| SocketInitiator (Application &, MessageStoreFactory &, const SessionSettings &) throw ( ConfigError )
| SocketInitiator (Application &, MessageStoreFactory &, const SessionSettings &, LogFactory &) throw ( ConfigError )
virtual | ~SocketInitiator ()
## Private Types
typedef std::map< int, SocketConnection * > | SocketConnections
typedef std::map< SessionID, int > | SessionToHostNum
## 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 &, const Dictionary &d)
| Implemented to connect a session to its target.
void | onConnect (SocketConnector &, int)
void | onWrite (SocketConnector &, int)
bool | onData (SocketConnector &, int)
void | onDisconnect (SocketConnector &, int)
void | onError (SocketConnector &)
void | onTimeout (SocketConnector &)
void | getHost (const SessionID &, const Dictionary &, std::string &, short &)
## Private Attributes
SessionSettings | m_settings
SessionToHostNum | m_sessionToHostNum
SocketConnector | m_connector
SocketConnections | m_pendingConnections
SocketConnections | m_connections
time_t | m_lastConnect
int | m_reconnectInterval
bool | m_noDelay
int | m_sendBufSize
int | m_rcvBufSize
bool | m_stop

## Detailed Description

Socket implementation of Initiator.

Definition at line 36 of file SocketInitiator.h.

## Member Typedef Documentation

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

Definition at line 48 of file SocketInitiator.h.

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

Definition at line 47 of file SocketInitiator.h.

## Constructor & Destructor Documentation

FIX::SocketInitiator::SocketInitiator | ( | Application & | application,
|  | MessageStoreFactory & | factory,
|  | const SessionSettings & | settings |
| ) |  |  | throw ( ConfigError )

Definition at line 33 of file SocketInitiator.cpp.

    00037 : Initiator( application, factory, settings ),
    00038   m_connector( 1 ), m_lastConnect( 0 ),
    00039   m_reconnectInterval( 30 ), m_noDelay( false ), m_sendBufSize( 0 ),
    00040   m_rcvBufSize( 0 )
    00041 {
    00042 }

FIX::SocketInitiator::SocketInitiator | ( | Application & | application,
|  | MessageStoreFactory & | factory,
|  | const SessionSettings & | settings,
|  | LogFactory & | logFactory |
| ) |  |  | throw ( ConfigError )

Definition at line 44 of file SocketInitiator.cpp.

    00049 : Initiator( application, factory, settings, logFactory ),
    00050   m_connector( 1 ), m_lastConnect( 0 ),
    00051   m_reconnectInterval( 30 ), m_noDelay( false ), m_sendBufSize( 0 ),
    00052   m_rcvBufSize( 0 )
    00053 {
    00054 }

FIX::SocketInitiator::~SocketInitiator | ( |  | ) | `[virtual]`

Definition at line 56 of file SocketInitiator.cpp.

References m_connections, and m_pendingConnections.

    00057 {
    00058   SocketConnections::iterator i;
    00059   for (i = m_connections.begin();
    00060        i != m_connections.end(); ++i)
    00061     delete i->second;
    00062
    00063   for (i = m_pendingConnections.begin();
    00064        i != m_pendingConnections.end(); ++i)
    00065     delete i->second;
    00066 }

## Member Function Documentation

void FIX::SocketInitiator::doConnect | ( | const SessionID & | ,
|  | const Dictionary & |  |
| ) |  |  | `[private, virtual]`

Implemented to connect a session to its target.

Implements FIX::Initiator.

Definition at line 139 of file SocketInitiator.cpp.

References FIX::SocketConnector::connect(), FIX::IntConvertor::convert(), getHost(), FIX::Session::getLog(), FIX::SocketConnector::getMonitor(), FIX::Session::isSessionTime(), FIX::Session::lookupSession(), m_connector, m_noDelay, m_pendingConnections, m_rcvBufSize, m_sendBufSize, FIX::Log::onEvent(), QF_STACK_POP, QF_STACK_PUSH, and FIX::Initiator::setPending().

    00140 { QF_STACK_PUSH(SocketInitiator::doConnect)
    00141
    00142   try
    00143   {
    00144     std::string address;
    00145     short port = 0;
    00146     Session* session = Session::lookupSession( s );
    00147     if( !session->isSessionTime(UtcTimeStamp()) ) return;
    00148
    00149     Log* log = session->getLog();
    00150
    00151     getHost( s, d, address, port );
    00152
    00153     log->onEvent( "Connecting to " + address + " on port " + IntConvertor::convert((unsigned short)port) );
    00154     int result = m_connector.connect( address, port, m_noDelay, m_sendBufSize, m_rcvBufSize );
    00155
    00156     if( result != -1 )
    00157     {
    00158       setPending( s );
    00159
    00160       m_pendingConnections[ result ]
    00161         = new SocketConnection( *this, s, result, &m_connector.getMonitor() );
    00162     }
    00163   }
    00164   catch ( std::exception& ) {}
    00165
    00166   QF_STACK_POP
    00167 }

void FIX::SocketInitiator::getHost | ( | const SessionID & | s,
|  | const Dictionary & | d,
|  | std::string & | address,
|  | short & | port |
| ) |  |  | `[private]`

Definition at line 262 of file SocketInitiator.cpp.

References FIX::Dictionary::getLong(), FIX::Dictionary::getString(), FIX::Dictionary::has(), m_sessionToHostNum, QF_STACK_POP, QF_STACK_PUSH, FIX::SOCKET_CONNECT_HOST, and FIX::SOCKET_CONNECT_PORT.

Referenced by doConnect().

    00264 { QF_STACK_PUSH(SocketInitiator::getHost)
    00265
    00266   int num = 0;
    00267   SessionToHostNum::iterator i = m_sessionToHostNum.find( s );
    00268   if ( i != m_sessionToHostNum.end() ) num = i->second;
    00269
    00270   std::stringstream hostStream;
    00271   hostStream << SOCKET_CONNECT_HOST << num;
    00272   std::string hostString = hostStream.str();
    00273
    00274   std::stringstream portStream;
    00275   std::string portString = portStream.str();
    00276   portStream << SOCKET_CONNECT_PORT << num;
    00277
    00278   if( d.has(hostString) && d.has(portString) )
    00279   {
    00280     address = d.getString( hostString );
    00281     port = ( short ) d.getLong( portString );
    00282   }
    00283   else
    00284   {
    00285     num = 0;
    00286     address = d.getString( SOCKET_CONNECT_HOST );
    00287     port = ( short ) d.getLong( SOCKET_CONNECT_PORT );
    00288   }
    00289
    00290   m_sessionToHostNum[ s ] = ++num;
    00291
    00292   QF_STACK_POP
    00293 }

void FIX::SocketInitiator::onConfigure | ( | const SessionSettings & |  | ) | throw ( ConfigError )`[private, virtual]`

Implemented to configure acceptor.

Reimplemented from FIX::Initiator.

Definition at line 68 of file SocketInitiator.cpp.

References QF_STACK_POP, QF_STACK_PUSH, FIX::RECONNECT_INTERVAL, FIX::SOCKET_NODELAY, FIX::SOCKET_RECEIVE_BUFFER_SIZE, and FIX::SOCKET_SEND_BUFFER_SIZE.

    00070 { QF_STACK_PUSH(SocketInitiator::onConfigure)
    00071
    00072   try { m_reconnectInterval = s.get().getLong( RECONNECT_INTERVAL ); }
    00073   catch ( std::exception& ) {}
    00074   if( s.get().has( SOCKET_NODELAY ) )
    00075     m_noDelay = s.get().getBool( SOCKET_NODELAY );
    00076   if( s.get().has( SOCKET_SEND_BUFFER_SIZE ) )
    00077     m_sendBufSize = s.get().getLong( SOCKET_SEND_BUFFER_SIZE );
    00078   if( s.get().has( SOCKET_RECEIVE_BUFFER_SIZE ) )
    00079     m_rcvBufSize = s.get().getLong( SOCKET_RECEIVE_BUFFER_SIZE );
    00080
    00081   QF_STACK_POP
    00082 }

void FIX::SocketInitiator::onConnect | ( | SocketConnector & | ,
|  | int | s |
| ) |  |  | `[private, virtual]`

Implements FIX::SocketConnector::Strategy.

Definition at line 169 of file SocketInitiator.cpp.

References FIX::SocketConnection::getSession(), FIX::Session::getSessionID(), m_connections, m_pendingConnections, FIX::SocketConnection::onTimeout(), QF_STACK_POP, QF_STACK_PUSH, and FIX::Initiator::setConnected().

    00170 { QF_STACK_PUSH(SocketInitiator::onConnect)
    00171
    00172   SocketConnections::iterator i = m_pendingConnections.find( s );
    00173   if( i == m_pendingConnections.end() ) return;
    00174   SocketConnection* pSocketConnection = i->second;
    00175
    00176   m_connections[s] = pSocketConnection;
    00177   m_pendingConnections.erase( i );
    00178   setConnected( pSocketConnection->getSession()->getSessionID() );
    00179   pSocketConnection->onTimeout();
    00180
    00181   QF_STACK_POP
    00182 }

bool FIX::SocketInitiator::onData | ( | SocketConnector & | connector,
|  | int | s |
| ) |  |  | `[private, virtual]`

Implements FIX::SocketConnector::Strategy.

Definition at line 196 of file SocketInitiator.cpp.

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

    00197 { QF_STACK_PUSH(SocketInitiator::onData)
    00198
    00199   SocketConnections::iterator i = m_connections.find( s );
    00200   if ( i == m_connections.end() ) return false;
    00201   SocketConnection* pSocketConnection = i->second;
    00202   return pSocketConnection->read( connector );
    00203
    00204   QF_STACK_POP
    00205 }

void FIX::SocketInitiator::onDisconnect | ( | SocketConnector & | ,
|  | int | s |
| ) |  |  | `[private, virtual]`

Implements FIX::SocketConnector::Strategy.

Definition at line 207 of file SocketInitiator.cpp.

References FIX::Session::disconnect(), FIX::SocketConnection::getSession(), FIX::Session::getSessionID(), m_connections, m_pendingConnections, QF_STACK_POP, QF_STACK_PUSH, and FIX::Initiator::setDisconnected().

    00208 { QF_STACK_PUSH(SocketInitiator::onDisconnect)
    00209
    00210   SocketConnections::iterator i = m_connections.find( s );
    00211   SocketConnections::iterator j = m_pendingConnections.find( s );
    00212
    00213   SocketConnection* pSocketConnection = 0;
    00214   if( i != m_connections.end() )
    00215           pSocketConnection = i->second;
    00216   if( j != m_pendingConnections.end() )
    00217           pSocketConnection = j->second;
    00218   if( !pSocketConnection )
    00219           return;
    00220
    00221   setDisconnected( pSocketConnection->getSession()->getSessionID() );
    00222
    00223   Session* pSession = pSocketConnection->getSession();
    00224   if ( pSession )
    00225   {
    00226     pSession->disconnect();
    00227     setDisconnected( pSession->getSessionID() );
    00228   }
    00229
    00230   delete pSocketConnection;
    00231   m_connections.erase( s );
    00232   m_pendingConnections.erase( s );
    00233
    00234   QF_STACK_POP
    00235 }

void FIX::SocketInitiator::onError | ( | SocketConnector & | connector | ) | `[private, virtual]`

Implements FIX::SocketConnector::Strategy.

Definition at line 237 of file SocketInitiator.cpp.

References onTimeout(), QF_STACK_POP, and QF_STACK_PUSH.

    00238 { QF_STACK_PUSH(SocketInitiator::onError)
    00239   onTimeout( connector );
    00240   QF_STACK_POP
    00241 }

void FIX::SocketInitiator::onInitialize | ( | const SessionSettings & |  | ) | throw ( RuntimeError )`[private, virtual]`

Implemented to initialize initiator.

Reimplemented from FIX::Initiator.

Definition at line 84 of file SocketInitiator.cpp.

References QF_STACK_POP, and QF_STACK_PUSH.

    00086 { QF_STACK_PUSH(SocketInitiator::onInitialize)
    00087   QF_STACK_POP
    00088 }

bool FIX::SocketInitiator::onPoll | ( | double | timeout | ) | `[private, virtual]`

Implemented to connect and poll for events.

Implements FIX::Initiator.

Definition at line 112 of file SocketInitiator.cpp.

References FIX::SocketConnector::block(), FIX::Initiator::isLoggedOn(), FIX::Initiator::isStopped(), m_connector, QF_STACK_POP, QF_STACK_PUSH, and FIX::Initiator::start().

    00113 { QF_STACK_PUSH(SocketInitiator::onPoll)
    00114
    00115   time_t start = 0;
    00116   time_t now = 0;
    00117
    00118   if( isStopped() )
    00119   {
    00120     if( start == 0 )
    00121       ::time( &start );
    00122     if( !isLoggedOn() )
    00123       return false;
    00124     if( ::time(&now) - 5 >= start )
    00125       return false;
    00126   }
    00127
    00128   m_connector.block( *this, true, timeout );
    00129   return true;
    00130
    00131   QF_STACK_POP
    00132 }

void FIX::SocketInitiator::onStart | ( |  | ) | `[private, virtual]`

Implemented to start connecting to targets.

Implements FIX::Initiator.

Definition at line 90 of file SocketInitiator.cpp.

References FIX::SocketConnector::block(), FIX::Initiator::connect(), FIX::Initiator::isLoggedOn(), FIX::Initiator::isStopped(), m_connector, QF_STACK_POP, QF_STACK_PUSH, and FIX::Initiator::start().

    00091 { QF_STACK_PUSH(SocketInitiator::onStart)
    00092
    00093   connect();
    00094
    00095   while ( !isStopped() )
    00096     m_connector.block( *this );
    00097
    00098   time_t start = 0;
    00099   time_t now = 0;
    00100
    00101   ::time( &start );
    00102   while ( isLoggedOn() )
    00103   {
    00104     m_connector.block( *this );
    00105     if( ::time(&now) -5 >= start )
    00106       break;
    00107   }
    00108
    00109   QF_STACK_POP
    00110 }

void FIX::SocketInitiator::onStop | ( |  | ) | `[private, virtual]`

Implemented to stop a running initiator.

Implements FIX::Initiator.

Definition at line 134 of file SocketInitiator.cpp.

References QF_STACK_POP, and QF_STACK_PUSH.

    00135 { QF_STACK_PUSH(SocketInitiator::onStop)
    00136   QF_STACK_POP
    00137 }

void FIX::SocketInitiator::onTimeout | ( | SocketConnector & |  | ) | `[private, virtual]`

Reimplemented from FIX::SocketConnector::Strategy.

Definition at line 243 of file SocketInitiator.cpp.

References FIX::Initiator::connect(), m_connections, m_lastConnect, m_reconnectInterval, QF_STACK_POP, and QF_STACK_PUSH.

Referenced by onError().

    00244 { QF_STACK_PUSH(SocketInitiator::onTimeout)
    00245
    00246   time_t now;
    00247   ::time( &now );
    00248
    00249   if ( (now - m_lastConnect) >= m_reconnectInterval )
    00250   {
    00251     connect();
    00252     m_lastConnect = now;
    00253   }
    00254
    00255   SocketConnections::iterator i;
    00256   for ( i = m_connections.begin(); i != m_connections.end(); ++i )
    00257     i->second->onTimeout();
    00258
    00259   QF_STACK_POP
    00260 }

void FIX::SocketInitiator::onWrite | ( | SocketConnector & | connector,
|  | int | s |
| ) |  |  | `[private, virtual]`

Implements FIX::SocketConnector::Strategy.

Definition at line 184 of file SocketInitiator.cpp.

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

    00185 { QF_STACK_PUSH(SocketInitiator::onWrite)
    00186
    00187   SocketConnections::iterator i = m_connections.find( s );
    00188   if ( i == m_connections.end() ) return ;
    00189   SocketConnection* pSocketConnection = i->second;
    00190   if( pSocketConnection->processQueue() )
    00191     pSocketConnection->unsignal();
    00192
    00193   QF_STACK_POP
    00194 }

## Member Data Documentation

SocketConnections FIX::SocketInitiator::m_connections`[private]`

Definition at line 71 of file SocketInitiator.h.

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

SocketConnector FIX::SocketInitiator::m_connector`[private]`

Definition at line 69 of file SocketInitiator.h.

Referenced by doConnect(), onPoll(), and onStart().

time_t FIX::SocketInitiator::m_lastConnect`[private]`

Definition at line 72 of file SocketInitiator.h.

Referenced by onTimeout().

bool FIX::SocketInitiator::m_noDelay`[private]`

Definition at line 74 of file SocketInitiator.h.

Referenced by doConnect().

SocketConnections FIX::SocketInitiator::m_pendingConnections`[private]`

Definition at line 70 of file SocketInitiator.h.

Referenced by doConnect(), onConnect(), onDisconnect(), and ~SocketInitiator().

int FIX::SocketInitiator::m_rcvBufSize`[private]`

Definition at line 76 of file SocketInitiator.h.

Referenced by doConnect().

int FIX::SocketInitiator::m_reconnectInterval`[private]`

Definition at line 73 of file SocketInitiator.h.

Referenced by onTimeout().

int FIX::SocketInitiator::m_sendBufSize`[private]`

Definition at line 75 of file SocketInitiator.h.

Referenced by doConnect().

SessionToHostNum FIX::SocketInitiator::m_sessionToHostNum`[private]`

Definition at line 68 of file SocketInitiator.h.

Referenced by getHost().

SessionSettings FIX::SocketInitiator::m_settings`[private]`

Reimplemented from FIX::Initiator.

Definition at line 67 of file SocketInitiator.h.

bool FIX::SocketInitiator::m_stop`[private]`

Reimplemented from FIX::Initiator.

Definition at line 77 of file SocketInitiator.h.

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

- /home/omiller/Development/autobuild/quickfix/src/C++/SocketInitiator.h
- /home/omiller/Development/autobuild/quickfix/src/C++/SocketInitiator.cpp