![]() |
Socket implementation of Acceptor. More...
#include <SocketAcceptor.h>
Public Member Functions | |
SocketAcceptor (Application &, MessageStoreFactory &, const SessionSettings &) throw ( ConfigError ) | |
SocketAcceptor (Application &, MessageStoreFactory &, const SessionSettings &, LogFactory &) throw ( ConfigError ) | |
virtual | ~SocketAcceptor () |
Private Types | |
typedef std::set< SessionID > | Sessions |
typedef std::map< int, Sessions > | PortToSessions |
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 Attributes | |
SocketServer * | m_pServer |
PortToSessions | m_portToSessions |
SocketConnections | m_connections |
Friends | |
class | SocketConnection |
Socket implementation of Acceptor.
Definition at line 36 of file SocketAcceptor.h.
typedef std::map< int, Sessions > FIX::SocketAcceptor::PortToSessions [private] |
Definition at line 51 of file SocketAcceptor.h.
typedef std::set< SessionID > FIX::SocketAcceptor::Sessions [private] |
Reimplemented from FIX::Acceptor.
Definition at line 50 of file SocketAcceptor.h.
typedef std::map< int, SocketConnection* > FIX::SocketAcceptor::SocketConnections [private] |
Definition at line 52 of file SocketAcceptor.h.
FIX::SocketAcceptor::SocketAcceptor | ( | Application & | application, | |
MessageStoreFactory & | factory, | |||
const SessionSettings & | settings | |||
) | throw ( ConfigError ) |
Definition at line 35 of file SocketAcceptor.cpp.
FIX::SocketAcceptor::SocketAcceptor | ( | Application & | application, | |
MessageStoreFactory & | factory, | |||
const SessionSettings & | settings, | |||
LogFactory & | logFactory | |||
) | throw ( ConfigError ) |
Definition at line 41 of file SocketAcceptor.cpp.
FIX::SocketAcceptor::~SocketAcceptor | ( | ) | [virtual] |
Definition at line 50 of file SocketAcceptor.cpp.
References m_connections.
00051 { 00052 SocketConnections::iterator iter; 00053 for ( iter = m_connections.begin(); iter != m_connections.end(); ++iter ) 00054 delete iter->second; 00055 }
void FIX::SocketAcceptor::onConfigure | ( | const SessionSettings & | ) | throw ( ConfigError ) [private, virtual] |
Implemented to configure acceptor.
Reimplemented from FIX::Acceptor.
Definition at line 57 of file SocketAcceptor.cpp.
References FIX::Dictionary::getBool(), FIX::Dictionary::getLong(), FIX::Dictionary::has(), QF_STACK_POP, QF_STACK_PUSH, FIX::SOCKET_ACCEPT_PORT, FIX::SOCKET_NODELAY, and FIX::SOCKET_REUSE_ADDRESS.
00059 { QF_STACK_PUSH(SocketAcceptor::onConfigure) 00060 00061 std::set<SessionID> sessions = s.getSessions(); 00062 std::set<SessionID>::iterator i; 00063 for( i = sessions.begin(); i != sessions.end(); ++i ) 00064 { 00065 const Dictionary& settings = s.get( *i ); 00066 settings.getLong( SOCKET_ACCEPT_PORT ); 00067 if( settings.has(SOCKET_REUSE_ADDRESS) ) 00068 settings.getBool( SOCKET_REUSE_ADDRESS ); 00069 if( settings.has(SOCKET_NODELAY) ) 00070 settings.getBool( SOCKET_NODELAY ); 00071 } 00072 00073 QF_STACK_POP 00074 }
void FIX::SocketAcceptor::onConnect | ( | SocketServer & | server, | |
int | a, | |||
int | s | |||
) | [private, virtual] |
Implements FIX::SocketServer::Strategy.
Definition at line 180 of file SocketAcceptor.cpp.
References FIX::Acceptor::getLog(), FIX::SocketServer::getMonitor(), m_connections, m_portToSessions, FIX::Log::onEvent(), QF_STACK_POP, QF_STACK_PUSH, FIX::socket_isValid(), FIX::socket_peername(), SocketConnection, and FIX::SocketServer::socketToPort().
00181 { QF_STACK_PUSH(SocketAcceptor::onConnect) 00182 00183 if ( !socket_isValid( s ) ) return; 00184 SocketConnections::iterator i = m_connections.find( s ); 00185 if ( i != m_connections.end() ) return; 00186 int port = server.socketToPort( a ); 00187 Sessions sessions = m_portToSessions[port]; 00188 m_connections[ s ] = new SocketConnection( s, sessions, &server.getMonitor() ); 00189 00190 std::stringstream stream; 00191 stream << "Accepted connection from " << socket_peername( s ) << " on port " << port; 00192 00193 if( getLog() ) 00194 getLog()->onEvent( stream.str() ); 00195 00196 QF_STACK_POP 00197 }
bool FIX::SocketAcceptor::onData | ( | SocketServer & | server, | |
int | s | |||
) | [private, virtual] |
Implements FIX::SocketServer::Strategy.
Definition at line 211 of file SocketAcceptor.cpp.
References m_connections, QF_STACK_POP, QF_STACK_PUSH, and FIX::SocketConnection::read().
00212 { QF_STACK_PUSH(SocketAcceptor::onData) 00213 00214 SocketConnections::iterator i = m_connections.find( s ); 00215 if ( i == m_connections.end() ) return false; 00216 SocketConnection* pSocketConnection = i->second; 00217 return pSocketConnection->read( *this, server ); 00218 00219 QF_STACK_POP 00220 }
void FIX::SocketAcceptor::onDisconnect | ( | SocketServer & | , | |
int | s | |||
) | [private, virtual] |
Implements FIX::SocketServer::Strategy.
Definition at line 222 of file SocketAcceptor.cpp.
References FIX::Session::disconnect(), FIX::SocketConnection::getSession(), m_connections, QF_STACK_POP, and QF_STACK_PUSH.
00223 { QF_STACK_PUSH(SocketAcceptor::onDisconnect) 00224 00225 SocketConnections::iterator i = m_connections.find( s ); 00226 if ( i == m_connections.end() ) return ; 00227 SocketConnection* pSocketConnection = i->second; 00228 00229 Session* pSession = pSocketConnection->getSession(); 00230 if ( pSession ) pSession->disconnect(); 00231 00232 delete pSocketConnection; 00233 m_connections.erase( s ); 00234 00235 QF_STACK_POP 00236 }
void FIX::SocketAcceptor::onError | ( | SocketServer & | ) | [private, virtual] |
void FIX::SocketAcceptor::onInitialize | ( | const SessionSettings & | ) | throw ( RuntimeError ) [private, virtual] |
Implemented to initialize acceptor.
Reimplemented from FIX::Acceptor.
Definition at line 76 of file SocketAcceptor.cpp.
References FIX::IntConvertor::convert(), FIX::Dictionary::getLong(), FIX::Dictionary::has(), QF_STACK_POP, QF_STACK_PUSH, FIX::SOCKET_ACCEPT_PORT, FIX::SOCKET_NODELAY, FIX::SOCKET_RECEIVE_BUFFER_SIZE, FIX::SOCKET_REUSE_ADDRESS, and FIX::SOCKET_SEND_BUFFER_SIZE.
Referenced by onTimeout().
00078 { QF_STACK_PUSH(SocketAcceptor::onInitialize) 00079 00080 short port = 0; 00081 00082 try 00083 { 00084 m_pServer = new SocketServer( 1 ); 00085 00086 std::set<SessionID> sessions = s.getSessions(); 00087 std::set<SessionID>::iterator i = sessions.begin(); 00088 for( ; i != sessions.end(); ++i ) 00089 { 00090 Dictionary settings = s.get( *i ); 00091 short port = (short)settings.getLong( SOCKET_ACCEPT_PORT ); 00092 00093 const bool reuseAddress = settings.has( SOCKET_REUSE_ADDRESS ) ? 00094 s.get().getBool( SOCKET_REUSE_ADDRESS ) : true; 00095 00096 const bool noDelay = settings.has( SOCKET_NODELAY ) ? 00097 s.get().getBool( SOCKET_NODELAY ) : false; 00098 00099 const int sendBufSize = settings.has( SOCKET_SEND_BUFFER_SIZE ) ? 00100 s.get().getLong( SOCKET_SEND_BUFFER_SIZE ) : 0; 00101 00102 const int rcvBufSize = settings.has( SOCKET_RECEIVE_BUFFER_SIZE ) ? 00103 s.get().getLong( SOCKET_RECEIVE_BUFFER_SIZE ) : 0; 00104 00105 m_portToSessions[port].insert( *i ); 00106 m_pServer->add( port, reuseAddress, noDelay, sendBufSize, rcvBufSize ); 00107 } 00108 } 00109 catch( SocketException& e ) 00110 { 00111 throw RuntimeError( "Unable to create, bind, or listen to port " 00112 + IntConvertor::convert( (unsigned short)port ) + " (" + e.what() + ")" ); 00113 } 00114 00115 QF_STACK_POP 00116 }
bool FIX::SocketAcceptor::onPoll | ( | double | second | ) | [private, virtual] |
Implemented to connect and poll for events.
Implements FIX::Acceptor.
Definition at line 144 of file SocketAcceptor.cpp.
References FIX::SocketServer::block(), FIX::Acceptor::isLoggedOn(), FIX::Acceptor::isStopped(), m_pServer, QF_STACK_POP, QF_STACK_PUSH, and FIX::Acceptor::start().
00145 { QF_STACK_PUSH(SocketAcceptor::onPoll) 00146 00147 if( !m_pServer ) 00148 return false; 00149 00150 time_t start = 0; 00151 time_t now = 0; 00152 00153 if( isStopped() ) 00154 { 00155 if( start == 0 ) 00156 ::time( &start ); 00157 if( !isLoggedOn() ) 00158 { 00159 start = 0; 00160 return false; 00161 } 00162 if( ::time(&now) - 5 >= start ) 00163 { 00164 start = 0; 00165 return false; 00166 } 00167 } 00168 00169 m_pServer->block( *this, true, timeout ); 00170 return true; 00171 00172 QF_STACK_POP 00173 }
void FIX::SocketAcceptor::onStart | ( | ) | [private, virtual] |
Implemented to start listening for connections.
Implements FIX::Acceptor.
Definition at line 118 of file SocketAcceptor.cpp.
References FIX::SocketServer::block(), FIX::SocketServer::close(), FIX::Acceptor::isLoggedOn(), FIX::Acceptor::isStopped(), m_pServer, QF_STACK_POP, QF_STACK_PUSH, and FIX::Acceptor::start().
00119 { QF_STACK_PUSH(SocketAcceptor::onStart) 00120 00121 while ( !isStopped() && m_pServer && m_pServer->block( *this ) ) {} 00122 00123 if( !m_pServer ) 00124 return; 00125 00126 time_t start = 0; 00127 time_t now = 0; 00128 00129 ::time( &start ); 00130 while ( isLoggedOn() ) 00131 { 00132 m_pServer->block( *this ); 00133 if( ::time(&now) -5 >= start ) 00134 break; 00135 } 00136 00137 m_pServer->close(); 00138 delete m_pServer; 00139 m_pServer = 0; 00140 00141 QF_STACK_POP 00142 }
void FIX::SocketAcceptor::onStop | ( | ) | [private, virtual] |
Implemented to stop a running acceptor.
Implements FIX::Acceptor.
Definition at line 175 of file SocketAcceptor.cpp.
References QF_STACK_POP, and QF_STACK_PUSH.
00176 { QF_STACK_PUSH(SocketAcceptor::onStop) 00177 QF_STACK_POP 00178 }
void FIX::SocketAcceptor::onTimeout | ( | SocketServer & | ) | [private, virtual] |
Reimplemented from FIX::SocketServer::Strategy.
Definition at line 240 of file SocketAcceptor.cpp.
References m_connections, onInitialize(), QF_STACK_POP, and QF_STACK_PUSH.
00241 { QF_STACK_PUSH(SocketAcceptor::onInitialize) 00242 00243 SocketConnections::iterator i; 00244 for ( i = m_connections.begin(); i != m_connections.end(); ++i ) 00245 i->second->onTimeout(); 00246 00247 QF_STACK_POP 00248 }
void FIX::SocketAcceptor::onWrite | ( | SocketServer & | server, | |
int | s | |||
) | [private, virtual] |
Implements FIX::SocketServer::Strategy.
Definition at line 199 of file SocketAcceptor.cpp.
References m_connections, FIX::SocketConnection::processQueue(), QF_STACK_POP, QF_STACK_PUSH, and FIX::SocketConnection::unsignal().
00200 { QF_STACK_PUSH(SocketAcceptor::onWrite) 00201 00202 SocketConnections::iterator i = m_connections.find( s ); 00203 if ( i == m_connections.end() ) return ; 00204 SocketConnection* pSocketConnection = i->second; 00205 if( pSocketConnection->processQueue() ) 00206 pSocketConnection->unsignal(); 00207 00208 QF_STACK_POP 00209 }
bool FIX::SocketAcceptor::readSettings | ( | const SessionSettings & | ) | [private] |
friend class SocketConnection [friend] |
Definition at line 38 of file SocketAcceptor.h.
Referenced by onConnect().
Definition at line 70 of file SocketAcceptor.h.
Referenced by onConnect(), onData(), onDisconnect(), onTimeout(), onWrite(), and ~SocketAcceptor().
Definition at line 69 of file SocketAcceptor.h.
Referenced by onConnect().
SocketServer* FIX::SocketAcceptor::m_pServer [private] |
Definition at line 68 of file SocketAcceptor.h.