-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverCS.c
More file actions
360 lines (326 loc) · 11.2 KB
/
serverCS.c
File metadata and controls
360 lines (326 loc) · 11.2 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
#include "common.h"
typedef struct CourseInfoT
{
char courseCode[MAX_COURSECODE_LEN];
char credits[MAX_CREDITS_LEN];
char professor[MAX_PROF_LEN];
char days[MAX_DAYS_LEN];
char courseName[MAX_COURSENAME_LEN];
} CourseInfo;
CourseInfo courseInfo[MAX_FILE_LINES] = {0};
int totalLines = 0;
int sockfd = 0;
// Associate socket with a port
void bindUDPSocket()
{
// Beej’s guide to network programming, 6.3
struct addrinfo hints, *servinfo, *p;
int rv;
char serverCSPort[10];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET; // set to AF_INET to use IPv4
hints.ai_socktype = SOCK_DGRAM;
sprintf(serverCSPort, "%d", SERVERCS_PORT);
/* convert a text string representation of an IP address to an addrinfo structure
that contains a sockaddr structure for the IP address and other information. */
if ((rv = getaddrinfo(localhost, serverCSPort, &hints, &servinfo)) != 0)
{
perror("serverCS: UDP getaddrinfo");
return;
}
// loop through all the results and make a socket
for (p = servinfo; p != NULL; p = p->ai_next)
{
// make a socket using the information gleaned from getaddrinfo():
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1)
{
perror("serverCS: UDP socket");
continue;
}
// bind the socket to the port we passed in to getaddrinfo():
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("serverCS: Failed to bind");
continue;
}
break;
}
// check if the socket was bound successfully or not
if (p == NULL)
{
printf("serverCS: failed to create UDP socket\n");
return;
}
printf(DEPT_BOOT_MSG, "CS", SERVERCS_PORT);
}
// Read and parse the course information file.
void readFile()
{
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t bytes;
fp = fopen(CS_FILE, "r");
if (fp == NULL)
{
printf("serverCS : Couldn't open file : %s", CS_FILE);
exit(EXIT_FAILURE);
}
int i = 0;
while ((bytes = getline(&line, &len, fp)) != -1)
{
size_t delimiterPos = 0;
size_t lineDelimiterPos = 0;
// Parse course code
strncpy(courseInfo[i].courseCode, line, MAX_COURSECODE_LEN);
// Reference for usage of strcspn -
// https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input
delimiterPos = strcspn(line, COMMA);
lineDelimiterPos += delimiterPos + 1;
courseInfo[i].courseCode[delimiterPos] = 0;
// Parse course credits
strncpy(courseInfo[i].credits, line + lineDelimiterPos, MAX_CREDITS_LEN);
delimiterPos = strcspn(line + lineDelimiterPos, COMMA);
lineDelimiterPos += delimiterPos + 1;
courseInfo[i].credits[delimiterPos] = 0;
// Parse professor
strncpy(courseInfo[i].professor, line + lineDelimiterPos, MAX_PROF_LEN);
delimiterPos = strcspn(line + lineDelimiterPos, COMMA);
lineDelimiterPos += delimiterPos + 1;
courseInfo[i].professor[delimiterPos] = 0;
// Parse days
strncpy(courseInfo[i].days, line + lineDelimiterPos, MAX_DAYS_LEN);
delimiterPos = strcspn(line + lineDelimiterPos, COMMA);
lineDelimiterPos += delimiterPos + 1;
courseInfo[i].days[delimiterPos] = 0;
// Parse course name
strncpy(courseInfo[i].courseName, line + lineDelimiterPos, MAX_COURSENAME_LEN);
delimiterPos = strcspn(line + lineDelimiterPos, "\r\n");
lineDelimiterPos += delimiterPos + 1;
courseInfo[i].courseName[delimiterPos] = 0;
debug("%s , %s, %s , %s, %s ", courseInfo[i].courseCode, courseInfo[i].credits, courseInfo[i].professor, courseInfo[i].days, courseInfo[i].courseName);
i++;
}
totalLines = i;
if (line)
free(line);
fclose(fp);
}
// Check if the input list of courses are present in the provided course information list
void getMultCourseResponse(char *courseCodes, char *courseRes)
{
int resDelimiterPos = 0;
int delimiterPos = 0;
int courseFound = FALSE;
char curCourseCode[MAX_COURSE_LEN] = {0};
char courseInfoRes[MAX_COURSE_RES_LEN] = {0};
while (strlen(courseCodes) >= resDelimiterPos)
{
courseFound = FALSE;
memzero(curCourseCode, MAX_COURSE_LEN);
memzero(courseInfoRes, MAX_COURSE_RES_LEN);
delimiterPos = strcspn(courseCodes + resDelimiterPos, " |");
strncpy(curCourseCode, courseCodes + resDelimiterPos, delimiterPos);
curCourseCode[delimiterPos] = '\0';
resDelimiterPos += delimiterPos + 1;
debug("delimiterPos %d", delimiterPos);
debug("resdelimiterPos %d", resDelimiterPos);
if (delimiterPos == 0)
{
break;
}
for (int i = 0; i < totalLines; i++)
{
if (!strcmp(courseInfo[i].courseCode, curCourseCode))
{
strcat(courseInfoRes, courseInfo[i].courseCode);
strcat(courseInfoRes, ": ");
strcat(courseInfoRes, courseInfo[i].credits);
strcat(courseInfoRes, ", ");
strcat(courseInfoRes, courseInfo[i].professor);
strcat(courseInfoRes, ", ");
strcat(courseInfoRes, courseInfo[i].days);
strcat(courseInfoRes, ", ");
strcat(courseInfoRes, courseInfo[i].courseName);
strcat(courseInfoRes, "\n");
strcat(courseRes, courseInfoRes);
courseFound = TRUE;
printf(DEPT_MULT_COURSE_FOUND, curCourseCode, courseInfoRes);
}
}
if (courseFound == FALSE)
{
strcat(courseRes, curCourseCode);
strcat(courseRes, ":");
strcat(courseRes, DELIMITER);
strcat(courseRes, COURSE_NOT_FOUND);
strcat(courseRes, DELIMITER);
strcat(courseRes, "\n");
printf(DEPT_COURSE_NOT_FOUND, curCourseCode);
}
}
debug("Courses : %s", courseCodes);
debug("CourseResponse : \n %s", courseRes);
}
// Check if course is present in the provided course information list
void getCourseResponse(char *courseReq, char *courseRes)
{
char courseCode[MAX_COURSE_LEN + 1];
char category[MAX_CATEGORY_LEN + 1];
strcpy(courseCode, courseReq);
size_t delimiterPos = strcspn(courseReq, DELIMITER);
courseCode[delimiterPos] = 0;
strcpy(category, courseReq + delimiterPos + 1);
int i = 0;
char result[MAX_COURSENAME_LEN] = {0};
int courseFound = FALSE;
for (i = 0; i < totalLines; i++)
{
if (!strcmp(courseInfo[i].courseCode, courseCode))
{
debug(" in : %s : %s ", courseInfo[i].courseCode, courseCode);
debug(" cmp : %s : %s ", courseInfo[i].professor, category);
if (!strcmp(category, "Credit"))
{
strcpy(result, courseInfo[i].credits);
courseFound = TRUE;
}
else if (!strcmp(category, "Professor"))
{
strcpy(result, courseInfo[i].professor);
courseFound = TRUE;
}
else if (!strcmp(category, "Days"))
{
strcpy(result, courseInfo[i].days);
courseFound = TRUE;
}
else if (!strcmp(category, "CourseName"))
{
strcpy(result, courseInfo[i].courseName);
courseFound = TRUE;
}
break;
}
}
printf(DEPT_COURSE_RECV, "CS", category, courseCode);
if (courseFound != TRUE)
{
strcpy(result, COURSE_NOT_FOUND);
printf(DEPT_COURSE_NOT_FOUND, courseCode);
}
else
{
printf(DEPT_COURSE_FOUND, category, courseCode, result);
}
strcat(courseRes, MSG_TYPE_COURSE_RES);
strcat(courseRes, DELIMITER);
strcat(courseRes, courseCode);
strcat(courseRes, DELIMITER);
strcat(courseRes, category);
strcat(courseRes, DELIMITER);
strcat(courseRes, result);
strcat(courseRes, DELIMITER);
debug(courseRes);
return;
}
// send course details as per the course request (single or multi-course)
void sendCourseDetails(char *courseReq, char *msgType)
{
char courseRes[MAX_COURSE_RES_LEN] = {0};
int numBytes;
// Beej’s guide to network programming, 6.3
struct addrinfo hints, *servinfo, *p;
int rv;
char serverMPort[10];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET; // set to AF_INET to use IPv4
hints.ai_socktype = SOCK_DGRAM;
sprintf(serverMPort, "%d", SERVERM_UDP_PORT);
/* convert a text string representation of an IP address to an addrinfo structure
that contains a sockaddr structure for the IP address and other information. */
if ((rv = getaddrinfo(localhost, serverMPort, &hints, &servinfo)) != 0)
{
perror("serverCS: getaddrinfo");
return;
}
// loop through all the results and make a socket
for (p = servinfo; p != NULL; p = p->ai_next)
{
if (p != NULL)
{
break;
}
}
if (p == NULL)
{
printf("serverCS: failed to create socket\n");
return;
}
// take necessary action based on the course request
if (!strcmp(msgType, MSG_TYPE_COURSE_REQ))
{
getCourseResponse(courseReq, courseRes);
}
else if (!strcmp(msgType, MSG_TYPE_MULT_COURSE_REQ))
{
getMultCourseResponse(courseReq, courseRes);
}
else
{
debug("serverCS: invalid message received : \"%s\"", msgType);
return;
}
if ((numBytes = sendto(sockfd, courseRes, strlen(courseRes), 0,
p->ai_addr, p->ai_addrlen)) == -1)
{
perror("serverCS: sendto");
close(sockfd);
exit(1);
}
debug("Sent : %s : %d bytes", courseRes, numBytes);
printf(DEPT_COURSE_SENT, "CS");
}
// receive course details from the socket
void receiveCourseDetails()
{
int numbytes;
struct sockaddr_storage their_addr;
char courseReq[MAX_COURSE_LEN];
socklen_t addr_len;
size_t resDelimiterPos = 0;
addr_len = sizeof their_addr;
while (1)
{
memzero(courseReq, MAX_COURSE_LEN);
// receive data from socket
if ((numbytes = recvfrom(sockfd, courseReq, MAX_COURSE_REQ_LEN - 1, 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1)
{
perror("serverCS: recvfrom");
close(sockfd);
exit(1);
}
resDelimiterPos = 0;
courseReq[numbytes] = '\0';
debug("serverCS: message received : \"%s\"", courseReq);
char msgType[MSG_TYPE_LEN] = {0};
strncpy(msgType, courseReq, MSG_TYPE_LEN);
size_t delimiterPos = strcspn(courseReq, DELIMITER);
resDelimiterPos += delimiterPos + 1;
msgType[delimiterPos] = 0;
debug("msgType : %s ", msgType);
// send course details to process details and respond as per the course request
sendCourseDetails(courseReq + resDelimiterPos, msgType);
}
close(sockfd);
}
int main()
{
readFile();
bindUDPSocket();
receiveCourseDetails();
return 0;
}