-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyClientHandler.cpp
More file actions
72 lines (68 loc) · 2.54 KB
/
Copy pathMyClientHandler.cpp
File metadata and controls
72 lines (68 loc) · 2.54 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
//
// Created by yael and linoy on 16/01/2020.
//
#include "MyClientHandler.h"
MyClientHandler::MyClientHandler() {
this->cacheManager = new FileCacheManager();
this->solver = new ObjectAdapter<string, string>();
Searcher<string>* s = new AStar<string>();
this->solver->setSearcher(s);
}
//Main function of MyClientHandler. Receive a socket and handle the client according to the given problem.
void MyClientHandler::handleClient(int socket_fd) {
int index;
vector<string> matrixVec;
//reading from client as long as the parser function didn't end its' work
while (true) {
index = 0;
char buffer[1] = {0};
char totalBuffer[1024] = {0};
int val_read = read(socket_fd, buffer, 1);
while (buffer[0] != '\n') {
totalBuffer[index] = buffer[0];
index++;
val_read = read(socket_fd, buffer, 1);
}
//check if the total line (received from totalBuffer) contains "end"
string bufferAsString;
for(int i = 0; i < 1024; i++) {
if (totalBuffer[i] != '\000') {
bufferAsString += totalBuffer[i];
}
}
if (bufferAsString != "end") {
matrixVec.push_back(bufferAsString);
} else {
// convert the vector to a problem string to find in the CacheManager
auto i = matrixVec.begin();
string problem, solution;
while(i != matrixVec.end()) {
problem += *i;
i++;
}
//Update the name of this current search algorithm
this->cacheManager->setAlg(this->solver->getNameOfCurrAlg());
bool isExist = this->cacheManager->doesASolutionExist(problem);
if(isExist) {
solution = this->cacheManager->getSolution(problem);
} else {
i = matrixVec.begin();
string dilimProblem;
while(i != matrixVec.end()) {
dilimProblem += *i;
dilimProblem += ";";
i++;
}
solution = this->solver->solve(dilimProblem);
this->cacheManager->saveSolution(problem, solution);
}
solution += "\r\n";
const char* solutionToClient = solution.c_str();
int is_sent = send(socket_fd, solutionToClient, strlen(solutionToClient), 0);
if (is_sent < 0) {
cout<<"Failed Sending Solution To Client!"<<endl;
}
break;
}
}
}