-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
312 lines (244 loc) · 8.17 KB
/
server.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
/*
* Faraz Heravi
* server.c
*/
#include "server.h"
#include "queue.h"
#include <fcntl.h> // for open
#include <unistd.h> // for close
#include <string.h>
#include <pthread.h>
#include <curses.h>
#include <ctype.h>
#define WORKERS 10
#define DELIMS " \"\'?!.,"
#define PORT 12345
// global variables
char **dictionary;
int dictionaryLength = 0;
// queues
queue *workQueue;
queue *logQueue;
// mutexes and condition variables
pthread_mutex_t workMutex, logMutex;
pthread_cond_t workSignal, logSignal;
// messages
char* clientMessage = "Hello! Welcome to Spell Checker.\n";
char* msgRequest = "Send me some words and I'll tell you if they are spelled correctly!\nSend the \"+\" key to close the connection.\n";
char* msgPrompt = ">>>";
char* msgError = "I didn't get your message. ):\n";
char* msgClose = "Goodbye!\n";
// creates a data structure of a given dictionary
int makeDict(FILE *dict) {
// original size
int dictSize = 10000;
dictionary = (char **) malloc(dictSize * sizeof(char *));
// word buffer
// Assuming the longest word is 100 characters
char word[100];
int index = 0;
while (fscanf(dict, "%s", word) > 0) {
// realloc memory for array
if (index == dictSize) {
dictSize += 1000;
dictionary = realloc(dictionary, dictSize * sizeof(char *));
}
// Copy the word to the dictionary
dictionary[index] = malloc(sizeof(word));
strcpy(dictionary[index], word);
index++;
}
fclose(dict);
return index;
}
// convert a word into all lower case
char *lowerCase(char *word) {
int i;
char *lower = (char *) malloc(sizeof(word));
for(i = 0; word[i] != '\0'; ++i) {
lower[i] = (char) tolower(word[i]);
}
lower[i] = '\0';
return lower;
}
// searches dictionary
int search(char *word) {
for (int i = 0; dictionary[i] != NULL; ++i) {
if (strcmp(lowerCase(dictionary[i]), lowerCase(word)) == 0) {
return 1;
}
}
return 0;
}
// logs the results into a text file
void logging(const char* result) {
// Lock the Following Code
pthread_mutex_lock(&logMutex);
// Open Log File as Append
FILE* logFile = fopen("log.txt", "a");
// Write to File
fprintf(logFile, "%s\n", result);
// Close File
fclose(logFile);
// Unlock
pthread_mutex_unlock(&logMutex);
}
// main worker thread which gets words and checks them
void *worker (void *arg) {
int bytesReturned;
char recvBuffer[BUF_LEN];
recvBuffer[0] = '\0';
// lock while removing from queue
pthread_mutex_lock(&workMutex);
// producer consumer problem (make sure queue is not empty)
while(empty(workQueue)) {
pthread_cond_wait(&workSignal, &workMutex);
}
// dequeue socket
int *socketThread = (int *) dequeue(workQueue);
// unlock
pthread_mutex_unlock(&workMutex);
while(1) {
// send prompt
send(*socketThread, msgPrompt, strlen(msgPrompt), 0);
// accept bytes from the client
bytesReturned = recv(*socketThread, recvBuffer, BUF_LEN, 0);
// error if -1 is returned
if(bytesReturned == -1){
send(*socketThread, msgError, strlen(msgError), 0);
}
//'43' is the "+" key.
else if(recvBuffer[0] == 43){
printf("Connection %d is disconnected from server.\n", *socketThread);
send(*socketThread, msgClose, strlen(msgClose), 0);
close(*socketThread);
// lock
pthread_mutex_lock(&workMutex);
// make sure queue is not empty
while(empty(workQueue)) {
pthread_cond_wait(&workSignal, &workMutex);
}
// dequeue socket
socketThread = (int *) dequeue(workQueue);
send(*socketThread, msgPrompt, strlen(msgPrompt), 0);
// unlock
pthread_mutex_unlock(&workMutex);
}
// if the buffer actually receives a sentence
else {
recvBuffer[bytesReturned-2] = '\0';
// tokenize by word
char *words = strtok(recvBuffer, DELIMS);
while(words != NULL) {
printf("%s ", words);
char *result = malloc(sizeof(char) * BUF_LEN);
// if the word is found in the dictionary
if(search(words)) {
sprintf(result, "\"%s\" is a word\n", words);
// if the word is not found
} else {
sprintf(result, "\"%s\" is not a word\n", words);
}
// print the message in client's side
send(*socketThread, result, strlen(result), 0);
logging(result);
words = strtok(NULL, DELIMS);
free(result);
}
puts("");
}
}
return NULL;
}
//main
int main(int argc, char** argv) {
// initialize queues
workQueue = createQueue();
logQueue = createQueue();
// threads
pthread_t workers[WORKERS];
pthread_t logger;
// Initialize mutexes and condition variables
pthread_mutex_init(&workMutex, NULL);
pthread_cond_init(&workSignal, NULL);
pthread_mutex_init(&logMutex, NULL);
pthread_cond_init(&logSignal, NULL);
// read dictionary
FILE *dict;
if(argc == 1) {
printf("No port number entered.\n");
return -1;
// default dictionary
} else if(argc == 2) {
if((dict=fopen("dictionaries/words.txt", "r")) == NULL) {
puts("ERROR: opening default dictionary");
exit(1);
}
puts("Default dictionary has been opened.");
// specified dictionary
} else if(argc == 3) {
// guides to dictionaries directory
char dir[BUF_LEN] = "dictionaries/"; // buffer
strcat(dir, argv[2]);
dict = fopen(dir, "r");
// if could not find specified dictionary try opening default dictionary
if(dict == NULL) {
puts("ERROR: opening chosen dictionary");
if((dict=fopen("dictionaries/words.txt", "r")) == NULL) {
puts("ERROR: opening default dictionary");
exit(1);
}
puts("Default dictionary has been opened.");
} else {
puts("Chosen dictionary has been opened.");
}
}
// puts dictionary into a data structure for future thread use
dictionaryLength = makeDict(dict);
// create threads
for(int i = 0; i < WORKERS; ++i) {
pthread_create((&workers[i]), NULL, worker, NULL);
}
//sockaddr_in holds information about the user connection.
//We don't need it, but it needs to be passed into accept().
struct sockaddr_in client;
int clientLen = sizeof(client);
int connectionSocket, bytesReturned;
int *clientSocket;
int connectionPort = atoi(argv[1]);
char recvBuffer[BUF_LEN];
recvBuffer[0] = '\0';
//We can't use ports below 1024 and ports above 65535 don't exist.
if(connectionPort < 1024 || connectionPort > 65535){
printf("Port number is either too low(below 1024), or too high(above 65535).\n");
return -1;
}
//Does all the hard work for us.
connectionSocket = open_listenfd(connectionPort);
if(connectionSocket == -1){
printf("Could not connect to %s, maybe try another port number?\n", argv[1]);
return -1;
}
while(1) {
clientSocket = (int *)malloc(sizeof(int));
// take in a client
if((*clientSocket = accept(connectionSocket, (struct sockaddr*)&client, &clientLen)) < 0) {
printf("Error connecting to client.\n");
return -1;
} else {
// welcome messages
send(*clientSocket, clientMessage, strlen(clientMessage), 0);
send(*clientSocket, msgRequest, strlen(msgRequest), 0);
}
// mutual exclusion to the queue
pthread_mutex_lock(&workMutex);
// add client to the queue
printf("Connection to %d success!\n", *clientSocket);
enqueue(workQueue, clientSocket);
// signal condition variable
pthread_cond_signal(&workSignal);
// unlock the lock
pthread_mutex_unlock(&workMutex);
}
return 0;
}