Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
FIX::SocketInitiator Class Reference

Socket implementation of Initiator. More...

#include <SocketInitiator.h>

Inheritance diagram for FIX::SocketInitiator:
Inheritance graph
[legend]
Collaboration diagram for FIX::SocketInitiator:
Collaboration graph
[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...
 
SessiongetSession (const SessionID &sessionID, Responder &)
 
const std::set< SessionID > & getSessions () const
 
SessiongetSession (const SessionID &sessionID) const
 
const Dictionary *const getSessionSettings (const SessionID &sessionID) const
 
bool has (const SessionID &id)
 
bool isStopped ()
 
ApplicationgetApplication ()
 
MessageStoreFactorygetMessageStoreFactory ()
 
LoggetLog ()
 

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 &)
 
- 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 ()
 

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 32 of file SocketInitiator.cpp.

36 : Initiator( application, factory, settings ),
37  m_connector( 1 ), m_lastConnect( 0 ),
38  m_reconnectInterval( 30 ), m_noDelay( false ), m_sendBufSize( 0 ),
39  m_rcvBufSize( 0 )
40 {
41 }
SocketConnector m_connector
Initiator(Application &, MessageStoreFactory &, const SessionSettings &)
Definition: Initiator.cpp:36
FIX::SocketInitiator::SocketInitiator ( Application application,
MessageStoreFactory factory,
const SessionSettings settings,
LogFactory logFactory 
)
throw (ConfigError
)

Definition at line 43 of file SocketInitiator.cpp.

48 : Initiator( application, factory, settings, logFactory ),
49  m_connector( 1 ), m_lastConnect( 0 ),
50  m_reconnectInterval( 30 ), m_noDelay( false ), m_sendBufSize( 0 ),
51  m_rcvBufSize( 0 )
52 {
53 }
SocketConnector m_connector
Initiator(Application &, MessageStoreFactory &, const SessionSettings &)
Definition: Initiator.cpp:36
FIX::SocketInitiator::~SocketInitiator ( )
virtual

Definition at line 55 of file SocketInitiator.cpp.

References m_connections, and m_pendingConnections.

56 {
57  SocketConnections::iterator i;
58  for (i = m_connections.begin();
59  i != m_connections.end(); ++i)
60  delete i->second;
61 
62  for (i = m_pendingConnections.begin();
63  i != m_pendingConnections.end(); ++i)
64  delete i->second;
65 }
SocketConnections m_pendingConnections
SocketConnections m_connections

Member Function Documentation

void FIX::SocketInitiator::doConnect ( const SessionID ,
const Dictionary  
)
privatevirtual

Implemented to connect a session to its target.

Implements FIX::Initiator.

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

132 {
133  try
134  {
135  std::string address;
136  short port = 0;
137  Session* session = Session::lookupSession( s );
138  if( !session->isSessionTime(UtcTimeStamp()) ) return;
139 
140  Log* log = session->getLog();
141 
142  getHost( s, d, address, port );
143 
144  log->onEvent( "Connecting to " + address + " on port " + IntConvertor::convert((unsigned short)port) );
145  int result = m_connector.connect( address, port, m_noDelay, m_sendBufSize, m_rcvBufSize );
146  setPending( s );
147 
148  m_pendingConnections[ result ]
149  = new SocketConnection( *this, s, result, &m_connector.getMonitor() );
150  }
151  catch ( std::exception& ) {}
152 }
static Session * lookupSession(const SessionID &)
Definition: Session.cpp:1403
SocketConnections m_pendingConnections
SocketConnector m_connector
static std::string convert(signed_int value)
SocketMonitor & getMonitor()
void getHost(const SessionID &, const Dictionary &, std::string &, short &)
int connect(const std::string &address, int port, bool noDelay, int sendBufSize, int rcvBufSize)
void setPending(const SessionID &)
Definition: Initiator.cpp:145
void FIX::SocketInitiator::getHost ( const SessionID s,
const Dictionary d,
std::string &  address,
short &  port 
)
private

Definition at line 231 of file SocketInitiator.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().

