-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
685 lines (485 loc) · 15 KB
/
server.cpp
File metadata and controls
685 lines (485 loc) · 15 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/* (c) Intel Corporation 2015, All Rights Reserved */
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/time.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <signal.h>
#include <pthread.h>
#include "parse.h"
#include "common.h"
#include <iostream>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <sys/syscall.h>
#include <netinet/tcp.h>
struct timeval tv_start;
static int options_socket_fd = -1;
static int __lookup_zone_id(int client_id, int camera_id) {
int row;
int column;
row = ((client_id-1)/5) * 2 + camera_id;
column = 4 - (client_id - 1) % 5;
return row * 5 + (column + 1);
}
static void __show_timeval(void) {
struct timeval tv_now;
unsigned long delta;
int sec;
int usec;
gettimeofday(&tv_now, 0);
delta = (tv_now.tv_sec - tv_start.tv_sec) * 1000000 + (tv_now.tv_usec - tv_start.tv_usec);
sec = delta / 1000000;
usec = delta % 1000000;
printf("[%d:%d][tid:%lu] ", sec, (int)((usec)/10000.0), syscall(SYS_gettid));
}
#define PR_LOG(x...) __show_timeval(); printf("[line: %d] ", __LINE__); printf(x);
static void __dump_log_and_exit(void) {
// TODO: gather log
PR_LOG("Fatal error.. exiting\n");
exit(-1);
}
static int do_exit = 0;
static int no_vis_server = 1;
using namespace std;
typedef struct connect_info_t {
pthread_t *thread;
int connfd;
struct sockaddr_in cliaddr;
hub_settings_t *hub_settings;
pthread_mutex_t update_ready_mutex;
int am_alive;
} connect_info_t;
typedef struct connect_node_t {
connect_info_t *info;
struct connect_node_t *next;
} connect_node_t;
connect_node_t *cl_head = NULL;
pthread_mutex_t cl_list_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t options_mutex = PTHREAD_MUTEX_INITIALIZER;
static int client_list_changed = 0;
void __client_list_del(connect_info_t *info) {
connect_node_t *curr;
connect_node_t *prev;
pthread_mutex_lock(&cl_list_mutex);
client_list_changed = 1;
curr = cl_head;
prev = NULL;
if (cl_head == NULL) {
pthread_mutex_unlock(&cl_list_mutex);
return;
}
while (curr) {
if (curr->info == info) {
// printf("DBG: found node \n");
break;
}
prev = curr;
curr = curr->next;
}
if (curr == NULL) {
pthread_mutex_unlock(&cl_list_mutex);
return;
}
if (curr == cl_head) {
cl_head = cl_head->next;
} else {
prev->next = curr->next;
}
free(curr);
pthread_mutex_unlock(&cl_list_mutex);
}
void __client_list_add(connect_info_t *info) {
connect_node_t *node;
node = (connect_node_t*)malloc(sizeof(connect_node_t));
pthread_mutex_lock(&cl_list_mutex);
client_list_changed = 1;
node->info = info;
node->next = cl_head;
cl_head = node;
pthread_mutex_unlock(&cl_list_mutex);
return;
}
void __client_list_update(int options_changed) {
connect_node_t *tmp;
pthread_mutex_lock(&cl_list_mutex);
// If nothing changed in client list and options havent changed
// then dont send update
if (client_list_changed == 0 && options_changed == 0) {
pthread_mutex_unlock(&cl_list_mutex);
return;
}
tmp = cl_head;
while (tmp != NULL) {
pthread_mutex_unlock(&tmp->info->update_ready_mutex);
tmp = tmp->next;
}
client_list_changed = 0;
pthread_mutex_unlock(&cl_list_mutex);
return;
}
typedef struct vis_server_info_t {
char *ip;
int port;
int v_sockfd;
struct sockaddr_in addr;
} vis_server_info_t;
#define MAX_NUM_VIS_SERVERS 5
vis_server_info_t vis_server[MAX_NUM_VIS_SERVERS];
int num_vis_servers = 0;
static void __connect_to_visualization(void) {
int rc;
int i;
for (i=0; i<num_vis_servers; i++) {
#ifdef VIS_SERVER_TCP_CONNECTION
PR_LOG("Connecting to visual server %s:%d... \n", vis_server[i].ip, vis_server[i].port);
vis_server[i].v_sockfd = socket(AF_INET, SOCK_STREAM, 0);
#else
vis_server[i].v_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
#endif
memset(&vis_server[i].addr, 0x0, sizeof(struct sockaddr_in));
vis_server[i].addr.sin_family = AF_INET;
vis_server[i].addr.sin_port = htons(vis_server[i].port);
vis_server[i].addr.sin_addr.s_addr = inet_addr(vis_server[i].ip);
#ifdef VIS_SERVER_TCP_CONNECTION
rc = connect(vis_server[i].v_sockfd, (struct sockaddr*)&vis_server[i].addr, sizeof(vis_server[i].addr));
if (rc != 0) {
PR_LOG("!!!!!!!!!ERROR: failed to connect to %s:%d!!!!!!!!!!\n", vis_server[i].ip, vis_server[i].port);
} else {
PR_LOG("[connected to %s:%d]\n", vis_server[i].ip, vis_server[i].port);
}
#endif
}
}
static pthread_mutex_t vis_mutex = PTHREAD_MUTEX_INITIALIZER;
// Thread function to read from a given clients socket
// Clients will send analyzed data back to server
void *client_read_function(void *data) {
analyzed_info_t *p_data;
client_packet_t client_packet;
connect_info_t *c_info;
int rc;
visual_packet_t vis_packet;
c_info = (connect_info_t*)data;
PR_LOG("Starting read function\n");
int valid_packet = 1;
while (1) {
valid_packet = 1;
rc = __recvfrom(c_info->connfd, &client_packet, sizeof(client_packet_t));
if (rc != 0) {
//PR_LOG("Failed in __recvfrom; exiting read\n");
c_info->am_alive = 0;
pthread_mutex_unlock(&c_info->update_ready_mutex);
return NULL;
}
pthread_mutex_lock(&options_mutex);
vis_packet.client_id = c_info->hub_settings->client_id;
pthread_mutex_unlock(&options_mutex);
p_data = &client_packet.data;
// Verify packet
{
unsigned char exp_checksum;
unsigned char actual_checksum;
actual_checksum = client_packet.checksum;
exp_checksum = __calc_checksum(p_data);
if (actual_checksum != exp_checksum) {
PR_LOG("Packet checksum from client [id = %d] doesnt match\n", vis_packet.client_id);
PR_LOG("expected = 0x%x actual = 0x%x\n\n", exp_checksum, actual_checksum);
valid_packet = 0;
}
if (client_packet.header.signature != CLIENT_HEADER_SIGNATURE) {
PR_LOG("Packet signature failure for client [id = %d]; got 0x%x\n",
vis_packet.client_id,
client_packet.header.signature);
valid_packet = 0;
}
}
int retry_count = 0;
if (!valid_packet) {
client_packet_header_t hdr;
PR_LOG("Packet was invalid... attempting recovery\n");
while (1) {
rc = __recvfrom(c_info->connfd, &hdr, sizeof(hdr));
if (rc != 0) {
PR_LOG("Failed in __recvfrom for signature retrieval\n");
}
// If we found client header signature, read the rest of the packet
if (hdr.signature == CLIENT_HEADER_SIGNATURE) {
PR_LOG("Packet signature detected\n");
rc = __recvfrom(c_info->connfd,
((unsigned char*)&client_packet) + sizeof(hdr),
sizeof(client_packet_t) - sizeof(hdr));
if (client_packet.checksum == __calc_checksum(&client_packet.data)) {
break;
}
} else {
PR_LOG("Not a signature.. retrying\n");
}
}
retry_count++;
if (retry_count > sizeof(client_packet_t)/sizeof(client_packet_header_t)) {
PR_LOG("FATAL: Retry count exceeded.. signature not spotted\n");
__dump_log_and_exit();
}
}
pthread_mutex_lock(&vis_mutex);
#ifdef ENABLE_TIMESTAMPS
struct timeval tv_recv;
GET_TIMESTAMP(&tv_recv);
#define DELTA(x,y) ((x.tv_sec - y.tv_sec) * 1000000 + (x.tv_usec - y.tv_usec))
{
unsigned long delta1, delta2, delta3;
delta1 = DELTA(tv_recv, p_data->timestamp_gotframe);
delta2 = DELTA(tv_recv, p_data->timestamp_sent);
delta3 = DELTA(p_data->timestamp_sent, p_data->timestamp_gotframe);
printf("Processing time: %lu Send time: %lu total time : %lu\n", delta3, delta2, delta1);
}
#undef DELTA
#endif
memcpy(&vis_packet.data, p_data, sizeof(analyzed_info_t));
if (vis_packet.data.frame_id == 0) {
PR_LOG("FRAME ID was 0 for client %d packet was %lu\n", vis_packet.client_id, sizeof(analyzed_info_t));
}
if (!no_vis_server) {
int i;
vis_packet.zone_id = __lookup_zone_id(vis_packet.client_id, vis_packet.data.camera_id);
// Send same packet to all servers
for (i=0; i<num_vis_servers; i++) {
rc = sendto(vis_server[i].v_sockfd, &vis_packet, sizeof(visual_packet_t), MSG_DONTWAIT, (struct sockaddr*)&vis_server[i].addr, sizeof(vis_server[i].addr));
if (rc < 0) {
PR_LOG("Failed to send data to visual server %s:%d\n",
vis_server[i].ip, vis_server[i].port);
// __dump_log_and_exit();
}
}
}
pthread_mutex_unlock(&vis_mutex);
}
}
// Main client thread function; spawned for each client
// Spawns 'read' thread for the client, and sends periodic updates to the client regarding clients settings
void *client_function(void *data) {
hub_settings_t current_hub_settings;
int rc;
pthread_t recv_thread;
connect_info_t *c_info;
c_info = (connect_info_t*)data;
pthread_create(&recv_thread, 0, client_read_function, c_info);
while (1) {
// We dont send anything until there is an update ready
pthread_mutex_lock(&c_info->update_ready_mutex);
if (c_info->am_alive == 0) {
//PR_LOG("Client %s not alive. exiting\n", c_info->hub_settings->ip_address);
pthread_mutex_unlock(&c_info->update_ready_mutex);
break;
}
pthread_mutex_lock(&options_mutex);
current_hub_settings = *(c_info->hub_settings);
pthread_mutex_unlock(&options_mutex);
pthread_mutex_unlock(&c_info->update_ready_mutex);
rc = sendto(c_info->connfd, ¤t_hub_settings, sizeof(hub_settings_t), 0, (struct sockaddr *)&c_info->cliaddr,sizeof(c_info->cliaddr));
if (rc == 0 || rc == -1) {
PR_LOG("Client %s is no longer alive\n", c_info->hub_settings->ip_address);
break;
}
pthread_mutex_lock(&c_info->update_ready_mutex);
}
pthread_join(recv_thread, NULL);
shutdown(c_info->connfd, SHUT_RDWR);
close(c_info->connfd);
//PR_LOG("Client %s exiting...\n", c_info->hub_settings->ip_address);
__client_list_del(c_info);
pthread_mutex_destroy(&c_info->update_ready_mutex);
free(c_info->thread);
free(c_info);
return NULL;
}
/* Starts listening server on SERVER_PORT port */
void *start_server(void* arg)
{
int listenfd,connfd;
struct sockaddr_in servaddr,cliaddr;
socklen_t clilen;
char client_name[INET6_ADDRSTRLEN];
hub_settings_t *hub_settings;
int rc;
int client_id;
pthread_t *client_thread;
connect_info_t *p_connect_info;
if (!no_vis_server) {
__connect_to_visualization();
}
PR_LOG("Initializing main server...\n");
listenfd = socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERVER_PORT);
rc = bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
if (rc == -1) {
perror("bind error: ");
return NULL;
}
rc = listen(listenfd,1024);
if (rc == -1) {
perror("Listen error: ");
return NULL;
}
PR_LOG("Accepting connections on main server...\n");
// Repeat until we get killed
while (do_exit == 0) {
clilen = sizeof(cliaddr);
connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);
int one = 1;
if (connfd == -1) {
PR_LOG("ACCEPT ERROR\n");
continue;
}
setsockopt(connfd, SOL_TCP, TCP_NODELAY, &one, sizeof(one));
client_id = -1;
hub_settings = NULL;
__recvfrom(connfd, &client_id, sizeof(int));
PR_LOG("Received client id = %d\n", client_id);
// Extract clients IP address
if (inet_ntop(AF_INET, &cliaddr.sin_addr.s_addr, client_name, sizeof(client_name)) != NULL) {
PR_LOG("Client connected; Address = %s\n", client_name);
// Lookup client based on its stringified address
// hub_settings = __get_hub_settings(client_name);
}
if (client_id != -1) {
hub_settings = __get_client_settings(client_id);
}
if (hub_settings == NULL) {
PR_LOG("ERROR: unknown client connected from IP %s\n", client_name);
continue;
}
// Allocate new structure that holds all client information
p_connect_info = (connect_info_t*)malloc(sizeof(connect_info_t));
client_thread = (pthread_t*)malloc(sizeof(pthread_t));
p_connect_info->thread = client_thread;
p_connect_info->connfd = connfd;
memcpy(&p_connect_info->cliaddr, &cliaddr, sizeof(cliaddr));
p_connect_info->hub_settings = hub_settings;
pthread_mutex_init(&p_connect_info->update_ready_mutex, NULL);
pthread_mutex_lock(&p_connect_info->update_ready_mutex);
// Add client to list of clients
__client_list_add(p_connect_info);
// __dbg_init_analyzed_data(p_connect_info);
p_connect_info->am_alive = 1;
// Spawn a thread for reading/writing to socket for the given client
pthread_create(client_thread, NULL, client_function, p_connect_info);
}
PR_LOG("-Server Exited-\n");
return NULL;
}
static void sighandler(int val)
{
if (val == SIGTERM || val == SIGINT) {
PR_LOG("EXITING SERVER due to signal %d\n", val);
do_exit = 1;
exit(0);
}
}
static void signals_init(void)
{
struct sigaction action;
action.sa_handler = sighandler;
action.sa_flags = 0;
sigemptyset (&action.sa_mask);
sigaction (SIGINT, &action, NULL);
sigaction (SIGTERM, &action, NULL);
sigaction (SIGHUP, &action, NULL);
sigaction (SIGPIPE, &action, NULL);
}
static void __opt_dump_stats(void) {
printf("\n");
connect_node_t *tmp;
struct sockaddr_in *p_addr;
char client_name[INET6_ADDRSTRLEN];
pthread_mutex_lock(&cl_list_mutex);
tmp = cl_head;
printf("-----------STATS-------------\n");
while (tmp != NULL) {
p_addr = &tmp->info->cliaddr;
if (inet_ntop(AF_INET, &p_addr->sin_addr.s_addr, client_name, sizeof(client_name)) != NULL) {
printf("IP = %s\n", client_name);
}
tmp = tmp->next;
}
printf("-----------------------------\n\n");
pthread_mutex_unlock(&cl_list_mutex);
}
void *handle_options(void *data) {
option_packet_t option_packet;
int rc;
while (1) {
memset(&option_packet, 0x0, sizeof(option_packet));
rc = __recvfrom(options_socket_fd, &option_packet, sizeof(option_packet));
if (rc != 0) {
PR_LOG("Failed to recieve option.. ignoring\n");
continue;
}
switch (option_packet.id) {
case OID_DUMP_STATS:
__opt_dump_stats();
break;
default:
PR_LOG("Unknown option recieved %d\n", option_packet.id);
break;
}
}
}
int main(int argc, char **argv)
{
int update_options = 0;
pthread_t main_server_thread;
int i;
vis_server_info_t *vinfo;
pthread_t options_thread;
gettimeofday(&tv_start, 0);
options_socket_fd = __create_udp_listen_socket(OPTION_PORT_MAIN_SERV);
pthread_create(&options_thread, 0, handle_options, 0);
if (argc == 1) {
} else if ((argc >= 3) && (argc % 2 == 1)) {
for (i = 1; i < argc; i += 2) {
vinfo = &vis_server[num_vis_servers];
vinfo->ip = argv[i];
vinfo->port = atoi(argv[i+1]);
num_vis_servers++;
}
no_vis_server = 0;
} else {
printf("usage: ./server [visual_server_ip] [visual_server_port] [v_ip2] [port2] [v_ip3] [port3] ...\n");
return -1;
}
signals_init();
__parse_options();
//__print_options(); // DEBUG
pthread_create(&main_server_thread, NULL, start_server, NULL);
while (1) {
update_options = 0;
pthread_mutex_lock(&options_mutex);
if (__parse_options() == 0) {
update_options = 1;
}
pthread_mutex_unlock(&options_mutex);
// Update clients with new options if needed
__client_list_update(update_options);
//TODO: check if clients have been active
{
}
usleep(250000);
}
return 0;
}