Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
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]

Public Member Functions

 ThreadedSocketInitiator (Application &, MessageStoreFactory &, const SessionSettings &) throw ( ConfigError )
 
 ThreadedSocketInitiator (Application &, MessageStoreFactory &, const SessionSettings &, LogFactory &) throw ( ConfigError )
 
virtual ~ThreadedSocketInitiator ()
 
- 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, 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. 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 &s, const Dictionary &d)
 Implemented to connect a session to its target. More...
 
void addThread (int s, thread_id t)
 
void removeThread (int s)
 
void lock ()
 
void getHost (const SessionID &, const Dictionary &, std::string &, short &, 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
 

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

Threaded Socket implementation of Initiator.

Definition at line 56 of file ThreadedSocketInitiator.h.

Member Typedef Documentation

◆ SessionToHostNum

Definition at line 69 of file ThreadedSocketInitiator.h.

◆ SocketToThread

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

Definition at line 68 of file ThreadedSocketInitiator.h.

◆ ThreadPair

Definition at line 70 of file ThreadedSocketInitiator.h.

Constructor & Destructor Documentation

◆ ThreadedSocketInitiator() [1/2]

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

Definition at line 49 of file ThreadedSocketInitiator.cpp.

51 {
52  socket_init();
53 }
54 
56 {
57  socket_term();
58 }

References FIX::socket_init().

◆ ThreadedSocketInitiator() [2/2]

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

Definition at line 60 of file ThreadedSocketInitiator.cpp.

62 {
63  const Dictionary& dict = s.get();
64 
65  if( dict.has( RECONNECT_INTERVAL ) )
66  m_reconnectInterval = dict.getInt( RECONNECT_INTERVAL );
67  if( dict.has( SOCKET_NODELAY ) )
68  m_noDelay = dict.getBool( SOCKET_NODELAY );
69  if( dict.has( SOCKET_SEND_BUFFER_SIZE ) )
70  m_sendBufSize = dict.getInt( SOCKET_SEND_BUFFER_SIZE );

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.

◆ ~ThreadedSocketInitiator()

FIX::ThreadedSocketInitiator::~ThreadedSocketInitiator ( )
virtual

Definition at line 72 of file ThreadedSocketInitiator.cpp.

77 {

Member Function Documentation

◆ addThread()

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

Definition at line 200 of file ThreadedSocketInitiator.cpp.

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

◆ doConnect()

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

Implemented to connect a session to its target.

Implements FIX::Initiator.

Definition at line 150 of file ThreadedSocketInitiator.cpp.

157  :" + IntConvertor::convert((unsigned short)sourcePort) + ")");
158 
159  ThreadedSocketConnection* pConnection =
160  new ThreadedSocketConnection( s, socket, address, port, getLog(), sourceAddress, sourcePort );
161 
162  ThreadPair* pair = new ThreadPair( this, pConnection );
163 
164  {
165  Locker l( m_mutex );
166  thread_id thread;
167  if ( thread_spawn( &socketThread, pair, thread ) )
168  {
169  addThread( socket, thread );
170  }
171  else
172  {
173  delete pair;
174  pConnection->disconnect();
175  delete pConnection;
176  setDisconnected( s );
177  }
178  }
179  }
180  catch ( std::exception& ) {}
181 }
182 
183 void ThreadedSocketInitiator::addThread( int s, thread_id t )
184 {
185  Locker l(m_mutex);
186 
187  m_threads[ s ] = t;
188 }
189 
190 void ThreadedSocketInitiator::removeThread( int s )
191 {
192  Locker l(m_mutex);
193  SocketToThread::iterator i = m_threads.find( s );
194 
195  if ( i != m_threads.end() )
196  {
197  thread_detach( i->second );
198  m_threads.erase( i );

◆ getHost()

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

Definition at line 257 of file ThreadedSocketInitiator.cpp.

257  {
258  address = d.getString( hostString );
259  port = ( short ) d.getInt( portString );
260 
261  std::stringstream sourceHostStream;
262  sourceHostStream << SOCKET_CONNECT_SOURCE_HOST << num;
263  hostString = sourceHostStream.str();
264  if( d.has(hostString) )
265  sourceAddress = d.getString( hostString );
266 
267  std::stringstream sourcePortStream;
268  sourcePortStream << SOCKET_CONNECT_SOURCE_PORT << num;
269  portString = sourcePortStream.str();
270  if( d.has(portString) )
271  sourcePort = ( short ) d.getInt( portString );
272  }
273  else
274  {
275  num = 0;
276  address = d.getString( SOCKET_CONNECT_HOST );
277  port = ( short ) d.getInt( SOCKET_CONNECT_PORT );
278 
279  if( d.has(SOCKET_CONNECT_SOURCE_HOST) )
280  sourceAddress = d.getString( SOCKET_CONNECT_SOURCE_HOST );
281  if( d.has(SOCKET_CONNECT_SOURCE_PORT) )
282  sourcePort = ( short ) d.getInt( SOCKET_CONNECT_SOURCE_PORT );
283  }
284 
285  m_sessionToHostNum[ s ] = ++num;
286 }
287 
288 }

