-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcity.cpp
180 lines (154 loc) · 5.3 KB
/
city.cpp
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
#include <dirent.h>
#include <bits/stdc++.h>
#include <fstream>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include "my_defines.hpp"
#include "Tools.hpp"
int main(int argc, char const *argv[])
{
std::string CityFilePath;
std::string CityfifoPath;
std::string Cityinput;
std::string Cityfinal_res;
std::string store_final_res;
std::string Cityordering;
std::vector<std::string> Cityresults;
std::vector<int> CityintegerResults;
std::vector<std::string> Cityinputs;
std::vector<std::string> stores;
size_t StoreCount;
int Cityfd;
int Cityfifo;
int CityreadBit;
char Citybuff[MSGSIZE];
char storeBuff[MSGSIZE];
CityFilePath = std::string(argv[1]); // path of city directory that should searching for own stores
Cityfd = atoi(argv[2]); // file descriptor ro reading from unnamed pipe
CityfifoPath = std::string(argv[3]); // path of named pipe for sending filtered result
stores = getDirFiles(CityFilePath); // get list of csv files in this dir
StoreCount = stores.size();
CityreadBit = read(Cityfd, Citybuff, MSGSIZE); // read data from pipe
if (CityreadBit < 0)
{
std::cerr << "error in reading from pipe" << std::endl;
return 2;
}
else if (CityreadBit == 0)
{
std::cerr << "error in reading from pipe [ can't read all data ]" << std::endl;
}
if (close(Cityfd) < 0)
{
std::cerr << "error in closing pipe read fd" << std::endl;
return 2;
}
Cityinput = std::string(Citybuff);
Cityinputs = splitter(Cityinput, DELIMITER);
Cityordering = Cityinputs[3];
/* ---------------------------------------------------------------------- */
/* -------------- handling fork and city childs --------------- */
std::vector<std::vector<int>> storePipes;
std::vector<std::string> storenamedpipes;
std::string StoreExecPath;
std::string storefifo;
/* create unnamed pipes */
for (int i = 0; i < StoreCount; i++)
{
int cityfd[2];
if (pipe(cityfd) < 0)
{
std::cerr << "can not create store pipe " << i << std::endl;
return 2;
}
std::vector<int> fds;
fds.push_back(cityfd[0]);
fds.push_back(cityfd[1]);
storePipes.push_back(fds);
}
/* -------------------- */
/* create fifos */
for (int i = 0; i < StoreCount; i++)
{
storefifo = std::string(CITYNAMEDPIPE) + stores[i] + std::to_string(i);
mkfifo(storefifo.c_str(), 0666);
storenamedpipes.push_back(storefifo);
}
/* ------------ */
/*--- forking --- */
for (int i = 0; i < StoreCount; i++)
{
StoreExecPath = CityFilePath + '/' + stores[i];
pid_t pid = fork();
if (pid < 0)
{
std::cerr << "can not fork store" << std::endl;
}
else if (pid == 0)
{
close(storePipes[i][1]);
const char *argv[5];
argv[0] = "store";
argv[1] = StoreExecPath.c_str();
argv[2] = std::to_string(storePipes[i][0]).c_str();
argv[3] = storenamedpipes[i].c_str();
argv[4] = NULL;
execv(argv[0], (char **)argv);
}
else
{
close(storePipes[i][0]);
if (write(storePipes[i][1], Cityinput.c_str(), (Cityinput.length()) + 1) < 0)
{
std::cerr << "error in sending filters to stores via pipe" << std::endl;
return 2;
}
if (close(storePipes[i][1]) < 0)
{
std::cerr << "error in closing store pipe" << std::endl;
return 2;
}
}
}
/*-------------- */
/***************************************************************/
/* getting data from cities */
for (int i = 0; i < StoreCount; i++)
{
int fifo_fd;
fifo_fd = open(storenamedpipes[i].c_str(), O_RDONLY); // open namedpipe to receive filtered data from cities
if (read(fifo_fd, storeBuff, MSGSIZE) < 0)
{
std::cerr << "error in receiving final res from store via FIFO" << std::endl;
return 2;
}
if (close(fifo_fd) < 0)
{
std::cerr << "error in closing namedpipe [FIFO]" << std::endl;
return 2;
}
store_final_res = std::string(storeBuff);
if (store_final_res != "-1" && store_final_res.length() > 0)
CityintegerResults.push_back(stoi(store_final_res));
}
while (wait(NULL) > 0 || errno != ECHILD);
/****************************/
/* ---------------------------------------------------------------------- */
/* calculatios and send final result to parent */
Cityfinal_res = findMinMax(CityintegerResults, Cityordering);
Cityfifo = open(CityfifoPath.c_str(), O_WRONLY); // open namedpipe to send data
if (write(Cityfifo, Cityfinal_res.c_str(), (Cityfinal_res.length()) + 1) < 0)
{
std::cerr << "error in sending final city result via FIFO" << std::endl;
return 2;
}
if (close(Cityfifo) < 0)
{
std::cerr << "error in closing namedpipe [FIFO]" << std::endl;
return 2;
}
/* ------------------------------------------- */
}