-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
1247 lines (1067 loc) · 50.4 KB
/
test.c
File metadata and controls
1247 lines (1067 loc) · 50.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
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Cameron O'Neill (23340022)
// Akhil Gorasia (23424609)
// Martin Mitanoski (23385544)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <time.h>
#include <stdbool.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#define VERBOSE true // Verbose printing
#define DEBUG true // Debug Printing
#define MAX_ENTRIES 100 // Arbitrary limit
#define TCP_BACKLOG_LENGTH 10 // Number of TCP connections the TCP socket will hold up to
#define MAX_NEIGHBORS 20 // Number of possible neighbors a station can accomadate
#define MAX_STATION_NAME 32 // Maximum station name length
#define MAX_ID_LENGTH 48 // Maximum ID length (packetID/routeID)
#define MAX_IP_LEGNTH 24 // Maximum length of an IP address
#define MAX_PACKET_TYPE_LENGTH 16 // Maximum length of packet type
#define MAX_JSON_BUFFER 2048
#define MAX_NAME_LENGTH 32
#define PING "PING"
#define ACK "ACK"
#define ROUTE_QUERY "ROUTE_QUERY"
#define ROUTE_SUCCESS "ROUTE_SUCCESS"
#define ROUTE_FAIL "ROUTE_FAIL"
// Printing messages
#define DEBUG_PRINT(msg) if (DEBUG) { printf("[DEBUG %d] %s\n", STATION_IDENTIFIER, msg); }
#define VERBOSE_PRINT(msg) if (VERBOSE) { printf("[INFO %d] %s\n", STATION_IDENTIFIER, msg); }
#define ERROR_PRINT(msg) printf("[ERROR %d] %s\n", STATION_IDENTIFIER, msg)
#define VERBOSE_PRINT_EMPTY() if (VERBOSE) { printf("\n"); }
// Structures
extern struct sent_message {
char packetID[MAX_ID_LENGTH]; // UUID length
char packetType[MAX_PACKET_TYPE_LENGTH];
time_t time_latestSent;
int attempts;
struct sockaddr_in targetAddress;
char packet[MAX_JSON_BUFFER];
} sent_messages[100];
extern struct active_routingRequest {
char destination[MAX_STATION_NAME];
char responses[MAX_JSON_BUFFER];
time_t time_sent;
int socket;
char request_id[MAX_ID_LENGTH]; // Adding request_id here for tracking
} active_routingRequests[100];
extern int active_routingRequests_count;
typedef struct NeighborInfo {
char stationName[MAX_STATION_NAME];
char ip[256];
int port;
} NeighborInfo;
extern NeighborInfo active_neighbors[MAX_NEIGHBORS];
typedef struct {
char departure_time[16];
char service_name[MAX_STATION_NAME];
char departure_station[MAX_STATION_NAME];
char arrival_time[16];
char arrival_station[MAX_STATION_NAME];
} TimetableEntry;
typedef struct {
char station_name[MAX_STATION_NAME];
TimetableEntry entries[MAX_ENTRIES];
int num_entries;
char timetable_file[64];
float location[2];
time_t mod_time;
} StationTimetable;
// Packet structure
typedef struct {
char type[16];
char originStationName[MAX_STATION_NAME];
char packetID[MAX_ID_LENGTH];
char msg[MAX_JSON_BUFFER];
} UDP_Packet;
typedef struct {
char station_name[MAX_STATION_NAME];
char arrival_time[6];
char departure_time[6];
char service_name[MAX_STATION_NAME];
} VisitedStation;
typedef struct {
char destination[MAX_STATION_NAME];
char time[6]; // Changed from time_t to char[6]
VisitedStation visited[MAX_ENTRIES];
char original_departure_time[6];
char original_service[MAX_STATION_NAME];
char request_id[MAX_ID_LENGTH];
int visited_count;
} RoutingRequest;
typedef struct {
char depature_time[6];
char type[16];
char arrival_time[6];
char destination[MAX_STATION_NAME];
char original_time[6];
char leaving_service[MAX_STATION_NAME];
char route[MAX_JSON_BUFFER];
char request_id[MAX_ID_LENGTH];
int visited_index;
VisitedStation visited[MAX_ENTRIES];
int visited_count;
} RoutingResponse;
// Global variables
struct sent_message sent_messages[100]; // Sent packets that need to be ACKnowledged
int sent_messages_count = 0; // Number of active send messages (havent been ACK'd)
StationTimetable station; // Station timetable for the station the server is acting on
int TCP_SOCKET, UDP_SOCKET; //
int TCP_PORT, UDP_PORT; // Port number for TCP and UDP socket
struct sockaddr_in client_address; //
int TCP_CONNECTION; // The socket descriptor for the TCP connection
char *STATION_NAME; // Station name the server is acting on
char STATION_IDENTIFIER; // Single character to identify station in console print messages
char **NEIGHBORS; // Station's possible neighbors (inactive/active)
int NEIGHBOR_COUNT; // Number of possible neighbors to station
struct NeighborInfo active_neighbors[MAX_NEIGHBORS]; // List of active neighbors
int active_neighbor_count = 0; // Number of active neighbors
struct active_routingRequest active_routingRequests[100];
int active_routingRequests_count = 0;
// Function prototypes
void parse_arguments(int argc, char *argv[]);
void init_station_timetable(StationTimetable *timetable, const char *station_name);
void setup_sockets(void);
void load_timetable(StationTimetable *timetable);
void handle_client_connection(int tcp_socket);
void TCP_sendMessage(int sock, const char *msg);
TimetableEntry *find_direct_or_query_indirect(const char *selected_time, const char *selected_destination, int tcp_socket);
int time_to_minutes(const char *time_str);
void error_print(const char *message);
void UDP_sendMessage(const char *ip, int port, const char *packetType, const char *msg, const char *ACK_responseID, int resendIndex);
void pingNeighbors(bool all, bool inactive);
void deserialize_udp_packet(char *data, UDP_Packet *packet);
void handle_udp_message(char *data, struct sockaddr_in addr);
void extract_json_value(char *result, size_t max_len, const char *data, const char *key);
void query_neighbors(RoutingRequest *message);
void serialize_routing_request(const RoutingRequest *request, char *buffer, size_t buf_size);
void deserialize_routing_request(const char *data, RoutingRequest *request);
TimetableEntry *find_next_departure(StationTimetable *timetable, const char *target_time_str, const char *destination);
void generate_unique_id(char *buffer, size_t size);
void print_active_neighbors(void);
time_t get_mod_time(const char *filename);
void update_timetable(StationTimetable *timetable);
bool is_time_after_last_departure(const char *selected_time);
void serialize_routing_response(const RoutingResponse *response, char *buffer, size_t buf_size);
void deserialize_routing_response(const char *data, RoutingResponse *response);
void decode_url(char *dst, const char *src);
void construct_route_message(RoutingResponse *response, char *route_message, size_t size);
void deserialize_visited_station(const char *data, VisitedStation *vstation);
void append_to_visited(RoutingRequest *request, const char *station_name, const char *arrival_time, const char *departure_time, const char *service_name);
void append_to_visitedR(RoutingResponse *request, const char *station_name, const char *arrival_time, const char *departure_time, const char *service_name);
int main(int argc, char *argv[]) {
struct timeval timeout;
fd_set readfds, exceptfds;
int activity, pingCounter = 0;
parse_arguments(argc, argv);
init_station_timetable(&station, argv[1]); // Station is a global variable storing the information for this station
setup_sockets();
int max_sd = TCP_SOCKET > UDP_SOCKET ? TCP_SOCKET : UDP_SOCKET;
sleep(3);
pingNeighbors(true, false); // Initial ping to ALL neighbors
while (1) {
sleep(2);
pingCounter++;
// Check resend packets
// func here -- INCOMPLETE
// Check active routing requests
// func here -- INCOMPLETE
// Ping inactive neighbours
if (pingCounter >= 5) {
update_timetable(&station);
//pingNeighbors(false, true); // Ping inactive neighbors -- INCOMPLETE
pingCounter = 0;
}
// Reset file descriptor sets and timeout on each iteration
FD_ZERO(&readfds);
FD_ZERO(&exceptfds);
FD_SET(TCP_SOCKET, &readfds);
FD_SET(UDP_SOCKET, &readfds);
FD_SET(TCP_SOCKET, &exceptfds);
FD_SET(UDP_SOCKET, &exceptfds);
// Hold select for at most 0.1 seconds
timeout.tv_sec = 0;
timeout.tv_usec = 100000; // 0.1 seconds
activity = select(max_sd + 1, &readfds, NULL, &exceptfds, &timeout);
if (activity < 0 && errno != EINTR) {
perror("Select error");
continue;
}
if (FD_ISSET(TCP_SOCKET, &readfds)) {
printf("TCP MESSAGE ACCEPTED\n");
socklen_t addrlen = sizeof(client_address);
TCP_CONNECTION = accept(TCP_SOCKET, (struct sockaddr *)&client_address, &addrlen);
if (TCP_CONNECTION < 0) {
perror("Accept failed");
} else {
fcntl(TCP_CONNECTION, F_SETFL, O_NONBLOCK); // Set non-blocking mode
handle_client_connection(TCP_CONNECTION);
}
}
if (FD_ISSET(UDP_SOCKET, &readfds)) {
char buffer[4096];
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
ssize_t bytes_received = recvfrom(UDP_SOCKET, buffer, sizeof(buffer), 0, (struct sockaddr *)&addr, &len);
if (bytes_received > 0) {
buffer[bytes_received] = '\0';
handle_udp_message(buffer, addr);
} else if (bytes_received < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
perror("Failed to receive UDP packet");
}
}
if (FD_ISSET(TCP_SOCKET, &exceptfds) || FD_ISSET(UDP_SOCKET, &exceptfds)) {
perror("Socket exception");
// Handle socket exceptions or errors here
}
}
return 0;
}
void error_print(const char *message) {
fprintf(stderr, "%s\n", message);
}
int time_to_minutes(const char *time_str) {
int hours, minutes;
sscanf(time_str, "%d:%d", &hours, &minutes);
return hours * 60 + minutes;
}
void parse_arguments(int argc, char *argv[]) {
if (argc < 5) {
error_print("Usage: ./station-server <Station-Name> <TCP-Port> <UDP-Port> <Neighbor1> ...");
exit(EXIT_FAILURE);
}
STATION_NAME = argv[1];
TCP_PORT = atoi(argv[2]);
UDP_PORT = atoi(argv[3]);
NEIGHBORS = &argv[4];
NEIGHBOR_COUNT = argc - 4;
STATION_IDENTIFIER = STATION_NAME[strlen(STATION_NAME) - 1]; // Last character of the station name
}
int count_stations(const char *route) {
int count = 0;
const char *ptr = route;
while ((ptr = strchr(ptr, ';')) != NULL) {
count++;
ptr++;
}
if (count > 0) {
count++;
}
return count;
}
// --------------------------------------------------------- HANDLE UDP MESSAGE ---------------------------------------------------------
void handle_udp_message(char *data, struct sockaddr_in addr) {
UDP_Packet packet;
deserialize_udp_packet(data, &packet);
printf("%s --> Full UDP Packet Received: Type: %s, Origin: %s, ID: %s\n", STATION_NAME, packet.type, packet.originStationName, packet.packetID);
printf("%s --> Packet: %s \n", STATION_NAME, packet.msg);
printf("%s --> Packet TYPE: %s \n", STATION_NAME, packet.type);
// Get sender's information (IP/PORT) AND send ACK
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(addr.sin_addr), ip, INET_ADDRSTRLEN);
int port = ntohs(addr.sin_port);
if (strcmp(packet.type, ROUTE_QUERY) == 0) {
printf("Received ROUTE_QUERY from %s:%d\n", ip, port);
UDP_sendMessage(ip, port, "ACK", NULL, packet.packetID, -1);
RoutingRequest message;
deserialize_routing_request(packet.msg, &message);
// Check if the current station has already been visited
bool already_visited = false;
for (int i = 0; i < message.visited_count; i++) {
if (strcmp(message.visited[i].station_name, STATION_NAME) == 0) {
already_visited = true;
break;
}
}
if (already_visited) {
printf("Query already processed here, dropping to prevent loop.\n");
already_visited = false;
return;
}
printf("Processing query for destination: %s\n", message.destination);
TimetableEntry *departure = find_next_departure(&station, message.time, message.destination);
if (departure) {
printf("Route found. Sending back success.\n");
RoutingResponse response;
strcpy(response.depature_time, departure->departure_time);
strcpy(response.type, "success");
strcpy(response.arrival_time, departure->arrival_time);
strcpy(response.destination, departure->arrival_station);
strcpy(response.original_time, message.original_departure_time);
strcpy(response.leaving_service, departure->service_name);
strcpy(response.request_id, message.request_id);
response.visited_count = message.visited_count;
for (int i = 0; i < message.visited_count; i++) {
response.visited[i] = message.visited[i];
}
append_to_visitedR(&response, STATION_NAME, departure->departure_time, departure->arrival_time, departure->service_name);
response.visited_index = message.visited_count - 2;
char response_data[MAX_JSON_BUFFER];
serialize_routing_response(&response, response_data, sizeof(response_data));
UDP_sendMessage(ip, port, "ROUTE_SUCCESS", response_data, NULL, -1);
} else {
printf("No departure available. Querying further neighbors.\n");
query_neighbors(&message);
}
} else if (strcmp(packet.type, PING) == 0) {
printf("Received PING from %s:%d\n", ip, port);
UDP_sendMessage(ip, port, ACK, NULL, packet.packetID, -1);
} else if (strcmp(packet.type, ACK) == 0) {
printf("ACK received for Packet ID: %s\n", packet.packetID);
for (int i = 0; i < sent_messages_count; i++) {
if (strcmp(sent_messages[i].packetID, packet.packetID) == 0) {
if (strcmp(sent_messages[i].packetType, PING) == 0) {
printf("UDP Neighbor Discovered: %s (%s:%d)\n", packet.originStationName, ip, port);
struct NeighborInfo *neighbor = &active_neighbors[active_neighbor_count];
strcpy(neighbor->stationName, packet.originStationName);
strcpy(neighbor->ip, ip);
neighbor->port = port;
active_neighbor_count++;
}
for (int j = i; j < sent_messages_count - 1; j++) {
sent_messages[j] = sent_messages[j + 1];
}
sent_messages_count--;
printf("Deleted packet ID: %s from sent messages\n", packet.packetID);
break;
}
}
} else if (strcmp(packet.type, ROUTE_SUCCESS) == 0) {
printf("%s --> ROUTE_SUCCESS received from %s:%d\n", STATION_NAME, ip, port);
RoutingResponse response;
deserialize_routing_response(packet.msg, &response);
printf("%s --> Visited Index: %i\n", STATION_NAME, response.visited_index);
printf("%s --> Request_id: %s\n", STATION_NAME, response.request_id);
printf("%s --> Active Request count: %i\n", STATION_NAME, active_routingRequests_count);
if (response.visited_index <= -1) {
// Route response has returned to the original station
for (int i = 0; i < active_routingRequests_count; i++) {
if (strcmp(active_routingRequests[i].request_id, response.request_id) == 0) {
printf("%s --> SENIND SUCCESSFUL MESSAGE TO TCP\n", STATION_NAME);
char route_message[MAX_JSON_BUFFER];
construct_route_message(&response, route_message, sizeof(route_message));
TCP_sendMessage(active_routingRequests[i].socket, route_message);
// Optional (we dont have to remove active routing requests)
// Removing the active routing request from the array
for (int j = i; j < active_routingRequests_count - 1; j++) {
active_routingRequests[j] = active_routingRequests[j + 1];
}
active_routingRequests_count--;
break;
}
}
} else {
printf("%s --> NOT ORIGIN STATION, SENDING TO NEIGHBOUR\n", STATION_NAME);
// Route response needs to be sent to the next neighbor
int index = response.visited_index;
printf("INDEX: %d\n", index);
printf("VISITED[0].name: %s\n", response.visited[0].station_name);
// Check if the index is valid
if (index >= 0 && index < response.visited_count) {
// Get the next neighbor station name
const char *next_neighbor = response.visited[index].station_name;
printf("%s --> NEXT NEIGHBOUR TO SEND TO: %s\n", STATION_NAME, next_neighbor);
// Update the response
response.visited_index = response.visited_index - 1;
strcpy(response.type, "success");
// Serialize the updated RoutingResponse object
char response_data[MAX_JSON_BUFFER];
serialize_routing_response(&response, response_data, sizeof(response_data));
// Find the neighbor's IP and port
for (int i = 0; i < active_neighbor_count; i++) {
if (strcmp(active_neighbors[i].stationName, next_neighbor) == 0) {
UDP_sendMessage(active_neighbors[i].ip, active_neighbors[i].port, "ROUTE_SUCCESS", response_data, NULL, -1);
break;
}
}
}
}
} else {
printf("Unknown packet type: %s\n", packet.type);
}
}
// --------------------------------------------------------- CONSTRUCT RESPONSE MESSAGE ---------------------------------------------------------
void construct_route_message(RoutingResponse *response, char *route_message, size_t size) {
snprintf(route_message, size, "Route Success: %s to %s via [", STATION_NAME, response->destination);
for (int i = 0; i < response->visited_count; i++) {
char segment[MAX_JSON_BUFFER];
snprintf(segment, sizeof(segment), "%s (Depart: %s, Arrive: %s, Service: %s) ",
response->visited[i].station_name,
response->visited[i].arrival_time,
response->visited[i].departure_time,
response->visited[i].service_name);
// Concatenate the segment to the route message
strncat(route_message, segment, size - strlen(route_message) - 1);
}
strncat(route_message, "]", size - strlen(route_message) - 1);
snprintf(route_message + strlen(route_message), size - strlen(route_message),
", departing at %s and arriving at time %s",
response->original_time, response->arrival_time);
}
// --------------------------------------------------------- APPEND TO VISITED STRUCT ---------------------------------------------------------
void append_to_visited(RoutingRequest *request, const char *station_name, const char *arrival_time, const char *departure_time, const char *service_name) {
if (request->visited_count >= MAX_ENTRIES) {
fprintf(stderr, "Visited array full, cannot append station %s\n", station_name);
return;
}
strncpy(request->visited[request->visited_count].station_name, station_name, MAX_NAME_LENGTH);
strncpy(request->visited[request->visited_count].arrival_time, arrival_time, 6);
strncpy(request->visited[request->visited_count].departure_time, departure_time, 6);
strncpy(request->visited[request->visited_count].service_name, service_name, MAX_NAME_LENGTH);
request->visited_count++;
}
void append_to_visitedR(RoutingResponse *request, const char *station_name, const char *arrival_time, const char *departure_time, const char *service_name) {
if (request->visited_count >= MAX_ENTRIES) {
fprintf(stderr, "Visited array full, cannot append station %s\n", station_name);
return;
}
strncpy(request->visited[request->visited_count].station_name, station_name, MAX_NAME_LENGTH);
strncpy(request->visited[request->visited_count].arrival_time, arrival_time, 6);
strncpy(request->visited[request->visited_count].departure_time, departure_time, 6);
strncpy(request->visited[request->visited_count].service_name, service_name, MAX_NAME_LENGTH);
request->visited_count++;
}
// --------------------------------------------------------- JSON DESERIALIZE/SERIALIZE ---------------------------------------------------------
void deserialize_visited_station(const char *data, VisitedStation *vstation) {
extract_json_value(vstation->station_name, sizeof(vstation->station_name), data, "\"station_name\":");
extract_json_value(vstation->arrival_time, sizeof(vstation->arrival_time), data, "\"arrival_time\":");
extract_json_value(vstation->departure_time, sizeof(vstation->departure_time), data, "\"departure_time\":");
extract_json_value(vstation->service_name, sizeof(vstation->service_name), data, "\"service_name\":");
}
void deserialize_routing_request(const char *data, RoutingRequest *request) {
extract_json_value(request->destination, sizeof(request->destination), data, "\"destination\":");
extract_json_value(request->time, sizeof(request->time), data, "\"time\":");
extract_json_value(request->original_departure_time, sizeof(request->original_departure_time), data, "\"original_departure_time\":");
extract_json_value(request->original_service, sizeof(request->original_service), data, "\"original_service\":");
extract_json_value(request->request_id, sizeof(request->request_id), data, "\"request_id\":");
const char *visited_start = strstr(data, "\"visited\": [");
if (visited_start) {
visited_start += strlen("\"visited\": [");
const char *visited_end = strchr(visited_start, ']');
if (visited_end) {
char visited_buffer[MAX_JSON_BUFFER];
size_t visited_len = (size_t)(visited_end - visited_start);
strncpy(visited_buffer, visited_start, visited_len);
visited_buffer[visited_len] = '\0';
request->visited_count = 0;
const char *entry_start = visited_buffer;
while ((entry_start = strchr(entry_start, '{')) != NULL) {
const char *entry_end = strchr(entry_start, '}');
if (entry_end) {
char entry_buffer[256];
size_t entry_len = (size_t)(entry_end - entry_start + 1);
strncpy(entry_buffer, entry_start, entry_len);
entry_buffer[entry_len] = '\0';
deserialize_visited_station(entry_buffer, &request->visited[request->visited_count++]);
entry_start = entry_end + 1;
} else {
break;
}
}
}
}
}
void serialize_routing_response(const RoutingResponse *response, char *buffer, size_t buf_size) {
char visited_buffer[MAX_JSON_BUFFER] = "[";
for (int i = 0; i < response->visited_count; i++) {
char entry_buffer[256];
snprintf(entry_buffer, sizeof(entry_buffer),
"{\"station_name\":\"%s\",\"arrival_time\":\"%s\",\"departure_time\":\"%s\",\"service_name\":\"%s\"}",
response->visited[i].station_name,
response->visited[i].arrival_time,
response->visited[i].departure_time,
response->visited[i].service_name);
strcat(visited_buffer, entry_buffer);
if (i < response->visited_count - 1) {
strcat(visited_buffer, ",");
}
}
strcat(visited_buffer, "]");
snprintf(buffer, buf_size,
"{\"departure_time\": \"%s\",\"type\": \"%s\",\"arrival_time\": \"%s\",\"destination\": \"%s\",\"original_time\": \"%s\",\"leaving_service\": \"%s\",\"route\": %s,\"request_id\": \"%s\",\"visited_index\": %d}",
response->depature_time,
response->type,
response->arrival_time,
response->destination,
response->original_time,
response->leaving_service,
visited_buffer,
response->request_id,
response->visited_index);
}
void deserialize_routing_response(const char *data, RoutingResponse *response) {
extract_json_value(response->depature_time, sizeof(response->depature_time), data, "\"departure_time\":");
extract_json_value(response->type, sizeof(response->depature_time), data, "\"type\":");
extract_json_value(response->arrival_time, sizeof(response->arrival_time), data, "\"arrival_time\":");
extract_json_value(response->destination, sizeof(response->destination), data, "\"destination\":");
extract_json_value(response->original_time, sizeof(response->original_time), data, "\"original_time\":");
extract_json_value(response->leaving_service, sizeof(response->leaving_service), data, "\"leaving_service\":");
extract_json_value(response->request_id, sizeof(response->request_id), data, "\"request_id\":");
char visited_index_str[16];
extract_json_value(visited_index_str, sizeof(visited_index_str), data, "\"visited_index\":");
response->visited_index = atoi(visited_index_str);
const char *visited_start = strstr(data, "\"route\": [");
if (visited_start) {
visited_start += strlen("\"route\": [");
const char *visited_end = strchr(visited_start, ']');
if (visited_end) {
char visited_buffer[MAX_JSON_BUFFER];
size_t visited_len = (size_t)(visited_end - visited_start);
strncpy(visited_buffer, visited_start, visited_len);
visited_buffer[visited_len] = '\0';
response->visited_count = 0;
const char *entry_start = visited_buffer;
while ((entry_start = strchr(entry_start, '{')) != NULL) {
const char *entry_end = strchr(entry_start, '}');
if (entry_end) {
char entry_buffer[256];
size_t entry_len = (size_t)(entry_end - entry_start + 1);
strncpy(entry_buffer, entry_start, entry_len);
entry_buffer[entry_len] = '\0';
deserialize_visited_station(entry_buffer, &response->visited[response->visited_count++]);
entry_start = entry_end + 1;
} else {
break;
}
}
}
}
}
void serialize_routing_request(const RoutingRequest *request, char *buffer, size_t buf_size) {
char visited_buffer[MAX_JSON_BUFFER] = "[";
for (int i = 0; i < request->visited_count; i++) {
char entry_buffer[256];
snprintf(entry_buffer, sizeof(entry_buffer),
"{\"station_name\": \"%s\",\"arrival_time\": \"%s\",\"departure_time\": \"%s\",\"service_name\": \"%s\"}",
request->visited[i].station_name,
request->visited[i].arrival_time,
request->visited[i].departure_time,
request->visited[i].service_name);
strcat(visited_buffer, entry_buffer);
if (i < request->visited_count - 1) {
strcat(visited_buffer, ",");
}
}
strcat(visited_buffer, "]");
snprintf(buffer, buf_size,
"{\"destination\": \"%s\",\"time\": \"%s\",\"visited\": %s,\"original_departure_time\": \"%s\",\"original_service\": \"%s\",\"request_id\": \"%s\"}",
request->destination,
request->time,
visited_buffer,
request->original_departure_time,
request->original_service,
request->request_id);
}
// Function to extract value from a JSON-like string by key
void extract_json_value(char *result, size_t max_len, const char *data, const char *key) {
const char *start = strstr(data, key);
if (start) {
start += strlen(key); // Move to the end of the key
while (*start && (*start == ' ' || *start == ':' || *start == '=')) start++; // Skip spaces, colon, or equals sign
// Check if the value starts with a quote or a bracket (for nested JSON)
int is_quoted = 0;
int is_nested = 0;
if (*start == '\"') {
is_quoted = 1;
start++; // Skip the starting quote
} else if (*start == '{' || *start == '[') {
is_nested = 1;
}
// Handle nested JSON or regular values
const char *end = start;
if (is_quoted) {
// Find the closing quote for quoted values
while (*end && *end != '\"') end++;
} else if (is_nested) {
// Find the end of the nested structure
int nested = 0;
while (*end) {
if (*end == '{' || *end == '[') nested++;
if (*end == '}' || *end == ']') nested--;
end++;
if (nested == 0) break;
}
} else {
// Find the end of the value for non-quoted values
while (*end && *end != ',' && *end != '}' && *end != ']') end++;
}
size_t len = (size_t)(end - start);
if (len > 0 && len < max_len) {
strncpy(result, start, len);
result[len] = '\0'; // Ensure null-termination
} else if (max_len > 0) {
result[0] = '\0'; // Provide an empty string if the length is zero or too large
}
} else if (max_len > 0) {
result[0] = '\0'; // Ensure the result is always a valid string
}
}
// Example function to deserialize data into our packet struct
void deserialize_udp_packet(char *data, UDP_Packet *packet) {
extract_json_value(packet->type, sizeof(packet->type), data, "\"type\":");
extract_json_value(packet->originStationName, sizeof(packet->originStationName), data, "\"originStationName\":");
extract_json_value(packet->packetID, sizeof(packet->packetID), data, "\"packetID\":");
extract_json_value(packet->msg, sizeof(packet->msg), data, "\"msg\":");
}
// --------------------------------------------------------- PINGING NEIGHBOURS ---------------------------------------------------------
void pingNeighbors(bool all, bool inactive) {
if (all) {
// Reset active neighbors
memset(active_neighbors, 0, sizeof(active_neighbors));
active_neighbor_count = 0;
for (int i = 0; i < NEIGHBOR_COUNT; i++) {
char *neighbor_addr = NEIGHBORS[i];
char ip[100];
int port;
sscanf(neighbor_addr, "%99[^:]:%d", ip, &port);
UDP_sendMessage(ip, port, PING, NULL, NULL, -1);
}
} else if (inactive) {
for (int i = 0; i < NEIGHBOR_COUNT; i++) {
char *neighbor_addr = NEIGHBORS[i];
char ip[MAX_IP_LEGNTH];
int port;
sscanf(neighbor_addr, "%99[^:]:%d", ip, &port);
bool isActive = false;
for (int j = 0; j < active_neighbor_count; j++) {
if (htons((uint16_t)active_neighbors[j].port) == htons((uint16_t)port) &&
inet_addr(active_neighbors[j].ip) == inet_addr(ip)) {
isActive = true;
break;
}
}
if (!isActive) {
UDP_sendMessage(ip, port, PING, NULL, NULL, -1);
}
}
}
}
// Simple function to generate a "unique" packet ID
void generate_unqiue_id(char *id, size_t size) {
static int counter = 0;
snprintf(id, size, "%d-%s", counter++, STATION_NAME); // Simple counter based ID for demonstration
}
// Simplified JSON-like packet creation
void create_packet(char *buffer, const char *type, const char *stationName, const char *packetID, const char *msg) {
if (msg && msg[0] != '\0') {
// If msg is provided and not empty, include it without quotes
snprintf(buffer, MAX_JSON_BUFFER, "{\"type\": \"%s\",\"originStationName\": \"%s\",\"packetID\": \"%s\",\"msg\": %s}",
type, stationName, packetID, msg);
} else {
// If msg is empty or NULL, include "None" with quotes
snprintf(buffer, MAX_JSON_BUFFER, "{\"type\": \"%s\",\"originStationName\": \"%s\",\"packetID\": \"%s\",\"msg\": \"None\"}",
type, stationName, packetID);
}
}
// --------------------------------------------------------- UDP SEND MESSAGE ---------------------------------------------------------
void UDP_sendMessage(const char *ip, int port, const char *packetType, const char *msg, const char *ACK_responseID, int resendIndex) {
if (!ip || port == 0 || !packetType) {
ERROR_PRINT("Invalid parameters for UDP message.");
return;
}
struct sockaddr_in targetAddress;
targetAddress.sin_family = AF_INET;
targetAddress.sin_port = htons((uint16_t)port);
inet_pton(AF_INET, ip, &targetAddress.sin_addr);
char packetID[MAX_ID_LENGTH];
if (strcmp(packetType, ACK) == 0 && ACK_responseID) {
strncpy(packetID, ACK_responseID, sizeof(packetID));
} else {
generate_unqiue_id(packetID, sizeof(packetID));
}
char packet[MAX_JSON_BUFFER];
if (resendIndex < 0) {
create_packet(packet, packetType, STATION_NAME, packetID, msg);
} else {
strcpy(packet, sent_messages[resendIndex].packet); // Resend the existing packet
}
// Log and send the message
printf("%s --> Sending to IP: %s, Port: %d, Type: %s\n",STATION_NAME, ip, port, packetType);
ssize_t bytes_sent = sendto(UDP_SOCKET, packet, strlen(packet), 0, (struct sockaddr *)&targetAddress, sizeof(targetAddress));
if (bytes_sent < 0) {
perror("Failed to send UDP packet");
}
// Store sent message if not ACK
if (strcmp(packetType, ACK) != 0) {
struct sent_message *sm = &sent_messages[sent_messages_count++];
strcpy(sm->packetID, packetID);
strcpy(sm->packetType, packetType);
sm->time_latestSent = time(NULL);
sm->attempts = 1;
sm->targetAddress = targetAddress;
strcpy(sm->packet, packet);
}
}
// --------------------------------------------------------- SET UP SOCKETS ---------------------------------------------------------
void setup_sockets() {
if (TCP_PORT == 0 || UDP_PORT == 0) {
fprintf(stderr, "TCP_PORT or UDP_PORT not set correctly.\n");
exit(EXIT_FAILURE);
}
// Create TCP socket
TCP_SOCKET = socket(AF_INET, SOCK_STREAM, 0);
if (TCP_SOCKET < 0) {
perror("Failed to create TCP socket");
exit(EXIT_FAILURE);
}
// Set TCP socket options
int opt = 1;
if (setsockopt(TCP_SOCKET, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
perror("Failed to set TCP socket options");
close(TCP_SOCKET);
exit(EXIT_FAILURE);
}
// Bind TCP socket
struct sockaddr_in tcp_addr;
tcp_addr.sin_family = AF_INET;
tcp_addr.sin_addr.s_addr = INADDR_ANY;
tcp_addr.sin_port = htons((uint16_t)TCP_PORT);
if (bind(TCP_SOCKET, (struct sockaddr *)&tcp_addr, sizeof(tcp_addr)) < 0) {
perror("Failed to bind TCP socket");
close(TCP_SOCKET);
exit(EXIT_FAILURE);
}
// Listen on TCP socket
if (listen(TCP_SOCKET, TCP_BACKLOG_LENGTH) < 0) {
perror("Failed to listen on TCP socket");
close(TCP_SOCKET);
exit(EXIT_FAILURE);
}
// Create UDP socket
UDP_SOCKET = socket(AF_INET, SOCK_DGRAM, 0);
if (UDP_SOCKET < 0) {
perror("Failed to create UDP socket");
close(TCP_SOCKET); // Clean up TCP socket before exit
exit(EXIT_FAILURE);
}
// Bind UDP socket
struct sockaddr_in udp_addr;
udp_addr.sin_family = AF_INET;
udp_addr.sin_addr.s_addr = INADDR_ANY;
udp_addr.sin_port = htons((uint16_t)UDP_PORT);
if (bind(UDP_SOCKET, (struct sockaddr *)&udp_addr, sizeof(udp_addr)) < 0) {
perror("Failed to bind UDP socket");
close(TCP_SOCKET); // Clean up TCP socket
close(UDP_SOCKET); // Clean up UDP socket
exit(EXIT_FAILURE);
}
// Set UDP socket to non-blocking mode
int flags = fcntl(UDP_SOCKET, F_GETFL, 0);
if (fcntl(UDP_SOCKET, F_SETFL, flags | O_NONBLOCK) == -1) {
perror("Failed to set UDP socket to non-blocking mode");
close(TCP_SOCKET);
close(UDP_SOCKET);
exit(EXIT_FAILURE);
}
}
// --------------------------------------------------------- HANDLE CLIENT CONNECTION ---------------------------------------------------------
void handle_client_connection(int tcp_socket) {
char buffer[4096];
memset(buffer, 0, sizeof(buffer));
ssize_t recv_len = recv(tcp_socket, buffer, sizeof(buffer) - 1, 0);
if (recv_len < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return;
} else {
perror("Error receiving data");
return;
}
} else if (recv_len == 0) {
return;
}
buffer[recv_len] = '\0';
char *method = strtok(buffer, " ");
char *path = strtok(NULL, " ");
char *version = strtok(NULL, "\r\n");
if (!method || !path || !version) {
return;
}
if (strcmp(method, "GET") == 0 && strstr(path, "to=")) {
char *query = strchr(path, '?');
if (query) {
char *selected_destination = NULL;
char *leave_time = NULL;
query++;
char *token = strtok(query, "&");
while (token) {
if (strncmp(token, "to=", 3) == 0) {
selected_destination = token + 3;
} else if (strncmp(token, "leave=", 6) == 0) {
leave_time = token + 6;
}
token = strtok(NULL, "&");
}
if (selected_destination && leave_time) {
// Decode URL-encoded characters (e.g., %3A -> :) (Used to extract the time from the website)
char decoded_leave_time[16];
decode_url(decoded_leave_time, leave_time);
bool destination_found = false;
for (int i = 0; i < station.num_entries; ++i) {
if (strcmp(station.entries[i].arrival_station, selected_destination) == 0) {
destination_found = true;
break;
}
}
if (destination_found) {
TimetableEntry *departure = find_direct_or_query_indirect(decoded_leave_time, selected_destination, tcp_socket);
if (departure) {
char response_message[512];
snprintf(response_message, sizeof(response_message),
"Depart at %s from %s, arrive by %s at %s.",
departure->departure_time, departure->departure_station,
departure->arrival_time, departure->arrival_station);
TCP_sendMessage(tcp_socket, response_message);
return;
} else {
char response_message[512];
snprintf(response_message, sizeof(response_message),
"There is no journey from %s to %s leaving after %s.",
STATION_NAME, selected_destination, decoded_leave_time);
TCP_sendMessage(tcp_socket, response_message);
}
} else if (is_time_after_last_departure(decoded_leave_time)) {
char response_message[512];
snprintf(response_message, sizeof(response_message),
"There is no journey from %s to %s leaving after %s.",
STATION_NAME, selected_destination, decoded_leave_time);
TCP_sendMessage(tcp_socket, response_message);
} else {
find_direct_or_query_indirect(decoded_leave_time, selected_destination, tcp_socket);
}
}
}
}
return;
}
// This function extracts the chosen time by the user from the website.
void decode_url(char *dst, const char *src) {
while (*src) {
if (*src == '%') {
if (src[1] && src[2]) {
char hex[3];
hex[0] = src[1];
hex[1] = src[2];
hex[2] = '\0';
*dst++ = (char)strtol(hex, NULL, 16);
src += 2;
}
} else if (*src == '+') {
*dst++ = ' ';
} else {
*dst++ = *src;
}
src++;
}
*dst = '\0';
}