FIX::SocketInitiator Class Reference

Socket implementation of Initiator. More...

`#include <SocketInitiator.h>`

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

## Public Member Functions

| SocketInitiator (Application &, MessageStoreFactory &, const SessionSettings &) throw ( ConfigError )
| SocketInitiator (Application &, MessageStoreFactory &, const SessionSettings &, LogFactory &) throw ( ConfigError )
virtual | ~SocketInitiator ()
Public Member Functions inherited from FIX::Initiator
| Initiator (Application &, MessageStoreFactory &, const SessionSettings &) throw ( ConfigError )
| Initiator (Application &, MessageStoreFactory &, const SessionSettings &, LogFactory &) throw ( ConfigError )
virtual | ~Initiator ()
void | start () throw ( ConfigError, RuntimeError )
| Start initiator. More...
void | block () throw ( ConfigError, RuntimeError )
| Block on the initiator. More...
bool | poll (double timeout=0.0) throw ( ConfigError, RuntimeError )
| Poll the initiator. More...
void | stop (bool force=false)
| Stop initiator. More...
bool | isLoggedOn ()
| Check to see if any sessions are currently logged on. More...
Session * | getSession (const SessionID &sessionID, Responder &)
const std::set< SessionID > & | getSessions () const
Session * | getSession (const SessionID &sessionID) const
const Dictionary *const | getSessionSettings (const SessionID &sessionID) const
bool | has (const SessionID &id)
bool | isStopped ()
Application & | getApplication ()
MessageStoreFactory & | getMessageStoreFactory ()
Log * | getLog ()

## 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. More...
void | onInitialize (const SessionSettings &) throw ( RuntimeError )
| Implemented to initialize initiator. More...
void | onStart ()
| Implemented to start connecting to targets. More...
bool | onPoll (double timeout)
| Implemented to connect and poll for events. More...
void | onStop ()
| Implemented to stop a running initiator. More...
void | doConnect (const SessionID &, const Dictionary &d)
| Implemented to connect a session to its target. More...
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 &, std::string &, short &)
Private Member Functions inherited from FIX::SocketConnector::Strategy
virtual | ~Strategy ()

## 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

## Additional Inherited Members

Protected Member Functions inherited from FIX::Initiator
void | setPending (const SessionID &)
void | setConnected (const SessionID &)
void | setDisconnected (const SessionID &)
bool | isPending (const SessionID &)
bool | isConnected (const SessionID &)
bool | isDisconnected (const SessionID &)
void | connect ()
Protected Attributes inherited from FIX::Initiator
SessionSettings | m_settings

## Detailed Description

Socket implementation of Initiator.

Definition at line 36 of file SocketInitiator.h.

## Member Typedef Documentation

## SessionToHostNum

typedef std::map< SessionID, int > FIX::SocketInitiator::SessionToHostNum

private

Definition at line 48 of file SocketInitiator.h.

## SocketConnections

typedef std::map< int, SocketConnection* > FIX::SocketInitiator::SocketConnections

private

Definition at line 47 of file SocketInitiator.h.

## Constructor & Destructor Documentation

## SocketInitiator() [1/2]

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

Definition at line 32 of file SocketInitiator.cpp.

: Initiator( application, factory, settings ),
m_connector( 1 ), m_lastConnect( 0 ),
m_reconnectInterval( 30 ), m_noDelay( false ), m_sendBufSize( 0 ),
m_rcvBufSize( 0 )
{
}
FIX::Initiator::Initiator
Initiator(Application &, MessageStoreFactory &, const SessionSettings &)
Definition: Initiator.cpp:36
FIX::SocketInitiator::m_rcvBufSize
int m_rcvBufSize
Definition: SocketInitiator.h:76
FIX::SocketInitiator::m_sendBufSize
int m_sendBufSize
Definition: SocketInitiator.h:75
FIX::SocketInitiator::m_connector
SocketConnector m_connector
Definition: SocketInitiator.h:69
FIX::SocketInitiator::m_lastConnect
time_t m_lastConnect
Definition: SocketInitiator.h:72
FIX::SocketInitiator::m_noDelay
bool m_noDelay
Definition: SocketInitiator.h:74
FIX::SocketInitiator::m_reconnectInterval
int m_reconnectInterval
Definition: SocketInitiator.h:73

## SocketInitiator() [2/2]

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

Definition at line 43 of file SocketInitiator.cpp.

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

## ~SocketInitiator()

