-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
378 lines (286 loc) · 8.43 KB
/
client.cpp
File metadata and controls
378 lines (286 loc) · 8.43 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
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
//
// Created by leonardo on 13/05/20.
//
#include "client.h"
#include "config.h"
using namespace std;
int udp_socket;
string username;
CODE client_status;
bool running;
int main(int argc, char *argv[])
{
cout << "Signal handler setup." << endl;
signal_handler_init();
cout << "Client setup." << endl;
udp_init();
// authentication
while (!client_authentication());
client_status = SUCCESS;
cout << "Welcome " << username << endl;
// Handles writing to the shell
thread send_thread(sender);
running = true;
while (running){
if(input_available(udp_socket)){
receiver();
}
}
cout << "Closing client." << endl;
send_thread.join();
udp_close();
return EXIT_SUCCESS;
}
void print_info_message() {
cout << "Type <users> to see users online. " << endl
<< "Type <chat> to send message. " << endl
<< "Type <quit> to disconnect." << endl;
}
void print_message(char *msg) {
cout << "[ " << msg << " ][ " << msg + MSG_H_CODE_SIZE << " ][ " << msg + MSG_H_CODE_SIZE + MSG_H_SRC_SIZE << " ][ "
<< msg + MSG_HEADER_SIZE << " ]" << endl;
}
// Functionality
void client_video() {
client_status = CODE::VIDEO;
}
void client_quit() {
client_status = CODE::QUIT;
running = false;
char msg[MSG_SIZE];
memset(msg, 0, MSG_SIZE);
strcpy(msg, to_string(CODE::QUIT).c_str());
strcpy(msg + MSG_H_CODE_SIZE, username.c_str());
if(LOG){
cout << "send: ";
print_message(msg);
}
int ret = sendto(udp_socket, msg, MSG_SIZE, 0, NULL, 0);
if(ret < 0){
perror("Error during send operation");
exit(EXIT_FAILURE);
}
}
void client_users() {
client_status = CODE::USERS;
char msg[MSG_SIZE];
memset(msg, 0, MSG_SIZE);
strcpy(msg, to_string(CODE::USERS).c_str());
strcpy(msg + MSG_H_CODE_SIZE, username.c_str());
if(LOG){
cout << "send: ";
print_message(msg);
}
int ret;
ret = sendto(udp_socket, msg, MSG_SIZE, 0, NULL, 0);
if(ret < 0){
perror("Error during send operation.");
exit(EXIT_FAILURE);
}
}
void client_chat() {
client_status = CODE::CHAT;
cout << "Type recipient: " << endl;
string dst;
if(input_available(0))
getline(cin,dst);
else return;
cout << "Type message: " << endl;
string message;
if(input_available(0))
getline(cin,message);
else return;
char msg[MSG_SIZE];
memset(msg, 0, MSG_SIZE);
strcpy(msg,to_string(CODE::CHAT).c_str()); // code
strcpy(msg + MSG_H_CODE_SIZE, username.c_str()); // src
strcpy(msg + MSG_H_CODE_SIZE + MSG_H_SRC_SIZE, dst.c_str()); // dst
strcpy(msg + MSG_HEADER_SIZE, message.c_str()); // content
if(LOG){
cout << "send: ";
print_message(msg);
}
int ret;
ret = sendto(udp_socket, msg, MSG_SIZE, 0, NULL, 0);
if(ret < 0){
perror("Error during send operation.");
exit(EXIT_FAILURE);
}
}
bool client_authentication() {
client_status = CODE::AUTHENTICATION;
regex r("[a-zA-Z0-9]+ [a-zA-Z0-9]+");
cout << "Digit <username> <password> for access to service." << endl;
char credential[MSG_CONTENT_SIZE];
cin.getline(credential, MSG_CONTENT_SIZE);
if(!regex_match(credential, r)){
cout << "No pattern mathing (<username> <password>)." << endl;
return false;
}
char msg[MSG_SIZE];
memset(msg, 0, MSG_SIZE);
sprintf(msg, "%d", CODE::AUTHENTICATION);
sprintf(msg + MSG_HEADER_SIZE, "%s", credential);
if(LOG){
cout << "send: ";
print_message(msg);
}
int ret;
ret = sendto(udp_socket, msg, MSG_SIZE, 0, (struct sockaddr *) NULL, 0);
if(ret < 0) {
perror("Error during send operation");
exit(EXIT_FAILURE);
}
// clear buffer
memset(msg, 0, MSG_SIZE);
// waiting for response
ret = recvfrom(udp_socket, msg, MSG_SIZE, 0, (struct sockaddr *) NULL, 0);
if(ret < 0){
perror("Error during recv operation");
exit(EXIT_FAILURE);
}
if(LOG){
cout << "recv: ";
print_message(msg);
}
cout << "[Server] " << msg + MSG_HEADER_SIZE << endl;
char code[MSG_H_CODE_SIZE];
memcpy(code, msg, MSG_H_CODE_SIZE);
CODE code_ = (CODE) atoi(code);
if(code_ == CODE::SUCCESS){
char user[MSG_H_DST_SIZE];
memcpy(user, msg + MSG_H_CODE_SIZE + MSG_H_SRC_SIZE, MSG_H_DST_SIZE);
username = user;
return true;
}else return false;
}
void receiver() {
int ret;
char msg[MSG_SIZE];
ret = recvfrom(udp_socket, msg, MSG_SIZE, 0, (struct sockaddr *) NULL, 0);
if(ret < 0){
perror("Error during recv operation");
exit(EXIT_FAILURE);
}
if(LOG){
cout << "recv: ";
print_message(msg);
}
char code[MSG_H_CODE_SIZE];
memcpy(code, msg, MSG_H_CODE_SIZE);
CODE code_ = (CODE) atoi(code);
switch (code_) {
case BROADCAST:
cout << "[Server] " << msg + MSG_HEADER_SIZE << endl;
break;
case CHAT:
cout << "[" << msg + MSG_H_CODE_SIZE << "] " << msg + MSG_HEADER_SIZE << endl;
break;
case QUIT:
cout << "[Server] Service interrupted." << endl;
running = false;
break;
case USERS:
cout << "Logged users:" << endl << msg + MSG_HEADER_SIZE << endl;
break;
case ERROR:
cout << "[Server] " << msg + MSG_HEADER_SIZE << endl;
break;
case INFO:
cout << "[Server] " << msg + MSG_HEADER_SIZE << endl;
break;
default:
break;
}
}
void sender(){
while (running){
print_info_message();
string line;
if(input_available(0))
getline(cin,line);
else break;
if(line.compare("chat") == 0 ) client_chat();
else if(line.compare("users") == 0 ) client_users();
else if(line.compare("quit") == 0 ) client_quit();
else if(line.compare("video") == 0 ) client_video();
else if(line.compare("audio") == 0 ) client_audio();
else cout << "Unknown command." << endl;
}
}
bool input_available(int fd) {
fd_set set;
FD_ZERO(&set); // clear set
FD_SET(fd, &set); // add server descriptor on set
// set timeout
timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
while (running){
int ret = select( fd + 1, &set, NULL, NULL, &timeout);
if(ret < 0) {
if(errno == EINTR) continue;
perror("Error during select operation: ");
exit(EXIT_FAILURE); // error
}else if(ret == 0) {
// timeout occurred
timeout.tv_sec = 1;
FD_ZERO(&set); // clear set
FD_SET(fd, &set);
// timeout occurred
if(!running) break;
}else return true; // input available
}
return false;
}
void client_audio() {
}
/* Signal Handler for SIGINT */
void sigintHandler(int sig_num)
{
// signal(SIGINT, sigintHandler);
cout << endl <<"Catch Ctrl+C - System status: " << client_status << endl;
switch (client_status) {
case CODE::AUTHENTICATION:
cout << "Authentication stopped." << endl;
exit(EXIT_SUCCESS);
default:
cout << "Service Stopped." << endl;
client_quit();
break;
}
}
// Initialization
void udp_close() {
// close the descriptor
int ret = close(udp_socket);
if(ret < 0){
perror("Error during close operation");
exit(EXIT_FAILURE);
}
}
void udp_init() {
struct sockaddr_in server_address;
// clear servaddr
bzero(&server_address, sizeof(server_address));
server_address.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);
server_address.sin_port = htons(SERVER_CHAT_PORT);
server_address.sin_family = AF_INET;
// create datagram socket
udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
// connect to server
if(connect(udp_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0)
{
printf("\n Error : Connect Failed \n");
exit(0);
}
}
void signal_handler_init() {
/* signal handler */
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = sigintHandler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0; //SA_RESTART
sigaction(SIGINT, &sigIntHandler, NULL);
/* end signal handler */
}