-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcl-srv.c
499 lines (416 loc) · 15.5 KB
/
cl-srv.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "msgs.h"
//Message Header
//reply_port operation numberOfMessages modify_location
//Delete operation
//redeclarations
//Status Code for operation
// 0 - add
// 1 - delete
// 2 - modify
// 3 - display
//Allocating ports 0 to 9 for servers
int srvPort = 0;
//Allocating ports 10 to 99 for clients
int clientPorts = 1;
void encode(char * string, int *** mat);
void decode(int ** mat, int rows, char ** string);
void print_matrix(int rows, int **mat);
void free_matrix(int rows, int **mat);
void encode(char * string, int *** mat){
int length = strlen(string);
int rows = length/10 + (length%10 !=0);
*mat = (int **) malloc(sizeof(int *)*rows);
int i=0,j=0;
for(i=0; i<rows; i++)
/* Allocate array, store pointer */
(*mat)[i] = (int *) malloc(sizeof(int)*10);
//Initialize all values to -1
for(i=0;i<rows;i++){
for(j=0;j<10;j++)
(*mat)[i][j] = -1;
}
int counter=0,msgNumber=0;
for(i=0;i<length;i++){
(*mat)[msgNumber][counter++] = (int)string[i];
if(counter%10==0) {
msgNumber++;
counter=0;
}
}
}
void print_matrix(int rows, int **mat){
int i=0,j=0;
for(i=0; i<rows; i++){ /* Iterate of each row */
for(j=0; j<10; j++){ /* In each row, go over each col element */
printf("%d ",mat[i][j]); /* Print each row element */
}
printf("\n");
}
}
void free_matrix(int rows, int **mat){
int i=0;
for(i=0;i<rows;i++)
free(mat[i]);
free(mat);
}
void decode(int ** mat, int rows, char ** string){
int offset=0,i=0,msgNumber=0,counter=0;
while(mat[rows-1][i]!=-1 && i<10){
offset++;
i++;
}
int stringLength = 10*(rows-1) + offset;
*string = (char*)malloc((stringLength+1) * sizeof(char));
for(i=0;i<stringLength;i++){
(*string)[i]=mat[msgNumber][counter++];
if(counter%10==0) {
msgNumber++;
counter=0;
}
}
(*string)[stringLength] = '\0';
}
void server(void){
//No mutex needed as we are using user threads
int replyPort,numMessages,length;
int **messages=0;
char * table[10];
int pos=0;
int location=0;
int id=srvPort;
int msg[10]={0},i,j,k;
int numMsgs[10]={0};
for(i=0;i<10;i++)
table[i]=NULL;
while(1){
//printf("Server %d: Receiving message on port %d\n",id,id);
Receive(&p[id],msg);
switch(msg[1]){
case 0: //add operation
printf("Server%d: Received add request from Client%d\n",id,msg[0]);
replyPort = msg[0];
numMessages = msg[2];
messages = (int **) malloc(sizeof(int *)*numMessages);
for(i=0; i<numMessages; i++)
/* Allocate array, store pointer */
messages[i] = (int *) malloc(sizeof(int)*10);
//Reconstruct message array
for(i=0;i<numMessages;i++){
Receive(&p[id],msg);
//print_matrix(1,msg);
for(j=0;j<10;j++)
messages[i][j] = msg[j];
}
// printf("Printing Matrix on server side\n");
// print_matrix(numMessages,messages);
// printf("Decoded message at server%s\n", decode(messages,numMessages));
if(pos<10){
decode(messages,numMessages,&table[pos++]);
msg[0]=1;
//Success
Send(&p[replyPort],msg);
}
else{
msg[0]=0;
Send(&p[replyPort],msg);
}
free_matrix(numMessages,messages);
break;
case 1: //delete operation
printf("Server%d: Received delete request from Client%d\n",id,msg[0]);
replyPort = msg[0];
if(pos>0){
// free(table[--pos]);
table[--pos]=NULL;
msg[0]=1;
//Success
Send(&p[replyPort],msg);
}
else{
msg[0]=0;
Send(&p[replyPort],msg);
}
break;
case 2: //modify operation
printf("Server%d: Received modify request from Client%d\n",id,msg[0]);
replyPort = msg[0];
numMessages = msg[2];
location = msg[3];
messages = (int **) malloc(sizeof(int *)*numMessages);
for(i=0; i<numMessages; i++)
/* Allocate array, store pointer */
messages[i] = (int *) malloc(sizeof(int)*10);
//Reconstruct message array
for(i=0;i<numMessages;i++){
Receive(&p[id],msg);
//print_matrix(1,msg);
for(j=0;j<10;j++)
messages[i][j] = msg[j];
}
// printf("Printing Matrix on server side\n");
// print_matrix(numMessages,messages);
// printf("Decoded message at server%s\n", decode(messages,numMessages));
if(table[location]!=NULL){
//free(table[location]);
decode(messages,numMessages,&table[location]);
msg[0]=1;
//Success
Send(&p[replyPort],msg);
}
else{
msg[0]=0;
Send(&p[replyPort],msg);
}
free_matrix(numMessages,messages);
break;
case 3: //display operation
printf("Server%d: Received display request from Client%d\n",id,msg[0]);
replyPort = msg[0];
for(i=0;i<10;i++){
if(table[i]==NULL){
numMsgs[i]=-1;
}
else{
length = strlen(table[i]);
numMessages = length/10 + (length%10 !=0);
numMsgs[i]=numMessages;
}
}
//Message Header
Send(&p[replyPort],numMsgs);
for(i=0;i<10;i++){
if(numMsgs[i]!=-1){
encode(table[i],&messages);
for(j=0;j<numMsgs[i];j++){
for(k=0; k<10; k++)
msg[k]=messages[j][k];
Send(&p[replyPort],msg);
}
free_matrix(numMsgs[i],messages);
}
}
break;
}
sleep(1);
}
}
void client(void){
//No mutex needed as we are using user threads
int id=clientPorts++;
int **messages=0;
char * messageString=0;
int msg[10]={0},i,j;
int serverPort;
int location=0;
int length=0,numMessages=0;
int randNum=0;
while(1){
serverPort = srvPort;
// currentServerPort = (currentServerPort+1) % numServerPorts;
int operation = rand()%3;
//int operation = 0;
//Sending the client port on the array index 0 of the message.
msg[0]=id;
msg[1]=operation;
switch(operation){
case 0: //add operation
printf("\tClient%d: Sending add request to server\n",id);
randNum=rand()%2;
if(randNum==0){
messageString="Adding new message";
}
else{
messageString = "addMsg";
}
length = strlen(messageString);
numMessages = length/10 + (length%10 !=0);
msg[2]=numMessages;
//Message Header
Send(&p[serverPort],msg);
encode(messageString,&messages);
// printf("Printing Matrix on client side\n");
// print_matrix(numMessages,messages);
// printf("%s\n", decode(messages,numMessages));
for(i=0;i<numMessages;i++){
for(j=0; j<10; j++)
msg[j]=messages[i][j];
//print_matrix(1,msg);
Send(&p[serverPort],msg);
}
free_matrix(numMessages,messages);
Receive(&p[id],msg);
if(msg[0])
printf("\tClient%d: Add request for message \"%s\" acknowledged by Server\n", id,messageString);
else
printf("\tClient%d: Add request for message \"%s\" not acknowledged by Server\n", id,messageString);
break;
case 1: //delete operation
printf("\tClient%d: Sending delete request to server\n",id);
//Message Header
Send(&p[serverPort],msg);
Receive(&p[id],msg);
if(msg[0])
printf("\tClient%d: Delete request acknowledged by Server\n", id);
else
printf("\tClient%d: Delete request not acknowledged by Server\n", id);
break;
case 2: //modify operation
printf("\tClient%d: Sending modify request to server\n",id);
randNum=rand()%2;
if(randNum==0){
messageString="Modifying message";
}
else{
messageString = "modMsg";
}
messageString = "mmsg";
length = strlen(messageString);
numMessages = length/10 + (length%10 !=0);
msg[2]=numMessages;
msg[3]=rand()%10;
printf("\tClient%d: Modification location is %d\n",id, msg[3]);
//Message Header
Send(&p[serverPort],msg);
encode(messageString,&messages);
// printf("Printing Matrix on client side\n");
// print_matrix(numMessages,messages);
// printf("%s\n", decode(messages,numMessages));
for(i=0;i<numMessages;i++){
for(j=0; j<10; j++)
msg[j]=messages[i][j];
//print_matrix(1,msg);
Send(&p[serverPort],msg);
}
free_matrix(numMessages,messages);
Receive(&p[id],msg);
if(msg[0])
printf("\tClient%d: Modify request for message \"%s\" acknowledged by Server\n", id,messageString);
else
printf("\tClient%d: Modify request for message \"%s\" not acknowledged by Server\n", id,messageString);
break;
}
sleep(1);
}
}
//send request to server
//server sends a message array with the number of messages per table entry
void displayClient(void){
//No mutex needed as we are using user threads
int id=clientPorts++;
char * table[10];
int **messages=0;
char * messageString=0;
int msg[10]={0},i,j,k;
int numMsgs[10] = {0};
int serverPort;
int location=0;
int length=0,numMessages=0;
int randNum=0;
for(i=0;i<10;i++)
table[i]=NULL;
while(1){
serverPort = srvPort;
// currentServerPort = (currentServerPort+1) % numServerPorts;
int operation = 3;
//int operation = 0;
//Sending the client port on the array index 0 of the message.
msg[0]=id;
msg[1]=operation;
switch(operation){
case 3: //display operation
printf("\t\tClient%d: Sending display request to server\n",id);
Send(&p[serverPort],msg);
//Receive message header from server which will contain the number of entries for each message
Receive(&p[id],msg);
for(i=0;i<10;i++)
numMsgs[i]=msg[i];
for(i=0;i<10;i++){
if(numMsgs[i]!=-1){
messages = (int **) malloc(sizeof(int *)*numMessages);
for(j=0;j<numMsgs[i];j++){
messages[j] = (int *) malloc(sizeof(int)*10);
}
//Reconstruct message array
for(j=0;j<numMsgs[i];j++){
Receive(&p[id],msg);
//printf("Server%d: Received add message%d from Client%d\n",id,i,replyPort);
//print_matrix(1,msg);
for(k=0;k<10;k++)
messages[j][k] = msg[k];
}
// printf("Printing Matrix on server side\n");
// print_matrix(numMessages,messages);
//printf("\t\tTable Entry[%d]: %s\n", i, );
decode(messages,numMsgs[i],&table[i]);
free_matrix(numMsgs[i],messages);
}
}
randNum=rand()%2;
if(randNum==0){
for(i=0;i<10;i++){
if(table[i]==NULL)
printf("\t\tTable Entry[%d]: Empty\n", i);
else
printf("\t\tTable Entry[%d]: %s\n", i, table[i]);
}
}
for(i=0;i<10;i++){
if(table[i]!=NULL)
//free(table[i]);
table[i]=NULL;
}
break;
}
sleep(1);
}
}
//-------------------------------------------------------
int main()
{
printf("The string message is encoded into messages (each character's ascii value is stored in the message) and sent to the server\n");
printf("The server decodes the message and stores it in its table.\n");
printf("Based on the size of the string the number of messages will vary\n");
printf("For client3, the entire table contents will be encoded and sent to the client via messages.\n");
sleep(2);
srand(time(NULL));
init_ports();
// int i;
//Creating 5 servers.
//for(i=0;i<5;i++)
start_thread(server);
//Creating 90 clients.
//for(i=10;i<100;i++)
start_thread(client);
start_thread(client);
start_thread(displayClient);
run();
while (1) sleep(1);
}
// int main(){
// srand(time(NULL));
// int **matrix;
// /* First matrix */
// // printf("Matrix 1\n");
// // printf("Enter # of rows and cols: ");
// // scanf("%d",&rows);
// int length = strlen("HeyWhatsupY");
// int rows = length/10 + (length%10 !=0);
// matrix = encode("HeyWhatsupY");
// printf("Your Matrix\n"); /* Print the entered data */
// print_matrix(rows,matrix);
// char * key;
// key = (char*)malloc(3 * sizeof(char));
// key[0] = 'a';
// key[1] = 'a';
// key[2] = '\0';
// printf("%s\n", key);
// free(key);
// char * decodedString = decode(matrix,rows);
// printf("%s\n", decodedString);
// free_matrix(rows, matrix); /* Free the matrix */
// free(decodedString);
// return 0;
// }