Repository navigation
Expand file tree
/
Copy pathserver.cpp
More file actions
356 lines (287 loc) · 11.4 KB
/
Copy pathserver.cpp
File metadata and controls
356 lines (287 loc) · 11.4 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "server.hh"
void server_tcp::start() {
spdlog::info("server démare...");
listen_options lo;
lo.reuse_address = true;
_listener = engine().listen(make_ipv4_address({_port}), lo);
#ifdef PRELOAD_MOM
boost::uuids::string_generator gen;
auto id1 = makeIdentity("e7ff25c4-d64f-40f1-a649-0d1d8aafe3de", gen);
auto id2 = makeIdentity("635311da-3a70-4a57-9d3e-af6d10b4f582", gen);
auto id3 = makeIdentity("ea83b8b9-af30-4d2f-bc5f-4ec090be7da8", gen);
this->_tp.addClient(id1, socket_address{}, 10000);
this->_tp.addClient(id2, socket_address{}, 15000);
this->_tp.addClient(id3, socket_address{}, 20000);
Identity<std::string> gr1{"singlDel"};
Identity<std::string> gr2{"perMatch"};
Identity<std::string> gr3{"pubSub"};
Identity<std::string> gr4{"ToBeDeleted"};
this->_tp.addGroup(gr1, "single-delivery" );
this->_tp.addGroup(gr2, "per-match");
this->_tp.addGroup(gr3, "pub-sub");
this->_tp.addGroup(gr4, "pub-sub");
#endif
spdlog::info("écoute sur {}", _port);
auto v = keep_doing([this] () {
return _listener.accept().then(
[this](seastar::accept_result res) mutable {
auto conn = make_lw_shared<Connection>(std::move(res.connection), res.remote_address);
return handleConnection(conn);
});
})
.or_terminate();
}
future<> server_tcp::handleConnection(lw_shared_ptr<Connection> conn) {
return conn->readFromStream()
.then([this](
temporary_buffer<char> buf) -> std::optional<Specification> {
return parseRawRequest(std::move(buf));
})
.then([ this, addr = conn->getRemoteAddress() ](
std::optional<Specification> osp) mutable
->std::optional<std::string> {
if (!osp.has_value()) {
return {};
}
auto sp = osp.value();
return {handleRequest(sp, addr)};
})
.then([this, conn](std::optional<std::string> rep) {
if (conn->isAlive() && rep.has_value()) {
return conn->m_out.write(rep.value().c_str())
.then([conn] { return conn->m_out.flush(); })
.then([conn] { return conn->m_out.close(); })
.then([conn] {});
}
return make_ready_future();
})
.finally([conn] { return make_ready_future(); });
}
std::string server_tcp::handleRequest(Specification sp, socket_address addr) {
using namespace std::string_literals;
switch (sp.cmd) {
case COMMAND::CONTEXT: {
auto cltId = generateIdentity();
if (_tp.addClient(cltId, addr, 11232))
return "CONTEXT_OK\r\n"s + "Identity : "s + cltId.toString() +
"\r\n\r\n"s;
return "CONTEXT_ERR\r\nerror inconnue\r\n\r\n"s;
} break;
case COMMAND::UPDATE: {
if (sp.args.find("Identity") == sp.args.end())
return "UPDATE_ERR\r\n"s + "l'argument identité manque\r\n"s + "\r\n"s;
std::string idtmp;
try {
idtmp = std::get<std::string>(sp.args["Identity"]);
} catch (std::exception& ex) {
return "UPDATE_ERR\r\n"s + ex.what() + " niveau identité\r\n\r\n"s;
}
boost::uuids::string_generator gen;
auto id = makeIdentity(idtmp.c_str(), gen);
if (!_tp.clientExists(id))
return "UPDATE_ERR\r\n"s + "le contexte avec l'identité "s + idtmp +
" n'existe pas\r\n"s + "\r\n"s;
if (sp.args.find("Port") == sp.args.end())
return "UPDATE_ERR\r\n"s + "l'argument port manque\r\n"s + "\r\n"s;
int port;
try {
port = std::get<int>(sp.args["Port"]);
} catch (std::exception& ex) {
return "UPDATE_ERR\r\n"s + ex.what() + "\r\n\r\n"s;
}
return _tp.updateClient(id, addr, static_cast<uint16_t>(port));
} break;
case COMMAND::DISASSOCIATE: {
return "DISASSOCIATE\r\nnot implemened\r\n\r\n"s;
} break;
case COMMAND::NEW: {
if (sp.args.find("Entity") == sp.args.end())
return "NEW_ERR\r\n"s + "l'argument Entity manque\r\n"s + "\r\n"s;
if (sp.args.find("GroupType") == sp.args.end())
return "NEW_ERR\r\n"s + "l'argument GroupType manque\r\n"s + "\r\n"s;
if (sp.args.find("EntityName") == sp.args.end())
return "NEW_ERR\r\n"s + "l'argument EntityName manque\r\n"s + "\r\n"s;
std::string name, grpType;
try {
name = std::get<std::string>(sp.args["EntityName"]);
} catch (std::exception& ex) {
return "NEW_ERR\r\n"s + ex.what() + " au niveau EntityName\r\n\r\n"s;
}
try {
grpType = std::get<std::string>(sp.args["GroupType"]);
} catch (std::exception& ex) {
return "NEW_ERR\r\n"s + ex.what() + " au niveau Group Type\r\n\r\n"s;
}
std::array<std::string, 3> memTypes = {"single-delivery"s, "per-match"s,
"pub-sub"s};
if (std::none_of(
std::begin(memTypes),
std::end(memTypes), [grpType = grpType](const std::string &str) {
return grpType == str;
})) {
return "NEW_ERR\r\nMembershipType "s + grpType +" n'est pas supporté\r\n\r\n"s;
}
Identity gid{name};
if (!_tp.addGroup(gid, grpType))
return "NEW_ERR\r\nUn autre groupe existe dejà avec ce nom "s + name +
"\r\n\r\n"s;
return "NEW_OK\r\n\r\n"s;
} break;
case COMMAND::DELETE: {
if (sp.args.find("Entity") == sp.args.end())
return "DELETE_ERR\r\n"s + "l'argument Entity manque\r\n"s + "\r\n"s;
if (sp.args.find("EntityName") == sp.args.end())
return "DELETE_ERR\r\n"s + "l'argument EntityName manque\r\n"s + "\r\n"s;
std::string name;
try {
name = std::get<std::string>(sp.args["EntityName"]);
} catch (std::exception& ex) {
return "DELETE_ERR\r\n"s + ex.what() + " au niveau EntityName\r\n\r\n"s;
}
Identity gid{name};
if (!_tp.removeGroup(gid))
return "DELETE_ERR\r\nle groupe "s + name +
" n'existe pas pour le supprimer\r\n\r\n"s;
return "DELETE_OK\r\n\r\n"s;
} break;
case COMMAND::JOIN: {
if (sp.args.find("Identity") == sp.args.end())
return "JOIN_ERR\r\n"s + "l'argument Identity manque\r\n"s + "\r\n"s;
if (sp.args.find("EntityName") == sp.args.end())
return "JOIN_ERR\r\n"s + "l'argument EntityName manque\r\n"s + "\r\n"s;
if (sp.args.find("Entity") == sp.args.end())
return "JOIN_ERR\r\n"s + "l'argument Entity manque\r\n"s + "\r\n"s;
if (sp.args.find("MembershipType") == sp.args.end())
return "JOIN_ERR\r\n"s + "l'argument MembershipType manque\r\n"s +
"\r\n"s;
if (sp.args.find("Annotation") == sp.args.end())
return "JOIN_ERR\r\n"s + "l'argument Annotation manque\r\n"s + "\r\n"s;
std::string name, memType, ann;
try {
name = std::get<std::string>(sp.args["EntityName"]);
} catch (std::exception& ex) {
return "JOIN_ERR\r\n"s + ex.what() + " au niveau EntityName\r\n\r\n"s;
}
try {
ann = std::get<std::string>(sp.args["Annotation"]);
} catch (std::exception& ex) {
return "JOIN_ERR\r\n"s + ex.what() + " niveau Annotation\r\n\r\n"s;
}
try {
memType = std::get<std::string>(sp.args["MembershipType"]);
} catch (std::exception& ex) {
return "JOIN_ERR\r\n"s + ex.what() + " au niveau MembershipType\r\n\r\n"s;
}
std::string idtmp;
try {
idtmp = std::get<std::string>(sp.args["Identity"]);
} catch (std::exception& ex) {
return "JOIN_ERR\r\n"s + ex.what() + " niveau identité\r\n\r\n"s;
}
boost::uuids::string_generator gen;
Identity<boost::uuids::uuid> id;
try {
id = makeIdentity(idtmp.c_str(), gen);
} catch (std::exception& ex) {
return "JOIN_ERR\r\n"s + ex.what() + " "+ idtmp+ " n'est pas une identité valid\r\n\r\n"s;
}
Identity<std::string> gid{name};
if (!_tp.clientExists(id))
return "JOIN_ERR\r\n"s + "le contexte avec l'identité "s + idtmp +
" n'existe pas\r\n"s + "\r\n"s;
if (!_tp.groupExists(gid))
return "JOIN_ERR\r\nle groupe "s + name +
" n'existe pas\r\n\r\n"s;
;
return _tp.registerClientAs(gid, id, memType, ann);
} break;
case COMMAND::LEAVE: {
if (sp.args.find("Identity") == sp.args.end())
return "LEAVE_ERR\r\n"s + "l'argument Identity manque\r\n"s + "\r\n"s;
if (sp.args.find("EntityName") == sp.args.end())
return "LEAVE_ERR\r\n"s + "l'argument EntityName manque\r\n"s + "\r\n"s;
if (sp.args.find("Entity") == sp.args.end())
return "LEAVE_ERR\r\n"s + "l'argument Entity manque\r\n"s + "\r\n"s;
if (sp.args.find("MembershipType") == sp.args.end())
return "LEAVE_ERR\r\n"s + "l'argument MembershipType manque\r\n"s +
"\r\n"s;
std::string name, memType;
try {
name = std::get<std::string>(sp.args["EntityName"]);
} catch (std::exception& ex) {
return "LEAVE_ERR\r\n"s + ex.what() + " au niveau EntityName\r\n\r\n"s;
}
try {
memType = std::get<std::string>(sp.args["MembershipType"]);
} catch (std::exception& ex) {
return "LEAVE_ERR\r\n"s + ex.what() + " au niveau MembershipType\r\n\r\n"s;
}
std::string idtmp;
try {
idtmp = std::get<std::string>(sp.args["Identity"]);
} catch (std::exception& ex) {
return "LEAVE_ERR\r\n"s + ex.what() + " niveau identité\r\n\r\n"s;
}
boost::uuids::string_generator gen;
Identity<boost::uuids::uuid> id;
try {
id = makeIdentity(idtmp.c_str(), gen);
} catch (std::exception& ex) {
return "LEAVE_ERR\r\n"s + ex.what() + " "+ idtmp+ " n'est pas une identité valid\r\n\r\n"s;
}
Identity<std::string> gid{name};
if (!_tp.clientExists(id))
return "LEAVE_ERR\r\n"s + "le contexte avec l'identité "s + idtmp +
" n'existe pas\r\n"s + "\r\n"s;
if (!_tp.groupExists(gid))
return "LEAVE_ERR\r\nle groupe "s + name +
" n'existe pas\r\n\r\n"s;
return _tp.unregisterClientFrom(gid, id, memType);
} break;
case COMMAND::PUSH: {
if (sp.args.find("Identity") == sp.args.end())
return "PUSH_ERR\r\n"s + "l'argument Identity manque\r\n"s + "\r\n"s;
if (sp.args.find("EntityName") == sp.args.end())
return "PUSH_ERR\r\n"s + "l'argument EntityName manque\r\n"s + "\r\n"s;
if (sp.args.find("Entity") == sp.args.end())
return "PUSH_ERR\r\n"s + "l'argument Entity manque\r\n"s + "\r\n"s;
std::string name;
try {
name = std::get<std::string>(sp.args["EntityName"]);
} catch (std::exception& ex) {
return "PUSH_ERR\r\n"s + ex.what() + " au niveau EntityName\r\n\r\n"s;
}
std::string idtmp;
try {
idtmp = std::get<std::string>(sp.args["Identity"]);
} catch (std::exception& ex) {
return "PUSH_ERR\r\n"s + ex.what() + " niveau identité\r\n\r\n"s;
}
boost::uuids::string_generator gen;
Identity<boost::uuids::uuid> id;
try {
id = makeIdentity(idtmp.c_str(), gen);
} catch (std::exception& ex) {
return "PUSH_ERR\r\n"s + ex.what() + " "+ idtmp+ " n'est pas une identité valid\r\n\r\n"s;
}
Identity<std::string> gid{name};
if (!_tp.clientExists(id))
return "PUSH_ERR\r\n"s + "le contexte avec l'identité "s + idtmp +
" n'existe pas\r\n"s + "\r\n"s;
if (!_tp.groupExists(gid))
return "PUSH_ERR\r\nle groupe "s + name +
" n'existe pas\r\n\r\n"s;
return _tp.pushMessageTo(gid, id, sp.payload);
} break;
case COMMAND::UNKOWN:
default: {
spdlog::error("shoudln't get here");
return "ERROR"s;
}
// log the pspecification
}
// return "";
}
future<> server_tcp::stop() {
return make_ready_future<>();
}