HttpServer.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (c) 2001-2014
3 **
4 ** This file is part of the QuickFIX FIX Engine
5 **
6 ** This file may be distributed under the terms of the quickfixengine.org
7 ** license as defined by quickfixengine.org and appearing in the file
8 ** LICENSE included in the packaging of this file.
9 **
10 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12 **
13 ** See http://www.quickfixengine.org/LICENSE for licensing information.
14 **
15 ** Contact ask@quickfixengine.org if any conditions of this licensing are
16 ** not clear to you.
17 **
18 ****************************************************************************/
19 
20 #ifdef _MSC_VER
21 #include "stdafx.h"
22 #else
23 #include "config.h"
24 #endif
25 
26 #include "HttpServer.h"
27 #include "HttpConnection.h"
28 #include "Settings.h"
29 #include "Utility.h"
30 
31 namespace FIX
32 {
34 int HttpServer::s_count = 0;
35 HttpServer* HttpServer::s_pServer = 0;
36 
37 void HttpServer::startGlobal( const SessionSettings& s )
38 throw ( ConfigError, RuntimeError )
39 {
40  Locker l( s_mutex );
41 
42  if( !s.get().has(HTTP_ACCEPT_PORT) )
43  return;
44 
45  s_count += 1;
46  if( !s_pServer )
47  {
48  s_pServer = new HttpServer( s );
49  s_pServer->start();
50  }
51 }
52 
54 {
55  Locker l( s_mutex );
56 
57  s_count -= 1;
58  if( !s_count && s_pServer )
59  {
60  s_pServer->stop();
61  delete s_pServer;
62  s_pServer = 0;
63  }
64 }
65 
66 HttpServer::HttpServer( const SessionSettings& settings ) throw( ConfigError )
67 : m_pServer( 0 ), m_settings( settings ), m_threadid( 0 ), m_port( 0 ), m_stop( false ) {}
68 
69 void HttpServer::onConfigure( const SessionSettings& s )
70 throw ( ConfigError )
71 {
72  m_port = s.get().getInt( HTTP_ACCEPT_PORT );
73 }
74 
76 throw ( RuntimeError )
77 {
78  try
79  {
80  m_pServer = new SocketServer( 1 );
81  m_pServer->add( m_port, true, false, 0, 0 );
82  }
83  catch( std::exception& )
84  {
85  throw RuntimeError( "Unable to create, bind, or listen to port " + IntConvertor::convert( (unsigned short)m_port ) );
86  }
87 }
88 
89 void HttpServer::start() throw ( ConfigError, RuntimeError )
90 {
91  m_stop = false;
94 
95  if( !thread_spawn( &startThread, this, m_threadid ) )
96  throw RuntimeError("Unable to spawn thread");
97 }
98 
99 void HttpServer::stop()
100 {
101  if( m_stop ) return;
102  m_stop = true;
103  onStop();
104 
105  if( m_threadid )
107  m_threadid = 0;
108 }
109 
110 void HttpServer::onStart()
111 {
112  while ( !m_stop && m_pServer && m_pServer->block( *this ) ) {}
113 
114  if( !m_pServer )
115  return;
116 
117  m_pServer->close();
118  delete m_pServer;
119  m_pServer = 0;
120 }
121 
122 bool HttpServer::onPoll()
123 {
124  if( !m_pServer || m_stop )
125  return false;
126 
127  m_pServer->block( *this, true );
128  return true;
129 }
130 
131 void HttpServer::onStop()
132 {
133 }
134 
135 void HttpServer::onConnect( SocketServer& server, int a, int s )
136 {
137  if ( !socket_isValid( s ) ) return;
138  HttpConnection connection( s );
139  while( connection.read() ) {}
140  m_pServer->getMonitor().drop( s );
141 }
142 
143 void HttpServer::onWrite( SocketServer& server, int s )
144 {
145 }
146 
147 bool HttpServer::onData( SocketServer& server, int s )
148 {
149  return true;
150 }
151 
153 {
154 }
155 
157 
158 void HttpServer::onTimeout( SocketServer& )
159 {
160 }
161 
163 {
164  HttpServer * pServer = static_cast < HttpServer* > ( p );
165  pServer->onStart();
166  return 0;
167 }
168 
169 }
HttpServer.h
FIX::HttpServer::onPoll
bool onPoll()
Definition: HttpServer.cpp:139
FIX::HttpServer::m_stop
bool m_stop
Definition: HttpServer.h:103
FIX::HttpServer::onWrite
void onWrite(SocketServer &, int)
Definition: HttpServer.cpp:160
FIX::HttpServer::onInitialize
void onInitialize(const SessionSettings &)
Definition: HttpServer.cpp:92
FIX::SocketServer::block
bool block(Strategy &strategy, bool poll=0, double timeout=0.0)
Definition: SocketServer.cpp:165
FIX::HttpServer::s_pServer
static HttpServer * s_pServer
Definition: HttpServer.h:107
FIX::HttpServer::onConfigure
void onConfigure(const SessionSettings &)
Definition: HttpServer.cpp:86
FIX::HttpServer::onConnect
void onConnect(SocketServer &, int, int)
Definition: HttpServer.cpp:152
FIX::HttpServer::m_settings
SessionSettings m_settings
Definition: HttpServer.h:100
FIX::HttpServer::stopGlobal
static void stopGlobal()
Definition: HttpServer.cpp:70
FIX::ConfigError
Application is not configured correctly
Definition: Exceptions.h:104
FIX::RuntimeError
Application encountered serious error during runtime
Definition: Exceptions.h:111
FIX::SocketServer::close
void close()
Definition: SocketServer.cpp:154
FIX::socket_isValid
bool socket_isValid(int socket)
Definition: Utility.cpp:294
FIX::SocketServer::getMonitor
SocketMonitor & getMonitor()
Definition: SocketServer.h:87
HttpConnection.h
FIX::HttpServer
Basic HTTP Server.
Definition: HttpServer.h:54
FIX::HttpServer::s_count
static int s_count
Definition: HttpServer.h:106
FIX::SessionSettings
Container for setting dictionaries mapped to sessions.
Definition: SessionSettings.h:237
THREAD_PROC
#define THREAD_PROC
Definition: Utility.h:184
FIX::IntConvertor::convert
static std::string convert(signed_int value)
Definition: FieldConvertors.h:170
FIX::HttpServer::onError
void onError(SocketServer &)
Definition: HttpServer.cpp:173
FIX::HttpServer::onData
bool onData(SocketServer &, int)
Definition: HttpServer.cpp:164
FIX::HttpServer::onStart
void onStart()
Definition: HttpServer.cpp:127
Settings.h
FIX
Definition: Acceptor.cpp:34
FIX::HttpServer::HttpServer
HttpServer(const SessionSettings &)
Definition: HttpServer.cpp:83
FIX::SocketMonitor::drop
bool drop(int socket)
Definition: SocketMonitor.cpp:115
FIX::HttpServer::startGlobal
static void startGlobal(const SessionSettings &)
Definition: HttpServer.cpp:54
FIX::HttpServer::onTimeout
void onTimeout(SocketServer &)
Definition: HttpServer.cpp:175
FIX::HttpServer::startThread
static THREAD_PROC startThread(void *p)
Definition: HttpServer.cpp:179
FIX::Locker
Locks/Unlocks a mutex using RAII.
Definition: Mutex.h:112
FIX::thread_join
void thread_join(thread_id thread)
Definition: Utility.cpp:454
FIX::HttpServer::s_mutex
static Mutex s_mutex
Definition: HttpServer.h:105
FIX::HttpServer::m_threadid
thread_id m_threadid
Definition: HttpServer.h:101
FIX::SocketServer
Listens for and accepts incoming socket connections on a port.
Definition: SocketServer.h:73
FIX::HttpServer::onStop
void onStop()
Definition: HttpServer.cpp:148
FIX::HttpServer::m_pServer
SocketServer * m_pServer
Definition: HttpServer.h:99
Utility.h
FIX::HTTP_ACCEPT_PORT
const char HTTP_ACCEPT_PORT[]
Definition: SessionSettings.h:148
FIX::HttpServer::onDisconnect
void onDisconnect(SocketServer &, int)
Definition: HttpServer.cpp:169
FIX::thread_spawn
bool thread_spawn(THREAD_START_ROUTINE func, void *var, thread_id &thread)
Definition: Utility.cpp:433
FIX::HttpServer::start
void start()
Definition: HttpServer.cpp:106
FIX::HttpServer::stop
void stop()
Definition: HttpServer.cpp:116

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