-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyTestClientHandler.h
More file actions
72 lines (53 loc) · 1.57 KB
/
MyTestClientHandler.h
File metadata and controls
72 lines (53 loc) · 1.57 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 shmuelsuna on 08/01/2020.
//
#ifndef EX4__MYTESTCLIENTHANDLER_H_
#define EX4__MYTESTCLIENTHANDLER_H_
#include "ClientHandler.h"
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
//template <typename P, typename S>
class MyTestClientHandler : public ClientHandler {
private:
// Solver<P,S>* solver;
Solver<string, string> *solver;
int protocol;
public:
~MyTestClientHandler() {
}
MyTestClientHandler(int protocol_, Solver<string, string> *solver_) {
this->protocol = protocol_;
this->solver = solver_;
}
void handleClient(int client_socket) override {
char buffer[1024] = {0};
while (true) {
bzero(buffer, 1024);
string s;
int valread = read(client_socket, buffer, 1024);
s = fromBufferToString(buffer);
cout << "Message from client: " << s << endl;
if (s.compare("end") == 0) {
cout << "End the communication with the client" << endl;
break;
}
string solution = solver->solve(s);
cout << solution << endl;
const char *csolution = solution.c_str();
int is_send = send(client_socket, csolution, strlen(csolution), 0);
}
close(client_socket);
return;
}
string fromBufferToString(char buffer[]) {
string s;
int i = 0;
while (buffer[i] != '\0') {
s.push_back(buffer[i]);
i++;
}
return s;
}
};
#endif //EX4__MYTESTCLIENTHANDLER_H_