FIX::SocketInitiator::~SocketInitiator | ( |  | ) |

virtual

Definition at line 55 of file SocketInitiator.cpp.

{
SocketConnections::iterator i;
for (i = m_connections.begin();
i != m_connections.end(); ++i)
delete i->second;
for (i = m_pendingConnections.begin();
i != m_pendingConnections.end(); ++i)
delete i->second;
}
FIX::SocketInitiator::m_pendingConnections
SocketConnections m_pendingConnections
Definition: SocketInitiator.h:70
FIX::SocketInitiator::m_connections
SocketConnections m_connections
Definition: SocketInitiator.h:71

References m_connections, and m_pendingConnections.

## Member Function Documentation

## doConnect()

void FIX::SocketInitiator::doConnect | ( | const SessionID & | ,
|  | const Dictionary & |
| ) |  |

private virtual

Implemented to connect a session to its target.

Implements FIX::Initiator.

Definition at line 131 of file SocketInitiator.cpp.

{
try
{
std::string address;
short port = 0;
std::string sourceAddress;
short sourcePort = 0;
Session* session = Session::lookupSession( s );
if( !session->isSessionTime(UtcTimeStamp()) ) return;
Log* log = session->getLog();
getHost( s, d, address, port, sourceAddress, sourcePort );
log->onEvent( "Connecting to " + address + " on port " + IntConvertor::convert((unsigned short)port) + " (Source " + sourceAddress + ":" + IntConvertor::convert((unsigned short)sourcePort) + ")");
int result = m_connector.connect( address, port, m_noDelay, m_sendBufSize, m_rcvBufSize, sourceAddress, sourcePort );
setPending( s );
m_pendingConnections[ result ]
= new SocketConnection( *this, s, result, &m_connector.getMonitor() );
}
catch ( std::exception& ) {}
}
FIX::Initiator::setPending
void setPending(const SessionID &)
Definition: Initiator.cpp:145
FIX::Session::lookupSession
static Session * lookupSession(const SessionID &)
Definition: Session.cpp:1496
FIX::SocketConnector::connect
int connect(const std::string &address, int port, bool noDelay, int sendBufSize, int rcvBufSize, const std::string &sourceAddress="", int sourcePort=0)
Definition: SocketConnector.cpp:85
FIX::SocketConnector::getMonitor
SocketMonitor & getMonitor()
Definition: SocketConnector.h:48
FIX::SocketInitiator::getHost
void getHost(const SessionID &, const Dictionary &, std::string &, short &, std::string &, short &)
Definition: SocketInitiator.cpp:234
FIX::TYPE::UtcTimeStamp
@ UtcTimeStamp
Definition: FieldTypes.h:923
FIX::IntConvertor::convert
static std::string convert(signed_int value)
Definition: FieldConvertors.h:153

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(), and FIX::Initiator::setPending().

## getHost()

void FIX::SocketInitiator::getHost | ( | const SessionID & | s,
|  | const Dictionary & | d,
|  | std::string & | address,
|  | short & | port,
|  | std::string & | sourceAddress,
|  | short & | sourcePort
| ) |  |

private

Definition at line 234 of file SocketInitiator.cpp.

{
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();
sourcePort = 0;
sourceAddress.empty();
if( d.has(hostString) && d.has(portString) )
{
address = d.getString( hostString );
port = ( short ) d.getInt( portString );
std::stringstream sourceHostStream;
sourceHostStream << SOCKET_CONNECT_SOURCE_HOST << num;
hostString = sourceHostStream.str();
if( d.has(hostString) )
sourceAddress = d.getString( hostString );
std::stringstream sourcePortStream;
sourcePortStream << SOCKET_CONNECT_SOURCE_PORT << num;
portString = sourcePortStream.str();
if( d.has(portString) )
sourcePort = ( short ) d.getInt( portString );
}
else
{
num = 0;
address = d.getString( SOCKET_CONNECT_HOST );
port = ( short ) d.getInt( SOCKET_CONNECT_PORT );
if( d.has(SOCKET_CONNECT_SOURCE_HOST) )
sourceAddress = d.getString( SOCKET_CONNECT_SOURCE_HOST );
if( d.has(SOCKET_CONNECT_SOURCE_PORT) )
sourcePort = ( short ) d.getInt( SOCKET_CONNECT_SOURCE_PORT );
}
m_sessionToHostNum[ s ] = ++num;
}
FIX::SocketInitiator::m_sessionToHostNum
SessionToHostNum m_sessionToHostNum
Definition: SocketInitiator.h:68
FIX::SOCKET_CONNECT_PORT
const char SOCKET_CONNECT_PORT[]
Definition: SessionSettings.h:65
FIX::SOCKET_CONNECT_HOST
const char SOCKET_CONNECT_HOST[]
Definition: SessionSettings.h:64
FIX::SOCKET_CONNECT_SOURCE_PORT
const char SOCKET_CONNECT_SOURCE_PORT[]
Definition: SessionSettings.h:67
FIX::SOCKET_CONNECT_SOURCE_HOST
const char SOCKET_CONNECT_SOURCE_HOST[]
Definition: SessionSettings.h:66

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

