-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.cpp
422 lines (354 loc) · 11.9 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* COP5570 | Parallel, Concurrent, Distributed Programming
* Asg #4 | Tic Tac Toe Game Server
* Summer C | 07/24/15
*
* by Adam Stallard, Steven Rohr
*
*/
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <sstream>
#include "talk.h"
#include "archive.h"
#define _read(a, b, c) if(read((a), (b), (c)))
#define _write(a, b, c) if(write((a), (b), (c)))
using namespace std;
const unsigned MAXBUFLEN = 512;
pthread_mutex_t accept_lock = PTHREAD_MUTEX_INITIALIZER;
extern pthread_mutex_t games_lock;
extern pthread_mutex_t users_lock;
int serv_sockfd;
extern bool Parse(string, user&);
extern vector<user> users;
static bool exit_sequence = false;
static int *cli_sockfds;
string ClientGet(int cli_sockfd) {
int n;
string str = "";
char buf[512];
while((n = read(cli_sockfd, buf, MAXBUFLEN)) > 0) {
if (buf[n] == '\4') {
buf[n] = '\0';
str += buf;
return str;
}
else
str += buf;
}
return str;
}
void *scheduled_maintenance(void *arg) {
time_t stopwatch = 0, abs_time = time(NULL);
cerr << "Server maintenance thread initiated.\n";
do {
pthread_mutex_lock(&users_lock);
if (exit_sequence) break;
pthread_mutex_unlock(&users_lock);
stopwatch += difftime(time(NULL), abs_time);
abs_time = time(NULL);
if (stopwatch > 300) {
pthread_mutex_lock(&users_lock);
cerr << "Initiating scheduled server backup.\n";
save_server();
cerr << "Backup complete.\n";
pthread_mutex_unlock(&users_lock);
stopwatch = 0;
}
sleep(5);
} while (1);
cerr << "Data management thread exited.\n";
pthread_exit(0);
}
void *one_thread(void *arg) {
int & cli_sockfd = cli_sockfds[*((int *)arg)];
struct sockaddr_in cli_addr;
socklen_t sock_len;
int tid = *((int *)arg);
free(arg);
ssize_t n = 0;
char buf[MAXBUFLEN];
string msg;
for (;;) {
sock_len = sizeof(cli_addr);
pthread_mutex_lock(&accept_lock);
cli_sockfd = accept(serv_sockfd, (struct sockaddr *)&cli_addr, &sock_len);
pthread_mutex_unlock(&accept_lock);
cout << "\nthread " << tid << ": \n"
<< "remote client IP == " << inet_ntoa(cli_addr.sin_addr)
<< ", port == " << ntohs(cli_addr.sin_port) << "\n\n";
user * usr = NULL;
stringstream ss;
string uname, psswrd, cmd = "";
bool nousr = true, logout = false, loggedin = false;
do {
pthread_mutex_lock(&users_lock);
if (exit_sequence) break;
pthread_mutex_unlock(&users_lock);
if (loggedin) {
cout << "Thread " << tid << ": Recieved " << n
<< " characters from user " << usr->name << endl << flush;
cout << "Thread " << tid
<< ": User is logged in. Awaiting input.\n";
pthread_mutex_lock(&users_lock);
for (unsigned int i = 0; i < users.size(); i++)
if (users[i].name == uname) {
usr = &users[i];
break;
}
pthread_mutex_unlock(&users_lock);
// Logged in user talking to us
if (usr != NULL) {
if (buf[n - 1] == '\4') {
buf[n] = '\0';
// Commit message
cmd += buf;
cmd[cmd.size() - 1] = '\0'; // Overwrite EOT with NULL so that printf doesn't have a stroke.
logout = Parse(cmd, *usr);
cmd = "";
memset(buf, 0, strlen(buf));
}
else {
buf[n] = '\0';
//Portion of message
cmd += buf;
memset(buf, 0, strlen(buf));
}
if (logout) break;
}
}
else {
cout << "Thread " << tid << ": User attempting to log in!\n";
n = read(cli_sockfd, buf, MAXBUFLEN);
if (n > 0)
buf[n] = '\0';
else {
cerr << "User exited before logging in.\n";
break;
}
uname = buf;
uname.pop_back();
memset(buf, 0, strlen(buf));
if (uname.compare("guest") != 0) {
// Login - check password
msg = "Password: ";
_write(cli_sockfd, msg.c_str(), strlen(msg.c_str()));
cout << "Client attempting login.\n";
n = read(cli_sockfd, buf, MAXBUFLEN);
if (n > 0)
buf[n] = '\0';
else {
cerr << "Error reading from client. Closing connection\n";
break;
}
buf[n] = '\0';
psswrd = buf;
psswrd = psswrd.substr(0, psswrd.size() - 1);
memset(buf, 0, strlen(buf));
pthread_mutex_lock(&users_lock);
for (unsigned int i = 0; i < users.size() && !loggedin; i++) {
if (users[i].name == uname) {
nousr = false;
if (users[i].passwd == psswrd) {
usr = &users[i];
msg = "You are now logged in as " + uname + "\n";
msg += usr->mail_meta();
users[i].cli_sockfd = cli_sockfd;
users[i].online = true;
users[i].clientcounter = 0;
SendToClient(*usr, msg);
loggedin = true;
}
else {
msg = "Incorrect password.\n";
_write(cli_sockfd, msg.c_str(), strlen(msg.c_str()));
logout = true;
}
break;
}
}
pthread_mutex_unlock(&users_lock);
if (nousr) {
msg = "No such user.\n";
_write(cli_sockfd, msg.c_str(), strlen(msg.c_str()));
logout = true;
}
}
else {
cout << "Registration sequence.\n";
msg = "You are now in registration mode.\n";
_write(cli_sockfd, msg.c_str(), strlen(msg.c_str()));
while (!logout && (n = read(cli_sockfd, buf, MAXBUFLEN)) > 0) {
cout << "User entered: \"" << buf << "\"\n";
//Registration
if (buf[n - 1] == '\4') {
buf[n] = '\0';
//Commit message
cmd += buf;
cmd[cmd.size() - 1] = '\0'; //Overwrite EOT with NULL so that printf doesn't have a stroke.
vector<string> v = Split(cmd);
for (unsigned int i = 0; i < v.size(); i++)
cout << "v[" << i << "] = " << v[i] << " (" << v[i].size() << ")\n";
if (v[0] == "register") {
cout << "User attempting registration.\n";
bool okay = true;
if (v.size() != 3) {
okay = false;
msg = "Format: register <username> <password>\n";
_write(cli_sockfd, msg.c_str(), strlen(msg.c_str()));
}
else {
if (v[1].size() == 0 || v[1].size() > 15) {
okay = false;
msg = "Username must be between 1 and 15 characters.\n";
_write(cli_sockfd, msg.c_str(), strlen(msg.c_str()));
}
if (v[2].size() == 0 || v[2].size() > 20) {
okay = false;
msg = "Password must be between 1 and 20 characters.\n";
_write(cli_sockfd, msg.c_str(), strlen(msg.c_str()));
}
}
if (okay) {
pthread_mutex_lock(&users_lock);
for (unsigned int i = 0; i < users.size(); i++){
if (users[i].name == v[1]){
okay = false;
msg = "Username has been taken. Please try another.\n";
_write(cli_sockfd, msg.c_str(), strlen(msg.c_str()));
break;
}
}
if (okay) {
user u;
u.name = v[1];
u.passwd = v[2];
u.online = false;
users.push_back(u);
msg = "You have been registered. You may now exit and login.\n";
_write(cli_sockfd, msg.c_str(), strlen(msg.c_str()));
}
pthread_mutex_unlock(&users_lock);
}
}
else if (v[0] == "quit" || v[0] == "exit" || v[0] == "bye") {
msg = "Goodbye.\r\n";
_write(cli_sockfd, msg.c_str(), strlen(msg.c_str()));
logout = true;
}
else if (v[0] == "?" || v[0] == "help") {
cout << "User requesting help.\n";
msg = "Register: register <username> <password>\nQuit: [quit|exit]\n";
_write(cli_sockfd, msg.c_str(), strlen(msg.c_str()));
}
else if (v[0] == "fuck" || v[0] == "shit") {
msg = "Don't swear!\r\n";
_write(cli_sockfd, msg.c_str(), strlen(msg.c_str()));
}
else {
msg = "Invalid command detected. In guest mode you may only register or quit.\n";
_write(cli_sockfd, msg.c_str(), strlen(msg.c_str()));
}
cmd = "";
}
else {
//Portion of message
cmd += buf;
}
memset(buf,0,strlen(buf));
//Register user v[1] with password v[2]
}
}
}
pthread_mutex_lock(&users_lock);
if (exit_sequence) break;
pthread_mutex_unlock(&users_lock);
} while (loggedin && !logout && ((n = read(cli_sockfd, buf, MAXBUFLEN)) > 0));
if (n == 0) {
if (usr != NULL) {
usr->online = false;
cout << usr->name << " has logged out.\n";
usr = NULL;
}
}
else
cerr << "Getting new connection.\n";
close(cli_sockfd);
pthread_mutex_lock(&users_lock);
if (exit_sequence) break;
pthread_mutex_unlock(&users_lock);
}
cerr << "Thread " << tid << " exited.\n";
pthread_exit(0); // happens at exit sequence
}
int main(int argc, char *argv[]) {
struct sockaddr_in serv_addr;
int port, number_thread;
pthread_t *tid;
int *tid_ptr;
string response;
if (argc != 3) {
cerr << "Usage: " << argv[0] << " <port> <num_threads>\n";
exit(1);
}
int load_res = load_server();
if (load_res) {
cout << "Would you like to continue? (y/n)\n" << flush;
getline(cin, response);
if (tolower(response[0]) == 'n')
exit(0);
}
port = atoi(argv[1]);
number_thread = atoi(argv[2]);
cout << "\nConnecting tictactoe server to port " << port << " with "
<< number_thread << " threads\n\n";
serv_sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero((void*)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);
bind(serv_sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
listen(serv_sockfd, 5);
tid = new pthread_t[number_thread + 1];
cli_sockfds = new int[number_thread];
for (int i = 0; i < number_thread; ++i) {
tid_ptr = new int;
*tid_ptr = i;
pthread_create(&tid[i], NULL, &one_thread, (void *)tid_ptr);
cout << "thread " << i << " created\n" << flush;
}
pthread_create(&tid[number_thread], NULL, &scheduled_maintenance, (void *)NULL);
for (;;) {
string line;
getline(cin, line);
if (line.compare("quit") == 0 || line.compare("exit") == 0) {
cout << "Initiating server shut down sequence.\n";
pthread_mutex_lock(&users_lock);
exit_sequence = true;
pthread_mutex_unlock(&users_lock);
break;
}
}
shutdown(serv_sockfd, SHUT_RDWR);
close(serv_sockfd);
// shut down all recv sockets
for (int i = 0; i < number_thread; ++i) {
shutdown(cli_sockfds[i], SHUT_RDWR);
close(cli_sockfds[i]);
}
// wait for client children
// for(int i = 0; i < number_thread + 1; ++i)
// pthread_join(tid[i], NULL);
// final save
save_server();
return 0;
}