-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyTestClientHandler.cpp
More file actions
58 lines (54 loc) · 1.95 KB
/
Copy pathMyTestClientHandler.cpp
File metadata and controls
58 lines (54 loc) · 1.95 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
//
// Created by linoy and yael on 12/01/2020.
//
#include "MyTestClientHandler.h"
bool isEndOfRead = false;
//Constructor
MyTestClientHandler::MyTestClientHandler() {
this->cacheManager = new FileCacheManager();
this->solver = new StringReverser();
}
//This is the main method of MyTestClientHandler.
void MyTestClientHandler::handleClient(int socket_fd) {
int index;
//reading from client as long as the parser function didn't end its' work
while (!isEndOfRead) {
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 (receieved from totalBuffer) contains "end"
string problem, solution;
for (int i = 0; i < 1024; i++) {
if (totalBuffer[i] != '\000') {
problem += totalBuffer[i];
}
}
if (problem.find("end") != std::string::npos) {
isEndOfRead = true;
continue;
} else {
//Set the current algorithm according to the solver's searcher.
this->cacheManager->setAlg(this->solver->getNameOfCurrAlg());
bool isExist = this->cacheManager->doesASolutionExist(problem);
if(isExist) {
solution = this->cacheManager->getSolution(problem);
} else {
solution = this->solver->solve(problem);
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;
}
}
}
isEndOfRead = false;
}