-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_client.cpp
More file actions
105 lines (95 loc) · 3.27 KB
/
main_client.cpp
File metadata and controls
105 lines (95 loc) · 3.27 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
#include "client.h"
#include <cli/cli.h>
#include <cli/clifilesession.h>
#include <boost/algorithm/string.hpp> // split
#include <boost/lexical_cast.hpp> // lexical_cast
using namespace cli;
int main(void)
{
client client;
bool connected = false;
auto rootMenu = std::make_unique< Menu >("CLIENT");
rootMenu->Insert(
"connect",
[&](std::ostream& out, const std::string& host_port)
{
if (!host_port.empty())
{
try
{
std::vector<std::string> result_vect;
boost::split(result_vect, host_port, boost::is_any_of(":"));
if (result_vect.size() == 2)
{
int port = boost::lexical_cast<int>(result_vect[1].c_str());
if (port > 0 && port < 65536)
{
if (!connected)
{
client.ConnectToServer(
result_vect[0], // host
result_vect[1] // port
);
connected = true;
}
else {
out << "\tYou are connected to server!" << "\n";
}
}
else
{
std::cout << "\tUNABLE TO CONNECT\n" << std::endl;
throw std::runtime_error("\tError: Invalid Port. Port must be greter than 0 and less than 65536\n");
}
}
else
{
out << "\tConnect string wrong! Use: \"connect <string>\"" << std::endl;
}
}
catch (const std::exception& ex)
{
out << "CLIENT> Connect exception: " << ex.what() << "\n";
}
}
else
{
out << "\tConnect string must be not empty! Use: \"connect <string>\"" << std::endl;
}
},
"Connect to Server");
rootMenu->Insert(
"send",
[&client, &connected](std::ostream& out, const std::vector<std::string>& msg)
{
if (msg.size() > 0)
{
try
{
if (connected) {
client.Send(msg);
}
else {
out << "\tYou need to connect to the server! Use: \"connect <string>\"" << "\n";
}
}
catch (const std::exception& ex)
{
out << "CLIENT> Send exception: " << ex.what() << "\n";
}
}
else
{
out << "\tMessage string must be not empty! Use: \"send <string>\"" << std::endl;
}
},
"Send message");
Cli cli(std::move(rootMenu));
cli.ExitAction([](auto& out)
{
out << "\tEXIT. Goodbye!\n";
});
CliFileSession input(cli);
input.Start();
return 0;
}