-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.cpp
More file actions
280 lines (257 loc) · 9.61 KB
/
Copy pathfunc.cpp
File metadata and controls
280 lines (257 loc) · 9.61 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
#include "header.h"
pid_t pid;
int child_flag = 1;
pid_t& getPid(){return pid;}
void parentProccess(int* parentToChild, int* childToParent, pid_t& pid)
{
signal(SIGUSR2, signalHandler);
signal(SIGINT, signalHandler);
signal(SIGUSR1, signalHandler);
signal(SIGCONT, signalHandler);
close(parentToChild[READ_END]);
close(childToParent[WRITE_END]);
int choice;
std::string input;
std::vector<std::string> strings;
pause();//Wait from the child that
while (true) {
printMenu();
std::cin >> choice;
if(choice < 1 || choice > 7){
cout << "Invalid choice, please try again." << endl;
continue;
}
write(parentToChild[WRITE_END], &choice, sizeof(choice));
if (choice == 7) {
break;
}
switch (choice) {
case 1: {
std::cout << "Enter the number of airports you want get details: " << endl;
int numStrings;
std::cin >> numStrings;
checkNumStringsInput(numStrings);
write(parentToChild[WRITE_END], &numStrings, sizeof(numStrings));
getInputsAndWriteToPipe(numStrings, input, parentToChild[WRITE_END]);
break;
}
case 2:{
std::cout << "Enter the number of airports you want get details: " << endl;
int numStrings;
std::cin >> numStrings;
checkNumStringsInput(numStrings);
write(parentToChild[WRITE_END], &numStrings, sizeof(numStrings));
getInputsAndWriteToPipe(numStrings, input, parentToChild[WRITE_END]);
break;
}
case 3:{
int numStrings = 1;
write(parentToChild[WRITE_END], &numStrings, sizeof(numStrings));
getInputsAndWriteToPipe(numStrings, input, parentToChild[WRITE_END]);
break;
}
case 7:{
kill(pid, SIGINT);
break;
}
default:
// Other cases don't require additional input from the user
break;
}
vector<string> bufferVector;
int sizeOfResult;
read(childToParent[READ_END], &sizeOfResult, sizeof(sizeOfResult));
while(sizeOfResult > 0){
char buffer[1200000];
int bytesRead = read(childToParent[READ_END], buffer, sizeof(buffer));
bufferVector.push_back(buffer);
sizeOfResult -= bytesRead;
}
for(string& str:bufferVector){
cout<< str;
}
cout<<"\n";
}
close(parentToChild[WRITE_END]);
close(childToParent[READ_END]);
wait(NULL); // Wait for the child process to terminate
}
void childProccess(int* parentToChild, int* childToParent){
signal(SIGINT, signalHandler);
signal(SIGUSR1, signalHandler);
cleanUp(parentToChild[WRITE_END],childToParent[READ_END]);
try{
execution mainObject;
kill(getppid(), SIGCONT);
int choice;
string result;
while (true) {
read(parentToChild[READ_END], &choice, sizeof(choice));
if (choice == 7) {
break;
}
switch (choice) {
case 1: {
std::vector<std::string> strings = readInputs(parentToChild[READ_END]);
result = mainObject.getAllArrivalFlightsDetails(strings);
break;
}
case 2:{
std::vector<std::string> strings = readInputs(parentToChild[READ_END]);
result = mainObject.getFull_schedule(strings);
break;
}
case 3:{
std::vector<std::string> strings = readInputs(parentToChild[READ_END]);
result = mainObject.getAirplaneFlight(strings[0]);
break;
}
case 4:{
result = mainObject.updateDB();
break;
}
case 5:{
bool isZipped = mainObject.zipDB();
if(isZipped)
result = "Data Base ZIP archive created successfully";
else
result = "Data Base ZIP archive not created due error while zipping the files";
break;
}
case 6:{
string str = "Child pid is: ";
result = str + to_string(getpid());
break;
}
default:
result = "method " + std::to_string(choice);
}
int sizeOfResult = result.size() + 1;
write(childToParent[WRITE_END], &sizeOfResult, sizeof(sizeOfResult));
write(childToParent[WRITE_END], result.c_str(), result.size() + 1);
}
}catch(string no_db){
sleep(1);
kill(getppid(), SIGUSR2);
}
close(parentToChild[READ_END]);
close(childToParent[WRITE_END]);
std::exit(0);
}
std::vector<std::string> readInputs(int pipeReadEnd){
int numStrings;
read(pipeReadEnd, &numStrings, sizeof(numStrings));
std::vector<std::string> strings(numStrings);
for (int i = 0; i < numStrings; ++i) {
char buffer[256];
read(pipeReadEnd, buffer, sizeof(buffer));
strings[i] = buffer;
}
return strings;
}
void getInputsAndWriteToPipe(int numStrings, string input, int pipeWriteEnd){
std::cin.ignore(); // Ignore the newline character from previous input
for (int i = 0; i < numStrings; ++i) {
std::cout << "Enter input " << i + 1 << ": ";
std::getline(std::cin, input);
write(pipeWriteEnd, input.c_str(), input.size() + 1);
}
}
void checkNumStringsInput(int& numStrings){
while(numStrings <= 0){
cout << "Invalid input, please try again enter positive number: " << endl;
cin >> numStrings;
}
}
void printMenu(){
std::cout << "Menu:\n";
std::cout << "1. Fetch airports incoming flights.\n";
std::cout << "2. Fetch airports full flights schedule.\n";
std::cout << "3. Fetch aircraft full flights schedule.\n";
std::cout << "4. Update DB\n";
std::cout << "5. Zip DB files\n";
std::cout << "6. GEt child process PID\n";
std::cout << "7. Exit\n";
std::cout << "Enter your choice: ";
}
void cleanUp(int pipe1, int pipe2){
close(pipe1);
close(pipe2);
}
void signalHandler(int signumber){
if(signumber == SIGINT && pid > 0)
{
kill(pid,SIGINT);
wait(NULL);
std::exit(0);
}
else if(signumber == SIGINT && pid == 0){
zipDB();
std::exit(0);
}
else if(signumber == SIGUSR1 && pid == 0){
zipDB();
kill(getppid(), SIGINT);
std::exit(0);
}
else if(signumber == SIGUSR2 && pid > 0){
cout <<"No data base." << endl;
std::exit(0);
}
else if(signumber == SIGCONT){
//data base load succefully
}
}
bool zipDB(){
bool res = true;
const string libraryPath = "./flightDB/";
const string zipFilename = "DB.zip";
if (!(std::filesystem::exists(libraryPath) && std::filesystem::is_directory(libraryPath))) {
return false;
}
int error;
zip* archive = zip_open(zipFilename.c_str(), ZIP_CREATE | ZIP_TRUNCATE, &error);
if (!archive) {
//std::cerr << "Failed to create ZIP archive: " << zip_strerror(archive) << std::endl;
return false;
}
filesystem::path libraryDirectory(libraryPath);
if (filesystem::exists(libraryDirectory) && filesystem::is_directory(libraryDirectory)) {
addDirectoryToZip(archive, libraryDirectory, filesystem::path(""));
} else {
cerr << "Invalid library path: " << libraryPath << endl;
zip_close(archive);
return 1;
}
if (zip_close(archive) == -1) {
//std::cerr << "Failed to close ZIP archive: " << zip_strerror(archive) << std::endl;
return false;
}
return res;
}
void addFileToZip(zip* archive, const filesystem::path& filePath, const filesystem::path& relativePath) {
std::string fileStr = filePath.string();
zip_source* source = zip_source_file(archive, fileStr.c_str(), 0, -1);
if (!source) {
std::cerr << "Failed to add file to ZIP archive: " << zip_strerror(archive) << std::endl;
return;
}
std::string relativePathStr = relativePath.string();
zip_int64_t index = zip_file_add(archive, relativePathStr.c_str(), source, ZIP_FL_OVERWRITE);
if (index == -1) {
//std::cerr << "Failed to add file to ZIP archive: " << zip_strerror(archive) << std::endl;
zip_source_free(source);
return;
}
}
void addDirectoryToZip(zip* archive, const filesystem::path& directoryPath, const filesystem::path& relativePath) {
for (const auto& entry : fs::directory_iterator(directoryPath)) {
const auto& path = entry.path();
const auto& relativeSubPath = relativePath / path.filename();
if (filesystem::is_directory(path)) {
addDirectoryToZip(archive, path, relativeSubPath);
} else if (filesystem::is_regular_file(path)) {
addFileToZip(archive, path, relativeSubPath);
}
}
}