-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchClientHandler.h
More file actions
156 lines (117 loc) · 3.92 KB
/
SearchClientHandler.h
File metadata and controls
156 lines (117 loc) · 3.92 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
//
// Created by ori on 12/01/2020.
//
#ifndef EX4_SEARCHCLIENTHANDLER_H
#define EX4_SEARCHCLIENTHANDLER_H
#include "ClientHandler.h"
#include "Searchable.h"
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <vector>
#include "CacheManager.h"
#include "AStarsearch.h"
#include <thread>
#include <chrono>
#include <mutex>
template<typename P, typename S>
class SearchClientHandler : public ClientHandler {
private:
Solver<P, S> *solver;
CacheManager<string, string> *cache_manager;
public:
~SearchClientHandler() {
}
SearchClientHandler(Solver<P, S> *solver_, CacheManager<string, string> *cache_manager_) {
this->solver = solver_;
this->cache_manager = cache_manager_;
}
void handleClient(int client_socket) override {
char buffer[1024] = {0};
vector<string> clientData;
while (true) {
bzero(buffer, 1024);
string line;
int valread = read(client_socket, buffer, 1024);
line = fromBufferToString(buffer);
if (line.size() == 0) {
continue;
}
//end of messages from the client, we need to break the loop
if (line.compare("end\n") == 0 || line.compare("end") == 0) {
cout << "End the communication with the client... solving the problem" << endl;
break;
} else {
clientData.push_back(line);
const char *readConfirm = "";
int is_send = send(client_socket, readConfirm, strlen(readConfirm), 0);
}
}
if (clientData.size() == 0) {
close(client_socket);
return;
}
P t;
string string_of_problom = clientsDataToStringOfProblom(clientData);
try {
t = solver->createProblem(clientData);
} catch (const char *exception) {
cout << exception << endl;
int is_send = send(client_socket, exception, strlen(exception), 0);
close(client_socket);
return;
}
//function that deals with all things with cache(checks if solution is in cache/if not solves and saves
S solution = activatingCache(string_of_problom, t);
const char *csolution = solution.c_str();
int is_send = send(client_socket, csolution, strlen(csolution), 0);
close(client_socket);
return;
}
string fromBufferToString(char buffer[]) {
int i = 0;
string s = "";
while (buffer[i] != '\0') {
s.push_back(buffer[i]);
i++;
}
return s;
}
string activatingCache(string problom_in_string, P problom) {
string solution;
mutex m;
m.lock();
if (this->cache_manager->isThereASolution(problom_in_string)) {
solution = this->cache_manager->getSolution(problom_in_string);
} else {
solution = solver->solve(problom);
//save to files
this->cache_manager->save(problom_in_string, solution);
}
m.unlock();
return solution;
}
string clientsDataToStringOfProblom(vector<string> clientData) {
vector<string>::iterator it = clientData.begin();
std::string delimiter1 = ",";
string srting_of_line;
string string_of_problom;
for (it; it != clientData.end(); ++it) {
std::string s = *it;
for (int i = 0; i < s.length(); ++i) {
if (s[i] == ',') {
continue;
} else {
srting_of_line += s[i];
}
}
}
string_of_problom = srting_of_line;
return string_of_problom;
}
ClientHandler *clone() override {
ClientHandler *newObj = new SearchClientHandler(this->solver->clone(), this->cache_manager);
return newObj;
}
};
#endif //EX4_SEARCHCLIENTHANDLER_H