233 {
234  int num = 0;
235  SessionToHostNum::iterator i = m_sessionToHostNum.find( s );
236  if ( i != m_sessionToHostNum.end() ) num = i->second;
237 
238  std::stringstream hostStream;
239  hostStream << SOCKET_CONNECT_HOST << num;
240  std::string hostString = hostStream.str();
241 
242  std::stringstream portStream;
243  portStream << SOCKET_CONNECT_PORT << num;
244  std::string portString = portStream.str();
245 
246  if( d.has(hostString) && d.has(portString) )
247  {
248  address = d.getString( hostString );
249  port = ( short ) d.getInt( portString );
250  }
251  else
252  {
253  num = 0;
254  address = d.getString( SOCKET_CONNECT_HOST );
255  port = ( short ) d.getInt( SOCKET_CONNECT_PORT );
256  }
257 
258  m_sessionToHostNum[ s ] = ++num;
259 }
SessionToHostNum m_sessionToHostNum
const char SOCKET_CONNECT_HOST[]
const char SOCKET_CONNECT_PORT[]
void FIX::SocketInitiator::onConfigure ( const SessionSettings )
throw (ConfigError
)
privatevirtual

Implemented to configure acceptor.

Reimplemented from FIX::Initiator.

Definition at line 67 of file SocketInitiator.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.

69 {
70  const Dictionary& dict = s.get();
71 
72  if( dict.has( RECONNECT_INTERVAL ) )
73  m_reconnectInterval = dict.getInt( RECONNECT_INTERVAL );
74  if( dict.has( SOCKET_NODELAY ) )
75  m_noDelay = dict.getBool( SOCKET_NODELAY );
76  if( dict.has( SOCKET_SEND_BUFFER_SIZE ) )
77  m_sendBufSize = dict.getInt( SOCKET_SEND_BUFFER_SIZE );
78  if( dict.has( SOCKET_RECEIVE_BUFFER_SIZE ) )
80 }
const char RECONNECT_INTERVAL[]
const char SOCKET_NODELAY[]
const char SOCKET_SEND_BUFFER_SIZE[]
const char SOCKET_RECEIVE_BUFFER_SIZE[]
void FIX::SocketInitiator::onConnect ( SocketConnector ,
int  s 
)
privatevirtual

Implements FIX::SocketConnector::Strategy.

Definition at line 154 of file SocketInitiator.cpp.

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

155 {
156  SocketConnections::iterator i = m_pendingConnections.find( s );
157  if( i == m_pendingConnections.end() ) return;
158  SocketConnection* pSocketConnection = i->second;
159 
160  m_connections[s] = pSocketConnection;
161  m_pendingConnections.erase( i );
162  setConnected( pSocketConnection->getSession()->getSessionID() );
163  pSocketConnection->onTimeout();
164 }
SocketConnections m_pendingConnections
SocketConnections m_connections
void setConnected(const SessionID &)
Definition: Initiator.cpp:154
bool FIX::SocketInitiator::onData ( SocketConnector connector,
int  s 
)
privatevirtual

Implements FIX::SocketConnector::Strategy.

Definition at line 175 of file SocketInitiator.cpp.

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

176 {
177  SocketConnections::iterator i = m_connections.find( s );
178  if ( i == m_connections.end() ) return false;
179  SocketConnection* pSocketConnection = i->second;
180  return pSocketConnection->read( connector );
181 }
SocketConnections m_connections
void FIX::SocketInitiator::onDisconnect ( SocketConnector ,
int  s 
)
privatevirtual

Implements FIX::SocketConnector::Strategy.

Definition at line 183 of file SocketInitiator.cpp.

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

184 {
185  SocketConnections::iterator i = m_connections.find( s );
186  SocketConnections::iterator j = m_pendingConnections.find( s );
187 
188  SocketConnection* pSocketConnection = 0;
189  if( i != m_connections.end() )
190  pSocketConnection = i->second;
191  if( j != m_pendingConnections.end() )
192  pSocketConnection = j->second;
193  if( !pSocketConnection )
194  return;
195 
196  setDisconnected( pSocketConnection->getSession()->getSessionID() );
197 
198  Session* pSession = pSocketConnection->getSession();
199  if ( pSession )
200  {
201  pSession->disconnect();
202  setDisconnected( pSession->getSessionID() );
203  }
204 
205  delete pSocketConnection;
206  m_connections.erase( s );
207  m_pendingConnections.erase( s );
208 }
SocketConnections m_pendingConnections
SocketConnections m_connections
void setDisconnected(const SessionID &)
Definition: Initiator.cpp:163
void FIX::SocketInitiator::onError ( SocketConnector connector)
privatevirtual

