forked from pokhrel/ProtocolDesign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cc
371 lines (359 loc) · 11.4 KB
/
server.cc
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* IoTPS Server */
#include <algorithm>
#include <arpa/inet.h>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <vector>
#include <cstdio>
#include "comm.hh"
#include "helpers.hh"
#include "logging.hh"
#include "sensormsg.hh"
#include "server.hh"
#define IPLEN 50
int main(int argc, char *argv[])
{
char fname[50];
// parse command line options
char pport[PORTLEN], sport[PORTLEN],localip[IPLEN]; // publish port, subscribe port
if (getServerCmdLOpts(argc, argv, pport, sport,localip, PORTLEN) == -1)
return -1;
// create directory for log files
if (createDir(SLOGDIR) == -1)
return -1;
// tmpnam for unix socket
memset(fname,'\0',50);
tmpnam(fname);
// start server binary protocol process
pid_t pid;
if ((pid = fork()) < 0)
{
error("fork");
return -1;
}
else if (pid == 0)
{
if (execl("serverb", sport,fname,localip,(void*)0) == -1)
{
error("execl");
return -1;
}
}
// create publish socket
int pfd;
if ((pfd = custom_socket(AF_INET, pport,localip)) == -1)
return -1;
// create subscribe socket (UNIX domain)
int sfd;
if ((sfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
{
error("create subscribe socket");
return -1;
}
// bind and listen subscribe socket
if (bindAndListenUnixS(sfd, fname) == -1)
return -1;
fd_set rfds;
std::vector<int> fds;
fds.push_back(pfd);
fds.push_back(sfd);
int maxfd;
int ret;
struct timespec t;
t.tv_sec = 1;
t.tv_nsec = 0;
size_t rsize;
unsigned char buff[SBUFFSIZE];
struct sockaddr addr;
socklen_t len;
len = sizeof(struct sockaddr);
std::vector<std::string> sensors; // list of active sensors (device IDs)
std::map< std::string, std::vector<int> > sublists; // device IDs mapped to fd lists (subscribers)
std::map< int, std::string> clientids; // file descriptors mapped to client IDs
CommMessage text;
unsigned char obuff[SBUFFSIZE]; // send buffer
std::string str;
std::string cmd;
int temp;
int wr;
int count;
count = 0;
#ifdef vv
std::cout << "Server started" << std::endl;
#endif
while (1)
{
FD_ZERO(&rfds); // clear file descriptors from the set
for (size_t c = 0; c < fds.size(); c++)
FD_SET(fds[c], &rfds); // add file descriptor to the set
std::sort(fds.begin(), fds.end());
maxfd = fds[fds.size()-1] + 1; // check here
if ((ret = pselect(maxfd+1, &rfds, NULL, NULL, &t, NULL)) == -1)
{
error("pselect");
continue;
}
for (size_t c = 0; c < fds.size(); c++)
{
if (FD_ISSET(fds[c], &rfds))
{
if (fds[c] == pfd) // message from sensor
{
if ((rsize = recvfrom(pfd, (char*)buff, SBUFFSIZE, 0, &addr, &len)) == -1)
{
error("recvfrom");
continue;
}
else
{
double ts = getTimeStamp();
std::string msg((char*)buff, rsize);
SensorMessage sensormsg = SensorMessage(msg, ts);
if (!sensormsg.parse())
{
std::cerr << "sensor message parsing failed" << std::endl;
continue;
}
else // message successfully received
{
// log message
if (sensormsg.sensortype == CAMERA)
{
if (sensormsg.sensordata == "NO_MOTION")
logServerIncoming(SLOGDIR, sensormsg, false);
else // binary data
logServerIncoming(SLOGDIR, sensormsg, true);
}
else
logServerIncoming(SLOGDIR, sensormsg, false);
if (std::find(sensors.begin(), sensors.end(), sensormsg.deviceid) == sensors.end()) // new sensor
sensors.push_back(sensormsg.deviceid);
else // old sensor
{
if (sublists.size() == 0)
continue;
std::vector<int> subs = sublists.find(sensormsg.deviceid)->second; // subscribers to this sensor
#ifdef vv
std::cout << "number of subs to " << sensormsg.deviceid << ": " << subs.size() << std::endl;
#endif
for (std::vector<int>::const_iterator it = subs.begin(); it != subs.end(); it++)
{
text.updateServerID("server334");
text.updateCount(1);
text.updateSize(sensormsg.datasize);
std::vector<std::string> tt;
tt.push_back(sensormsg.deviceid);
text.updateDeviceIDs(tt);
text.updateSeqNo(sensormsg.seqno);
text.updateTimeStamp(sensormsg.sensorts);
str = text.createUpdatesMessage();
memset(obuff, 0, SBUFFSIZE); // clear previous messages
memcpy((char*)obuff, str.c_str(), str.size());
std::string clientid = clientids[*it];
if (sensormsg.sensortype == CAMERA)
{
if (sensormsg.sensordata == "NO_MOTION")
{
memcpy((char*)obuff + str.size(), sensormsg.sensordata.c_str(), sensormsg.datasize);
double ts = getTimeStamp();
logServerOutgoing(SLOGDIR, clientid, sensormsg.deviceid, (char*)obuff + str.size(), sensormsg.datasize, ts, false, sensormsg.seqno);
}
else // append binary data
{
unsigned char camdata[sensormsg.datasize];
sensormsg.camDataToArray(camdata);
memcpy((char*)obuff + str.size(), camdata, sensormsg.datasize);
logServerOutgoing(SLOGDIR, clientid, sensormsg.deviceid, (char*)obuff + str.size(), sensormsg.datasize, ts, true, sensormsg.seqno);
}
}
else
{
memcpy((char*)obuff + str.size(), sensormsg.sensordata.c_str(), sensormsg.datasize);
double ts = getTimeStamp();
logServerOutgoing(SLOGDIR, clientid, sensormsg.deviceid, (char*)obuff + str.size(), sensormsg.datasize, ts, false, sensormsg.seqno);
}
wr = send((*it), (char*)obuff, sensormsg.datasize + str.size(), 0);
}
}
}
}
}
else if (fds[c] == sfd) // connection from new client
{
temp = accept(sfd, NULL, NULL);
std::cout << setngetS(temp) << std::endl;
fds.push_back(temp);
}
else // data from existing client
{
memset(buff, 0, SBUFFSIZE); // clear previous messages
rsize = recv(fds[c], (char *)buff, SBUFFSIZE, 0);
if (rsize == 0)
{
#ifdef vv
std::cout << "client closed connection, cleaning client data...";
#endif
std::map< std::string, std::vector<int> >::iterator it;
for (it = sublists.begin(); it != sublists.end(); it++) // remove subscriptions for this client
{
std::vector<int>::iterator itr = std::find(it->second.begin(), it->second.end(), fds[c]);
if (itr != it->second.end())
it->second.erase(itr);
}
if (clientids.erase(fds[c]) != 1) // remove client ID
{
std::cerr << "failed to remove client ID" << std::endl;
return -1;
}
std::vector<int>::iterator itrt = std::find(fds.begin(), fds.end(), fds[c]);
if (itrt != fds.end())
fds.erase(itrt);
else
std::cerr << "failed to remove file descriptor" << std::endl;
if (close(fds[c]) == -1)
{
perror("close");
return -1;
}
#ifdef vv
std::cout << "cleaned" << std::endl;
#endif
continue;
}
text.updateMessage((char*)buff);
#ifdef vv
text.print();
#endif
text.parse();
cmd = text.getCommand();
if (cmd == "LIST")
{
text.updateServerID("server334");
text.updateDeviceIDs(sensors);
str = text.createListReply();
memcpy((char *)buff, str.c_str(), str.size());
send(fds[c], (char *)buff, str.size(), 0);
std::cout<<"UTotal"<<count++<<std::endl;
}
else if (cmd == "SUBSCRIBE")
{
clientids.insert(std::pair< int, std::string>(fds[c], text.getClientID()));
std::vector<std::string> devsToSub = text.getDeviceIDs(); // devices to subscribe
std::vector<std::string>::iterator it;
for (it = devsToSub.begin(); it != devsToSub.end(); it++)
{
if (std::find(sensors.begin(), sensors.end(), *it) == sensors.end())
{
std::cerr << "invalid device ID in SUBSCRIBE message" << std::endl;
devsToSub.erase(it);
continue;
}
std::map< std::string, std::vector<int> >::iterator itr;
if ((itr = sublists.find(*it)) == sublists.end()) // no subscribers to this sensor
{
#ifdef vv
std::cout << "no previous subscribers" << std::endl;
#endif
std::vector<int> clients; // create new list of clients subscribed to this sensor
clients.push_back(fds[c]);
sublists.insert(std::pair< std::string, std::vector<int> >(*it, clients));
}
else // sensor has subscribers
{
#ifdef vv
std::cout << "sensor has subscribers" << std::endl;
#endif
if (std::find(itr->second.begin(), itr->second.end(), fds[c]) == itr->second.end())
{
itr->second.push_back(fds[c]);
}
else // client has already subscribed to this sensor
{
std::cerr << "client has already subscribed to this sensor" << std::endl;
continue;
}
}
}
text.updateDeviceIDs(devsToSub);
text.updateCount(devsToSub.size());
text.updateServerID("server334");
str = text.createSubscribeReply();
#ifdef vv
std::cout << str << std::endl;
#endif
memcpy((char *)buff, str.c_str(), str.size());
send(fds[c], (char *)buff, str.size(), 0);
std::cout<<"UTotal"<<count++<<std::endl;
}
else if (cmd == "UNSUBSCRIBE")
{
#ifdef vv
std::cout << "unsub request from " << text.getDeviceIDs().size() << " device(s)" << std::endl;
#endif
std::vector<std::string> devsToUnsub = text.getDeviceIDs(); // devices to unsubscribe from
std::vector<std::string>::iterator it;
for (it = devsToUnsub.begin(); it != devsToUnsub.end(); it++)
{
if (std::find(sensors.begin(), sensors.end(), *it) == sensors.end())
{
std::cerr << "invalid device ID in UNSUBSCRIBE message" << std::endl;
devsToUnsub.erase(it);
continue;
}
std::map< std::string, std::vector<int> >::iterator itr;
if ((itr = sublists.find(*it)) == sublists.end()) // sensor has no subscribers
{
std::cerr << "sensor has no subscribers" << std::endl;
devsToUnsub.erase(it);
continue;
}
else // sensor has subscribers
{
std::vector<int>::iterator itrt = std::find(itr->second.begin(), itr->second.end(), fds[c]);
if (itrt == itr->second.end()) // client has not subscribed to this sensor
{
std::cerr << "client has not subscribed to this sensor" << std::endl;
devsToUnsub.erase(it);
continue;
}
else // valid unsubscribe request
{
#ifdef vv
std::cout << "valid unsubscribe request" << std::endl;
#endif
itr->second.erase(itrt);
if (itr->second.size() == 0) // client is the only subscriber to this sensor
{
#ifdef vv
std::cout << "client is the only subscriber to this sensor" << std::endl;
#endif
sublists.erase(itr);
}
}
}
}
text.updateDeviceIDs(devsToUnsub);
text.updateCount(devsToUnsub.size());
text.updateServerID("server334");
str = text.createUnsubscribeReply();
#ifdef vv
std::cout << str << std::endl;
#endif
memcpy((char *)buff, str.c_str(), str.size());
send(fds[c], (char *)buff, str.size(), 0);
std::cout<<"UTotal"<<count++<<std::endl;
}
}
}
}
}
return 0;
}