forked from RoniCycode/embedded-programming-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe_handler.cpp
More file actions
121 lines (103 loc) · 3.34 KB
/
pipe_handler.cpp
File metadata and controls
121 lines (103 loc) · 3.34 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
#include "pipe_handler.h"
// Function to handle pipe creation
void createPipe(const string &type, int pipefd[]) {
if (pipe(pipefd) == -1) {
perror((type + "pipe creation failed").c_str());
exit(EXIT_FAILURE);
}
}
void closePipe(int pipe[]) {
close(pipe[0]);
close(pipe[1]);
}
// Function to handle pipe reading for integer values
int readFromPipe(int pipe) {
int value;
read(pipe, &value, sizeof(int));
return value;
}
// Function to handle pipe writing for integer values
void writeToPipe(int pipe, int value) {
write(pipe, &value, sizeof(int));
}
// Function to handle pipe reading for string values
char *readStringFromPipe(int pipe) {
int size = readFromPipe(pipe);
char *str = new char[size];
read(pipe, str, size);
return str;
}
// Function to handle pipe writing for string values
void writeStringToPipe(int pipe, const string &value) {
int size = (int) value.size() + 1;
writeToPipe(pipe, size);
write(pipe, value.c_str(), size);
}
// Function to handle node creation from pipe
Node *createNodeFromPipe(int pipe) {
Node *node = new Node;
node->setCallSign(readStringFromPipe(pipe));
node->setIcao24(readStringFromPipe(pipe));
node->setEstDepartureAirport(readStringFromPipe(pipe));
node->setEstArrivalAirport(readStringFromPipe(pipe));
node->setFirstSeen(readFromPipe(pipe));
node->setLastSeen(readFromPipe(pipe));
return node;
}
// Function to handle node writing to pipe
void writeNodeToPipe(int pipe, Node *node) {
writeStringToPipe(pipe, node->getCallSign());
writeStringToPipe(pipe, node->getIcao24());
writeStringToPipe(pipe, node->getEstDepartureAirport());
writeStringToPipe(pipe, node->getEstArrivalAirport());
writeToPipe(pipe, node->getFirstSeen());
writeToPipe(pipe, node->getLastSeen());
}
pid_t tryFork() {
pid_t pid = fork();
if (pid < 0) {
perror("Error creating child process");
exit(EXIT_FAILURE);
}
return pid;
}
void exitProcess(int exitCode, int pipeToNotify) {
writeToPipe(pipeToNotify, exitCode);
exit(exitCode);
}
void readStringVectorFromPipe(int pipe, vector<string> &result) {
result.clear();
int size = readFromPipe(pipe);
for (int i = 0; i < size; i++) {
result.push_back(readStringFromPipe(pipe));
}
}
void writeStringVectorToPipe(int pipe, vector<string> &input) {
int size = input.size();
writeToPipe(pipe, size);
for (int i = 0; i < size; i++) {
writeStringToPipe(pipe, input[i]);
}
}
void printNodeVectorFromPipe(const string& inputName, int pipe, function<void(vector<Node *> &, const string &)> printFunction) {
vector<Node *> result;
int size = readFromPipe(pipe);
for (int i = 0; i < size; i++) {
result.push_back(createNodeFromPipe(pipe));
}
printFunction(result, inputName);
freeArr(result);
}
void printMultiNodeVectorFromPipe(vector<string> &inputNames, int pipe,
function<void(vector<Node *> &, const string &)> printFunction) {
for (int i = 0; i < inputNames.size(); i++) {
printNodeVectorFromPipe(inputNames[i], pipe, printFunction);
}
}
void writeNodeVectorToPipe(int pipe, vector<Node *> &input) {
int size = input.size();
writeToPipe(pipe, size);
for (int i = 0; i < size; i++) {
writeNodeToPipe(pipe, input[i]);
}
}