-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.h
88 lines (73 loc) · 2.69 KB
/
Client.h
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
//
// Created by marcin on 11/20/22.
//
#ifndef EPOLL_WORK_QUEUE_CLIENT_H
#define EPOLL_WORK_QUEUE_CLIENT_H
#include <array>
#include <string>
#include <vector>
// This is the kind of the event from a client
// A client can either connect, disconnect (or be disconnected in case heartbeat expires)
// or send us a message
enum class ClientEventKind { CONNECTED,
DISCONNECTED,
MESSAGE_RECEIVED };
// This is the action in response to worker event.
// We can either send the worker a message, disconnect it
// gracefully from our list, or do nothing.
enum class WorkerActionKind { SEND_MESSAGE,
DISCONNECT,
NOOP,
EXIT };
// This is the object representing the response
// to a worker event. It has a kind and optional message.
class WorkerAction {
public:
WorkerAction() : kind(WorkerActionKind::NOOP), message{} {}
WorkerAction(WorkerActionKind kind) : kind(kind), message{} {}
WorkerAction(WorkerActionKind kind, std::vector<char> message) : kind(kind), message(message) {}
WorkerActionKind kind;
std::vector<char> message;
friend std::ostream& operator<<(std::ostream& os, const WorkerAction& w);
};
// This is the object representing the client event.
// It has a kind, client ID and optional message.
class ClientEvent {
public:
ClientEvent(ClientEventKind kind, unsigned int worker_id) : kind(kind), worker_id(worker_id), message{} {}
ClientEvent(ClientEventKind kind, unsigned int worker_id, std::vector<char> message) : kind(kind), worker_id(worker_id), message(message) {}
ClientEventKind kind;
unsigned int worker_id;
std::vector<char> message;
friend std::ostream& operator<<(std::ostream& os, const ClientEvent& w);
};
class Client {
public:
Client(
unsigned int id,
int timer_fd,
int client_fd,
std::string ip_address,
unsigned short port) : id(id),
timer_fd(timer_fd),
client_fd(client_fd),
ip_address(ip_address),
port(port) {}
unsigned int getID() const;
int getTimerFD() const;
int getClientFD() const;
int getStringOccurences(std::string listUrl) const;
private:
// The unique ID of the client
unsigned int id;
// File descriptor of associated heartbeat timer
int timer_fd;
// File descriptor of associated client socket
int client_fd;
// IP address of the client
std::string ip_address;
// Port of the client
unsigned short port;
friend std::ostream& operator<<(std::ostream& os, const Client& w);
};
#endif //EPOLL_WORK_QUEUE_CLIENT_H