-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
654 lines (614 loc) · 18.6 KB
/
client.cpp
File metadata and controls
654 lines (614 loc) · 18.6 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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#include <vector>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unordered_map>
#include <set>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <regex>
#include <iterator>
#include <map>
/*************Macro Definitions*************/
using namespace std;
static void sig_int(int);
vector<string> read_File(fstream&);
vector<string> parse_String(string line,char delimiter);
int connectToRemoteMachine(const char *ip, int port,bool exitIferror);
int serverPort = 5100;
void parseServerMessage(int sockfd, char *buf);
void *processServerConection(void *arg);
void readFromSocket(int sockfd);
void *acceptPeerConnection(void *arg);
void startReadThreadForSocket(int sockfd);
vector<string> parseBufferstring(string bufferString);
void logoutFromServer(bool showpromt);
void *process_connection(void *arg);
string username="";
bool checkStringForMessage(string bufferString, int choice);
void printOnlineFriend();
int serverSockfd;
bool loginflag=false;
string originalUser="";
vector<int> peers;
set <string> inviterUsers;
class UserFriend
{
public:
int sockfd=-1;
int listeningPort=-1;
string username;
string address;
UserFriend()
{
listeningPort=-1;
sockfd=-1;
username="";
address="";
}
UserFriend(string friendUsername,string friendIp,int friendPort)
{
username=friendUsername;
address=friendIp;
listeningPort=friendPort;
}
void setSockfd(int socket){
sockfd = socket;
}
void setListeningPort(int port){
listeningPort = port;
}
void setUsername(string friendUsername){
username = friendUsername;
}
void setAddress(string friendIp){
address = friendIp;
}
int getSockfd(){
return(sockfd);
}
int getListeningPort(){
return(listeningPort);
}
string getUsername(){
return(username);
}
string getAddress(){
return(address);
}
};
map<string,UserFriend> mapOfFriends;
map<string,UserFriend> onlineFriends;
int main(int argc, char * argv[])
{
onlineFriends.clear();
int servport,*sock_ptr;
string bufferString,password,payloadString,servhost;
pthread_t tid;
fstream userFileFd;
struct sigaction abc;
abc.sa_handler = sig_int;
sigemptyset(&abc.sa_mask);
abc.sa_flags = 0;
sigaction(SIGINT, &abc, NULL);
if (argc < 2)
{
printf("Please provide client configuration file\n");
exit(0);
}
vector<string> clientConfigFileLines;
fstream clientFileFd;
clientFileFd.open(argv[1],ios::in);
if(!clientFileFd){//Checking if the file containing the User Information is opened properly.
cout<<"Error: Error in openinf the file";
}else{
clientConfigFileLines = read_File(clientFileFd);
}
clientFileFd.close();
vector<string>::iterator it;
for(it=clientConfigFileLines.begin();it != clientConfigFileLines.end(); it++){
vector<string> mystring;
mystring = parse_String(*it,':');
if((mystring.at(0).compare("Serverhost"))==0){
servhost=mystring.at(1);
}
if((mystring.at(0).compare("Port"))==0){
servport=stoi(mystring.at(1));
serverPort=servport;
}
}
if(argc == 3){
serverPort=stoi(argv[2]);
}
serverSockfd=connectToRemoteMachine(servhost.c_str(),servport,true);
sock_ptr = (int *)malloc(sizeof(int));
*sock_ptr = serverSockfd;
cout<<"For registration press r and then press enter"<<endl;
cout<<"For login press l and then press enter"<<endl;
pthread_create(&tid, NULL, &processServerConection, (void*)sock_ptr);
while(getline(cin,bufferString)){
if((bufferString.compare("r")==0)||(bufferString.compare("l")==0)){
int flag=0;
cout<<"Please enter username and password"<<endl;
cout<<"Username and password should be one word"<<endl;
string password;
cout<<"Username:"<<endl;
getline(cin,username);
cout<<"Password:"<<endl;
cin>>password;
string messagepayload;
messagepayload.clear();
if((bufferString.compare("r")==0)){
messagepayload = string("register")+'~'+string("username")+':'+username+'|'+string("password")+':'+password;
}
else if((bufferString.compare("l")==0)){
if(loginflag){
flag=1;
}
messagepayload = string("login")+'~'+string("username")+':'+username+'|'+string("password")+':'+password;
}
if((bufferString.compare("r")==0)||((bufferString.compare("l")==0)&&(!flag))){
write(serverSockfd, messagepayload.c_str(), strlen(messagepayload.c_str())+1);
}
}
else if((bufferString.compare("logout")==0)||(bufferString.compare("exit")==0)){
string response;
response.clear();
for(auto itr = peers.begin(); itr != peers.end(); itr++)
{
int fd;
fd = *itr;
close(fd);
}
onlineFriends.clear();
response=string("logout")+'~'+string("username")+':'+originalUser;
write(serverSockfd,response.c_str(),strlen(response.c_str())+1);
loginflag=false;
if(bufferString.compare("logout"))
{
cout<<"'r' for Registration"<<endl;
cout<<"'l' for login"<<endl;
}
}
else{
if(loginflag){
if(checkStringForMessage(bufferString,1))
{
vector<string> input;
string type;
string forUser;
string message;
message="";
input = parseBufferstring(bufferString);
if(input.at(0)!=""){
type = input.at(0);
}
if(input.at(1)==""){
forUser="";
cout<<"Please kindly enter the username format is m username message"<<endl;
continue;
}
else{
forUser=input.at(1);
}
for(unsigned int i=2; i < input.size(); i++){
message=message+input.at(i)+' ';
}
cout<<"Type :"<<type<<" For user: "<<forUser<<" Message: "<<message<<endl;
auto it=onlineFriends.find(forUser);
if(it == onlineFriends.end()){
cout<<"The friend of the user is not online currently"<<endl;
}
else
{
UserFriend User=it->second;
int socket=User.getSockfd();
cout<<User.listeningPort<<endl;
string response;
response=string("message")+'~'+string("fromuser")+':'+originalUser+'|'+string("touser")+':'+forUser+'|'+string("message")+':'+message;
cout<<response<<endl;
write(serverSockfd,response.c_str(),strlen(response.c_str())+1);
}
}
else if(checkStringForMessage(bufferString,2))
{
vector<string> input;
string type;
string forUser;
string message;
message = "";
input = parseBufferstring(bufferString);
if(input.at(0)!=""){
type = input.at(0);
}
if(input.at(1)==""){
forUser="";
cout<<"Please kindly enter the username format is i username message"<<endl;
continue;
}
else{
forUser=input.at(1);
}
for(unsigned int i=2; i < input.size(); i++){
message=message+input.at(i)+' ';
}
cout<<"Type :"<<type<<" For user: "<<forUser<<" Message: "<<message<<endl;
string response;
response.clear();
response = string("invite")+'~'+string("fromuser")+':'+username+'|'+string("touser")+':'+forUser+'|'+string("message")+':'+message;
cout<<response<<endl;
write(serverSockfd, response.c_str(), strlen(response.c_str())+1);
response.clear();
}
else if(checkStringForMessage(bufferString,3))
{
cout<<"In invite accept"<<endl;
vector<string> input;
string type;
string forUser;
string message;
message = "";
input = parseBufferstring(bufferString);
if(input.at(0)!=""){
type = input.at(0);
}
if(input.at(1)==""){
forUser="";
cout<<"Please kindly enter the username format is ia username message"<<endl;
continue;
}
else{
forUser=input.at(1);
}
for(unsigned int i=2; i < input.size(); i++){
message=message+input.at(i)+' ';
}
auto itr=inviterUsers.find(forUser);
if(itr==inviterUsers.end())
{
cout<<"No invitation found from "<<forUser<<endl;
continue;
}
string response;
response.clear();
response = string("accept_invite")+'~'+string("fromuser")+':'+username+'|'+string("touser")+':'+forUser+'|'+string("message")+':'+message;
cout<<"Response is:" <<response<<endl;
write(serverSockfd, response.c_str(), strlen(response.c_str())+1);
response.clear();
}
}
else{
cout<<"Login from a user before sending message, invite or invite accept"<<endl;
}
}
bufferString.clear();
}
}
vector<string> read_File(fstream& fileHandler){
vector<string> fileLines;
string line;
while(getline(fileHandler,line)){
fileLines.push_back(line);
}
return fileLines;
}
vector<string> parse_String(string line,char delimiter){
vector<string> wordVector;
stringstream stringStream(line);
string token;
while(getline(stringStream, token, delimiter)){
wordVector.push_back(token);
}
return wordVector;
}
int connectToRemoteMachine(const char *ip, int port,bool exitIferror)
{
int sockfd=-1;
int rv, flag;
struct addrinfo hints, *res, *ressave;
bzero(&hints, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo(ip, to_string(port).c_str() , &hints, &res)) != 0)
{
cout << "getaddrinfo wrong: " << gai_strerror(rv) << endl;
if(exitIferror)
{
exit(1);
}
return sockfd;
}
ressave = res;
flag = 0;
do
{
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd < 0)
continue;
if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
{
flag = 1;
break;
}
close(sockfd);
}
while ((res = res->ai_next) != NULL);
freeaddrinfo(ressave);
if (flag == 0)
{
fprintf(stderr, "cannot connect\n");
if(exitIferror)
{
exit(1);
}
}
return sockfd;
}
void *processServerConection(void *arg)
{
int n;
int sockfd;
char buf[4096];
sockfd = *((int *)arg);
free(arg);
pthread_detach(pthread_self());
while (1)
{
n = read(sockfd, buf, 4096);
if (n == 0)
{
printf("Server Crashed!!!\n");
close(sockfd);
exit(0);
}
parseServerMessage(sockfd, buf);
}
}
void parseServerMessage(int sockfd, char *buf){
string sbuf(buf);
stringstream message(sbuf);
string messageBody,response;
getline(message,messageBody,'~');
if(messageBody.compare("register")==0)
{
getline(message,messageBody,'~');
if(messageBody.compare("200")==0)
{
cout<<"Registration successful"<<endl;
}
else
{
cout<<"Registration failed. Please try again"<<endl;
}
}
else if(messageBody.compare("login")==0)
{
getline(message,messageBody,'~');
if(messageBody.compare("200")==0)
{
stringstream myport;
myport<<serverPort;
loginflag=true;
originalUser = username;
string response;
response.clear();
cout<<"******************************************************************************************************************"<<endl;
cout<<"******************************************************************************************************************"<<endl;
cout<<"******************************************************************************************************************"<<endl;
cout<<"******************************************************************************************************************"<<endl;
cout<<"******************************************************************************************************************"<<endl;
cout<<"***********************************************Login successful***************************************************"<<endl;
cout<<"**************************************'m 'username' 'message' to send message*************************************"<<endl;
cout<<"*************************************'i' 'username' 'message' to invite to chat***********************************"<<endl;
cout<<"************************************'ia 'username' 'message' to accept invitation*********************************"<<endl;
cout<<"************************************'logout' 'username' to logout from the server********************************"<<endl;
cout<<"******************************************************************************************************************"<<endl;
cout<<"******************************************************************************************************************"<<endl;
cout<<"******************************************************************************************************************"<<endl;
cout<<"******************************************************************************************************************"<<endl;
cout<<"******************************************************************************************************************"<<endl;
cout<<"||||||||||||||||||||||||N.B.--The client will terminate with SIGNAL if wrong input format is given||||||||||||||||"<<endl;
int serv_sockfd;
struct sockaddr_in serv_addr;
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(serverPort);
bind(serv_sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
listen(serv_sockfd, 5);
int * sock_ptr;
sock_ptr = (int *)malloc(sizeof(int));
*sock_ptr = serv_sockfd;
response = string("location")+'~'+string("username")+':'+username+'|'+string("port")+':'+(myport.str());
myport.clear();
myport.str("");
write(sockfd,response.c_str(),strlen(response.c_str())+1);
}
else
{
cout<<"Login failed"<<endl;
}
}
else if(messageBody.compare("location")==0)
{
getline(message,messageBody,'~');
stringstream mymessagestream(messageBody);
string firstpart, secondpart, thirdpart;
getline(mymessagestream,firstpart,'|');
getline(mymessagestream,secondpart,'|');
getline(mymessagestream,thirdpart,'|');
stringstream firstPartStream(firstpart);
stringstream secondPartStream(secondpart);
stringstream thirdPartStream(thirdpart);
string usernameTag,myusername,ipaddressTag,myipaddress,portTag,myport;
getline(firstPartStream,usernameTag,':');
getline(secondPartStream,portTag,':');
getline(thirdPartStream,ipaddressTag,':');
if(usernameTag.compare("username")==0)
{
getline(firstPartStream,myusername,':');
}
if(ipaddressTag.compare("ip")==0)
{
getline(thirdPartStream,myipaddress,':');
}
if(portTag.compare("port")==0)
{
getline(secondPartStream,myport,':');
}
cout<<"Friend:"<<myusername<<" is online"<<endl;
mymessagestream.clear();
firstPartStream.clear();
secondPartStream.clear();
thirdPartStream.clear();
mymessagestream.str("");
firstPartStream.str("");
secondPartStream.str("");
thirdPartStream.str("");
UserFriend onlineFriend(myusername,myipaddress,stoi(myport));
onlineFriends.insert({myusername,onlineFriend});
}
else if(messageBody.compare("invite")==0)
{
getline(message,messageBody,'~');
stringstream mymessagestream(messageBody);
string firstpart,secondpart,thirdpart,fromUserTag,fromuser,toUserTag,touser,messageTag,mymessage;
getline(mymessagestream,firstpart,'|');
getline(mymessagestream,secondpart,'|');
getline(mymessagestream,thirdpart,'|');
stringstream firstPartStream(firstpart);
stringstream secondPartStream(secondpart);
stringstream thirdPartStream(thirdpart);
getline(firstPartStream,fromUserTag,':');
getline(secondPartStream,toUserTag,':');
getline(thirdPartStream,messageTag,':');
if(fromUserTag.compare("fromuser")==0)
{
getline(firstPartStream,fromuser,':');
}
if(toUserTag.compare("toUserTag")==0)
{
getline(secondPartStream,touser,':');
}
if(messageTag.compare("message")==0)
{
getline(thirdPartStream,mymessage,':');
}
cout<<"You have received an invitation from "<<fromuser<<endl;
cout<<"Message is : "<<mymessage<<endl;
auto it=inviterUsers.find(fromuser);
if(it==inviterUsers.end())
{
inviterUsers.insert(fromuser);
}
mymessagestream.clear();
mymessagestream.str("");
firstPartStream.clear();
firstPartStream.str("");
secondPartStream.clear();
secondPartStream.str("");
thirdPartStream.clear();
thirdPartStream.str("");
}
else if(messageBody.compare("mesg")==0)
{
getline(message,messageBody,'~');
stringstream mymessagestream(messageBody);
string firstpart,secondpart,fromUserTag,fromuser,messageTag,mymessage;
getline(mymessagestream,firstpart,'|');
getline(mymessagestream,secondpart,'|');
stringstream firstPartStream(firstpart);
stringstream secondPartStream(secondpart);
getline(firstPartStream,fromUserTag,':');
getline(secondPartStream,messageTag,':');
if(fromUserTag.compare("fromuser")==0)
{
getline(firstPartStream,fromuser,':');
}
if(messageTag.compare("message")==0)
{
getline(secondPartStream,mymessage,':');
}
cout<<"Message from"<<fromuser<<" is : "<<mymessage<<endl;
mymessagestream.clear();
mymessagestream.str("");
firstPartStream.clear();
firstPartStream.str("");
secondPartStream.clear();
secondPartStream.str("");
}
message.clear();
message.str("");
}
static void sig_int(int signo){
string response;
response.clear();
for(auto itr = peers.begin(); itr != peers.end(); itr++)
{
int fd;
fd = *itr;
close(fd);
}
onlineFriends.clear();
printOnlineFriend();
response=string("logout")+'~'+string("username")+':'+username;
write(serverSockfd,response.c_str(),strlen(response.c_str())+1);
loginflag=false;
close(serverSockfd);
exit(0);
}
bool checkStringForMessage(string bufferString, int choice){
if(choice == 1){
if(bufferString[0]=='m'){
return true;
}
}
else if(choice == 2){
if((bufferString[0]=='i')&&(bufferString[1]!='a')){
return true;
}
}
else if(choice == 3){
if((bufferString[0]=='i')&&(bufferString[1]=='a')){
return true;
}
}
return false;
}
vector<string> parseBufferstring(string bufferString){
stringstream mystream;
vector<string> mytokens;
mystream.clear();
mystream.str(bufferString);
string temp;
while(getline(mystream,temp,' ')){
mytokens.push_back(temp);
temp.clear();
}
mystream.clear();
mystream.str("");
return mytokens;
}
void printOnlineFriend(){
for(auto it = onlineFriends.begin();it!= onlineFriends.end(); it++){
cout<<"it->first"<<it->first<<endl;
}
}