-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_balancer.cpp
More file actions
executable file
·235 lines (203 loc) · 5.58 KB
/
load_balancer.cpp
File metadata and controls
executable file
·235 lines (203 loc) · 5.58 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
#include "load_balancer.h"
#include "tools.h"
#include <stdlib.h>
load_balancer::load_balancer(string command)
{
this->_order = not_sorted;
this->sort_field = "";
this->fetch_command_data(command);
this->set_file_names();
this->workers_cnt = min(this->workers_cnt, this->files_cnt);
}
void load_balancer::make_worker_pipes()
{
for (int i = 0; i < workers_cnt; i++)
{
int worker_pipe[2];
if (pipe(worker_pipe) == -1)
{
perror("pipe creating failed\n");
return;
}
else
{
vector <int> tmp;
tmp.push_back(worker_pipe[0]);
tmp.push_back(worker_pipe[1]);
worker_pipes.push_back(tmp);
}
}
}
void load_balancer::set_file_names()
{
this->files_cnt = 0;
DIR *dir;
struct dirent *entry;
if ((dir = opendir(dir_path.c_str())) != NULL) {
while ((entry = readdir (dir)) != NULL)
{
if ((entry->d_name)[0] != '.')
{
this->file_names.push_back(entry->d_name);
this->files_cnt++;
}
}
closedir (dir);
}
else
{
perror("opening directory failed");
return;
}
}
void load_balancer::fetch_command_data(string command)
{
string key = "";
string value = "";
bool is_key = true;
for (int i = 0 ; ; i++)
{
if (i == command.size() - 1)
{
value.push_back(command[i]);
this->dir_path = value;
break;
}
if (command[i] == '=')
{
is_key = false;
continue;
}
else if (command[i] == ' ')
continue;
else if (command[i] == '-')
{
if (value == "ascending" || value == "descending")
{
this->sort_field = key;
this->_order = value == "ascending" ? ascending : descending;
is_key = true;
}
else if (key == "processes")
{
this->workers_cnt = stoi(value);
is_key = true;
}
else
{
(this->search_fields).insert({key, value});
is_key = true;
}
key = "";
value = "";
is_key = true;
continue;
}
(is_key ? key : value).push_back(command[i]);
}
}
void load_balancer::fork_workers()
{
for (int i = 0; i < this->workers_cnt; i++)
{
pid_t pid;
pid = fork();
if (pid < 0)
{
perror("fork failed");
return;
}
else if( pid == 0 )
{
char * argv[3];
argv[0] = (char*) WORKER_MAIN_EXEC;
argv[1] = (char*) to_string(this->worker_pipes[i][0]).c_str();
argv[2] = NULL;
close(worker_pipes[i][1]);
if(execv(argv[0], argv) == -1)
perror("exec failed");
}
else
{
close(worker_pipes[i][0]);
continue;
}
}
}
void load_balancer::fork_presenter()
{
pid_t pid;
pid = fork();
string order;
if (this->_order == ascending)
order = ASCENDING;
else if(this->_order == descending)
order = DESCENDING;
else
order = NOT_SORTED;
if (pid < 0)
{
perror("presenter forking failed");
return;
}
else if( pid == 0 )
{
char * argv[5];
argv[0] = (char*) PRESENTER_MAIN_EXEC;
argv[1] = (char*) to_string(this->workers_cnt).c_str();
argv[2] = (char*) order.c_str();
argv[3] = (char*) this->sort_field.c_str();
argv[4] = NULL;
if(execv(argv[0], argv) == -1)
perror("presenter execing failed");
}
}
void load_balancer::make_named_pipe()
{
remove(FIFO_PATHNAME);
if(mkfifo(FIFO_PATHNAME, 0666) < 0)
{
perror("couldn't create named pipe");
return;
}
}
void load_balancer::send_workers_data()
{
string *workers_data = new string[this->workers_cnt];
for (int i = 0; i < this->workers_cnt; i++)
workers_data[i] = "";
for (int i = 0; i < this->workers_cnt; i++)
workers_data[i] += (dir_path + "-");
for (int i = 0; i < this->files_cnt; i++)
workers_data[i % workers_cnt] += (this->file_names[i] + "-");
for (int i = 0; i < this->workers_cnt; i++)
workers_data[i] += ("searchfields:-");
for (int i = 0; i < this->workers_cnt; i++)
{
map<string, string>::iterator it;
for (it = search_fields.begin(); it != search_fields.end(); it++)
workers_data[i] += (it->first + "=" + it->second + "-");
}
// sending stuff
for (int i = 0; i < workers_cnt; i++)
{
write(worker_pipes[i][1], (char *)workers_data[i].c_str(), strlen(workers_data[i].c_str()) );
close(worker_pipes[i][1]);
}
}
load_balancer::~load_balancer()
{
for (int i = 0; i < this->workers_cnt; i++)
wait(NULL); // for each worker
wait(NULL); // for presenter
}
void load_balancer::print_attributes()
{
cout << "number of files: " << this->files_cnt << endl;
cout << "number of workers: " << this->workers_cnt << endl;
cout << "dir path: " << this->dir_path << endl;
map<string, string>::iterator it;
for (it = search_fields.begin(); it != search_fields.end(); it++)
cout << it->first << " : " << it->second << endl;
cout << "number of worker pipes created: " << worker_pipes.size() << endl;
}