References FIX::SOCKET_CONNECT_SOURCE_HOST, and FIX::SOCKET_CONNECT_SOURCE_PORT.

◆ lock()

void FIX::ThreadedSocketInitiator::lock ( )
inlineprivate

Definition at line 83 of file ThreadedSocketInitiator.h.

◆ onConfigure()

void FIX::ThreadedSocketInitiator::onConfigure ( const SessionSettings )
throw (ConfigError
)
privatevirtual

Implemented to configure acceptor.

Reimplemented from FIX::Initiator.

Definition at line 77 of file ThreadedSocketInitiator.cpp.

77 {
78 }
79 
81 {
82  while ( !isStopped() )
83  {
84  time_t now;
85  ::time( &now );
86 
87  if ( (now - m_lastConnect) >= m_reconnectInterval )
88  {
89  Locker l( m_mutex );
90  connect();

◆ onInitialize()

void FIX::ThreadedSocketInitiator::onInitialize ( const SessionSettings )
throw (RuntimeError
)
privatevirtual

Implemented to initialize initiator.

Reimplemented from FIX::Initiator.

Definition at line 92 of file ThreadedSocketInitiator.cpp.

◆ onPoll()

bool FIX::ThreadedSocketInitiator::onPoll ( double  timeout)
privatevirtual

Implemented to connect and poll for events.

Implements FIX::Initiator.

Definition at line 115 of file ThreadedSocketInitiator.cpp.

116  {
117  if( ::time(&now) -5 >= start )
118  break;

References FIX::Initiator::start().

◆ onStart()

void FIX::ThreadedSocketInitiator::onStart ( )
privatevirtual

Implemented to start connecting to targets.

Implements FIX::Initiator.

Definition at line 97 of file ThreadedSocketInitiator.cpp.

99 {
100  return false;
101 }
102 
104 {
105  SocketToThread threads;
106  SocketToThread::iterator i;
107 
108  {
109  Locker l(m_mutex);
110 
111  time_t start = 0;
112  time_t now = 0;
113 

◆ onStop()

void FIX::ThreadedSocketInitiator::onStop ( )
privatevirtual

Implemented to stop a running initiator.

Implements FIX::Initiator.

Definition at line 120 of file ThreadedSocketInitiator.cpp.

134 {
135  try
136  {
137  Session* session = Session::lookupSession( s );
138  if( !session->isSessionTime(UtcTimeStamp()) ) return;
139 
140  Log* log = session->getLog();
141 
142  std::string address;
143  short port = 0;
144  std::string sourceAddress;
145  short sourcePort = 0;
146  getHost( s, d, address, port, sourceAddress, sourcePort );
147 
148  int socket = socket_createConnector();

◆ removeThread()

void FIX::ThreadedSocketInitiator::removeThread ( int  s)
private

Definition at line 207 of file ThreadedSocketInitiator.cpp.

216  {
217  pInitiator->getLog()->onEvent( "Connection failed" );

◆ socketThread()

THREAD_PROC FIX::ThreadedSocketInitiator::socketThread ( void *  p)
staticprivate

Definition at line 219 of file ThreadedSocketInitiator.cpp.

230  {}
231 
232  delete pConnection;
233  if( !pInitiator->isStopped() )
234  pInitiator->removeThread( socket );
235 
236  pInitiator->setDisconnected( sessionID );
237  return 0;
238 }
239 
240 void ThreadedSocketInitiator::getHost( const SessionID& s, const Dictionary& d,
241  std::string& address, short& port,
242  std::string& sourceAddress, short& sourcePort )
243 {
244  int num = 0;
245  SessionToHostNum::iterator i = m_sessionToHostNum.find( s );
246  if ( i != m_sessionToHostNum.end() ) num = i->second;
247 
248  std::stringstream hostStream;
249  hostStream << SOCKET_CONNECT_HOST << num;
250  std::string hostString = hostStream.str();
251 
252  std::stringstream portStream;
253  portStream << SOCKET_CONNECT_PORT << num;
254  std::string portString = portStream.str();
255 

Member Data Documentation

◆ m_lastConnect

time_t FIX::ThreadedSocketInitiator::m_lastConnect
private

Definition at line 90 of file ThreadedSocketInitiator.h.

◆ m_mutex

Mutex FIX::ThreadedSocketInitiator::m_mutex
private

Definition at line 96 of file ThreadedSocketInitiator.h.

◆ m_noDelay

bool FIX::ThreadedSocketInitiator::m_noDelay
private

Definition at line 92 of file ThreadedSocketInitiator.h.

◆ m_rcvBufSize

int FIX::ThreadedSocketInitiator::m_rcvBufSize
private

Definition at line 94 of file ThreadedSocketInitiator.h.

◆ m_reconnectInterval

int FIX::ThreadedSocketInitiator::m_reconnectInterval
private

Definition at line 91 of file ThreadedSocketInitiator.h.

◆ m_sendBufSize

int FIX::ThreadedSocketInitiator::m_sendBufSize
private

Definition at line 93 of file ThreadedSocketInitiator.h.

◆ m_sessionToHostNum

SessionToHostNum FIX::ThreadedSocketInitiator::m_sessionToHostNum
private

Definition at line 89 of file ThreadedSocketInitiator.h.

◆ m_settings

SessionSettings FIX::ThreadedSocketInitiator::m_settings
private

Definition at line 88 of file ThreadedSocketInitiator.h.

◆ m_threads

SocketToThread FIX::ThreadedSocketInitiator::m_threads
private

Definition at line 95 of file ThreadedSocketInitiator.h.


The documentation for this class was generated from the following files:
FIX::ThreadedSocketInitiator::getHost
void getHost(const SessionID &, const Dictionary &, std::string &, short &, std::string &, short &)
Definition: ThreadedSocketInitiator.cpp:257
FIX::Initiator::start
void start()
Start initiator.
Definition: Initiator.cpp:207
FIX::ThreadedSocketInitiator::m_mutex
Mutex m_mutex
Definition: ThreadedSocketInitiator.h:96
FIX::Session::lookupSession
static Session * lookupSession(const SessionID &)
Definition: Session.cpp:1513
FIX::SOCKET_NODELAY
const char SOCKET_NODELAY[]
Definition: SessionSettings.h:85
FIX::SOCKET_CONNECT_SOURCE_PORT
const char SOCKET_CONNECT_SOURCE_PORT[]
Definition: SessionSettings.h:84
FIX::socket_init
void socket_init()
Definition: Utility.cpp:98
FIX::ThreadedSocketInitiator::onStart
void onStart()
Implemented to start connecting to targets.
Definition: ThreadedSocketInitiator.cpp:97
FIX::Initiator::connect
void connect()
Definition: Initiator.cpp:148
FIX::SOCKET_CONNECT_SOURCE_HOST
const char SOCKET_CONNECT_SOURCE_HOST[]
Definition: SessionSettings.h:83
FIX::SOCKET_SEND_BUFFER_SIZE
const char SOCKET_SEND_BUFFER_SIZE[]
Definition: SessionSettings.h:86
FIX::ThreadedSocketInitiator::m_sendBufSize
int m_sendBufSize
Definition: ThreadedSocketInitiator.h:93
FIX::socket_term
void socket_term()
Definition: Utility.cpp:113
FIX::TYPE::UtcTimeStamp
@ UtcTimeStamp
Definition: FieldTypes.h:940
FIX::RECONNECT_INTERVAL
const char RECONNECT_INTERVAL[]
Definition: SessionSettings.h:88
FIX::ThreadedSocketInitiator::m_reconnectInterval
int m_reconnectInterval
Definition: ThreadedSocketInitiator.h:91
FIX::ThreadedSocketInitiator::m_sessionToHostNum
SessionToHostNum m_sessionToHostNum
Definition: ThreadedSocketInitiator.h:89
FIX::socket_createConnector
int socket_createConnector()
Definition: Utility.cpp:160
FIX::ThreadedSocketInitiator::SocketToThread
std::map< int, thread_id > SocketToThread
Definition: ThreadedSocketInitiator.h:68
FIX::SOCKET_CONNECT_PORT
const char SOCKET_CONNECT_PORT[]
Definition: SessionSettings.h:82
FIX::ThreadedSocketInitiator::~ThreadedSocketInitiator
virtual ~ThreadedSocketInitiator()
Definition: ThreadedSocketInitiator.cpp:72
FIX::ThreadedSocketInitiator::ThreadPair
std::pair< ThreadedSocketInitiator *, ThreadedSocketConnection * > ThreadPair
Definition: ThreadedSocketInitiator.h:70
FIX::ThreadedSocketInitiator::m_lastConnect
time_t m_lastConnect
Definition: ThreadedSocketInitiator.h:90
FIX::ThreadedSocketInitiator::onStop
void onStop()
Implemented to stop a running initiator.
Definition: ThreadedSocketInitiator.cpp:120
FIX::ThreadedSocketInitiator::m_noDelay
bool m_noDelay
Definition: ThreadedSocketInitiator.h:92
FIX::Initiator::isStopped
bool isStopped()
Definition: Initiator.h:100
FIX::SOCKET_CONNECT_HOST
const char SOCKET_CONNECT_HOST[]
Definition: SessionSettings.h:81

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001