Referenced by doConnect().

## onConfigure()

void FIX::SocketInitiator::onConfigure | ( | const SessionSettings & |  | ) |
throw | ( | ConfigError
| ) |  |

private virtual

Implemented to configure acceptor.

Reimplemented from FIX::Initiator.

Definition at line 67 of file SocketInitiator.cpp.

{
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 );
}
FIX::SOCKET_SEND_BUFFER_SIZE
const char SOCKET_SEND_BUFFER_SIZE[]
Definition: SessionSettings.h:69
FIX::SOCKET_NODELAY
const char SOCKET_NODELAY[]
Definition: SessionSettings.h:68
FIX::SOCKET_RECEIVE_BUFFER_SIZE
const char SOCKET_RECEIVE_BUFFER_SIZE[]
Definition: SessionSettings.h:70
FIX::RECONNECT_INTERVAL
const char RECONNECT_INTERVAL[]
Definition: SessionSettings.h:71

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.

## onConnect()

void FIX::SocketInitiator::onConnect | ( | SocketConnector & | ,
|  | int | s
| ) |  |

private virtual

Implements FIX::SocketConnector::Strategy.

Definition at line 157 of file SocketInitiator.cpp.

{
SocketConnections::iterator i = m_pendingConnections.find( s );
if( i == m_pendingConnections.end() ) return;
SocketConnection* pSocketConnection = i->second;
m_connections[s] = pSocketConnection;
m_pendingConnections.erase( i );
setConnected( pSocketConnection->getSession()->getSessionID() );
pSocketConnection->onTimeout();
}
FIX::Initiator::setConnected
void setConnected(const SessionID &)
Definition: Initiator.cpp:154

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

## onData()

bool FIX::SocketInitiator::onData | ( | SocketConnector & | connector,
|  | int | s
| ) |  |

private virtual

Implements FIX::SocketConnector::Strategy.

Definition at line 178 of file SocketInitiator.cpp.

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

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

## onDisconnect()

void FIX::SocketInitiator::onDisconnect | ( | SocketConnector & | ,
|  | int | s
| ) |  |

private virtual

Implements FIX::SocketConnector::Strategy.

Definition at line 186 of file SocketInitiator.cpp.

{
SocketConnections::iterator i = m_connections.find( s );
SocketConnections::iterator j = m_pendingConnections.find( s );
SocketConnection* pSocketConnection = 0;
if( i != m_connections.end() )
pSocketConnection = i->second;
if( j != m_pendingConnections.end() )
pSocketConnection = j->second;
if( !pSocketConnection )
return;
setDisconnected( pSocketConnection->getSession()->getSessionID() );
Session* pSession = pSocketConnection->getSession();
if ( pSession )
{
pSession->disconnect();
setDisconnected( pSession->getSessionID() );
}
delete pSocketConnection;
m_connections.erase( s );
m_pendingConnections.erase( s );
}
FIX::Initiator::setDisconnected
void setDisconnected(const SessionID &)
Definition: Initiator.cpp:163

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

## onError()

void FIX::SocketInitiator::onError | ( | SocketConnector & | connector | ) |

private virtual

Implements FIX::SocketConnector::Strategy.

Definition at line 213 of file SocketInitiator.cpp.

{
onTimeout( connector );
}
FIX::SocketInitiator::onTimeout
void onTimeout(SocketConnector &)
Definition: SocketInitiator.cpp:218

References onTimeout().

## onInitialize()

void FIX::SocketInitiator::onInitialize | ( | const SessionSettings & |  | ) |
throw | ( | RuntimeError
| ) |  |

private virtual

Implemented to initialize initiator.

Reimplemented from FIX::Initiator.

