-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
212 lines (178 loc) · 5.99 KB
/
main.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "main.hpp"
#include "filesystem/filesystem.hpp"
#include "scripting_engine/process.hpp"
#include "scripting_engine/common_state_setup.hpp"
#include "scripting_engine/script.hpp"
#include "scripting_engine/state.hpp"
// routers
#include "routers/workspace.hpp"
#include "routers/toolbar.hpp"
#include "communication_center.hpp"
#include "session/session.hpp"
#include "config.hpp"
#include "log.hpp"
#include "termination_handler.hpp"
#include "session/session_storage.hpp"
#include "streaming/common_messages/server_time.hpp"
// FIXME REMOVE
#include "hybrid_read_sink.hpp"
#include <special-paths/special_paths.hpp>
#include <iostream>
#include <thread>
#include <chrono>
#include <sstream>
#include <atomic>
using namespace std::chrono_literals;
using namespace std::string_literals;
using namespace attender;
int main(int argc, char** argv)
{
setupLog();
setupCrashHandler();
buildDirectoryStructure();
// Load Config
Config config;
// an io_service wrapper for boost::asio::io_service.
// you can provide your own implementation, by subclassing "attender::async_model".
managed_io_context <thread_pooler> context
{
static_cast <std::size_t> (config.httpThreadCount),
[]()
{
setupSignalHandler();
},
[](std::exception const& exc)
{
std::stringstream sstr;
sstr << std::this_thread::get_id();
LOG() << "Uncaught exception in thread " << sstr.str() << ": " << exc.what() << "\n";
}
};
// create a server
http_server server(
context.get_io_service(),
[](auto* connection, auto const& ec, auto const& exc) {
// some error occured. (this is not thread safe)
// You MUST check the error code here, because some codes mean, that the connection went kaputt!
// Accessing the connection might be invalid then / crash you.
if (ec.value() == boost::system::errc::protocol_error)
{
std::cout << connection->get_remote_address() << ":" << connection->get_remote_port() << "\n";
}
std::cerr << ec << " " << exc.what() << "\n";
},
attender::settings
{
false,
true
}
);
installSessionHandler <timed_memory_session_storage <uuid_generator, Session>> (server, config.corsOption, 2h);
// start server on port 80. Numbers are also valid
server.start(std::to_string(config.port));
// Routings
using namespace Routers;
CommunicationCenter communicationCenter{&server, config};
communicationCenter.streamer().setSessionManager(server.get_installed_session_manager());
communicationCenter.streamer().start();
//Filesystem::DirectoryCache cache{"D:/Development/IDE2/test-project"};
std::cout << "Waiting for enter\n";
std::atomic_bool exit{false};
std::thread enterWait {[&exit](){
std::cin.get();
exit.store(true);
}};
for (int i = 0; !exit.load(); ++i)
{
std::this_thread::sleep_for(100ms);
if (i == 10)
{
//std::cout << "connection count: " << server.get_connections()->count() << "\n";
i = 0;
}
}
communicationCenter.streamer().shutdownAll();
std::this_thread::sleep_for(100ms);
if (enterWait.joinable())
enterWait.join();
return 0;
}
void setupLog()
{
auto logPath = []()
{
auto home = sfs::path{SpecialPaths::getHome()};
if (!sfs::exists(home / ".minIDE"))
sfs::create_directory(home / ".minIDE");
if (!sfs::exists(home / ".minIDE" / "logs"))
sfs::create_directory(home / ".minIDE" / "logs");
return (home / ".minIDE" / "logs" / "log").string();
};
// Logging
auto& log = LOG().log();
log.configureProjectMainFile(__FILE__);
log.setTerminalEnabled(true);
log.open(
logPath(),
10
);
LOG() << "Build Time and Date: " << __DATE__ << " " << __TIME__ << '\n';
}
void setupSignalHandler()
{
signal(SIGABRT, &onBadSignal);
signal(SIGFPE, &onBadSignal);
signal(SIGILL, &onBadSignal);
signal(SIGSEGV, &onBadSignal);
}
void setupCrashHandler()
{
std::set_terminate(&onTerminate);
setupSignalHandler();
}
void buildDirectoryStructure()
{
auto createAndRecheck = [](sfs::path const& where)
{
if (!sfs::exists(where))
sfs::create_directory(where);
if (!sfs::exists(where))
throw std::runtime_error("Cannot create path: "s + where.string());
};
auto home = sfs::path{SpecialPaths::getHome()};
auto minIdeUser = home / ".minIDE";
createAndRecheck(minIdeUser);
createAndRecheck(minIdeUser / "logs");
createAndRecheck(minIdeUser / "lua");
createAndRecheck(minIdeUser / "lua" / "lib");
createAndRecheck(minIdeUser / "toolbars");
createAndRecheck(minIdeUser / "toolbar_persistence");
}
void testLua()
{
/*
using namespace MinIDE::Scripting;
try
{
auto state = std::make_shared<StateCollection>();
sol::state& lua = state->lua;
commonStateSetup(lua, true);
loadProcessUtility(state);
auto home = sfs::path{SpecialPaths::getHome()};
auto cmakeScript = home / ".minIDE" / "toolbars" / "cmake" / "main.lua";
std::cout << cmakeScript.string() << "\n";
std::lock_guard <StateCollection::mutex_type> {state->globalMutex};
lua["debugging"] = false;
lua.safe_script(Script{cmakeScript}.script());
sol::protected_function runAction = lua["runAction"];
if (!runAction.valid())
throw std::runtime_error("script does not have 'runAction' function");
runAction(0);
std::cin.get();
}
catch(std::exception const& exc)
{
std::cout << exc.what() << "\n";
}
*/
}