-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_server.cpp
More file actions
115 lines (96 loc) · 3.05 KB
/
main_server.cpp
File metadata and controls
115 lines (96 loc) · 3.05 KB
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
#include "server.h"
#include <cli/cli.h>
#include <cli/clifilesession.h>
#include <boost/thread.hpp>
using namespace cli;
int main(void)
{
boost::asio::io_service io_service;
unsigned int port;
bool set_port = false, started = false;
std::unique_ptr<server> ser;
boost::thread run_service;
auto rootMenu = std::make_unique< Menu >("SERVER");
rootMenu->Insert(
"start",
[&](std::ostream& out)
{
if (set_port && !started)
{
try
{
started = true;
ser = std::make_unique<server>(std::move(io_service), port);
std::cout << "\tSERVER STARTED ON " << port << std::endl;
run_service = boost::thread([&io_service]
{
io_service.reset();
io_service.run();
});
}
catch (const std::exception& e)
{
std::cerr << "\tSERVER> Exception: " << e.what() << "\n";
}
}
else {
out << "\tYou already started the server or you must set the port for the server. Use: \"setPort <int>\" or \"start\"" << "\n";
}
},
"Start server");
rootMenu->Insert(
"setPort",
[&port, &set_port](std::ostream& out, const int& x)
{
if (x > 0 && x < 65536)
{
port = x;
out << "\tSERVER ON PORT: " << port << std::endl;
set_port = true;
}
else
{
out << "\tPort must be greter than 0 and less than 65536" << std::endl;
}
},
"Set port for server");
rootMenu->Insert(
"stop",
[&](std::ostream& out)
{
if (set_port) {
try
{
if (!io_service.stopped())
io_service.stop();
run_service.join();
delete ser.release();
ser.reset();
started = false;
out << "\tSTOPED ON PORT " << port << std::endl;
}
catch (const std::exception& e)
{
std::cerr << "\tSERVER> Exception: " << e.what() << "\n";
}
}
else {
out << "You don't specify the port or don't start the server." << "\n";
}
},
"Stop server");
Cli cli(std::move(rootMenu));
cli.ExitAction([&io_service, &run_service, &ser, &started](auto& out)
{
if (!io_service.stopped())
io_service.stop();
run_service.join();
delete ser.release();
ser.reset();
started = false;
out << "\tEXIT. Goodbye!\n";
});
CliFileSession input(cli);
input.Start();
return 0;
}