Definition at line 82 of file SocketInitiator.cpp.

{
}

## onPoll()

bool FIX::SocketInitiator::onPoll | ( | double | timeout | ) |

private virtual

Implemented to connect and poll for events.

Implements FIX::Initiator.

Definition at line 108 of file SocketInitiator.cpp.

{
time_t start = 0;
time_t now = 0;
if( isStopped() )
{
if( start == 0 )
::time( &start );
if( !isLoggedOn() )
return false;
if( ::time(&now) - 5 >= start )
return false;
}
m_connector.block( *this, true, timeout );
return true;
}
FIX::Initiator::isLoggedOn
bool isLoggedOn()
Check to see if any sessions are currently logged on.
Definition: Initiator.cpp:275
FIX::Initiator::start
void start()
Start initiator.
Definition: Initiator.cpp:190
FIX::Initiator::isStopped
bool isStopped()
Definition: Initiator.h:83
FIX::SocketConnector::block
void block(Strategy &strategy, bool poll=0, double timeout=0.0)
Definition: SocketConnector.cpp:114

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

## onStart()

void FIX::SocketInitiator::onStart | ( |  | ) |

private virtual

Implemented to start connecting to targets.

Implements FIX::Initiator.

Definition at line 87 of file SocketInitiator.cpp.

{
connect();
while ( !isStopped() ) {
m_connector.block( *this, false, 1.0 );
onTimeout( m_connector );
}
time_t start = 0;
time_t now = 0;
::time( &start );
while ( isLoggedOn() )
{
m_connector.block( *this );
if( ::time(&now) -5 >= start )
break;
}
}
FIX::Initiator::connect
void connect()
Definition: Initiator.cpp:131

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

## onStop()

void FIX::SocketInitiator::onStop | ( |  | ) |

private virtual

Implemented to stop a running initiator.

Implements FIX::Initiator.

Definition at line 127 of file SocketInitiator.cpp.

{
}

## onTimeout()

void FIX::SocketInitiator::onTimeout | ( | SocketConnector & |  | ) |

private virtual

Reimplemented from FIX::SocketConnector::Strategy.

Definition at line 218 of file SocketInitiator.cpp.

{
time_t now;
::time( &now );
if ( (now - m_lastConnect) >= m_reconnectInterval )
{
connect();
m_lastConnect = now;
}
SocketConnections::iterator i;
for ( i = m_connections.begin(); i != m_connections.end(); ++i )
i->second->onTimeout();
}

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

Referenced by onError(), and onStart().

## onWrite()

void FIX::SocketInitiator::onWrite | ( | SocketConnector & | connector,
|  | int | s
| ) |  |

private virtual

Implements FIX::SocketConnector::Strategy.

Definition at line 169 of file SocketInitiator.cpp.

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

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

## Member Data Documentation

## m_connections

SocketConnections FIX::SocketInitiator::m_connections

private

Definition at line 71 of file SocketInitiator.h.

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

## m_connector

SocketConnector FIX::SocketInitiator::m_connector

private

Definition at line 69 of file SocketInitiator.h.

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

## m_lastConnect

time_t FIX::SocketInitiator::m_lastConnect

private

Definition at line 72 of file SocketInitiator.h.

Referenced by onTimeout().

## m_noDelay

bool FIX::SocketInitiator::m_noDelay

private

Definition at line 74 of file SocketInitiator.h.

Referenced by doConnect().

## m_pendingConnections

SocketConnections FIX::SocketInitiator::m_pendingConnections

private

Definition at line 70 of file SocketInitiator.h.

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

## m_rcvBufSize

int FIX::SocketInitiator::m_rcvBufSize

private

Definition at line 76 of file SocketInitiator.h.

Referenced by doConnect().

## m_reconnectInterval

int FIX::SocketInitiator::m_reconnectInterval

private

Definition at line 73 of file SocketInitiator.h.

Referenced by onTimeout().

## m_sendBufSize

int FIX::SocketInitiator::m_sendBufSize

private

Definition at line 75 of file SocketInitiator.h.

Referenced by doConnect().

## m_sessionToHostNum

SessionToHostNum FIX::SocketInitiator::m_sessionToHostNum

private

Definition at line 68 of file SocketInitiator.h.

Referenced by getHost().

## m_settings

SessionSettings FIX::SocketInitiator::m_settings

private

Definition at line 67 of file SocketInitiator.h.

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

- SocketInitiator.h
- SocketInitiator.cpp