-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
281 lines (264 loc) · 13.7 KB
/
main.c
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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "source/classes.h"
#include "source/commands.h"
#include "source/c_time.h"
#define LOG_FILE_NAME "log/aeroport_log.txt"
int main(int argc, char *argv[])
{
srand(time(NULL));
char *curr_cmd;
char *curr_arg;
log_file = fopen("log/aeroport_log.txt", "a+");
if (log_file == NULL) // Initializing log file pointer and raising error if not able to open log file located in 'log/aeroport_log.txt'
{
printf("%s[error] ► Impossible to open log file.%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
fprintf(log_file, "%s[error] ► Impossible to open log file.%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
printf("\x1b[34m[system] ► <Session Terminated>\x1b[0m\n");
fprintf(log_file, "\x1b[34m[system] ► <Session Terminated>\x1b[0m\n");
return 0;
}
// Variables initialization
c_time local_time = {0, 6, 1, 1, 2021};
unsigned old_t = (unsigned)time(NULL);
waiting_queue *takeoff_queue = malloc(sizeof(waiting_queue));
waiting_queue *landing_queue = malloc(sizeof(waiting_queue));
company **company_list = make_clist(), **black_list = make_bl();
// Scenario planes initialization
printf("%s[system] ► Takeoff queue initializing...%s\n", ANSI_COLOR_BLUE, ANSI_COLOR_RESET);
fprintf(log_file, "%s[system] ► Takeoff queue initializing...%s\n", ANSI_COLOR_BLUE, ANSI_COLOR_RESET);
tk_init(takeoff_queue);
disp_wq(takeoff_queue);
printf("%s[system] ► Takeoff queue initializing...%s\n", ANSI_COLOR_BLUE, ANSI_COLOR_RESET);
fprintf(log_file, "%s[system] ► Takeoff queue initializing...%s\n", ANSI_COLOR_BLUE, ANSI_COLOR_RESET);
ld_init(landing_queue);
disp_wq(landing_queue);
printf("\n%s[system] ► Initialization Complete!%s\n", ANSI_COLOR_GREEN, ANSI_COLOR_RESET);
fprintf(log_file, "\n%s[system] ► Initialization Complete!%s\n", ANSI_COLOR_GREEN, ANSI_COLOR_RESET);
while (1) // Main simulator loop
{
update_time(&local_time, old_t); // Updating local_time variable every time the loop is executed
old_t = (unsigned)time(NULL);
tk_update(takeoff_queue, local_time, black_list); // Updating the takeoff and landing queue
ld_update(landing_queue, local_time, black_list);
disp_time(local_time);
printf("[admin] ► ");
curr_cmd = get_command();
fprintf(log_file, "[admin] ► %s\n", curr_cmd);
// determining the current command entered by the user
if (is_exit(curr_cmd))
{
break;
}
if (is_time_warp(curr_cmd))
{
curr_arg = get_command();
increment_time(&local_time, atoi(curr_arg));
}
else if (is_clear(curr_cmd))
{
system("clear");
}
else if (is_add(curr_cmd))
{
curr_arg = get_command();
if ((strcmp(curr_arg, "-t") == 0) | (strcmp(curr_arg, "--takeoff") == 0))
cmd_add(takeoff_queue);
else if ((strcmp(curr_arg, "-l") == 0) | (strcmp(curr_arg, "--landing") == 0))
cmd_add_rand_fuel(landing_queue);
}
else if (is_ls(curr_cmd))
{
curr_arg = get_command();
if ((strcmp(curr_arg, "-t") == 0) | (strcmp(curr_arg, "--takeoff") == 0))
disp_wq(takeoff_queue);
else if ((strcmp(curr_arg, "-l") == 0) | (strcmp(curr_arg, "--landing") == 0))
disp_wq(landing_queue);
else if ((strcmp(curr_arg, "-b") == 0) | (strcmp(curr_arg, "--blacklist") == 0))
disp_bl(black_list);
else if ((strcmp(curr_arg, "-c") == 0) | (strcmp(curr_arg, "--companies") == 0))
disp_clist(company_list);
else if ((strcmp(curr_arg, "-cc") == 0) | (strcmp(curr_arg, "--comp-caracteristics") == 0))
{
curr_arg = get_command();
disp_cc(get_company_by_acronym(curr_arg), takeoff_queue, landing_queue);
}
}
else if (is_land(curr_cmd))
{
curr_arg = get_command();
if ((strcmp(curr_arg, "-n") == 0) | (strcmp(curr_arg, "--number") == 0))
{
curr_arg = get_command();
land(find_plane_by_number(landing_queue, atoi(curr_arg)));
}
else if ((strcmp(curr_arg, "-i") == 0) | (strcmp(curr_arg, "--index") == 0))
{
curr_arg = get_command();
land(find_plane_by_index(landing_queue, atoi(curr_arg) - 1));
}
else if ((strcmp(curr_arg, "-!") == 0) | (strcmp(curr_arg, "--emergency") == 0))
{
curr_arg = get_command();
if ((strcmp(curr_arg, "-n") == 0) | (strcmp(curr_arg, "--number") == 0))
{
curr_arg = get_command();
force_land(find_plane_by_number(landing_queue, atoi(curr_arg)));
}
if ((strcmp(curr_arg, "-i") == 0) | (strcmp(curr_arg, "--index") == 0))
{
curr_arg = get_command();
force_land(find_plane_by_index(landing_queue, atoi(curr_arg) - 1));
}
}
}
else if (is_del(curr_cmd))
{
curr_arg = get_command();
if ((strcmp(curr_arg, "-t") == 0) | (strcmp(curr_arg, "--takeoff") == 0))
{
curr_arg = get_command();
if ((strcmp(curr_arg, "-n") == 0) | (strcmp(curr_arg, "--number") == 0))
{
curr_arg = get_command();
if (find_plane_by_number(takeoff_queue, atoi(curr_arg)))
{
printf("%s[info] ► Plane N°%d sucessfully removed from takeoff queue%s\n", ANSI_COLOR_GREEN, (find_plane_by_number(takeoff_queue, atoi(curr_arg)))->avion->number, ANSI_COLOR_RESET);
fprintf(log_file, "%s[info] ► Plane N°%d sucessfully removed from takeoff queue%s\n", ANSI_COLOR_GREEN, (find_plane_by_number(takeoff_queue, atoi(curr_arg)))->avion->number, ANSI_COLOR_RESET);
wq_del(find_plane_by_number(takeoff_queue, atoi(curr_arg)));
}
else
{
printf("%s[error] ► Value Error: Please enter a valid plane number%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
fprintf(log_file, "%s[error] ► Value Error: Please enter a valid plane number%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
}
}
if ((strcmp(curr_arg, "-i") == 0) | (strcmp(curr_arg, "--index") == 0))
{
curr_arg = get_command();
if (find_plane_by_index(takeoff_queue, atoi(curr_arg) - 1))
{
printf("%s[info] ► Plane N°%d sucessfully removed from takeoff queue%s\n", ANSI_COLOR_GREEN, (find_plane_by_index(takeoff_queue, atoi(curr_arg) - 1))->avion->number, ANSI_COLOR_RESET);
fprintf(log_file, "%s[info] ► Plane N°%d sucessfully removed from takeoff queue%s\n", ANSI_COLOR_GREEN, (find_plane_by_index(takeoff_queue, atoi(curr_arg) - 1))->avion->number, ANSI_COLOR_RESET);
wq_del(find_plane_by_index(takeoff_queue, atoi(curr_arg) - 1));
}
else
{
printf("%s[error] ► Index Error : Please enter a valid index%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
fprintf(log_file, "%s[error] ► Index Error : Please enter a valid index%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
}
}
}
else if ((strcmp(curr_arg, "-l") == 0) | (strcmp(curr_arg, "--landing") == 0))
{
curr_arg = get_command();
if ((strcmp(curr_arg, "-n") == 0) | (strcmp(curr_arg, "--number") == 0))
{
curr_arg = get_command();
if (find_plane_by_number(landing_queue, atoi(curr_arg)))
{
printf("%s[info] ► Plane N°%d sucessfully removed from landing queue%s\n", ANSI_COLOR_GREEN, (find_plane_by_number(landing_queue, atoi(curr_arg)))->avion->number, ANSI_COLOR_RESET);
fprintf(log_file, "%s[info] ► Plane N°%d sucessfully removed from landing queue%s\n", ANSI_COLOR_GREEN, (find_plane_by_number(landing_queue, atoi(curr_arg)))->avion->number, ANSI_COLOR_RESET);
wq_del(find_plane_by_number(landing_queue, atoi(curr_arg)));
}
else
{
printf("%s[error] ► Value Error: Please enter a valid plane number%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
fprintf(log_file, "%s[error] ► Value Error: Please enter a valid plane number%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
}
}
if ((strcmp(curr_arg, "-i") == 0) | (strcmp(curr_arg, "--index") == 0))
{
curr_arg = get_command();
if (find_plane_by_index(landing_queue, atoi(curr_arg) - 1))
{
printf("%s[info] ► Plane N°%d sucessfully removed from landing queue%s\n", ANSI_COLOR_GREEN, (find_plane_by_index(landing_queue, atoi(curr_arg) - 1))->avion->number, ANSI_COLOR_RESET);
fprintf(log_file, "%s[info] ► Plane N°%d sucessfully removed from landing queue%s\n", ANSI_COLOR_GREEN, (find_plane_by_index(landing_queue, atoi(curr_arg) - 1))->avion->number, ANSI_COLOR_RESET);
wq_del(find_plane_by_index(landing_queue, atoi(curr_arg) - 1));
}
else
{
printf("%s[error] ► Index Error : Please enter a valid index%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
fprintf(log_file, "%s[error] ► Index Error : Please enter a valid index%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
}
}
}
}
else if (is_info(curr_cmd))
{
curr_arg = get_command();
if ((strcmp(curr_arg, "-t") == 0) | (strcmp(curr_arg, "--takeoff") == 0))
{
printf("%s[info] ► Takeoff queue: \n", ANSI_COLOR_MAGENTA);
fprintf(log_file, "%s[info] ► Takeoff queue: \n", ANSI_COLOR_MAGENTA);
info_all(takeoff_queue);
}
else if ((strcmp(curr_arg, "-l") == 0) | (strcmp(curr_arg, "--landing") == 0))
{
printf("%s[info] ► Landing queue:\n", ANSI_COLOR_MAGENTA);
fprintf(log_file, "%s[info] ► Landing queue:\n", ANSI_COLOR_MAGENTA);
info_all(landing_queue);
}
else if ((strcmp(curr_arg, "-a") == 0) | (strcmp(curr_arg, "--all") == 0))
{
printf("%s[info] ► Takeoff queue: \n", ANSI_COLOR_MAGENTA);
fprintf(log_file, "%s[info] ► Takeoff queue: \n", ANSI_COLOR_MAGENTA);
info_all(takeoff_queue);
printf("%s[info] ► Landing queue:\n", ANSI_COLOR_MAGENTA);
fprintf(log_file, "%s[info] ► Landing queue:\n", ANSI_COLOR_MAGENTA);
info_all(landing_queue);
}
}
else if (is_man(curr_cmd))
{
FILE *fptr;
fptr = fopen("conf/manual.man", "r");
if (fptr == NULL)
{
printf("%s[error] ► Cannot open file %s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
fprintf(log_file, "%s[error] ► Cannot open file %s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
exit(0);
}
// Read contents from file
printf("%s", ANSI_COLOR_MAGENTA);
fprintf(log_file, "%s", ANSI_COLOR_MAGENTA);
char c = fgetc(fptr);
while (c != EOF)
{
printf("%c", c);
fprintf(log_file, "%c", c);
c = fgetc(fptr);
}
fclose(fptr);
printf("\n%s", ANSI_COLOR_RESET);
fprintf(log_file, "\n%s", ANSI_COLOR_RESET);
}
else if (is_bl(curr_cmd))
{
curr_arg = get_command();
if ((strcmp(curr_arg, "-a") == 0) | (strcmp(curr_arg, "add") == 0))
{
curr_arg = get_command();
blacklist_add(black_list, get_company_by_acronym(curr_arg));
}
if ((strcmp(curr_arg, "-r") == 0) | (strcmp(curr_arg, "remove") == 0))
{
curr_arg = get_command();
blacklist_remove(black_list, get_company_by_acronym(curr_arg));
}
}
else // if the command doesn't correspond to any defined command, displays help and finishes the loop
{
printf("%s[error] ► Unknown command. To see available commands, open manual with command 'man'%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
fprintf(log_file, "%s[error] ► Unknown command. To see available commands, open manual with command 'man'%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
}
}
// Cleanup after exiting the loop
fclose(log_file);
printf("\x1b[34m[system] ► <Session Terminated>\x1b[0m\n");
fprintf(log_file, "\x1b[34m[system] ► <Session Terminated>\x1b[0m\n");
return 0;
}