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
31namespace FIX
32{
35HttpServer* HttpServer::s_pServer = 0;
36
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
70throw ( ConfigError )
71{
72 m_port = s.get().getInt( HTTP_ACCEPT_PORT );
73}
74
76throw ( 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
135void 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
143void HttpServer::onWrite( SocketServer& server, int s )
144{
145}
146
147bool HttpServer::onData( SocketServer& server, int s )
148{
149 return true;
150}
151
153{
154}
155
157
161
163{
164 HttpServer * pServer = static_cast < HttpServer* > ( p );
165 pServer->onStart();
166 return 0;
167}
168
169}
#define THREAD_PROC
Definition Utility.h:184
Encapsulates a HTTP socket file descriptor.
Basic HTTP Server.
Definition HttpServer.h:38
static void startGlobal(const SessionSettings &)
static void stopGlobal()
bool onData(SocketServer &, int)
void onError(SocketServer &)
void onWrite(SocketServer &, int)
static int s_count
Definition HttpServer.h:72
void onConfigure(const SessionSettings &)
HttpServer(const SessionSettings &)
void onInitialize(const SessionSettings &)
static HttpServer * s_pServer
Definition HttpServer.h:73
static Mutex s_mutex
Definition HttpServer.h:71
SessionSettings m_settings
Definition HttpServer.h:66
static THREAD_PROC startThread(void *p)
void onTimeout(SocketServer &)
void onDisconnect(SocketServer &, int)
SocketServer * m_pServer
Definition HttpServer.h:65
thread_id m_threadid
Definition HttpServer.h:67
void onConnect(SocketServer &, int, int)
Locks/Unlocks a mutex using RAII.
Definition Mutex.h:96
Container for setting dictionaries mapped to sessions.
bool drop(int socket)
Listens for and accepts incoming socket connections on a port.
bool block(Strategy &strategy, bool poll=0, double timeout=0.0)
SocketMonitor & getMonitor()
bool thread_spawn(THREAD_START_ROUTINE func, void *var, thread_id &thread)
Definition Utility.cpp:416
void thread_join(thread_id thread)
Definition Utility.cpp:437
const char HTTP_ACCEPT_PORT[]
bool socket_isValid(int socket)
Definition Utility.cpp:277
Application is not configured correctly
Definition Exceptions.h:88
static std::string convert(signed_int value)
Application encountered serious error during runtime
Definition Exceptions.h:95

Generated on Sun Mar 31 2024 07:07:24 for QuickFIX by doxygen 1.9.8 written by Dimitri van Heesch, © 1997-2001