forked from RoniCycode/embedded-programming-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlightService.cpp
More file actions
174 lines (153 loc) · 4.73 KB
/
FlightService.cpp
File metadata and controls
174 lines (153 loc) · 4.73 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
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include "myLibrary.h"
#include "full_schedule.h"
#include "arrivals.h"
#include "airplane.h"
#include "signal_handler.h"
#include "error_handling.h"
#include <filesystem>
#include "zip.h"
#include "pipe_handler.h"
using namespace std;
int userOptions(vector<string> &names);
int main()
{
mkfifo("/tmp/flights_pipe", 0666);
// Open the named pipe for reading and writing
int pipeFd = open("/tmp/flights_pipe", O_WRONLY);
if (pipeFd < 0)
{
cout << "Failed to open the named pipe." << endl;
return 1;
}
int exitStatus;
vector<string> inputNames;
int userChoice;
// Set handler for SIGINT
signal(SIGINT, signal_handler::handleSignalParent);
// Wait for the child process to finish unzipping if zip exists
// read(pipeFd, &userChoice, sizeof(userChoice));
while (true)
{
// Get user input
userChoice = userOptions(inputNames);
// Write user choice to pipe
write(pipeFd, &userChoice, sizeof(userChoice));
if (userChoice >= 1 && userChoice <= 3)
{
writeStringVectorToPipe(pipeFd, inputNames);
}
switch (userChoice)
{
case 1:
cout << "Fetching airports data." << endl;
break;
case 2:
{
printMultiNodeVectorFromPipe(inputNames, pipeFd, printAirplaneArrivalFlights);
break;
}
case 3:
{
printNodeVectorFromPipe(inputNames[0], pipeFd, printFullSchedule);
break;
}
case 4:
{
printMultiNodeVectorFromPipe(inputNames, pipeFd, printAirplaneFlights);
break;
}
case 5:
cout << "Starting zipping process." << endl;
break;
case 6:
cout << "Exiting." << endl;
exitStatus = readFromPipe(pipeFd); // make sure child process finished
return exitStatus;
default:
return 1;
}
// Wait for the child process to finish
read(pipeFd, &userChoice, sizeof(userChoice));
cout << endl;
inputNames.clear();
}
// Close the named pipe
close(pipeFd);
return 0;
}
int userOptions(vector<string> &names)
{
int userChoice = 0;
string input;
names.clear();
while (userChoice < 1 || userChoice > 6)
{
cout << "1 - Fetch airports data." << endl;
cout << "2 - print airports incoming flights." << endl;
cout << "3 - print airports full flights schedule." << endl;
cout << "4 - print aircraft full flights schedule." << endl;
cout << "5 - Zip DB files." << endl;
cout << "6 - Shutdown." << endl;
cout << "Please make your choice <1,2,3,4,5,6>: " << endl;
while (input.empty()) // handle cases of user press enter
getline(cin, input);
while (!(isNumber(input, userChoice))) // validate input
{
cout << "Invalid input. Please enter a valid integer between 1 to 7: ";
getline(cin, input);
}
if (userChoice == 1)
{
int size;
cout << "insert how many icao names would you like to insert: ";
getline(cin, input);
while (!(isNumber(input, size))) // validate input
{
cout << "Invalid input. Please enter a valid integer: ";
getline(cin, input);
}
cout << "insert icao names (press enter after each one):" << endl;
for (int i = 0; i < size; i++)
{
string name;
cin >> name;
names.push_back(name);
}
}
else if (userChoice == 2)
{
cout << "insert icao name: ";
string name;
cin >> name;
names.push_back(name);
}
else if (userChoice == 3)
{
int size;
cout << "insert how many aircraft names would you like to insert: ";
getline(cin, input);
while (!(isNumber(input, size))) // validate input
{
cout << "Invalid input. Please enter a valid integer: ";
getline(cin, input);
}
cout << "insert aircraft names (press enter after each one):" << endl;
for (int i = 0; i < size; i++)
{
string name;
cin >> name;
names.push_back(name);
}
}
else if (userChoice > 7 || userChoice < 1)
{
cout << "Invalid input. Please enter a valid integer between 1 to 7: ";
getline(cin, input);
}
}
return userChoice;
}