-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpservice.cpp
164 lines (126 loc) · 3.28 KB
/
httpservice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "httpservice.h"
#include <thread>
#ifndef _MSC_VER
// save diagnostic state
#pragma GCC diagnostic push
// turn off the specific warning. Can also use "-Wall"
#pragma GCC diagnostic ignored "-Wall"
//#pragma GCC diagnostic ignored "-Wunused-variable"
//#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#define BOOST_CONFIG_WARNING_DISABLE_HPP
#pragma GCC system_header
#endif
#include <boost/asio.hpp>
#ifndef _MSC_VER
#pragma GCC diagnostic pop
#endif
#include "http/server.hpp"
#ifdef WITH_WEBSOCKET
#include "websocket/web_server.hpp"
#endif
#ifdef WITH_TCPNATIVE
#include "tcpnative/tcp_server.hpp"
#endif
#include "controller.h"
#include "modulemanager.h"
#include "javascriptmanager.h"
#include <iostream>
//using namespace openrc::http;
using namespace std;
std::vector<shared_ptr<thread>> _threads;
namespace openrc {
vector<weak_ptr<base_server>> _servers;
void RunServer(shared_ptr<base_server> _s)
{
try
{
// Initialise the server.
//_s = new T(address, port, arguments["root"]);
// Run the server until stopped.
_s->run();
}
catch (std::exception& e)
{
std::cerr << "exception: " << e.what() << "\n";
}
}
HttpService::HttpService(const HttpServiceArguments& arguments)
: _arguments(arguments)
{
ControllerManager::ConstructControllers();
ModuleManager::Instance().LoadModulesFromFolder("./");
const std::string rootDir = _arguments["root"];
ModuleManager::Instance().LoadModulesFromFolder(rootDir.empty() ? "./modules" : rootDir);
ModuleManager::Instance().InitializeAll();
#ifdef WITH_JAVASCRIPT
const std::string jsPath = _arguments["js"];
JavascriptManager::Instance().Initialize(jsPath.empty() ? "./js" : jsPath);
#endif
}
HttpService::~HttpService()
{
_threads.clear();
_servers.clear();
#ifdef WITH_JAVASCRIPT
JavascriptManager::Instance().Shutdown();
#endif
ModuleManager::Instance().ShutdownAll();
}
bool HttpService::Start(const string& address, const string& port)
{
_address = address;
_port = port;
// Jttp.
std::size_t thread_pool_size = _arguments["thread_pool_size"].empty() ? 1 : stoi(_arguments["thread_pool_size"]);
auto httpServer = make_shared<http::server::server>(_address, _port, _arguments["root"], thread_pool_size);
_servers.push_back(httpServer);
auto thread = make_shared<std::thread>(&RunServer, httpServer);
_threads.push_back(thread);
// WebSocket
#ifdef WITH_WEBSOCKET
auto wsServer = make_shared<websocket::web_server>(_address, "80", _arguments["root"]);
_servers.push_back(wsServer);
auto wsThread = make_shared<std::thread>(&RunServer, wsServer);
_threads.push_back(wsThread);
#endif
return true;
}
bool HttpService::Stop()
{
for(auto& serverWeak : _servers)
{
auto server = serverWeak.lock();
if(server)
{
server->stop();
}
}
return true;
}
bool HttpService::Join(float time)
{
for(auto& thread : _threads)
{
thread->join();
}
return true;
}
#if 0
void HttpService::Run()
{
try
{
// Initialise the server.
_s = new server::server(_address, _port, _arguments["root"]);
// Run the server until stopped.
_s->run();
}
catch (std::exception& e)
{
std::cerr << "exception: " << e.what() << "\n";
}
delete _s;
_s = nullptr;
}
#endif
} // End http.