Implements FIX::SocketConnector::Strategy.

Definition at line 210 of file SocketInitiator.cpp.

References onTimeout().

211 {
212  onTimeout( connector );
213 }
void onTimeout(SocketConnector &)
void FIX::SocketInitiator::onInitialize ( const SessionSettings )
throw (RuntimeError
)
privatevirtual

Implemented to initialize initiator.

Reimplemented from FIX::Initiator.

Definition at line 82 of file SocketInitiator.cpp.

84 {
85 }
bool FIX::SocketInitiator::onPoll ( double  timeout)
privatevirtual

Implemented to connect and poll for events.

Implements FIX::Initiator.

Definition at line 108 of file SocketInitiator.cpp.

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

109 {
110  time_t start = 0;
111  time_t now = 0;
112 
113  if( isStopped() )
114  {
115  if( start == 0 )
116  ::time( &start );
117  if( !isLoggedOn() )
118  return false;
119  if( ::time(&now) - 5 >= start )
120  return false;
121  }
122 
123  m_connector.block( *this, true, timeout );
124  return true;
125 }
SocketConnector m_connector
void block(Strategy &strategy, bool poll=0, double timeout=0.0)
void start()
Start initiator.
Definition: Initiator.cpp:190
bool isLoggedOn()
Check to see if any sessions are currently logged on.
Definition: Initiator.cpp:269
bool isStopped()
Definition: Initiator.h:83
void FIX::SocketInitiator::onStart ( )
privatevirtual

Implemented to start connecting to targets.

Implements FIX::Initiator.

Definition at line 87 of file SocketInitiator.cpp.

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

88 {
89  connect();
90 
91  while ( !isStopped() ) {
92  m_connector.block( *this, false, 1.0 );
94  }
95 
96  time_t start = 0;
97  time_t now = 0;
98 
99  ::time( &start );
100  while ( isLoggedOn() )
101  {
102  m_connector.block( *this );
103  if( ::time(&now) -5 >= start )
104  break;
105  }
106 }
SocketConnector m_connector
void block(Strategy &strategy, bool poll=0, double timeout=0.0)
void start()
Start initiator.
Definition: Initiator.cpp:190
bool isLoggedOn()
Check to see if any sessions are currently logged on.
Definition: Initiator.cpp:269
bool isStopped()
Definition: Initiator.h:83
void onTimeout(SocketConnector &)
void FIX::SocketInitiator::onStop ( )
privatevirtual

Implemented to stop a running initiator.

Implements FIX::Initiator.

Definition at line 127 of file SocketInitiator.cpp.

128 {
129 }
void FIX::SocketInitiator::onTimeout ( SocketConnector )
privatevirtual

Reimplemented from FIX::SocketConnector::Strategy.

Definition at line 215 of file SocketInitiator.cpp.

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

Referenced by onError(), and onStart().

216 {
217  time_t now;
218  ::time( &now );
219 
220  if ( (now - m_lastConnect) >= m_reconnectInterval )
221  {
222  connect();
223  m_lastConnect = now;
224  }
225 
226  SocketConnections::iterator i;
227  for ( i = m_connections.begin(); i != m_connections.end(); ++i )
228  i->second->onTimeout();
229 }
SocketConnections m_connections
void FIX::SocketInitiator::onWrite ( SocketConnector connector,
int  s 
)
privatevirtual

Implements FIX::SocketConnector::Strategy.

Definition at line 166 of file SocketInitiator.cpp.

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

167 {
168  SocketConnections::iterator i = m_connections.find( s );
169  if ( i == m_connections.end() ) return ;
170  SocketConnection* pSocketConnection = i->second;
171  if( pSocketConnection->processQueue() )
172  pSocketConnection->unsignal();
173 }
SocketConnections m_connections

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

Definition at line 67 of file SocketInitiator.h.


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

Generated on Sat Mar 29 2014 15:13:35 for QuickFIX by doxygen 1.8.5 written by Dimitri van Heesch, © 1997-2001