HttpServer.cpp
Go to the documentation of this file.
/****************************************************************************
** Copyright (c) 2001-2014
**
** This file is part of the QuickFIX FIX Engine
**
** This file may be distributed under the terms of the quickfixengine.org
** license as defined by quickfixengine.org and appearing in the file
** LICENSE included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.quickfixengine.org/LICENSE for licensing information.
**
** Contact ask@quickfixengine.org if any conditions of this licensing are
** not clear to you.
**
****************************************************************************/
#ifdef _MSC_VER
#include "stdafx.h"
#else
#include "config.h"
#endif
#include "HttpServer.h"
#include "HttpConnection.h"
#include "Settings.h"
#include "Utility.h"
namespace FIX
{
Mutex HttpServer::s_mutex;
int HttpServer::s_count = 0;
HttpServer* HttpServer::s_pServer = 0;
void HttpServer::startGlobal( const SessionSettings& s )
throw ( ConfigError, RuntimeError )
{
Locker l( s_mutex );
if( !s.get().has(HTTP_ACCEPT_PORT) )
return;
s_count += 1;
if( !s_pServer )
{
s_pServer = new HttpServer( s );
s_pServer->start();
}
}
void HttpServer::stopGlobal()
{
Locker l( s_mutex );
s_count -= 1;
if( !s_count && s_pServer )
{
s_pServer->stop();
delete s_pServer;
s_pServer = 0;
}
}
HttpServer::HttpServer( const SessionSettings& settings ) throw( ConfigError )
: m_pServer( 0 ), m_settings( settings ), m_threadid( 0 ), m_port( 0 ), m_stop( false ) {}
void HttpServer::onConfigure( const SessionSettings& s )
throw ( ConfigError )
{
m_port = s.get().getInt( HTTP_ACCEPT_PORT );
}
void HttpServer::onInitialize( const SessionSettings& s )
throw ( RuntimeError )
{
try
{
m_pServer = new SocketServer( 1 );
m_pServer->add( m_port, true, false, 0, 0 );
}
catch( std::exception& )
{
throw RuntimeError( "Unable to create, bind, or listen to port " + IntConvertor::convert( (unsigned short)m_port ) );
}
}
void HttpServer::start() throw ( ConfigError, RuntimeError )
{
m_stop = false;
onConfigure( m_settings );
onInitialize( m_settings );
if( !thread_spawn( &startThread, this, m_threadid ) )
throw RuntimeError("Unable to spawn thread");
}
void HttpServer::stop()
{
if( m_stop ) return;
m_stop = true;
onStop();
if( m_threadid )
thread_join( m_threadid );
m_threadid = 0;
}
void HttpServer::onStart()
{
while ( !m_stop && m_pServer && m_pServer->block( *this ) ) {}
if( !m_pServer )
return;
m_pServer->close();
delete m_pServer;
m_pServer = 0;
}
bool HttpServer::onPoll()
{
if( !m_pServer || m_stop )
return false;
m_pServer->block( *this, true );
return true;
}
void HttpServer::onStop()
{
}
void HttpServer::onConnect( SocketServer& server, int a, int s )
{
if ( !socket_isValid( s ) ) return;
HttpConnection connection( s );
while( connection.read() ) {}
m_pServer->getMonitor().drop( s );
}
void HttpServer::onWrite( SocketServer& server, int s )
{
}
bool HttpServer::onData( SocketServer& server, int s )
{
return true;
}
void HttpServer::onDisconnect( SocketServer&, int s )
{
}
void HttpServer::onError( SocketServer& ) {}
void HttpServer::onTimeout( SocketServer& )
{
}
THREAD_PROC HttpServer::startThread( void* p )
{
HttpServer * pServer = static_cast < HttpServer* > ( p );
pServer->onStart();
return 0;
}
}
Settings.h
FIX::HttpConnection::read
bool read()
Definition: HttpConnection.cpp:56
FIX::HttpConnection
Encapsulates a HTTP socket file descriptor.
Definition: HttpConnection.h:37
FIX::HttpServer::startGlobal
static void startGlobal(const SessionSettings &)
Definition: HttpServer.cpp:37
FIX::thread_join
void thread_join(thread_id thread)
Definition: Utility.cpp:415
FIX::socket_isValid
bool socket_isValid(int socket)
Definition: Utility.cpp:255
Utility.h
FIX::HttpServer::onStart
void onStart()
Definition: HttpServer.cpp:110
FIX::SocketServer::getMonitor
SocketMonitor & getMonitor()
Definition: SocketServer.h:70
FIX::HttpServer::HttpServer
HttpServer(const SessionSettings &)
Definition: HttpServer.cpp:66
FIX::HttpServer::onTimeout
void onTimeout(SocketServer &)
Definition: HttpServer.cpp:158
FIX::thread_spawn
bool thread_spawn(THREAD_START_ROUTINE func, void *var, thread_id &thread)
Definition: Utility.cpp:394
FIX::HttpServer::start
void start()
Definition: HttpServer.cpp:89
FIX::HttpServer::stop
void stop()
Definition: HttpServer.cpp:99
FIX::HttpServer::m_threadid
thread_id m_threadid
Definition: HttpServer.h:67
FIX::IntConvertor::convert
static std::string convert(signed_int value)
Definition: FieldConvertors.h:154
FIX::HttpServer::m_pServer
SocketServer * m_pServer
Definition: HttpServer.h:65
FIX::SocketServer::block
bool block(Strategy &strategy, bool poll=0, double timeout=0.0)
Definition: SocketServer.cpp:148
FIX
Definition: Acceptor.cpp:34
FIX::HttpServer::startThread
static THREAD_PROC startThread(void *p)
Definition: HttpServer.cpp:162
FIX::SocketServer::add
int add(int port, bool reuse=false, bool noDelay=false, int sendBufSize=0, int rcvBufSize=0)
Definition: SocketServer.cpp:97
FIX::HttpServer::onDisconnect
void onDisconnect(SocketServer &, int)
Definition: HttpServer.cpp:152
FIX::RuntimeError
Application encountered serious error during runtime
Definition: Exceptions.h:94
FIX::ConfigError
Application is not configured correctly
Definition: Exceptions.h:87
FIX::Locker
Locks/Unlocks a mutex using RAII.
Definition: Mutex.h:95
FIX::HttpServer::s_mutex
static Mutex s_mutex
Definition: HttpServer.h:71
FIX::SessionSettings
Container for setting dictionaries mapped to sessions.
Definition: SessionSettings.h:130
FIX::HttpServer::onStop
void onStop()
Definition: HttpServer.cpp:131
FIX::HttpServer::onPoll
bool onPoll()
Definition: HttpServer.cpp:122
FIX::HttpServer::onConfigure
void onConfigure(const SessionSettings &)
Definition: HttpServer.cpp:69
FIX::SocketMonitor::drop
bool drop(int socket)
Definition: SocketMonitor.cpp:98
FIX::HttpServer::onInitialize
void onInitialize(const SessionSettings &)
Definition: HttpServer.cpp:75
FIX::HttpServer::s_count
static int s_count
Definition: HttpServer.h:72
FIX::HttpServer::stopGlobal
static void stopGlobal()
Definition: HttpServer.cpp:53
FIX::SocketServer::close
void close()
Definition: SocketServer.cpp:137
FIX::HttpServer::m_stop
bool m_stop
Definition: HttpServer.h:69
FIX::HttpServer::m_settings
SessionSettings m_settings
Definition: HttpServer.h:66
FIX::HttpServer::onWrite
void onWrite(SocketServer &, int)
Definition: HttpServer.cpp:143
FIX::HttpServer::s_pServer
static HttpServer * s_pServer
Definition: HttpServer.h:73
FIX::HttpServer::onData
bool onData(SocketServer &, int)
Definition: HttpServer.cpp:147
THREAD_PROC
#define THREAD_PROC
Definition: Utility.h:158
FIX::SocketServer
Listens for and accepts incoming socket connections on a port.
Definition: SocketServer.h:56
FIX::HttpServer::onConnect
void onConnect(SocketServer &, int, int)
Definition: HttpServer.cpp:135
FIX::HttpServer::onError
void onError(SocketServer &)
Definition: HttpServer.cpp:156
HttpServer.h
HttpConnection.h
FIX::HTTP_ACCEPT_PORT
const char HTTP_ACCEPT_PORT[]
Definition: SessionSettings.h:126
FIX::HttpServer
Basic HTTP Server.
Definition: HttpServer.h:37
FIX::HttpServer::m_port
int m_port
Definition: HttpServer.h:68