|
3 | 3 | #include <cli/cli.h> |
4 | 4 | #include <cli/clifilesession.h> |
5 | 5 |
|
| 6 | +#include <boost/thread.hpp> |
| 7 | + |
6 | 8 | using namespace cli; |
7 | 9 |
|
8 | 10 | int main(void) |
9 | 11 | { |
10 | | - boost::asio::io_service io_service; |
11 | | - unsigned int port; |
12 | | - bool set_port = false; |
13 | | - |
14 | | - auto rootMenu = std::make_unique< Menu >("SERVER"); |
15 | | - rootMenu->Insert( |
16 | | - "start", |
17 | | - [&](std::ostream& out) |
18 | | - { |
19 | | - if (set_port) |
| 12 | + boost::asio::io_service io_service; |
| 13 | + unsigned int port; |
| 14 | + bool set_port = false; |
| 15 | + |
| 16 | + auto rootMenu = std::make_unique< Menu >("SERVER"); |
| 17 | + rootMenu->Insert( |
| 18 | + "start", |
| 19 | + [&](std::ostream& out) |
| 20 | + { |
| 21 | + if (set_port) |
| 22 | + { |
| 23 | + try |
20 | 24 | { |
21 | | - try |
22 | | - { |
23 | | - server s(io_service, port); |
24 | | - std::cout << "\tSERVER STARTED ON " << port << std::endl; |
25 | | - if(io_service.stopped()) |
26 | | - io_service.restart(); |
27 | | - io_service.run(); |
28 | | - } |
29 | | - catch (const std::exception& e) |
30 | | - { |
31 | | - std::cerr << "SERVER> Exception: " << e.what() << "\n"; |
32 | | - } |
33 | | - } |
34 | | - else { |
35 | | - out << "You must set port for server. Use: \"setPort <int>\"" << "\n"; |
| 25 | + server s(std::move(io_service), port); |
| 26 | + std::cout << "\tSERVER STARTED ON " << port << std::endl; |
| 27 | + |
| 28 | + if (io_service.stopped()) |
| 29 | + io_service.restart(); |
| 30 | + |
| 31 | + std::thread run_service([&] { io_service.run(); }); |
| 32 | + run_service.join(); |
| 33 | + |
36 | 34 | } |
37 | | - }, |
38 | | - "Start server"); |
39 | | - |
40 | | - rootMenu->Insert( |
41 | | - "setPort", |
42 | | - [&port, &set_port](std::ostream& out, int x) |
43 | | - { |
44 | | - if (x > 0 && x < 65536) |
| 35 | + catch (const std::exception& e) |
45 | 36 | { |
46 | | - port = x; |
47 | | - out << "\tSERVER ON PORT: " << port << std::endl; |
48 | | - set_port = true; |
| 37 | + std::cerr << "SERVER> Exception: " << e.what() << "\n"; |
49 | 38 | } |
50 | | - else |
| 39 | + } |
| 40 | + else { |
| 41 | + out << "You must set port for server. Use: \"setPort <int>\"" << "\n"; |
| 42 | + } |
| 43 | + }, |
| 44 | + "Start server"); |
| 45 | + |
| 46 | + rootMenu->Insert( |
| 47 | + "setPort", |
| 48 | + [&port, &set_port](std::ostream& out, const int& x) |
| 49 | + { |
| 50 | + if (x > 0 && x < 65536) |
| 51 | + { |
| 52 | + port = x; |
| 53 | + out << "\tSERVER ON PORT: " << port << std::endl; |
| 54 | + set_port = true; |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + out << "\tPort must be greter than 0 and less than 65536" << std::endl; |
| 59 | + } |
| 60 | + }, |
| 61 | + "Set port for server"); |
| 62 | + |
| 63 | + rootMenu->Insert( |
| 64 | + "stop", |
| 65 | + [&](std::ostream& out) |
| 66 | + { |
| 67 | + if (set_port) { |
| 68 | + try |
51 | 69 | { |
52 | | - out << "\tPort must be greter than 0 and less than 65536" << std::endl; |
53 | | - } |
54 | | - }, |
55 | | - "Set port for server"); |
56 | | - |
57 | | - rootMenu->Insert( |
58 | | - "stop", |
59 | | - [&](std::ostream& out) |
60 | | - { |
61 | | - if (set_port){ |
62 | | - try |
63 | | - { |
64 | | - if (!io_service.stopped()) |
65 | | - io_service.stop(); |
66 | | - out << "\tSTOPED ON PORT " << port << std::endl; |
67 | | - } |
68 | | - catch (const std::exception& e) |
69 | | - { |
70 | | - std::cerr << "SERVER> Exception: " << e.what() << "\n"; |
71 | | - } |
| 70 | + if (!io_service.stopped()) |
| 71 | + io_service.stop(); |
| 72 | + out << "\tSTOPED ON PORT " << port << std::endl; |
72 | 73 | } |
73 | | - else { |
74 | | - out << "You don't specify the port or don't start the server." << "\n"; |
| 74 | + catch (const std::exception& e) |
| 75 | + { |
| 76 | + std::cerr << "SERVER> Exception: " << e.what() << "\n"; |
75 | 77 | } |
76 | | - }, |
77 | | - "Stop server"); |
| 78 | + } |
| 79 | + else { |
| 80 | + out << "You don't specify the port or don't start the server." << "\n"; |
| 81 | + } |
| 82 | + }, |
| 83 | + "Stop server"); |
78 | 84 |
|
79 | | - Cli cli(std::move(rootMenu)); |
80 | | - cli.ExitAction([&io_service](auto& out) |
81 | | - { |
| 85 | + Cli cli(std::move(rootMenu)); |
| 86 | + cli.ExitAction([&io_service](auto& out) |
| 87 | + { |
82 | 88 | if (!io_service.stopped()) |
83 | 89 | io_service.stop(); |
84 | | - |
85 | | - out << "\tEXIT. Goodbye!\n"; |
| 90 | + |
| 91 | + out << "\tEXIT. Goodbye!\n"; |
86 | 92 | }); |
87 | 93 |
|
88 | | - CliFileSession input(cli); |
89 | | - input.Start(); |
| 94 | + CliFileSession input(cli); |
| 95 | + input.Start(); |
90 | 96 |
|
91 | 97 | return 0; |
92 | 98 | } |
0 commit comments