-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatServer.c
335 lines (314 loc) · 9.68 KB
/
chatServer.c
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
/* server process */
/* include the necessary header files */
#include<ctype.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<sys/select.h>
#include "protocol.h"
#include "libParseMessage.h"
#include "libMessageQueue.h"
int max(int a, int b) {
if (a>b)return a;
return b;
}
/**
* @brief replaces a null terminator within the given string to a new line charachter
*
* @param nullTermString a string that is null terminated
* @return int length i if succesfull, 0 on fail
*/
int toNewLine(char *nullTermString) {
int i;
for (i = 0; nullTermString[i] != '\0'; i++) continue;
if(nullTermString[i]=='\0') {
nullTermString[i]='\n';
return i;
}
return 0;
}
/**
* @return int length i if succesfull, 0 on fail
*/
int toNullTerm(char *newLineString, int numRecv) {
int i;
for (i = 0; i < numRecv && newLineString[i] != '\n'; i++) continue;
if (newLineString[i]=='\n') {
newLineString[i]='\0';
return i;
}
return 0;
}
/**
* send a single message to client
* sockfd: the socket to read from
* toClient: a buffer containing a null terminated string with length at most
* MAX_MESSAGE_LEN-1 characters. We send the message with \n replacing \0
* for a mximmum message sent of length MAX_MESSAGE_LEN (including \n).
* return 1, if we have successfully sent the message
* return 2, if we could not write the message
*/
int sendMessage(int sfd, char *toClient){
int i;
if ( (i = toNewLine(toClient)) == 0) return 2;
int numSend = send(sfd, toClient, i + 1, 0);
if(numSend!=1)return(2);
return(1);
}
/**
* read a single message from the client.
* sockfd: the socket to read from
* fromClient: a buffer of MAX_MESSAGE_LEN characters to place the resulting message
* the message is converted from newline to null terminated,
* that is the trailing \n is replaced with \0
* return 1, if we have received a newline terminated string
* return 2, if the socket closed (read returned 0 characters)
* return 3, if we have read more bytes than allowed for a message by the protocol
*/
int recvMessage(int sfd, char *incomingBuffer){
char buffer[MAX_MESSAGE_LEN];
int numRecv = recv(sfd, buffer, MAX_MESSAGE_LEN, 0);
if(numRecv==0)return(2);
int i;
for (i = 0; i < numRecv; i++) {
if (buffer[i] == '\n') {
i++;
break;
}
continue;
}
if(i == MAX_MESSAGE_LEN && buffer[numRecv - 1]!='\n')return(3);
if(strlen(incomingBuffer) + numRecv >= MAX_MESSAGE_LEN)return(3);
strncat(incomingBuffer, buffer, numRecv);
return(1);
}
/**
* Extract a new line terminated string from incomingBuffer to fromClient
* return 1 if there is a partial message left in the buffer, 0 otherwise
*/
int extractMessage(char *incomingBuffer, char *fromClient) {
int i;
for (i = 0; incomingBuffer[i] != '\n'; i++) {
if ( incomingBuffer[i] == '\0') return(0);
}
incomingBuffer[i] = '\0';
strncpy(fromClient, incomingBuffer, i+1);
strcpy(incomingBuffer, incomingBuffer + i + 1);
return(i);
}
int main (int argc, char ** argv) {
int sockfd;
if(argc!=2){
fprintf(stderr, "Usage: %s portNumber\n", argv[0]);
exit(1);
}
int port = atoi(argv[1]);
if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
perror ("socket call failed");
exit (1);
}
struct sockaddr_in server;
server.sin_family=AF_INET; // IPv4 address
server.sin_addr.s_addr=INADDR_ANY; // Allow use of any interface
server.sin_port = htons(port); // specify port
if (bind (sockfd, (struct sockaddr *) &server, sizeof(server)) == -1) {
perror ("bind call failed");
exit (1);
}
if (listen (sockfd, 5) == -1) {
perror ("listen call failed");
exit (1);
}
MessageQueue UserArray[32];
MessageQueue OutgoingArray[32];
char incommingBuffer[32][MAX_CHAT_MESSAGE_LEN];
for (int q = 0; q < 32; q++) {
initQueue(&UserArray[q]);
initQueue(&OutgoingArray[q]);
incommingBuffer[q][0] = '\0';
}
//char UserNameArray[32][MAX_USER_LEN];
//int fdlist[33];
int fdcount=0;
//fdlist[0]=sockfd;
fdcount++;
for (;;) {
fd_set readfds, writefds, exceptfds;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
int fdmax=0;
FD_SET(sockfd, &readfds);
fdmax=max(fdmax,sockfd);
for (int i=0; i<32; i++) {
if (UserArray[i].active) {
FD_SET(UserArray[i].fd, &readfds);
FD_SET(UserArray[i].fd, &writefds);
fdmax=max(fdmax,UserArray[i].fd);
}
}
struct timeval tv;
tv.tv_sec=5;
tv.tv_usec=0;
int numfds;
if ((numfds=select(fdmax+1, &readfds, &writefds, NULL, &tv))>0) {
// change loop to go through all 32 items or until fcount iterations
// change the user arrays to signify activation or not
// when you quit, de active and zero every buffer, dequeu and write everything
// only enter if active and FD_ISSSET
for (int i=-1; i<32; i++) {
int qFlag = 0;
int kFlag = -1;
//int fd;
if (i >= 0 && ! UserArray[i].active)continue;
if (i == -1 && FD_ISSET(sockfd, &readfds)) /*accept a connection */{
int newsockfd;
if ((newsockfd = accept (sockfd, NULL, NULL)) == -1) {
perror ("accept call failed");
continue;
}
// loop until you find the first inactive array
// set fd to newsockfd
if (fdcount == 32) {
close(newsockfd);
continue;
}
int u;
for (u = 0; u < 32; u++) {
if (! UserArray[u].active) break;
}
UserArray[u].userName[0] = '\0';
UserArray[u].fd = newsockfd;
UserArray[u].active = 1;
fdcount++;
continue;
}
if (UserArray[i].active && FD_ISSET(UserArray[i].fd,&readfds)) {
char fromClient[MAX_CHAT_MESSAGE_LEN], toClient[MAX_MESSAGE_LEN];
int retVal=recvMessage(UserArray[i].fd, incommingBuffer[i]);
int res;
if (retVal == 3) {
qFlag = 2;
kFlag = i;
}
else if (retVal == 2) {
continue;
}
while (kFlag != i && (res = extractMessage(incommingBuffer[i], fromClient) > 0)) {
char *part[4];
int numParts=parseMessage(fromClient, part);
if(numParts==0){
strcpy(toClient,"ERROR");
}
else if(strcmp(part[0], "list")==0) {
sprintf(toClient, "users:");
int total = 0;
for (int uc = 0; uc < 32 && total < 10; uc++) {
if (! UserArray[uc].active) continue;
if (UserArray[uc].userName[0] != '\0') {
strncat(toClient, UserArray[uc].userName, MAX_USER_LEN);
strcat(toClient, ":");
total++;
}
}
}
else if(strcmp(part[0], "message")==0) {
char *fromUser=part[1];
char *toUser=part[2];
char *message=part[3];
int u;
for (u = 0; u<32; u++) {
if (strcmp(toUser, UserArray[u].userName) == 0 && UserArray[u].active) {
break;
}
}
if(strcmp(fromUser, UserArray[i].userName)!=0) {
sprintf(toClient, "invalidFromUser:%s",fromUser);
}
else if(u == 32) {
sprintf(toClient, "invalidToUser:%s ",toUser);
}
else {
sprintf(toClient, "%s:%s:%s:%s","message", fromUser, toUser, message);
if(enqueue(&UserArray[u], toClient)) {
strcpy(toClient, "messageQueued");
}
else {
strcpy(toClient, "messageNotQueued");
qFlag = 2;
kFlag = u;
}
}
}
else if(strcmp(part[0], "quit")==0) {
strcpy(toClient, "closingConnection");
qFlag = 1;
kFlag = i;
}
else if(strcmp(part[0], "getMessage")==0){
if(dequeue(&UserArray[i], toClient) == 0){
strcpy(toClient, "noMessage");
}
}
else if(strcmp(part[0], "register")==0){
if (strlen(UserArray[i].userName) <= 0 && strlen(part[1]) > 0){
int cu;
for (cu = 0; cu<32; cu++) {
if (strcmp(part[1], UserArray[cu].userName) == 0) {
break;
}
}
if (cu != 32) {
strcpy(toClient, "userAlreadyRegistered");
}
else if(strncmp(UserArray[i].userName, part[1], MAX_USER_LEN)!=0) {
strcpy(UserArray[i].userName, part[1]);
strcpy(toClient, "registered");
}
}
else {
strcpy(toClient, "ERROR");
}
}
if (enqueue(&OutgoingArray[i], toClient) == 0) {
qFlag = 2;
kFlag = i;
}
if (qFlag) /* clean up and quit */{
if (FD_ISSET(UserArray[kFlag].fd, &writefds) && qFlag == 1) {
sendMessage(UserArray[kFlag].fd, toClient);
}
while (dequeue(&OutgoingArray[kFlag], toClient) == 1) {
continue;
}
while (dequeue(&UserArray[kFlag], toClient) == 1) {
continue;
}
close(UserArray[kFlag].fd);
UserArray[kFlag].active = 0;
UserArray[kFlag].fd = -1;
UserArray[kFlag].userName[0] = '\0';
kFlag = -1;
qFlag = 0;
fdcount--;
continue;
}
}
}
if (FD_ISSET(UserArray[i].fd,&writefds) && UserArray[i].active) {
/* code */
// dequeue a message from the outgoing buffer and send it
char toClient[MAX_MESSAGE_LEN];
if (dequeue(&OutgoingArray[i], toClient) == 0) continue;
sendMessage(UserArray[i].fd, toClient);
}
}
}
}
exit(0);
}