-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.cpp
More file actions
executable file
·174 lines (149 loc) · 3.83 KB
/
worker.cpp
File metadata and controls
executable file
·174 lines (149 loc) · 3.83 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 "worker.h"
#include "tools.h"
worker::worker(int read_fd)
{
this->read_pipe_fd = read_fd;
this->my_files_cnt = 0;
}
void worker::extract_files_contents()
{
for (int i = 0; i < this->my_files_cnt; i++)
{
ifstream file(this->dir_path + "/" + this->my_file_names[i]);
string line;
if (getline(file, line) && i == 0)
this->my_files_contents.push_back(split_line_by_dash(line));
while (getline(file, line))
this->my_files_contents.push_back(split_line_by_dash(line));
}
this->extract_all_fields();
}
void worker::extract_all_fields()
{
for (int i = 0; i < this->my_files_contents[0].size(); i++)
this->all_fields.push_back(this->my_files_contents[0][i]);
}
string worker::do_the_search()
{
string result = "";
string fields[10][2];
for (int i = 0; i < 10; i++)
for (int j = 0; j < 2; j++)
fields[i][j] = "";
map<string, string>::iterator it;
for (it = search_fields.begin(); it != search_fields.end(); it++)
{
for (int i = 0; i < all_fields.size(); i++)
if (it->first == all_fields[i])
{
fields[i][0] = it->first;
fields[i][1] = it->second;
break;
}
}
for (int i = 1; i < my_files_contents.size(); i++)
{
bool is_in_result = true;
for (int j = 0; j < my_files_contents[i].size(); j++)
{
if (fields[j][0] == "")
continue;
else if (fields[j][1] != my_files_contents[i][j])
{
is_in_result = false;
break;
}
}
if (is_in_result)
{
for (int k = 0; k < my_files_contents[i].size(); k++)
result += (my_files_contents[i][k] + "-");
result += "\n";
}
}
return result;
}
void worker::send_result_to_presenter(string my_result)
{
my_result += END_OF_THIS_WORKERS_RESULT;
mkfifo(FIFO_PATHNAME, 0666);
int fd = open(FIFO_PATHNAME, O_WRONLY);
if (fd == -1)
{
perror("WORKER : couldnt open fifo\n");
return;
}
write(fd, my_result.c_str(), my_result.size());
close(fd);
}
string worker::receive_data()
{
char buff[BUFF_LEN];
read(this->read_pipe_fd, buff, BUFF_LEN);
close(this->read_pipe_fd);
return (string)buff;
}
void worker::fetch_read_data(string data)
{
string key = "";
string value = "";
bool is_key = true;
int i;
for (i = 0; ; i++)
{
if (data[i] == '-')
break;
else
key.push_back(data[i]);
}
i++;
this->dir_path = key;
key = "";
for (;; i++)
{
if (data[i] == '-')
{
if (key == "searchfields:")
break;
else
{
this->my_file_names.push_back(key);
this->my_files_cnt++;
key = "";
}
}
else
key.push_back(data[i]);
}
i++;
key = "";
for (; i < data.size() ; i++)
{
if (data[i] == '-')
{
search_fields.insert({key, value});
key = "";
value = "";
is_key = true;
continue;
}
if (data[i] == '=')
{
is_key = false;
continue;
}
(is_key ? key : value).push_back(data[i]);
}
}
void worker::print_attributes()
{
cout<<"\n ------ worker attributes ------\n";
cout << "number of files: " << this-> my_files_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;
}
worker::~worker()
{
}