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 
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 
67 : m_pServer( 0 ), m_settings( settings ), m_threadid( 0 ), m_port( 0 ), m_stop( false ) {}
68 
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 
90 {
91  m_stop = false;
94 
95  if( !thread_spawn( &startThread, this, m_threadid ) )
96  throw RuntimeError("Unable to spawn thread");
97 }
98 
100 {
101  if( m_stop ) return;
102  m_stop = true;
103  onStop();
104 
105  if( m_threadid )
107  m_threadid = 0;
108 }
109 
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 
123 {
124  if( !m_pServer || m_stop )
125  return false;
126 
127  m_pServer->block( *this, true );
128  return true;
129 }
130 
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 
159 {
160 }
161 
163 {
164  HttpServer * pServer = static_cast < HttpServer* > ( p );
165  pServer->onStart();
166  return 0;
167 }
168 
169 }
Encapsulates a HTTP socket file descriptor.
static void startGlobal(const SessionSettings &)
Definition: HttpServer.cpp:37
void thread_join(thread_id thread)
Definition: Utility.cpp:415
bool socket_isValid(int socket)
Definition: Utility.cpp:255
SocketMonitor & getMonitor()
Definition: SocketServer.h:70
HttpServer(const SessionSettings &)
Definition: HttpServer.cpp:66
void onTimeout(SocketServer &)
Definition: HttpServer.cpp:158
bool thread_spawn(THREAD_START_ROUTINE func, void *var, thread_id &thread)
Definition: Utility.cpp:394
thread_id m_threadid
Definition: HttpServer.h:67
static std::string convert(signed_int value)
SocketServer * m_pServer
Definition: HttpServer.h:65
bool block(Strategy &strategy, bool poll=0, double timeout=0.0)
Definition: Acceptor.cpp:34
static THREAD_PROC startThread(void *p)
Definition: HttpServer.cpp:162
int add(int port, bool reuse=false, bool noDelay=false, int sendBufSize=0, int rcvBufSize=0)
void onDisconnect(SocketServer &, int)
Definition: HttpServer.cpp:152
Application encountered serious error during runtime
Definition: Exceptions.h:94
Application is not configured correctly
Definition: Exceptions.h:87
Locks/Unlocks a mutex using RAII.
Definition: Mutex.h:95
static Mutex s_mutex
Definition: HttpServer.h:71
Container for setting dictionaries mapped to sessions.
void onConfigure(const SessionSettings &)
Definition: HttpServer.cpp:69
bool drop(int socket)
void onInitialize(const SessionSettings &)
Definition: HttpServer.cpp:75
static int s_count
Definition: HttpServer.h:72
static void stopGlobal()
Definition: HttpServer.cpp:53
SessionSettings m_settings
Definition: HttpServer.h:66
void onWrite(SocketServer &, int)
Definition: HttpServer.cpp:143
static HttpServer * s_pServer
Definition: HttpServer.h:73
bool onData(SocketServer &, int)
Definition: HttpServer.cpp:147
#define THREAD_PROC
Definition: Utility.h:158
Listens for and accepts incoming socket connections on a port.
Definition: SocketServer.h:56
void onConnect(SocketServer &, int, int)
Definition: HttpServer.cpp:135
void onError(SocketServer &)
Definition: HttpServer.cpp:156
const char HTTP_ACCEPT_PORT[]
Basic HTTP Server.
Definition: HttpServer.h:37

Generated on Thu Sep 5 2019 11:07:58 for QuickFIX by doxygen 1.8.13 written by Dimitri van Heesch, © 1997-2001