-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverConnect 05.c
286 lines (232 loc) · 8.27 KB
/
serverConnect 05.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
#include"simpleServer.h"
# define maxRequest 3
FILE* open; //dictionary file is global
FILE* logfile; // log file
// -----------------------------------gloabal variables
int numClients=0;
int requests[maxRequest]; // client array
int rPutPtr=0; // put clients pointer
int rTakePtr=0;// take client pointer
char *logReports[maxRequest];// results to log
int correctness[maxRequest];// indicates if log = right/wrong
int numLogs = 0; //
int lPutPtr=0; // put log pointer
int lTakePtr=0; // take log pointer
pthread_t workers[maxRequest];
pthread_mutex_t clientLock;
pthread_cond_t consumedOne;
pthread_cond_t alo; //at least one client
pthread_t logger;
pthread_mutex_t logLock;
pthread_cond_t producedLog;
pthread_cond_t consumedLog;
//---------------------------------------------------------------
void produceClient(int clientSocket)
{
printf("in produce\n");
requests[rPutPtr] = clientSocket;
rPutPtr = (rPutPtr + 1) % maxRequest;
numClients = numClients +1;
printf("numClients= %d\n", numClients);
printf("client put ptr = %d\n",rPutPtr);
printf("out of produce\n");
}
int consumeClient () // this needs to be down while holding the lock
{
printf("in consume\n");
int socket = requests[rTakePtr];
requests[rTakePtr] = 0; // clear taken job
rTakePtr = (rTakePtr + 1) % maxRequest; // move pointer to next client (circular)
printf("client consume ptr = %d\n",rTakePtr);
numClients = numClients - 1 ;
printf("numClients= %d\n", numClients);
printf("out of it\n");
return socket;
}
void createLogEntry ( int correctnesslocal)
{
printf("creating log");
//strcat(logReports[lPutPtr],what);
correctness[lPutPtr] = correctnesslocal;
lPutPtr = (lPutPtr + 1) % maxRequest;
numLogs = numLogs +1;
//printf("%s\n",logReports[lPutPtr]);
printf("log created, numlogs = %d ",numLogs);
}
//-------------------------------------------------------------
void *worker_Behaviour (void *args)
{
int correct = 0; //assume wrong, 1 is right, maybe should be boolean
char* welcome = "Welcome to dope boy servers\n";
char* demand = "Gimme a word and I'll see if you can spell\n";
char* prompt = "I want to look up : ";
char* terminate = "Kicking you off the server\n";
char* right = "Spelt correct\n";
char* wrong = "Spelt wrong\n";
char* serSees = "You said ";
char* error = "Something went wrong with the input, try again and dont mess it up";
while(1) // i hope this loop keeps the thread alive to consume next job.. IT DID!!
{
pthread_mutex_lock(&clientLock);
while(numClients == 0 && rTakePtr == rPutPtr) // if know clients wait for full signal
{
pthread_cond_wait(&alo,&clientLock);
}
int socket = consumeClient();
pthread_cond_signal(&consumedOne);
pthread_mutex_unlock(&clientLock);
int input;
char recvBuffer[BUF_LEN];
char *transfer[BUF_LEN];
char compare[BUF_LEN];
recvBuffer[0] = '\0';
send(socket, welcome, strlen(welcome),0);
send(socket, demand, strlen(demand),0);
while(1) // on the job behaviour
{
fseek(open,0,SEEK_SET);
send(socket, prompt, strlen(prompt),0);
input = recv(socket,recvBuffer,BUF_LEN,0);
size_t replace = strlen(recvBuffer); // store an unknown size
recvBuffer[replace-2] = '\n';
recvBuffer[replace-1] = '\0';
if(input==-1)
{
send(socket,error,strlen(error),0);
}
if(recvBuffer[0]== 27)
{
send(socket, terminate, strlen(terminate),0);
close(socket);
break;
return NULL;
}
send(socket, serSees, strlen(serSees), 0);
send(socket, recvBuffer, input, 0);
fgets(compare,sizeof(compare),open);
while(1)
{
if(strcmp(compare,recvBuffer)== 0)
{
send(socket, right, strlen(right),0);
correct = 1;
break;
}
fgets(compare,sizeof(compare),open);
if(feof(open))
{
send(socket, wrong, strlen(wrong),0);
break;
}
}
// seg fualt here
pthread_mutex_lock(&logLock);// get lock
while(numLogs == maxRequest && lPutPtr==lTakePtr) // if LogQ is full && nothing has been taken since it was filled
{
pthread_cond_wait(&consumedLog,&logLock); // wait for consumed signal
}
createLogEntry(correct); // print to file and increment file
pthread_cond_signal(&producedLog); // signal that a job was produced
pthread_mutex_unlock(&logLock); // release lock
memset(recvBuffer,'\0', BUF_LEN); // clear buffer for house keeping
}
} // pull job while loop
}
//----------------------------------------------------------
void *removeLog (void *args)
{
char zero[15] = "Spelt Wrong\n";
char one[15] = "Spelt Correct\n";
pthread_mutex_lock(&logLock);
while(numLogs==0 && lTakePtr == lPutPtr)
{
pthread_cond_wait(&producedLog,&logLock);
}
while(1) // keep looger pulling jobs
{
//fprintf(logfile,"%s",logReports[lTakePtr]);
if(correctness[lTakePtr]== 1)
{
fprintf(logfile,"%s",one);
}
else
{
fprintf(logfile,"%s",zero);
}
correctness[lPutPtr] = 0;
//logReports[lPutPtr] = '\0';
lPutPtr = (lPutPtr + 1) % maxRequest;
numLogs = numLogs-1;
pthread_mutex_unlock(&logLock);
}
}
//--------------------------------------------------------------
int main(int argc, char** argv)
{
if(argc == 1)
{
printf("No port number entered\n");
return -1;
}
if(argv[2] == NULL)
{
open = fopen("words","r");
}
else
{
open = fopen(argv[2],"r");
}
logfile = fopen("log.txt","w");
if(open == NULL || logfile == NULL)
{
printf("Dictionary file not open, quitting");
return -1;
}
struct sockaddr_in client;
int clientLen = sizeof(client);
int connectionPort = atoi(argv[1]);
int connectionSocket,clientSock;
//ports below 1024 and 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;
}
connectionSocket = open_listenfd(connectionPort);
if(connectionSocket == -1)
{
printf("Could not connect to %s, maybe try another port number?\n", argv[1]);
return -1;
}
//-----------------------------------------------------------------------
// create thread workers and a logger that should go to sleep
for(int i= 0; i< maxRequest;)
{
pthread_create(&workers[i], NULL,worker_Behaviour,NULL);// id,something, function, what to pass to thread
i=i+1;
}
pthread_create(&logger,NULL,removeLog,NULL);
//----------------------------------------------
while(1) //main thread accepts clients
{
if((clientSock = accept(connectionSocket, (struct sockaddr*)&client, &clientLen)) == -1)
{
printf("Error connecting to client.\n");
return -1;
}
printf("Connection success!\n");
pthread_mutex_lock(&clientLock); // get clientQ lock
while(numClients == maxRequest-1 && rPutPtr == rTakePtr)
{
printf("main waiting");
pthread_cond_wait(&consumedOne,&clientLock); // wait for empty signal
}
produceClient(clientSock); // put client into Q
pthread_cond_signal(&alo);
pthread_mutex_unlock(&clientLock); // free clientQ
}
fclose(open);
fclose(logfile);
close(clientSock);
return 0;
}