-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
402 lines (347 loc) · 11.5 KB
/
server.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include "sockutil.h"
#include <algorithm>
#include <arpa/inet.h>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <errno.h>
#include <iostream>
#include <list>
#include <mutex>
#include <netinet/in.h>
#include <string.h>
#include <string>
#include <strings.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <thread>
#include <unistd.h>
#include <unordered_map>
#include <vector>
#define NUM 6
#define MAX_LEN 512
#define CLIENT_SIZE sizeof(clients)
int u_id = 0;
std::mutex client_mtx;
std::mutex cout_mtx;
std::string def_col = "\033[0m";
std::vector<std::string> colors = {"\033[31m", "\033[32m", "\033[33m",
"\033[34m", "\033[35m", "\033[36m"};
std::vector<CLIENT_INFO> clients;
void HANDLE_CLIENT(int client_socket, int id);
void BROADCASTING(std::string message, int SENDER_ID);
void END_CON(int ID);
void Shared_Print(std::string str, bool endline);
void BROADCAST_ID_FOR_COLOR(int num, int SENDER_ID);
int GET_CLIENT_COUNT();
std::string GetClientsInfo();
void SAVE_TO_CACHE(int u_id, std::string msg);
std::string Get_old_message();
// cache implementation with previous 5 messages
class LRUCache {
public:
struct Node {
std::string key;
std::string value;
Node *prev;
Node *next;
Node(const std::string &k, const std::string &v) {
key = k;
value = v;
}
};
std::unordered_map<std::string, Node *> cacheMap;
std::list<Node *> cacheList;
int capacity;
public:
LRUCache(int cap) { capacity = cap; }
void put(const std::string &key, const std::string &value) {
if (cacheList.size() >= capacity) {
Node *last = cacheList.back();
cacheMap.erase(last->key);
cacheList.pop_back();
delete last;
}
Node *newNode = new Node(key, value);
cacheList.push_front(newNode);
cacheMap[key] = newNode;
}
private:
void update(Node *node) {
cacheList.erase(find(cacheList.begin(), cacheList.end(), node));
cacheList.push_front(node);
}
};
// cache implementation with replacing key so only latest message from each
// client
class Cache {
public:
struct Node {
std::string key;
std::string value;
Node *prev;
Node *next;
Node() {
key = "";
value = "";
}
Node(const std::string &k, const std::string &v) {
key = k;
value = v;
}
};
std::unordered_map<std::string, Node *> cacheMap;
Node *head = new Node();
Node *tail = new Node();
int CAPACITY;
Cache(int cap) {
CAPACITY = cap;
head->next = tail;
tail->prev = head;
}
void ADD_Node(Node *newnode);
void Del_Node(Node *newnode);
void PUT_DATA(const std::string &key, const std::string &value);
};
void Cache::PUT_DATA(const std::string &key, const std::string &value) {
if (cacheMap.find(key) != cacheMap.end()) {
Node *exist = cacheMap[key];
cacheMap.erase(key);
Del_Node(exist);
}
if (cacheMap.size() == CAPACITY) {
cacheMap.erase(tail->prev->key);
Del_Node(tail->prev);
}
ADD_Node(new Node(key, value));
cacheMap[key] = head->next;
}
void Cache::Del_Node(Node *delnode) {
Node *nodeprev = delnode->prev;
Node *nodenext = delnode->next;
nodeprev->next = nodenext;
nodenext->prev = nodeprev;
}
void Cache::ADD_Node(Node *newnode) {
Node *temp = head->next;
newnode->next = temp;
newnode->prev = head;
head->next = newnode;
temp->prev = newnode;
}
Cache cache(5);
// LRUCache cache(5);
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cout << "argument in wrong order" << std::endl;
return 0;
}
int PORT_NO = atoi(argv[1]);
int server_socket = createTCPipv4socket();
if (server_socket < 0) {
std::cout << "failed to create socket" << std::endl;
exit(-1);
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT_NO);
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&server_addr.sin_zero, 0);
if (bind(server_socket, (struct sockaddr *)&server_addr,
sizeof(server_addr)) < 0) {
std::cout << "failed bind" << std::endl;
perror("bind error: ");
exit(-1);
}
if (listen(server_socket, 8) < 0) {
perror("listen error: ");
exit(-1);
}
// std::cout << colors[NUM - 1]
// << "\n\t ===== server is listening... =====" << def_col
// << std::endl;
std::cout << colors[NUM - 6]
<< "\n\t ▄████▄ ██░ ██ ▄▄▄ ▄▄▄█████▓ ██████ ▓█████ "
"██▀███ ██▒ █▓▓█████ ██▀███ \n"
"\t▒██▀ ▀█ ▓██░ ██▒▒████▄ ▓ ██▒ ▓▒ ▒██ ▒ ▓█ ▀ ▓██ ▒ "
"██▒▓██░ █▒▓█ ▀ ▓██ ▒ ██▒\n"
"\t▒▓█ ▄ ▒██▀▀██░▒██ ▀█▄ ▒ ▓██░ ▒░ ░ ▓██▄ ▒███ ▓██ "
"░▄█ ▒ ▓██ █▒░▒███ ▓██ ░▄█ ▒\n"
"\t▒▓▓▄ ▄██▒░▓█ ░██ ░██▄▄▄▄██░ ▓██▓ ░ ▒ ██▒▒▓█ ▄ "
"▒██▀▀█▄ ▒██ █░░▒▓█ ▄ ▒██▀▀█▄ \n"
"\t▒ ▓███▀ ░░▓█▒░██▓ ▓█ ▓██▒ ▒██▒ ░ ▒██████▒▒░▒████▒░██▓ "
"▒██▒ ▒▀█░ ░▒████▒░██▓ ▒██▒\n"
"\t░ ░▒ ▒ ░ ▒ ░░▒░▒ ▒▒ ▓▒█░ ▒ ░░ ▒ ▒▓▒ ▒ ░░░ ▒░ ░░ ▒▓ "
"░▒▓░ ░ ▐░ ░░ ▒░ ░░ ▒▓ ░▒▓░\n"
"\t ░ ▒ ▒ ░▒░ ░ ▒ ▒▒ ░ ░ ░ ░▒ ░ ░ ░ ░ ░\n";
std::cout << std::endl;
std::cout << std::endl;
struct sockaddr_in client_addr;
int client_socket;
unsigned int len = sizeof(sockaddr_in);
while (1) {
if ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr,
&len)) < 0) {
perror("accept error:");
exit(-1);
}
u_id++;
std::thread t(HANDLE_CLIENT, client_socket, u_id);
std::lock_guard<std::mutex> guard(client_mtx);
std::string random = "anonymous";
clients.push_back({u_id, random, client_socket, (move(t))});
}
for (int i = 0; i < clients.size(); i++) {
if (clients[i].th.joinable()) {
clients[i].th.join();
}
}
close(server_socket);
return 0;
}
void SET_NAME(int id, char name[]) {
for (int i = 0; i < CLIENT_SIZE; ++i) {
if (clients[i].id == id) {
clients[i].name = std::string(name);
}
}
}
int GET_CLIENT_COUNT() { return clients.size(); }
void HANDLE_CLIENT(int client_socket, int id) {
char name[MAX_LEN];
char msg[MAX_LEN];
recv(client_socket, name, sizeof(name), 0);
SET_NAME(id, name);
// welcome bitch
std::string WELCOME = std::string(" ") +
std::string(name) +
std::string(" has joined the party");
// std::cout << colors[NUM - 3] << WELCOME << std::endl;
BROADCASTING("NEW_CON", id);
BROADCAST_ID_FOR_COLOR(id, id);
BROADCASTING(WELCOME, id);
Shared_Print(color(id) + WELCOME + def_col, true);
while (true) {
int bytes_recv = recv(client_socket, msg, sizeof(msg), 0);
if (bytes_recv <= 0) {
return;
}
if (strcmp(msg, "#exit") == 0) {
// leaving message
std::string bye = std::string(" ") +
std::string(name) + std::string(" has left");
BROADCASTING("NEW_CON", id);
BROADCAST_ID_FOR_COLOR(id, id);
BROADCASTING(bye, id);
Shared_Print(color(id) + bye + def_col, true);
END_CON(id);
return;
}
if (strcmp(msg, "#gc") == 0) {
uint16_t COUNT = GET_CLIENT_COUNT();
std::string INFO = GetClientsInfo();
// "number of active members ->: " + std::to_string(COUNT);
char buff[MAX_LEN] = "NEW_CON";
send(client_socket, buff, sizeof(buff), 0);
send(client_socket, &id, sizeof(id), 0);
send(client_socket, INFO.c_str(), INFO.length(), 0);
continue;
}
if (strcmp(msg, "#cli") == 0) {
std::cout << " cli function hitted" << std::endl;
char target[MAX_LEN];
recv(client_socket, target, sizeof(target), 0);
int target_socket = -1;
for (const auto &client : clients) {
if (client.name == std::string(target)) {
target_socket = client.socket;
break;
}
}
if (target_socket != -1) {
std::string baat =
"You are now chatting separately with " + std::string(name);
// send(target_socket, baat.c_str(), sizeof(baat), 0);
char buff[MAX_LEN] = "NEW_CON";
send(target_socket, buff, sizeof(buff), 0);
send(target_socket, &id, sizeof(id), 0);
send(target_socket, baat.c_str(), baat.length(), 0);
std::string bat =
"You are now chatting separately with " + std::string(target);
// send(client_socket, bat.c_str(), sizeof(bat), 0);
} else {
std::string tar_msg = "target not found";
send(client_socket, tar_msg.c_str(), sizeof(tar_msg), 0);
}
}
if (strcmp(msg, "#getmsg") == 0) {
std::string INFO_MSG = Get_old_message();
// "number of active members ->: " + std::to_string(COUNT);
char buff[MAX_LEN] = "NEW_CONN";
size_t INFO_SIZE = INFO_MSG.size();
send(client_socket, buff, sizeof(buff), 0);
send(client_socket, &id, sizeof(id), 0);
send(client_socket, INFO_MSG.c_str(), INFO_SIZE, 0);
continue;
}
BROADCASTING(std::string(name), id);
BROADCAST_ID_FOR_COLOR(id, id);
SAVE_TO_CACHE(id, msg);
BROADCASTING(std::string(msg), id);
Shared_Print(color(id) + std::string(" ") +
std::string(name) + " : " + def_col + std::string(msg),
true);
}
}
void SAVE_TO_CACHE(int id, std::string msg) {
cache.PUT_DATA(std::to_string(id), msg);
}
void BROADCAST_ID_FOR_COLOR(int num, int SENDER_ID) {
for (int i = 0; i < CLIENT_SIZE; ++i) {
if (clients[i].id != SENDER_ID) {
send(clients[i].socket, &num, sizeof(num), 0);
}
}
}
void BROADCASTING(std::string message, int SENDER_ID) {
char msg[MAX_LEN];
strcpy(msg, message.c_str());
for (int i = 0; i < CLIENT_SIZE; ++i) {
if (clients[i].id != SENDER_ID) {
send(clients[i].socket, msg, sizeof(msg), 0);
}
}
}
void END_CON(int ID) {
for (int i = 0; i < CLIENT_SIZE; ++i) {
if (clients[i].id == ID) {
std::lock_guard<std::mutex> guard(client_mtx);
clients[i].th.detach();
clients.erase(clients.begin() + i);
break;
}
}
}
std::string GetClientsInfo() {
std::string clientInfo = "Active Members:\n";
for (const auto &client : clients) {
clientInfo +=
"ID: " + std::to_string(client.id) + ", Name: " + client.name + "\n";
}
return clientInfo;
}
std::string Get_old_message() {
std::string oldmsg = "last 5 messages:\n";
for (const auto &it : cache.cacheMap) {
// Append the ID to the message
oldmsg += "ID: " + it.first + " msg: " + it.second->value + "\n";
}
return oldmsg;
}
void Shared_Print(std::string msg, bool endline = true) {
std::lock_guard<std::mutex> guard(cout_mtx);
std::cout << msg;
if (endline)
std::cout << std::endl;
}
std::string color(int code) { return colors[code % NUM]; }