-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
862 lines (825 loc) · 25 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
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
/************************************HEADER FILES*************************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netdb.h>
#include <string>
#include <unistd.h>
#include <list>
#include <iterator>
#include <arpa/inet.h>
#include <algorithm>
#include <map>
/**************************************MACROS*****************************************************/
#define BUFFER 4096
/*************************************************************************************************/
using namespace std;
/********************************GLOBAL DATA STRUCTURES*******************************************/
//map<string,User> allUsers;
//map<string,User> onlineUsers;
/*********************************FUNTION PROTOTYPES***********************************************/
vector<string> parse_String(string,char);
vector<string> read_File(fstream&);
static void signalHandler(int);
bool parseClientMessage(int sockfd,char *buf);
bool userLogin(string messageBody);
string createLoginAndRegResponseString(string opType,string statusCode);
void logoutUser(string messageBody);
string getPeerAddressFromSock(int sockfd);
/**************************************************************************************************/
class User
{
public:
string username;
string password;
int sockfd;
bool online;
int listeningPort;
string address;
vector<string> friendList;
vector<string> onlineFriendList;
vector<string> inviteList;
User(string userConfigLine)
{
string friends,s;
stringstream f(userConfigLine);
getline(f, username, '|');
getline(f, password, '|');
getline(f, friends, '|');
stringstream friendStream(friends);
while (getline(friendStream, s, ';'))
{
friendList.push_back(s);
inviteList.push_back(s);
}
online = false;
sockfd=-1;
listeningPort=-1;
address="";
onlineFriendList.clear();
inviteList.clear();
}
User(string uname, string pass)
{
username=uname;
password=pass;
friendList.clear();
onlineFriendList.clear();
sockfd=-1;
listeningPort=-1;
address="";
online=false;
inviteList.clear();
}
User()
{
username="";
password="";
friendList.clear();
onlineFriendList.clear();
sockfd=-1;
listeningPort=-1;
address="";
online=false;
inviteList.clear();
}
void setOnline(bool status){
online = status;
}
bool getOnline(){
return online;
}
void setPassword(string mypassword){
password=mypassword;
}
string getPassword(){
return password;
}
void setUsername(string myusername){
username=myusername;
}
string getUsername(){
return username;
}
void setAddress(string myaddress){
address=myaddress;
}
string getAddress(){
return address;
}
void setSockfd(int mysockfd){
sockfd=mysockfd;
}
int getSockfd(){
return sockfd;
}
void setListeningPort(int mylisteningport){
listeningPort=mylisteningport;
}
int getListeningPort(){
return listeningPort;
}
vector<string> getFriendlist(){
return friendList;
}
vector<string> getOnlineFriendlist(){
return onlineFriendList;
}
void addFriendlist(string myfriend){
int flag = 0;
auto it = friendList.begin();
while(it != friendList.end()){
if(*it == myfriend){
flag=1;
break;
}
it++;
}
if(flag == 0)
friendList.push_back(myfriend);
}
void addOnlineFriendlist(string myfriend){
int flag = 0;
auto it = onlineFriendList.begin();
while(it != onlineFriendList.end()){
if(*it == myfriend){
flag=1;
break;
}
it++;
}
if(flag == 0)
onlineFriendList.push_back(myfriend);
}
void addInvitelist(string myfriend){
int flag = 0;
auto it = inviteList.begin();
while(it != inviteList.end()){
if(*it == myfriend){
flag=1;
break;
}
it++;
}
if(flag == 0)
inviteList.push_back(myfriend);
}
void printClassVariables(){
cout<<"Username :"<<username<<endl;
cout<<"Password :"<<password<<endl;
cout<<"Socket Descriptor :"<<sockfd<<endl;
cout<<"Address :"<<address<<endl;
cout<<"Listening Port :"<<listeningPort<<endl;
cout<<"Online Status :"<<online;
cout<<"The friendlist is :"<<endl;
for(auto it = friendList.begin();it != friendList.end(); it++){
cout<<*it<<"||";
}
cout<<"The online friendlist is :"<<endl;
for(auto it = onlineFriendList.begin();it != onlineFriendList.end(); it++){
cout<<*it<<"||";
}
for(auto it = inviteList.begin();it != inviteList.end(); it++){
cout<<*it<<"||";
}
}
void userLogout(){
onlineFriendList.clear();
address.clear();
sockfd=-1;
listeningPort=-1;
online=false;
}
};
fd_set allset, rset;
int sockfd, rec_sock;
map<string,User> allUsers;
map<string,User> onlineUsers;
int main(int argc, char ** argv){
struct sigaction interruptHandler;
typedef struct hostent hostent;
interruptHandler.sa_handler = signalHandler;
sigemptyset(&interruptHandler.sa_mask);
interruptHandler.sa_flags = 0;
if((sigaction(SIGINT, &interruptHandler, NULL)) == -1){
perror("Error: Error while catching the signal :");
exit(EXIT_FAILURE);
}
if(argc < 2){//Checking for the number of Arguments.
cout<<"Wrong Arguments Entered"<<endl;
cout<<"The correct form of entering the arguments are"<<endl;
cout<<"./messenger_server user_info server_config";
exit(EXIT_FAILURE);
}
vector<string> userInfoFileLines, serverConfigFileLines;
fstream userFileFd, serverFileFd;
userFileFd.open(argv[1],ios::in);
if(!userFileFd){//Checking if the file containing the User Information is opened properly.
cout<<"Error: Error in openinf the file";
}else{
userInfoFileLines = read_File(userFileFd);
}
userFileFd.close();
auto userfileit = userInfoFileLines.begin();
while(userfileit!=userInfoFileLines.end()){//add the user in the allUser vector
User newuser(*userfileit);
allUsers.insert({newuser.username,newuser});
userfileit++;
}
serverFileFd.open(argv[2],ios::in);
if(!serverFileFd){//Checking if the file containing the Server Configuration is opened properly.
cout<<"Error: Error in openinf the file";
}else{
serverConfigFileLines = read_File(serverFileFd);
}
serverFileFd.close();
struct sockaddr_in addr, recaddr;
char buf[BUFFER];
// fd_set allset, rset;
int maxfd;
socklen_t len;
vector<string> serverConfigurationFetch;
int port;
//int sockfd, rec_sock;
serverConfigurationFetch = parse_String(serverConfigFileLines.at(0),':');
if(((serverConfigurationFetch.at(0)).compare("port") == 0) || ((serverConfigurationFetch.at(0)).compare("Port") == 0) || ((serverConfigurationFetch.at(0)).compare("PORT") == 0)){
port = stoi(serverConfigurationFetch.at(1));
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("Error: Unable to find the socket : ");
exit(EXIT_FAILURE);
}
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_family = AF_INET;
addr.sin_port = htons((short)port);
if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("Error: Error in binding the socket: ");
exit(EXIT_FAILURE);
}
char hostname[BUFFER];
hostent *host;
if(gethostname(hostname, BUFFER) == -1){
perror("Error: Unable to get hostname: ");
exit(EXIT_FAILURE);
}
if((host = gethostbyname(hostname)) == NULL){
perror("Error: Error occured while retriving the host information :");
exit(EXIT_FAILURE);
}
cout<< "Server is starting"<< flush<<endl;
cout<< "Server:"<< host->h_name<< flush<< endl;
len = sizeof(addr);
if (getsockname(sockfd, (struct sockaddr *)&addr, (socklen_t *)&len) < 0){
perror("Error: can't get name");
exit(EXIT_FAILURE);
}
printf("\nPort:%d\n", htons(addr.sin_port));
if (listen(sockfd, 5) < 0)
{
perror("Error: Bind");
exit(EXIT_FAILURE);
}
FD_ZERO(&rset);
FD_ZERO(&allset);
FD_SET(sockfd, &allset);
maxfd = sockfd;
vector<int> clientSockets;
clientSockets.clear();
while (1)
{
rset = allset;
select(maxfd+1, &rset, NULL, NULL, NULL);
if (FD_ISSET(sockfd, &rset))
{
len = sizeof(recaddr);
if ((rec_sock = accept(sockfd, (struct sockaddr *)(&recaddr), &len)) < 0)
{
if (errno == EINTR)
continue;
else{
perror("Error: Connection accept error");
exit(EXIT_FAILURE);
}
}
if (rec_sock < 0)
{
perror("Error: Accept");
exit(EXIT_FAILURE);
}
printf("Connected Remote Machine = %s | Port = %d.\n",inet_ntoa(recaddr.sin_addr), ntohs(recaddr.sin_port));
clientSockets.push_back(rec_sock);
FD_SET(rec_sock, &allset);
if (rec_sock > maxfd){
maxfd = rec_sock;
}
}
vector<int>::iterator it;
it = clientSockets.begin();
while (it != clientSockets.end())
{
int num, fd;
fd = *it;
if (FD_ISSET(fd, &rset))
{
num = read(fd, buf, BUFFER);
if (num == 0)
{
close(fd);
FD_CLR(fd, &allset);
it = clientSockets.erase(it);
User user;
auto itra=onlineUsers.begin();
while(itra!=onlineUsers.end())
{
user=itra->second;
if(user.sockfd==fd)
{
user.setOnline(false);
itra=onlineUsers.erase(itra);
cout<<"Current Online Users:"<<onlineUsers.size()<<endl;
break;
}
++itra;
}
continue;
}
else
{
bool islogout=parseClientMessage(fd,buf);
if(islogout)
{
// continue;
}
}
}
++it;
}
maxfd = sockfd;
if (!clientSockets.empty())
{
maxfd = max(maxfd, *max_element(clientSockets.begin(), clientSockets.end()));
}
}
}
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;
}
bool parseClientMessage(int sockfd,char *buf)
{
string sbuf(buf);
stringstream message(sbuf);
string messageBody;
getline(message,messageBody,'~');
if(messageBody.compare("login")==0)
{
int flag = 0;
string mystring,mystringuser,mystringpass,username,password,response,bodyMsg;
getline(message,bodyMsg,'~');
stringstream mymessageBody(bodyMsg);
getline(mymessageBody,mystringuser,'|');
stringstream valueUser(mystringuser);
getline(mymessageBody,mystringpass,'|');
stringstream valuepass(mystringpass);
getline(valueUser,mystring,':');
if(mystring.compare("username")==0)
{
getline(valueUser,username,':');
}
getline(valuepass,mystring,':');
if(mystring.compare("password")==0)
{
getline(valuepass,password,':');
}
mymessageBody.clear();
valuepass.clear();
valueUser.clear();
mymessageBody.str("");
valuepass.str("");
valueUser.str("");
auto it=allUsers.find(username);
if(it != allUsers.end())
{
User user=it->second;
if(user.password.compare(password)==0)
{
auto findinonlinelist=onlineUsers.find(username);
if(findinonlinelist == onlineUsers.end()){
if(!(user.getOnline())){
user.setOnline(true);
}
onlineUsers.insert({user.username,user});
flag = 1 ;
}
}
it->second = user;
}
if(flag)
{
response = string("login")+'~'+string("200");
cout<<"Login success"<<endl;
cout<<"Number of online users: "<<onlineUsers.size();
}
else
{
response = string("login")+'~'+string("500");
cout<<"Login failed"<<endl;
}
write(sockfd,response.c_str(),strlen(response.c_str())+1);
message.clear();
message.str("");
}
else if(messageBody.compare("register")==0)
{
int flag = 0;
string mystring,mystringuser,mystringpass,username,password,response,bodyMsg;
getline(message,bodyMsg,'~');
stringstream mymessageBody(bodyMsg);
getline(mymessageBody,mystringuser,'|');
stringstream valueUser(mystringuser);
getline(mymessageBody,mystringpass,'|');
stringstream valuepass(mystringpass);
getline(valueUser,mystring,':');
if(mystring.compare("username")==0)
{
getline(valueUser,username,':');
}
getline(valuepass,mystring,':');
if(mystring.compare("password")==0)
{
getline(valuepass,password,':');
}
mymessageBody.clear();
valuepass.clear();
valueUser.clear();
mymessageBody.str("");
valuepass.str("");
valueUser.str("");
for(auto it=allUsers.begin();it!=allUsers.end();it++){
if(username == it->first)
flag=1;
}
if(!flag)
{
cout<<"Registration Successful"<<endl;
User user(username,password);
allUsers.insert({user.username,user});
response=string("register")+'~'+string("200");
}
else
{
response=string("register")+'~'+string("500");
cout<<"Registration Failed"<<endl;
}
write(sockfd,response.c_str(),strlen(response.c_str())+1);
message.clear();
message.str("");
}
else if(messageBody.compare("location")==0)
{
string bodyOfMsg,firstPart,secondPart,usernametag,username,porttag,port;
getline(message,bodyOfMsg,'~');
stringstream mymessage(bodyOfMsg);
getline(mymessage,firstPart,'|');
getline(mymessage,secondPart,'|');
stringstream mypartfirst(firstPart);
getline(mypartfirst,usernametag,':');
if(usernametag.compare("username")!=0){
cout<<"Error: In fetching the username location"<<endl;
}
else{
getline(mypartfirst,username,':');
}
stringstream mypartsecond(secondPart);
getline(mypartsecond,porttag,':');
if(porttag.compare("port")!=0){
cout<<"Error: In fetching the port location"<<endl;
}
else{
getline(mypartsecond,port,':');
}
mymessage.clear();
mymessage.str("");
mypartfirst.clear();
mypartfirst.str("");
mypartsecond.clear();
mypartsecond.str("");
string myipaddress;
myipaddress.clear();
myipaddress=getPeerAddressFromSock(sockfd);
string response;
response.clear();
response=string("location")+'~'+"username"+':'+username+'|'+"port"+':'+port+'|'+"ip"+':'+myipaddress;
auto it = allUsers.find(username);
if(it != allUsers.end()){
User currentuser;
currentuser=it->second;
if((currentuser.username.compare(username))==0){
currentuser.setListeningPort(stoi(port));
currentuser.setAddress(myipaddress);
currentuser.setSockfd(sockfd);
}
auto myitr = currentuser.inviteList.begin();
while(myitr != currentuser.inviteList.end()){
auto findfriend = onlineUsers.find(*myitr);
if(findfriend == onlineUsers.end()){
myitr = currentuser.inviteList.erase(myitr);
continue;
}
myitr++;
}
for(auto frienditr = currentuser.friendList.begin(); frienditr != currentuser.friendList.end(); frienditr++){
auto findfriend = onlineUsers.find(*frienditr);
if(findfriend != onlineUsers.end()){//Searching all the friend who are currently online
currentuser.addOnlineFriendlist(*frienditr);
User senduser;
senduser = findfriend->second;
int friendsocket = senduser.getSockfd();
write(friendsocket,response.c_str(),strlen(response.c_str())+1);
}
}
it->second = currentuser;
auto iter = onlineUsers.find(username);
if(iter != onlineUsers.end()){
iter->second=currentuser;
}
for(auto myiter = currentuser.friendList.begin(); myiter != currentuser.friendList.end(); myiter++){
auto findfriend = onlineUsers.find(*myiter);
if(findfriend != onlineUsers.end()){//Searching all the friend who are currently online
User myfriend=findfriend->second;
string myresponse;
myresponse.clear();
string myusername = myfriend.getUsername();
int myport = myfriend.getListeningPort();
string myaddress = myfriend.getAddress();
stringstream myportstream;
myportstream.clear();
myportstream.str("");
myportstream<<myport;
myresponse=string("location")+'~'+"username"+':'+myusername+'|'+"port"+':'+myportstream.str()+'|'+"ip"+':'+myaddress;
write(sockfd,myresponse.c_str(),strlen(myresponse.c_str())+1);
}
}
}
message.clear();
message.str("");
}
else if((messageBody.compare("invite")==0)||(messageBody.compare("accept_invite")==0))
{
cout<<"In invite accpt"<<endl;
string bodyOfMsg,firstPart,secondPart,thirdPart,toUserTag,toUser,fromUser,fromUserTag,sendMessageTag,sendMessage;
getline(message,bodyOfMsg,'~');
stringstream msgStream(bodyOfMsg);
getline(msgStream,firstPart,'|');
getline(msgStream,secondPart,'|');
getline(msgStream,thirdPart,'|');
stringstream firstPartSream(firstPart);
stringstream secondPartSream(secondPart);
stringstream thirdPartSream(thirdPart);
getline(firstPartSream,fromUserTag,':');
getline(firstPartSream,fromUser,':');
getline(secondPartSream,toUserTag,':');
getline(secondPartSream,toUser,':');
getline(thirdPartSream,sendMessageTag,':');
getline(thirdPartSream,sendMessage,':');
msgStream.clear();
msgStream.str("");
firstPartSream.clear();
firstPartSream.str("");
secondPartSream.clear();
secondPartSream.str("");
thirdPartSream.clear();
thirdPartSream.str("");
string response;
response.clear();
if(messageBody.compare("invite")==0){
response=string("invite")+'~'+string("fromuser")+':'+fromUser+'|'+string("touser")+':'+toUser+'|'+"message"+':'+sendMessage;
}
else if(messageBody.compare("accept_invite")==0){
response=string("accept_invite")+'~'+string("fromuser")+':'+fromUser+'|'+string("touser")+':'+toUser+'|'+"message"+':'+sendMessage;
}
cout<<"Resp: "<<response<<endl;
auto findfriend = onlineUsers.find(toUser);
if(findfriend == onlineUsers.end()){//Searching all the friend who are currently online
cout<<"The invited person is not online"<<endl;
}else{
if(messageBody.compare("invite")==0){
User senduser;
senduser = findfriend->second;
int friendsocket = senduser.getSockfd();
write(friendsocket,response.c_str(),strlen(response.c_str())+1);
}
else if(messageBody.compare("accept_invite")==0){
string inviterResponse, inviteeResponse;
int inviterSocket, inviteeSocket;
auto it=onlineUsers.find(toUser);
if(it!=onlineUsers.end())
{
User inviter=it->second;
inviter.friendList.push_back(fromUser);
inviter.addOnlineFriendlist(fromUser);
inviter.addInvitelist(fromUser);
it->second=inviter;
inviterSocket = inviter.sockfd;
string myusername = inviter.username;
string myaddress = inviter.address;
int myport = inviter.listeningPort;
stringstream myportstream;
myportstream.clear();
myportstream.str("");
myportstream<<myport;
inviterResponse=string("location")+'~'+"username"+':'+myusername+'|'+"port"+':'+myportstream.str()+'|'+"ip"+':'+myaddress;
myportstream.clear();
myportstream.str("");
write(inviter.sockfd,response.c_str(),strlen(response.c_str())+1);
}
auto it_1=allUsers.find(toUser);
if(it_1 != allUsers.end()){
User inviter=it_1->second;
inviter.friendList.push_back(fromUser);
inviter.addOnlineFriendlist(fromUser);
inviter.addInvitelist(fromUser);
it_1->second=inviter;
}
auto itr=onlineUsers.find(fromUser);
if(itr != onlineUsers.end()){
User invitee=itr->second;
invitee.friendList.push_back(toUser);
invitee.addOnlineFriendlist(toUser);
invitee.addInvitelist(toUser);
inviteeSocket = invitee.sockfd;
itr->second=invitee;
string myusername = invitee.username;
string myaddress = invitee.address;
int myport = invitee.listeningPort;
stringstream myportstream;
myportstream.clear();
myportstream.str("");
myportstream<<myport;
inviteeResponse=string("location")+'~'+"username"+':'+myusername+'|'+"port"+':'+myportstream.str()+'|'+"ip"+':'+myaddress;
myportstream.clear();
myportstream.str("");
}
auto itr_1=allUsers.find(fromUser);
if(itr_1 != allUsers.end()){
User inviteeUser=itr_1->second;
inviteeUser.friendList.push_back(toUser);
inviteeUser.addOnlineFriendlist(toUser);
inviteeUser.addInvitelist(toUser);
itr_1->second=inviteeUser;
}
cout<<"Inviter respon"<<inviterResponse<<endl;
cout<<"Invitee respon"<<inviteeResponse<<endl;
write(inviteeSocket,inviterResponse.c_str(),strlen(inviterResponse.c_str())+1);
write(inviterSocket,inviteeResponse.c_str(),strlen(inviteeResponse.c_str())+1);
}
message.clear();
message.str("");
}
message.clear();
message.str("");
}
else if(messageBody.compare("message")==0)
{
string bodyOfMsg,firstPart,secondPart,thirdPart,toUserTag,toUser,fromUser,fromUserTag,sendMessageTag,sendMessage;
getline(message,bodyOfMsg,'~');
stringstream msgStream(bodyOfMsg);
getline(msgStream,firstPart,'|');
getline(msgStream,secondPart,'|');
getline(msgStream,thirdPart,'|');
stringstream firstPartSream(firstPart);
stringstream secondPartSream(secondPart);
stringstream thirdPartSream(thirdPart);
getline(firstPartSream,fromUserTag,':');
getline(firstPartSream,fromUser,':');
getline(secondPartSream,toUserTag,':');
getline(secondPartSream,toUser,':');
getline(thirdPartSream,sendMessageTag,':');
getline(thirdPartSream,sendMessage,':');
msgStream.clear();
msgStream.str("");
firstPartSream.clear();
firstPartSream.str("");
secondPartSream.clear();
secondPartSream.str("");
thirdPartSream.clear();
thirdPartSream.str("");
string response;
response.clear();
auto it=onlineUsers.find(toUser);
if(it!=onlineUsers.end())
{
User receiver=it->second;
auto findIt = find (receiver.inviteList.begin(), receiver.inviteList.end(), fromUser);
if(findIt != receiver.inviteList.end()){
int receiverSocket = receiver.sockfd;
string receiverResponse;
receiverResponse=string("mesg")+'~'+string("username")+':'+fromUser+'|'+string("message")+':'+sendMessage;
write(receiverSocket,receiverResponse.c_str(),strlen(receiverResponse.c_str())+1);
}
}
message.clear();
message.str("");
}
else if(messageBody.compare("logout")==0)
{
string mymessageBody;
getline(message,mymessageBody,'~');
string username,usernametag;
stringstream firstPart(mymessageBody);
getline(firstPart,usernametag,':');
if(usernametag.compare("username")!=0){
cout<<"Error is fetching the username for logout"<<endl;
}
else{
getline(firstPart,username,':');
}
firstPart.clear();
firstPart.str("");
auto iter = onlineUsers.find(username);
if(iter == onlineUsers.end()){
cout<<"Error: User not found while loging out"<<endl;
}else{
User myuser;
myuser = iter->second;
myuser.userLogout();
onlineUsers.erase(iter);
auto myiter = allUsers.find(username);
if(myiter!=allUsers.end()){
myiter->second=myuser;
}
}
cout<<"Number of users currently logged in is :"<<onlineUsers.size()<<endl;
message.clear();
message.str("");
return true;
}
return false;
}
static void signalHandler(int signo)
{
cout<<endl<<"SIGINT"<<endl;
ofstream userInfoFile ("user_info_file");
User user;
if (userInfoFile.is_open())
{
auto it=allUsers.begin();
while(it!=allUsers.end())
{
user=it->second;
userInfoFile<<user.username<<"|"<<user.password<<"|";
auto itr=user.friendList.begin();
while(itr!=user.friendList.end())
{
userInfoFile<<*itr;
++itr;
if(itr!=user.friendList.end())
{
userInfoFile<<";";
}
}
userInfoFile<<"\n";
++it;
}
}
userInfoFile.close();
close(sockfd);
exit(0);
}
string getPeerAddressFromSock(int sockfd)
{
string peerAddress;
struct sockaddr_in addr;
socklen_t len;
memset(&addr, 0, sizeof(addr));
len=sizeof(addr);
getpeername( sockfd,(struct sockaddr *) &addr,&len);
peerAddress=inet_ntoa(addr.sin_addr);
cout<<"Peer address from socket "<<peerAddress<<endl;
return peerAddress;
}