HttpServer.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (c) 2001-2014
00003 **
00004 ** This file is part of the QuickFIX FIX Engine
00005 **
00006 ** This file may be distributed under the terms of the quickfixengine.org
00007 ** license as defined by quickfixengine.org and appearing in the file
00008 ** LICENSE included in the packaging of this file.
00009 **
00010 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00011 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00012 **
00013 ** See http://www.quickfixengine.org/LICENSE for licensing information.
00014 **
00015 ** Contact ask@quickfixengine.org if any conditions of this licensing are
00016 ** not clear to you.
00017 **
00018 ****************************************************************************/
00019 
00020 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #else
00023 #include "config.h"
00024 #endif
00025 
00026 #include "HttpServer.h"
00027 #include "HttpConnection.h"
00028 #include "Settings.h"
00029 #include "Utility.h"
00030 
00031 namespace FIX
00032 {
00033 Mutex HttpServer::s_mutex;
00034 int HttpServer::s_count = 0;
00035 HttpServer* HttpServer::s_pServer = 0;
00036 
00037 void HttpServer::startGlobal( const SessionSettings& s ) 
00038 throw ( ConfigError, RuntimeError )
00039 {
00040   Locker l( s_mutex );
00041 
00042   if( !s.get().has(HTTP_ACCEPT_PORT) )
00043     return;
00044 
00045   s_count += 1;
00046   if( !s_pServer )
00047   {
00048     s_pServer = new HttpServer( s );
00049     s_pServer->start();
00050   }
00051 }
00052 
00053 void HttpServer::stopGlobal()
00054 {
00055   Locker l( s_mutex );
00056 
00057   s_count -= 1;
00058   if( !s_count && s_pServer )
00059   {
00060     s_pServer->stop();
00061     delete s_pServer;
00062     s_pServer = 0;
00063   }  
00064 }
00065 
00066 HttpServer::HttpServer( const SessionSettings& settings ) throw( ConfigError )
00067 : m_pServer( 0 ), m_settings( settings ), m_threadid( 0 ), m_port( 0 ), m_stop( false ) {}
00068 
00069 void HttpServer::onConfigure( const SessionSettings& s )
00070 throw ( ConfigError )
00071 {  
00072   m_port = s.get().getInt( HTTP_ACCEPT_PORT );
00073 }
00074 
00075 void HttpServer::onInitialize( const SessionSettings& s )
00076 throw ( RuntimeError )
00077 {
00078   try
00079   {
00080     m_pServer = new SocketServer( 1 );
00081     m_pServer->add( m_port, true, false, 0, 0 );
00082   }
00083   catch( std::exception& )
00084   {
00085     throw RuntimeError( "Unable to create, bind, or listen to port " + IntConvertor::convert( (unsigned short)m_port ) );
00086   }
00087 }
00088 
00089 void HttpServer::start() throw ( ConfigError, RuntimeError )
00090 {
00091   m_stop = false;
00092   onConfigure( m_settings );
00093   onInitialize( m_settings );
00094 
00095   if( !thread_spawn( &startThread, this, m_threadid ) )
00096     throw RuntimeError("Unable to spawn thread");
00097 }
00098 
00099 void HttpServer::stop()
00100 {
00101   if( m_stop ) return;
00102   m_stop = true;
00103   onStop();
00104 
00105   if( m_threadid )
00106     thread_join( m_threadid );
00107   m_threadid = 0;
00108 }
00109 
00110 void HttpServer::onStart()
00111 {
00112   while ( !m_stop && m_pServer && m_pServer->block( *this ) ) {}
00113 
00114   if( !m_pServer )
00115     return;
00116 
00117   m_pServer->close();
00118   delete m_pServer;
00119   m_pServer = 0;
00120 }
00121 
00122 bool HttpServer::onPoll()
00123 {
00124   if( !m_pServer || m_stop )
00125     return false;
00126 
00127   m_pServer->block( *this, true );
00128   return true;
00129 }
00130 
00131 void HttpServer::onStop() 
00132 {
00133 }
00134 
00135 void HttpServer::onConnect( SocketServer& server, int a, int s )
00136 {
00137   if ( !socket_isValid( s ) ) return;
00138   HttpConnection connection( s );
00139   while( connection.read() ) {}
00140   m_pServer->getMonitor().drop( s );
00141 }
00142 
00143 void HttpServer::onWrite( SocketServer& server, int s ) 
00144 {
00145 }
00146 
00147 bool HttpServer::onData( SocketServer& server, int s )
00148 {
00149   return true;
00150 }
00151 
00152 void HttpServer::onDisconnect( SocketServer&, int s ) 
00153 {
00154 }
00155 
00156 void HttpServer::onError( SocketServer& ) {}
00157 
00158 void HttpServer::onTimeout( SocketServer& )
00159 {
00160 }
00161 
00162 THREAD_PROC HttpServer::startThread( void* p )
00163 {
00164   HttpServer * pServer = static_cast < HttpServer* > ( p );
00165   pServer->onStart();
00166   return 0;
00167 }
00168 
00169 }

Generated on Mon Sep 15 2014 01:23:54 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001