-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
294 lines (273 loc) · 8.13 KB
/
server.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <cstdio>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "server.h"
namespace server
{
const std::string Server::FAILED_STRING = "FAILED";
const std::string Server::OK_STRING = "OK";
const Server::CommandMap Server::COMMAND_MAP =
{
{"set-led-state", Command::SET_STATE},
{"get-led-state", Command::GET_STATE},
{"set-led-color", Command::SET_COLOR},
{"get-led-color", Command::GET_COLOR},
{"set-led-rate", Command::SET_RATE},
{"get-led-rate", Command::GET_RATE},
{"stop-server", Command::STOP_SERVER}
};
bool Server::set_state(const std::string& state_string, std::string& answer)
{
bool success = false;
auto state_pair = led::LED::STATE_MAP.find(state_string);
if (state_pair != led::LED::STATE_MAP.end())
{
if (_led.SetState(state_pair->second))
{
success = true;
}
}
answer = success ? Server::OK_STRING : Server::FAILED_STRING;
return success;
}
bool Server::set_color(const std::string& color_string, std::string& answer)
{
bool success = false;
auto color_pair = led::LED::COLOR_MAP.find(color_string);
if (color_pair != led::LED::COLOR_MAP.end())
{
if (_led.SetColor(color_pair->second))
{
success = true;
}
}
answer = success ? Server::OK_STRING : Server::FAILED_STRING;
return success;
}
bool Server::set_rate(const std::string& rate_string, std::string& answer)
{
bool success = false;
int rate = led::LED::INVALID_RATE;
try
{
rate = std::stoi(rate_string);
}
catch (...){}
if (_led.SetRate(rate))
{
success = true;
}
answer = success ? Server::OK_STRING : Server::FAILED_STRING;
return success;
}
bool Server::get_state(const std::string& args, std::string& answer)
{
led::State state = _led.GetState();
auto state_pair = std::find_if(led::LED::STATE_MAP.begin(), led::LED::STATE_MAP.end(),
[&state](const led::LED::StateMap::value_type& pair)
{return pair.second == state;});
if (state_pair != led::LED::STATE_MAP.end())
{
answer = Server::OK_STRING + " " + state_pair->first;
return true;
}
answer = Server::FAILED_STRING;
return false;
}
bool Server::get_color(const std::string& args, std::string& answer)
{
led::Color color = _led.GetColor();
auto color_pair = std::find_if(led::LED::COLOR_MAP.begin(), led::LED::COLOR_MAP.end(),
[&color](const led::LED::ColorMap::value_type& pair)
{return pair.second == color;});
if (color_pair != led::LED::COLOR_MAP.end())
{
answer = Server::OK_STRING + " " + color_pair->first;
return true;
}
answer = Server::FAILED_STRING;
return false;
}
bool Server::get_rate(const std::string& args, std::string& answer)
{
led::Rate rate = _led.GetRate();
if (rate >= led::LED::MIN_RATE && rate <= led::LED::MAX_RATE)
{
answer = Server::OK_STRING + " " + std::to_string(rate);
return true;
}
answer = Server::FAILED_STRING;
return false;
}
bool Server::unknown_command_handler(const std::string& args, std::string& answer)
{
printf("Unknown command with arguments: %s\n", args.c_str());
answer = Server::FAILED_STRING;
return true;
}
bool Server::stop_server(const std::string& args, std::string& answer)
{
answer = Server::OK_STRING;
return true;
}
bool Server::make_clean_pipe(const std::string& pipe_name)
{
if (pipe_name.empty())
{
printf("Pipe name is empty\n");
return false;
}
if (access(pipe_name.c_str(), F_OK) == 0)
{
if (unlink(pipe_name.c_str()) != 0)
{
perror(pipe_name.c_str());
return false;
}
}
if (mkfifo(pipe_name.c_str(), 0666) != 0)
{
perror(pipe_name.c_str());
return false;
}
return true;
}
int write_to_pipe(const std::string& pipe_out, const std::string& message)
{
int fd_out = open(pipe_out.c_str(), O_WRONLY);
if (fd_out == -1)
{
perror(pipe_out.c_str());
return -1;
}
int bytes = write(fd_out, message.c_str(), message.size()+1);
if (bytes == -1)
{
perror(pipe_out.c_str());
}
close(fd_out);
return bytes;
}
bool Server::parse_command(const std::string& buffer, const std::string& pipe_out)
{
auto command_pair = Server::COMMAND_MAP.find(buffer);
_last_command = (command_pair != Server::COMMAND_MAP.end()) ?
command_pair->second : Command::UNKNOWN;
if (_last_command == Command::STOP_SERVER)
{
process_command("", pipe_out);
printf("Stopping server\n");
return false;
}
return true;
}
bool Server::process_command(const std::string &arguments, const std::string& pipe_out)
{
std::string answer;
_handlers_map[_last_command](*this, arguments, answer);
write_to_pipe(pipe_out, answer);
_led.PrintState();
}
Server::Server()
{
_handlers_map =
{
{Command::SET_STATE, &server::Server::set_state},
{Command::GET_STATE, &server::Server::get_state},
{Command::SET_COLOR, &server::Server::set_color},
{Command::GET_COLOR, &server::Server::get_color},
{Command::SET_RATE, &server::Server::set_rate},
{Command::GET_RATE, &server::Server::get_rate},
{Command::STOP_SERVER, &server::Server::stop_server},
{Command::UNKNOWN, &server::Server::unknown_command_handler}
};
_last_command = Command::UNKNOWN;
}
void Server::Run(const std::string& pipe_in, const std::string& pipe_out)
{
printf("Starting server with pipes: %s, %s\n", pipe_in.c_str(), pipe_out.c_str());
if (!make_clean_pipe(pipe_in) || !make_clean_pipe(pipe_out))
{
return;
}
_led.PrintState();
int fd_in = open(pipe_in.c_str(), O_RDONLY);
if (fd_in == -1)
{
perror(pipe_in.c_str());
return;
}
char buffer[Server::MAX_COMMAND_LEN + 1] = "\0";
int bytes_number_read, args_start = 0;
bool is_reading_args = false;
for (int i=0; ; )
{
if ((bytes_number_read = read(fd_in, buffer + i, 1)) == -1)
{
perror("Error has occurred while reading from pipe");
return;
}
else if (bytes_number_read == 0)
{
sleep(1);
}
else
{
if (i >= Server::MAX_COMMAND_LEN - 1)
{
buffer[Server::MAX_COMMAND_LEN] = '\0';
printf("Wrong command format: _%s", buffer);
char c;
while (read(fd_in, &c, 1) > 0)
{
printf("%c", c);
if (c == '\n')
{
printf("_");
break;
}
}
_last_command = Command::UNKNOWN;
process_command("", pipe_out);
args_start = 0;
is_reading_args = false;
i=0;
buffer[0] = '\0';
}
// Finished reading first word (command)
else if (buffer[i] == ' ' && !is_reading_args)
{
args_start = i + 1;
is_reading_args = true;
buffer[i] = '\0';
if (!parse_command(buffer, pipe_out))
{
return;
}
}
// Finished reading the whole message
else if (buffer[i] == '\n')
{
buffer[i] = '\0';
_last_arguments = "";
if (is_reading_args)
{
_last_arguments = buffer + args_start;
}
else if (!parse_command(buffer, pipe_out))
{
return;
}
process_command(_last_arguments, pipe_out);
i = 0;
args_start = 0;
is_reading_args = false;
continue;
}
++i;
}
}
}
}