-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.cpp
62 lines (49 loc) · 1.31 KB
/
Client.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
//
// Created by marcin on 11/20/22.
//
#include "Client.h"
#include <iostream>
const char* WorkerEventKindText[] = {
"CONNECTED",
"DISCONNECTED",
"MESSAGE_RECEIVED"};
const char* WorkerActionKindText[] = {
"SEND_MESSAGE",
"DISCONNECT",
"NOOP",
"EXIT"};
unsigned int Client::getID() const {
return id;
}
int Client::getTimerFD() const {
return timer_fd;
}
int Client::getClientFD() const {
return client_fd;
}
std::ostream& operator<<(std::ostream& os, const Client& w) {
os << "worker(" << w.id << ',' << w.ip_address << ':' << w.port << ')';
return os;
}
std::ostream& operator<<(std::ostream& os, const WorkerAction& w) {
std::string s(w.message.begin(), w.message.end());
if (!s.empty() && s[s.length() - 1] == '\n') {
s.erase(s.length() - 1);
}
if (s.empty()) {
s = "<no_msg>";
}
os << "worker_action(" << WorkerActionKindText[((int) w.kind)] << ',' << s << ')';
return os;
}
std::ostream& operator<<(std::ostream& os, const ClientEvent& w) {
std::string s(w.message.begin(), w.message.end());
if (!s.empty() && s[s.length() - 1] == '\n') {
s.erase(s.length() - 1);
}
if (s.empty()) {
s = "<no_msg>";
}
os << "worker_event(" << WorkerEventKindText[((int) w.kind)] << ',' << w.worker_id << ',' << s << ')';
return os;
}