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

Socket implementation of Acceptor. More...

#include <SocketAcceptor.h>

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

Public Member Functions

 SocketAcceptor (Application &, MessageStoreFactory &, const SessionSettings &) throw ( ConfigError )
 
 SocketAcceptor (Application &, MessageStoreFactory &, const SessionSettings &, LogFactory &) throw ( ConfigError )
 
virtual ~SocketAcceptor ()
 
- Public Member Functions inherited from FIX::Acceptor
 Acceptor (Application &, MessageStoreFactory &, const SessionSettings &) throw ( ConfigError )
 
 Acceptor (Application &, MessageStoreFactory &, const SessionSettings &, LogFactory &) throw ( ConfigError )
 
virtual ~Acceptor ()
 
LoggetLog ()
 
void start () throw ( ConfigError, RuntimeError )
 Start acceptor.
 
void block () throw ( ConfigError, RuntimeError )
 Block on the acceptor.
 
bool poll (double timeout=0.0) throw ( ConfigError, RuntimeError )
 Poll the acceptor.
 
void stop (bool force=false)
 Stop acceptor.
 
bool isLoggedOn ()
 Check to see if any sessions are currently logged on.
 
SessiongetSession (const std::string &msg, 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 ()
 

Private Types

typedef std::set< SessionIDSessions
 
typedef std::map< int, SessionsPortToSessions
 
typedef std::map< int, SocketConnection * > SocketConnections
 

Private Member Functions

bool readSettings (const SessionSettings &)
 
void onConfigure (const SessionSettings &) throw ( ConfigError )
 Implemented to configure acceptor.
 
void onInitialize (const SessionSettings &) throw ( RuntimeError )
 Implemented to initialize acceptor.
 
void onStart ()
 Implemented to start listening for connections.
 
bool onPoll (double timeout)
 Implemented to connect and poll for events.
 
void onStop ()
 Implemented to stop a running acceptor.
 
void onConnect (SocketServer &, int, int)
 
void onWrite (SocketServer &, int)
 
bool onData (SocketServer &, int)
 
void onDisconnect (SocketServer &, int)
 
void onError (SocketServer &)
 
void onTimeout (SocketServer &)
 
- Private Member Functions inherited from FIX::SocketServer::Strategy
virtual ~Strategy ()
 

Private Attributes

SocketServerm_pServer
 
PortToSessions m_portToSessions
 
SocketConnections m_connections
 

Friends

class SocketConnection
 

Additional Inherited Members

- Protected Attributes inherited from FIX::Acceptor
SessionSettings m_settings
 

Detailed Description

Socket implementation of Acceptor.

Definition at line 36 of file SocketAcceptor.h.

Member Typedef Documentation

◆ PortToSessions

typedef std::map< int, Sessions > FIX::SocketAcceptor::PortToSessions
private

Definition at line 51 of file SocketAcceptor.h.

◆ Sessions

typedef std::set< SessionID > FIX::SocketAcceptor::Sessions
private

Definition at line 50 of file SocketAcceptor.h.

◆ SocketConnections

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

Definition at line 52 of file SocketAcceptor.h.

Constructor & Destructor Documentation

◆ SocketAcceptor() [1/2]

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

Definition at line 34 of file SocketAcceptor.cpp.

37: Acceptor( application, factory, settings ),
38 m_pServer( 0 ) {}
Acceptor(Application &, MessageStoreFactory &, const SessionSettings &)
Definition Acceptor.cpp:36
SocketServer * m_pServer

◆ SocketAcceptor() [2/2]

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

Definition at line 40 of file SocketAcceptor.cpp.

44: Acceptor( application, factory, settings, logFactory ),
45 m_pServer( 0 )
46{
47}

◆ ~SocketAcceptor()

FIX::SocketAcceptor::~SocketAcceptor ( )
virtual

Definition at line 49 of file SocketAcceptor.cpp.

50{
51 SocketConnections::iterator iter;
52 for ( iter = m_connections.begin(); iter != m_connections.end(); ++iter )
53 delete iter->second;
54}
SocketConnections m_connections

References m_connections.

Member Function Documentation

◆ onConfigure()

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

Implemented to configure acceptor.

Reimplemented from FIX::Acceptor.

Definition at line 56 of file SocketAcceptor.cpp.

58{
59 std::set<SessionID> sessions = s.getSessions();
60 std::set<SessionID>::iterator i;
61 for( i = sessions.begin(); i != sessions.end(); ++i )
62 {
63 const Dictionary& settings = s.get( *i );
64 settings.getInt( SOCKET_ACCEPT_PORT );
65 if( settings.has(SOCKET_REUSE_ADDRESS) )
66 settings.getBool( SOCKET_REUSE_ADDRESS );
67 if( settings.has(SOCKET_NODELAY) )
68 settings.getBool( SOCKET_NODELAY );
69 }
70}
const char SOCKET_ACCEPT_PORT[]
const char SOCKET_REUSE_ADDRESS[]
const char SOCKET_NODELAY[]

References FIX::Dictionary::getBool(), FIX::Dictionary::getInt(), FIX::Dictionary::has(), FIX::SOCKET_ACCEPT_PORT, FIX::SOCKET_NODELAY, and FIX::SOCKET_REUSE_ADDRESS.

◆ onConnect()

void FIX::SocketAcceptor::onConnect ( SocketServer server,
int  a,
int  s 
)
privatevirtual

Implements FIX::SocketServer::Strategy.

Definition at line 166 of file SocketAcceptor.cpp.

167{
168 if ( !socket_isValid( s ) ) return;
169 SocketConnections::iterator i = m_connections.find( s );
170 if ( i != m_connections.end() ) return;
171 int port = server.socketToPort( a );
172 Sessions sessions = m_portToSessions[port];
173 m_connections[ s ] = new SocketConnection( s, sessions, &server.getMonitor() );
174
175 std::stringstream stream;
176 stream << "Accepted connection from " << socket_peername( s ) << " on port " << port;
177
178 if( getLog() )
179 getLog()->onEvent( stream.str() );
180}
Log * getLog()
Definition Acceptor.h:59
virtual void onEvent(const std::string &)=0
friend class SocketConnection
std::set< SessionID > Sessions
PortToSessions m_portToSessions
bool socket_isValid(int socket)
Definition Utility.cpp:277
const char * socket_peername(int socket)
Definition Utility.cpp:353

References FIX::Acceptor::getLog(), FIX::SocketServer::getMonitor(), m_connections, m_portToSessions, FIX::Log::onEvent(), FIX::socket_isValid(), FIX::socket_peername(), SocketConnection, and FIX::SocketServer::socketToPort().

◆ onData()

bool FIX::SocketAcceptor::onData ( SocketServer server,
int  s 
)
privatevirtual

Implements FIX::SocketServer::Strategy.

Definition at line 191 of file SocketAcceptor.cpp.

192{
193 SocketConnections::iterator i = m_connections.find( s );
194 if ( i == m_connections.end() ) return false;
195 SocketConnection* pSocketConnection = i->second;
196 return pSocketConnection->read( *this, server );
197}

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

◆ onDisconnect()

void FIX::SocketAcceptor::onDisconnect ( SocketServer ,
int  s 
)
privatevirtual

Implements FIX::SocketServer::Strategy.

Definition at line 199 of file SocketAcceptor.cpp.

200{
201 SocketConnections::iterator i = m_connections.find( s );
202 if ( i == m_connections.end() ) return ;
203 SocketConnection* pSocketConnection = i->second;
204
205 Session* pSession = pSocketConnection->getSession();
206 if ( pSession ) pSession->disconnect();
207
208 delete pSocketConnection;
209 m_connections.erase( s );
210}

References FIX::Session::disconnect(), FIX::SocketConnection::getSession(), and m_connections.

◆ onError()

void FIX::SocketAcceptor::onError ( SocketServer )
privatevirtual

Implements FIX::SocketServer::Strategy.

Definition at line 212 of file SocketAcceptor.cpp.

213{
214}

◆ onInitialize()

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

Implemented to initialize acceptor.

Reimplemented from FIX::Acceptor.

Definition at line 72 of file SocketAcceptor.cpp.

74{
75 short port = 0;
76
77 try
78 {
79 m_pServer = new SocketServer( 1 );
80
81 std::set<SessionID> sessions = s.getSessions();
82 std::set<SessionID>::iterator i = sessions.begin();
83 for( ; i != sessions.end(); ++i )
84 {
85 const Dictionary& settings = s.get( *i );
86 port = (short)settings.getInt( SOCKET_ACCEPT_PORT );
87
88 const bool reuseAddress = settings.has( SOCKET_REUSE_ADDRESS ) ?
89 settings.getBool( SOCKET_REUSE_ADDRESS ) : true;
90
91 const bool noDelay = settings.has( SOCKET_NODELAY ) ?
92 settings.getBool( SOCKET_NODELAY ) : false;
93
94 const int sendBufSize = settings.has( SOCKET_SEND_BUFFER_SIZE ) ?
95 settings.getInt( SOCKET_SEND_BUFFER_SIZE ) : 0;
96
97 const int rcvBufSize = settings.has( SOCKET_RECEIVE_BUFFER_SIZE ) ?
98 settings.getInt( SOCKET_RECEIVE_BUFFER_SIZE ) : 0;
99
100 m_portToSessions[port].insert( *i );
101 m_pServer->add( port, reuseAddress, noDelay, sendBufSize, rcvBufSize );
102 }
103 }
104 catch( SocketException& e )
105 {
106 throw RuntimeError( "Unable to create, bind, or listen to port "
107 + IntConvertor::convert( (unsigned short)port ) + " (" + e.what() + ")" );
108 }
109}
int add(int port, bool reuse=false, bool noDelay=false, int sendBufSize=0, int rcvBufSize=0)
const char SOCKET_SEND_BUFFER_SIZE[]
const char SOCKET_RECEIVE_BUFFER_SIZE[]
static std::string convert(signed_int value)

References FIX::IntConvertor::convert(), FIX::Dictionary::getBool(), FIX::Dictionary::getInt(), FIX::Dictionary::has(), FIX::SOCKET_ACCEPT_PORT, FIX::SOCKET_NODELAY, FIX::SOCKET_RECEIVE_BUFFER_SIZE, FIX::SOCKET_REUSE_ADDRESS, and FIX::SOCKET_SEND_BUFFER_SIZE.

◆ onPoll()

bool FIX::SocketAcceptor::onPoll ( double  second)
privatevirtual

Implemented to connect and poll for events.

Implements FIX::Acceptor.

Definition at line 134 of file SocketAcceptor.cpp.

135{
136 if( !m_pServer )
137 return false;
138
139 time_t start = 0;
140 time_t now = 0;
141
142 if( isStopped() )
143 {
144 if( start == 0 )
145 ::time( &start );
146 if( !isLoggedOn() )
147 {
148 start = 0;
149 return false;
150 }
151 if( ::time(&now) - 5 >= start )
152 {
153 start = 0;
154 return false;
155 }
156 }
157
158 m_pServer->block( *this, true, timeout );
159 return true;
160}
void start()
Start acceptor.
Definition Acceptor.cpp:158
bool isStopped()
Definition Acceptor.h:87
bool isLoggedOn()
Check to see if any sessions are currently logged on.
Definition Acceptor.cpp:230
bool block(Strategy &strategy, bool poll=0, double timeout=0.0)

References FIX::SocketServer::block(), FIX::Acceptor::isLoggedOn(), FIX::Acceptor::isStopped(), m_pServer, and FIX::Acceptor::start().

◆ onStart()

void FIX::SocketAcceptor::onStart ( )
privatevirtual

Implemented to start listening for connections.

Implements FIX::Acceptor.

Definition at line 111 of file SocketAcceptor.cpp.

112{
113 while ( !isStopped() && m_pServer && m_pServer->block( *this ) ) {}
114
115 if( !m_pServer )
116 return;
117
118 time_t start = 0;
119 time_t now = 0;
120
121 ::time( &start );
122 while ( isLoggedOn() )
123 {
124 m_pServer->block( *this );
125 if( ::time(&now) -5 >= start )
126 break;
127 }
128
129 m_pServer->close();
130 delete m_pServer;
131 m_pServer = 0;
132}

References FIX::SocketServer::block(), FIX::SocketServer::close(), FIX::Acceptor::isLoggedOn(), FIX::Acceptor::isStopped(), m_pServer, and FIX::Acceptor::start().

◆ onStop()

void FIX::SocketAcceptor::onStop ( )
privatevirtual

Implemented to stop a running acceptor.

Implements FIX::Acceptor.

Definition at line 162 of file SocketAcceptor.cpp.

163{
164}

◆ onTimeout()

void FIX::SocketAcceptor::onTimeout ( SocketServer )
privatevirtual

Reimplemented from FIX::SocketServer::Strategy.

Definition at line 216 of file SocketAcceptor.cpp.

217{
218 SocketConnections::iterator i;
219 for ( i = m_connections.begin(); i != m_connections.end(); ++i )
220 i->second->onTimeout();
221}

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

◆ onWrite()

void FIX::SocketAcceptor::onWrite ( SocketServer server,
int  s 
)
privatevirtual

Implements FIX::SocketServer::Strategy.

Definition at line 182 of file SocketAcceptor.cpp.

183{
184 SocketConnections::iterator i = m_connections.find( s );
185 if ( i == m_connections.end() ) return ;
186 SocketConnection* pSocketConnection = i->second;
187 if( pSocketConnection->processQueue() )
188 pSocketConnection->unsignal();
189}

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

◆ readSettings()

bool FIX::SocketAcceptor::readSettings ( const SessionSettings )
private

Friends And Related Symbol Documentation

◆ SocketConnection

friend class SocketConnection
friend

Definition at line 38 of file SocketAcceptor.h.

Referenced by onConnect().

Member Data Documentation

◆ m_connections

SocketConnections FIX::SocketAcceptor::m_connections
private

Definition at line 70 of file SocketAcceptor.h.

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

◆ m_portToSessions

PortToSessions FIX::SocketAcceptor::m_portToSessions
private

Definition at line 69 of file SocketAcceptor.h.

Referenced by onConnect().

◆ m_pServer

SocketServer* FIX::SocketAcceptor::m_pServer
private

Definition at line 68 of file SocketAcceptor.h.

Referenced by onPoll(), and onStart().


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

Generated on Fri Sep 27 2024 13:45:21 for QuickFIX by doxygen 1.9.8 written by Dimitri van Heesch, © 1997-2001