-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyClientHandler.cpp
94 lines (83 loc) · 2.35 KB
/
MyClientHandler.cpp
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
//
// Created by yuvalshechter on 04/02/2020.
//
#include "MyClientHandler.h"
#include <unistd.h>
#include <iostream>
#include <netinet/in.h>
#include <sys/socket.h>
#include <thread>
#include <arpa/inet.h>
#include <cstring>
#include <netinet/in.h>
#include "MyTestClientHandler.h"
#include "FileCacheManager.h"
MyClientHandler::MyClientHandler(Solver<vector<string>,string>* solver1){
this-> cm = new FileCacheManager<string, string>();
this-> solver = solver1;
}
void MyClientHandler::handleClient(int socketClient){
char buffer[1024] = {0};
int bufferlength = 1024;
vector<string> matrix_vec;
string newBuffer = "";
string line = "";
string temp_buffer;
string strBuffer = "";
//while (true) {
memset(buffer, 0, bufferlength);
int i = 0;
read(socketClient, buffer, bufferlength);
while (buffer[i] != '\0' && i < bufferlength) {
line += buffer[i];
i++;
}
while (!isEnd(buffer)) {
int i = 0;
memset(buffer, 0, bufferlength);
read(socketClient, buffer, 1024);
while (buffer[i] != '\0' && i < bufferlength) {
line += buffer[i];
i++;
}
}
string temp_line = "";
for (int i = 0; i<line.length(); i++) {
if (line[i] != '\n' && line[i] != '\r') {
temp_line += line[i];
} else {
if (temp_line.length() > 0) {
matrix_vec.push_back(temp_line);
temp_line = "";
}
}
}
matrix_vec.erase(matrix_vec.end()-1);
//if (this->cm->isSolutionExist(line)) {
//string solution = cm->getSolution(line);
//connectAndSend(solution, socketClient);
//} else {
string solution;
solution = solver->solve(matrix_vec);
// cm->saveSolution(line, solution);
connectAndSend(solution, socketClient);
//}
}
int MyClientHandler:: connectAndSend(string msg, int clientSocket){
//create socket
int is_sent = send(clientSocket,(char*)msg.c_str() , strlen((char*)msg.c_str()) , 0);
if (is_sent == -1) {
std::cout<<"Error sending message"<<std::endl;
}
close(clientSocket);
}
MyClientHandler::~MyClientHandler(){}
bool MyClientHandler::isEnd(string buffer) {
int x = buffer.find("end");
if (x < 0)
return false;
if (x >= 0) {
buffer.erase(buffer.find("end"), 3);
return true;
}
}