-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
380 lines (340 loc) · 13.4 KB
/
client.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
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h> //required for perror
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <algorithm> // std::max
#include <sys/timerfd.h>
#include <sys/time.h>
#include <time.h>
#include <sys/time.h> //FD_SET, FD_ISSET, FD_ZERO macros
#include <iostream>
#include <set>
#include "clientutils.h"
#include "raw.h"
using namespace std;
/*-----------------------------------------------------------------------------
* Method: processList
*---------------------------------------------------------------------------*/
void processList(int s, struct sockaddr_in cServer)
{
request_list m5;
memset(&m5, 0, sizeof(m5)) ;
m5.req_type = REQ_LIST;
if (sendto(s, &m5, sizeof(m5), 0, (struct sockaddr *)&cServer, sizeof(cServer))==-1)
diep("processList-sendto()");
} /* -- processList -- */
/*-----------------------------------------------------------------------------
* Method: processExit
*---------------------------------------------------------------------------*/
void processExit(int s, struct sockaddr_in cServer)
{
request_logout m1;
memset(&m1, 0, sizeof(m1)) ;
m1.req_type = REQ_LOGOUT;
if (sendto(s, &m1, sizeof(m1), 0, (struct sockaddr *)&cServer, sizeof(cServer))==-1)
diep("processExit-sendto()");
} /* -- processExit -- */
/*-----------------------------------------------------------------------------
* Method: processJoin
*---------------------------------------------------------------------------*/
void processJoin(char* channel, set<string> *chSet, char* curChannel, int s, struct sockaddr_in cServer )
{
//cout<<"within processJoin()\n"; //ignore if joining an already joined channel
std::set<string>::iterator it;
it = (*chSet).find(channel);
if(it == (*chSet).end()){
(*chSet).insert(channel);
strcpy(curChannel, channel);
request_join m2;
memset(&m2, 0, sizeof(m2)) ;
m2.req_type = REQ_JOIN;
stpcpy(m2.req_channel, channel);
if (sendto(s, &m2, sizeof(m2), 0, (struct sockaddr *)&cServer, sizeof(cServer))==-1)
diep("processJoin-sendto()");
}
} /* -- processJoin -- */
/*-----------------------------------------------------------------------------
* Method: processLeave
*---------------------------------------------------------------------------*/
void processLeave(char* channel, set<string> *chSet, int s, struct sockaddr_in cServer)
{
//cout<<"within processLeave()\n";
std::set<string>::iterator it;
it = (*chSet).find(channel);
if(it != (*chSet).end()){ //remove channel from the local list at client only if its already in there
(*chSet).erase(channel);
}
//message is sent anyway...if wrong request server will reply with error
request_leave m3;
memset(&m3, 0, sizeof(m3)) ;
m3.req_type = REQ_LEAVE;
stpcpy(m3.req_channel, channel);
if (sendto(s, &m3, sizeof(m3), 0, (struct sockaddr *)&cServer, sizeof(cServer))==-1)
diep("processLeave-sendto()");
} /* -- processLeave -- */
/*-----------------------------------------------------------------------------
* Method: processWho
*---------------------------------------------------------------------------*/
void processWho(char* channel, int s, struct sockaddr_in cServer)
{
//cout<<"within processWho()\n";
request_who m6;
memset(&m6, 0, sizeof(m6)) ;
m6.req_type = REQ_WHO;
stpcpy(m6.req_channel, channel);
if (sendto(s, &m6, sizeof(m6), 0, (struct sockaddr *)&cServer, sizeof(cServer))==-1)
diep("processWho-sendto()");
} /* -- processWho -- */
/*-----------------------------------------------------------------------------
* Method: processCommand
*---------------------------------------------------------------------------*/
int processCommand(char* command, set<string> *chSet, char* curChannel, int s, struct sockaddr_in cServer, int& resettimer)
{
int closeClient = 1;
int commandType;
char Tokens[2][CHANNEL_MAX];
if(strlen(command) > CHANNEL_MAX + 10){
cout<<"\n*Unknown command";
return closeClient;
}
int commandToks = tokCommand(command, Tokens) ;
switch (commandToks)
{
case 1: //only commands
//cout<<"Command: "<<Tokens[0]<<"\n";
commandType = checkCommandType(Tokens[0]);
switch (commandType){
case LIST:
processList(s, cServer);
resettimer = 1;
break;
case EXIT:
processExit(s, cServer);
closeClient = 0;
break;
default:
cout<<"\n*Unknown command";
}
break;
case 2: //commands with args
//cout<<"Command: "<<Tokens[0]<<"\n";
//cout<<"Channel: "<<Tokens[1]<<"\n";
commandType = checkCommandType(Tokens[0]);
switch (commandType){
case JOIN:
processJoin(Tokens[1], chSet, curChannel, s, cServer);
resettimer = 1;
break;
case LEAVE:
processLeave(Tokens[1], chSet, s, cServer);
resettimer = 1;
break;
case WHO:
processWho(Tokens[1], s, cServer);
resettimer = 1;
break;
case SWITCH:
processSwitch(Tokens[1], chSet, curChannel);
break;
default:
cout<<"\n*Unknown command";
}
break;
default:
cout<<"*Unknown command\n";
}
return closeClient;
} /* -- processCommand -- */
int main(int argc , char *argv[])
{
//--------------Extract commnad line arguments------------------------TODO: type checking of input arguments
if(argc != 4)
{
usage(argv[0]);
exit(1);
}
//Extract server IP
char *hostname = argv[1];
char serverIp[20];
hostname_to_ip(hostname , serverIp);
//cout<<hostname<<" resolved to "<<serverIp;
//Extract server port
int sport = atoi(argv[2]);
if(sport == 0){
cout<<"invalid port..\n";
exit(1);
}
raw_mode();
//reading from socket and cin
fd_set readfds;
FD_ZERO(&readfds);
int activity;
char c = 'a';
//--------------Allocate & Initialize Variables in Client------------------------
char userInput[SAY_MAX];
char my_channel[CHANNEL_MAX]; //stores currently active channe
strcpy(my_channel, COMMON);
set<string> subscribedChannels; //stores channel list
subscribedChannels.insert(COMMON);
char my_username[USERNAME_MAX]; //stores user name
if(strlen(argv[3]) > USERNAME_MAX)
{
cout<<"error resolving hostname..\n";
exit(1);
}
strcpy(my_username, argv[3]);
//---------------Create Socket and check if server address is valid---------------
struct sockaddr_in cServer;
int s;
int slen = sizeof(cServer);
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
memset((char *) &cServer, 0, sizeof(cServer));
cServer.sin_family = AF_INET;
cServer.sin_port = htons(sport);
if (inet_aton(serverIp, &cServer.sin_addr)==0) {
cout<<"inet_aton() failed\n";
exit(1);
}
//------------------Structs for different messages (client to server)-----------------------
request_login m0;
request_join m2;
request_say m4;
//------------------Structs for different messages (server to client)-----------------------
char buf[BUFLEN];
//-----------------Send initial login and join---------------------------
//login
memset(&m0, 0, sizeof(m0)) ;
m0.req_type = REQ_LOGIN;
stpcpy(m0.req_username, my_username);
if (sendto(s, &m0, sizeof(m0), 0, (struct sockaddr *)&cServer, slen)==-1)
diep("Login-sendto()");
//join
memset(&m2, 0, sizeof(m2)) ;
m2.req_type = REQ_JOIN;
stpcpy(m2.req_channel, my_channel);
if (sendto(s, &m2, sizeof(m2), 0, (struct sockaddr *)&cServer, slen)==-1)
diep("Initial Join-sendto()");
//--------Set timer file descriptor, KEEPALIVE is the timer interval------
int timer_fd;
struct itimerspec its;
struct timespec now;
if (clock_gettime(CLOCK_REALTIME, &now) == -1)
cout<<"clock_gettime";
its.it_value.tv_sec=now.tv_sec + KEEP_ALIVE;
its.it_value.tv_nsec=now.tv_nsec;
its.it_interval.tv_sec=0;
its.it_interval.tv_nsec=0;
timer_fd=timerfd_create(CLOCK_REALTIME, 0);
if (timer_fd == -1)
cout<<"timerfd_create";
if (timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &its, NULL) == -1)
cout<<"timerfd_settime";
//--------generte prompt, distinguish between command and message------
int closeClient = 1;
int resettimer = 0;
int k;
while(closeClient){
k = 0; //for input chars
int smax = max(s, timer_fd);
cout<<"\n>";
fflush( stdout );
while(c != '\n'){
//Prepare file descriptors set for select()
FD_CLR(s, &readfds);
FD_CLR(timer_fd, &readfds);
FD_SET(STDIN_FILENO, &readfds); //Add cin descriptor to fd_set
FD_SET(s, &readfds); //Add udp socket descriptor to fd_set
FD_SET(timer_fd, &readfds); //Add timerfd descriptor to fd_set
activity = select( smax + 1 , &readfds , NULL , NULL , NULL);
if(activity){
if (FD_ISSET(s, &readfds)){
if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&cServer, (socklen_t*)&slen)==-1)
diep("recvfrom()");
//cout<<"Received packet from"<<inet_ntoa(cServer.sin_addr)<<" : "<< ntohs(cServer.sin_port);
cout << string(k+1,'\b');
displayReceived(buf);
cout<<"\n>";
for(int i = 0; i < k; i++)
cout<<userInput[i];
fflush( stdout );
}
else if(FD_ISSET(STDIN_FILENO, &readfds))
{
c = getchar();
if (c != '\n'){
cout<<c;
fflush( stdout );
userInput[k++] = c;
}
}
else if(FD_ISSET(timer_fd, &readfds)){
//cout<<"timer expired\n";
its.it_value.tv_sec=0; //disarm timer
its.it_value.tv_nsec=0; //disarm timer
if (timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &its, NULL) == -1)
cout<<"timerfd_resettime";
keepAlive(s, cServer);
//reset timer
if (clock_gettime(CLOCK_REALTIME, &now) == -1)
cout<<"clock_gettime";
its.it_value.tv_sec=now.tv_sec + KEEP_ALIVE;
its.it_value.tv_nsec=now.tv_nsec;
its.it_interval.tv_sec=0;
its.it_interval.tv_nsec=0;
if (timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &its, NULL) == -1)
cout<<"timerfd_settime";
}
}
else
cout<<"Error in selection()\n";
}
userInput[k]='\0';
//cout<<">"<<userInput<<"\n";
char inputType = userInput[0];
switch (inputType)
{
case '/' :
resettimer = 0;
closeClient = processCommand((userInput+1), &subscribedChannels, my_channel, s, cServer,resettimer);//+1 is to get rid of /
if(resettimer){
//reset timer
if (clock_gettime(CLOCK_REALTIME, &now) == -1)
cout<<"clock_gettime";
its.it_value.tv_sec=now.tv_sec + KEEP_ALIVE;
its.it_value.tv_nsec=now.tv_nsec;
its.it_interval.tv_sec=0;
its.it_interval.tv_nsec=0;
if (timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &its, NULL) == -1)
cout<<"timerfd_settime";
}
break;
default:
memset(&m4, 0, sizeof(m4)) ;
m4.req_type = REQ_SAY;
stpcpy(m4.req_channel, my_channel);
strcpy(m4.req_text, userInput);
if (sendto(s, &m4, sizeof(m4), 0, (struct sockaddr *)&cServer, slen)==-1)
diep("request_say-sendto()");
//reset timer
if (clock_gettime(CLOCK_REALTIME, &now) == -1)
cout<<"clock_gettime";
its.it_value.tv_sec=now.tv_sec + KEEP_ALIVE;
its.it_value.tv_nsec=now.tv_nsec;
its.it_interval.tv_sec=0;
its.it_interval.tv_nsec=0;
if (timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &its, NULL) == -1)
cout<<"timerfd_settime";
}
c = 'a';
}
close(s);
//change prompt back to line input
cooked_mode();
return 0;
}