-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
643 lines (577 loc) · 22.7 KB
/
server.cpp
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/**
* @Author: Atul Sahay <atul>
* @Date: 2018-10-06T20:05:51+05:30
* @Email: [email protected]
* @Last modified by: atul
* @Last modified time: 2018-10-07T15:07:17+05:30
*/
/*
1. MAXITERATIONS = macro used to control the numbers to produce by the
producer. For our case 10K.
2. MAXBUFFERSIZE = buffer length at max
3. SLEEP_NANOSEC_CONS, SLEEP_NANOSEC_PROD = to provide extra delay in the
producer and consumer routine, so that the other can do
their routine more frequently
4. mutex = for buffer lock, at any time only 1 thread is allowed to work on it
5. condp = condition variable, controlling the routine when buffer gets full
6. condc = condition variable, controlling the routine when buffer gets Empty
7. queue_count = size of the list at any timespec
8.
*/
#include <stdio.h>
#include <pthread.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <netdb.h>
#include <netinet/in.h>
#include <map>
#include <sys/socket.h>
#include <arpa/inet.h>
using namespace std;
#define MAXITERATIONS 10001 /* Numbers to produce */
#define MAXBUFFERSIZE 3
#define SLEEP_NANOSEC_PROD 100
#define SLEEP_NANOSEC_CONS 100
#define CONSTHREAD 100
///////////////////// Global definitions
pthread_cond_t condc, condp;
// for data processing
pthread_mutex_t Pmutex;
pthread_cond_t reader_can_enter, writer_can_enter;
bool writer_present;
int readcount,writer_waiting;
pthread_mutex_t mutex;
// Ends here
// Buffer vars are written here...............
int buffer[MAXBUFFERSIZE];
int front,rear;
int written;
int queue_count;
int numT;
/* Hashtable is used for global access of the key value pairs
pointer to pointer is used to have char array of pointers so that dynamic allocation
of keys can be done*/
map<long long int,char *> hashTable;
map<long long int,int> bitmap; /* Whether that particular key is present or not */
// Networking Things (Socket Programming essentials)
int sockfd, newsockfd, portno;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
//////////////////////////////////////////ENDS:::: Global definitions
//******************************** Funtion declarations*************************//
//********************** Read Instruction is performed here *******************//
void readInstruction(char buffer[], int sock, int thread_id);
//******************* All data base modification is done *********************//
void modifyInstruction(char buffer[], int sock, int thread_id);
// ***************** Locks and Unlocks Methods*********************/
void readLock(int thread_id);
void readUnLock(int thread_id);
void writeLock(int thread_id);
void writeUnLock(int thread_id);
//-------------- Query Processing --------------------------------//
void doprocessing (int sock, int thread_id);
// Buffer insertion and deletion is defined here
void insert(int value);
int release();
// Clean up routine is presented however it is incomplete
void cleanup_routine();
/* producer stores the incomming connection in the buffer so that consumer thread cleanup_routine
act upon it */
void* producer(void *ptr);
/* pops the socket from the queue and do the necessary operations on the request of user*/
void* consumer(void *ptr);
//******************************* Driver function
int main(int argc, char **argv) {
front = 0;
rear = -1;
queue_count = 0;
written=0;
numT=0;
//for data Processing
writer_present = false;
writer_waiting = 0;
readcount = 0; // how many readers are present
// Initialize the mutex and condition variables
pthread_mutex_init(&Pmutex, NULL);
pthread_cond_init(&reader_can_enter, NULL); /* Initialize reader condition variable */
pthread_cond_init(&writer_can_enter, NULL); /* Initialize writer condition variable */
// Ends
pthread_t pro, con[CONSTHREAD]; //con1,con2,con3,con4; // Pthreads
// Networking things
/* First call to socket() function */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}
int enable = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
perror("setsockopt(SO_REUSEADDR) failed");
/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
// portno = 5002;
// Command line address and portno is passed
char address[1024];
char portStr[1024];
strcpy(address,argv[1]);
strcpy(portStr,argv[2]);
// for string to int
int port;
sscanf(portStr, "%d", &port);
// int address;
// sscanf(addressStr, "%d", &address);
serv_addr.sin_family = AF_INET;
// address.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_addr.s_addr = inet_addr(address);
serv_addr.sin_port = htons(port);
/* Now bind the host address using bind() call.*/
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
exit(1);
}
/* Now start listening for the clients, here
* process will go in sleep mode and will wait
* for the incoming connection
*/
listen(sockfd,5);
clilen = sizeof(cli_addr);
// hashTable = (char **)malloc(10000008*sizeof(char *));
// bitmap = (int *)malloc(10000008*sizeof(int *));
// memset(bitmap, 0, sizeof(bitmap));
// Ends here
// Ids for every pthread
int prod_thread_id = 0;
// int con1_thread_id = 1;
// int con2_thread_id = 2;
// int con3_thread_id = 3;
// int con4_thread_id = 4;
int con_thread_id[CONSTHREAD];
int i = 0;
for(i = 0 ; i < CONSTHREAD ; i++)
{
con_thread_id[i] = i+1;
}
// Initialize the mutex and condition variables
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&condc, NULL); /* Initialize consumer condition variable */
pthread_cond_init(&condp, NULL); /* Initialize producer condition variable */
// Creation of threads
pthread_create(&pro, NULL, producer, (void *)&prod_thread_id);
for(i = 0 ; i < CONSTHREAD ; i++)
{
pthread_create(&con[i], NULL, consumer, (void *)&con_thread_id[i]);
}
// pthread_create(&con3, NULL, consumer, (void *)&con3_thread_id);
// pthread_create(&con4, NULL, consumer, (void *)&con4_thread_id);
// pthread_create(&con2, NULL, consumer, (void *)&con2_thread_id);
// pthread_create(&con1, NULL, consumer, (void *)&con1_thread_id);
sleep(1000);
printf("Captured Error: Resources not allocated\n");
// // For threads to join (Once they complete their tasks)
// pthread_join(&con1, NULL);
// pthread_join(&con2, NULL);
// pthread_join(&con3, NULL);
// pthread_join(&con4, NULL);
// pthread_join(&pro, NULL);
pthread_mutex_destroy(&mutex); /* Free up the_mutex */
pthread_cond_destroy(&condc); /* Free up consumer condition variable */
pthread_cond_destroy(&condp); /* Free up producer condition variable */
}
// Functions definition are written Here
//********************** Read Instruction is performed here *******************//
void readInstruction(char buffer[], int sock, int thread_id)
{
long long int key;
int n;
char bufferText[1024];
bool present = false;
snprintf(bufferText, sizeof(bufferText), "FROM THREAD :%d Recieved **READ** command\n", thread_id);
n = write(sock,bufferText,strlen(bufferText)+1);
bzero(buffer,256);
n = read(sock,buffer,255);
sscanf(buffer, "%lld", &key);
printf("%s size--->%d\n",buffer,n-1);
if(bitmap.count(key)==0)
snprintf(bufferText, sizeof(bufferText), "not\n");
else{
snprintf(bufferText, sizeof(bufferText), "FROM THREAD :%d Recieved **%s** \n", thread_id,buffer);
present = true;
}
printf("%s\n",bufferText);
n = write(sock,bufferText,strlen(bufferText)+1);
if(present){
// value size
int size = strlen(hashTable[key]);
snprintf(bufferText, sizeof(bufferText), "%d\n",size);
printf("%s\n",bufferText);
n = write(sock,bufferText,strlen(bufferText)+1);
char dummy[100];
n = read(sock,dummy,strlen(dummy)+1);
n = write(sock,hashTable[key],size+1);
printf("%s\n",hashTable[key]);
}
}
//******************* All data base modification is done *********************//
void modifyInstruction(char buffer[], int sock, int thread_id)
{
// commnon vars
char bufferText[100000];
int n;
// Choices start here ...............................
if(strncmp(buffer,"create",6)==0)
{
long long int key;
int size;
bool present = false;
snprintf(bufferText, sizeof(bufferText), "FROM THREAD :%d Recieved **CREATE** command\n", thread_id);
n = write(sock,bufferText,strlen(bufferText)+1);
bzero(buffer,256);
n = read(sock,buffer,255);
sscanf(buffer, "%lld", &key);
printf("%s size--->%d\n",buffer,n-1);
if(bitmap.count(key)==0)
snprintf(bufferText, sizeof(bufferText), "FROM THREAD :%d Recieved **%s** \n", thread_id,buffer);
else{
snprintf(bufferText, sizeof(bufferText), "present\n");
present = true;
}
printf("%s\n",bufferText);
n = write(sock,bufferText,strlen(bufferText)+1);
if(!present){
// value size
bzero(buffer,256);
n = read(sock,buffer,255);
sscanf(buffer, "%d", &size);
printf("%s size--->%d\n",buffer,n-1);
snprintf(bufferText, sizeof(bufferText), "FROM THREAD :%d Recieved **%s** \n", thread_id,buffer);
printf("%s\n",bufferText);
n = write(sock,bufferText,strlen(bufferText)+1);
int bytesRead = 0;
int bytesToRead =size+1;
int readThisTime;
char *buffer = (char *)malloc(bytesToRead*sizeof(char));
while (bytesToRead != bytesRead)
{
do
{
readThisTime = read(sock, buffer + bytesRead, (bytesToRead - bytesRead));
}
while(readThisTime == -1);
if (readThisTime == -1)
{
/* Real error. Do something appropriate. */
return;
}
bytesRead += readThisTime;
}
printf("Buffer :->%s\n",buffer);
snprintf(bufferText, sizeof(bufferText), "FROM THREAD :%d Recieved **%s** \n", thread_id,buffer);
printf("%s\n",bufferText);
n = write(sock,bufferText,strlen(bufferText)+1);
hashTable[key] = buffer;
bitmap[key] = 1;
snprintf(bufferText, sizeof(bufferText), "FROM THREAD :%d Ok: added\n", thread_id);
n = write(sock,bufferText,strlen(bufferText)+1);
}
}
else if(strncmp(buffer,"update",6)==0)
{
long long int key;
int size;
bool present = true;
snprintf(bufferText, sizeof(bufferText), "FROM THREAD :%d Recieved **UPDATE** command\n", thread_id);
n = write(sock,bufferText,strlen(bufferText)+1);
bzero(buffer,256);
n = read(sock,buffer,255);
sscanf(buffer, "%lld", &key);
printf("%s size--->%d\n",buffer,n-1);
if(bitmap.count(key)==1)
snprintf(bufferText, sizeof(bufferText), "FROM THREAD :%d Recieved **%s** \n", thread_id,buffer);
else{
snprintf(bufferText, sizeof(bufferText), "not\n");
present = false;
}
printf("%s\n",bufferText);
n = write(sock,bufferText,strlen(bufferText)+1);
if(present){
// value size
bzero(buffer,256);
n = read(sock,buffer,255);
sscanf(buffer, "%d", &size);
printf("%s size--->%d\n",buffer,n-1);
snprintf(bufferText, sizeof(bufferText), "FROM THREAD :%d Recieved **%s** \n", thread_id,buffer);
printf("%s\n",bufferText);
n = write(sock,bufferText,strlen(bufferText)+1);
int bytesRead = 0;
int bytesToRead =size+1;
int readThisTime;
char *buffer = (char *)malloc(bytesToRead*sizeof(char));
while (bytesToRead != bytesRead)
{
do
{
readThisTime = read(sock, buffer + bytesRead, (bytesToRead - bytesRead));
}
while(readThisTime == -1);
if (readThisTime == -1)
{
/* Real error. Do something appropriate. */
return;
}
bytesRead += readThisTime;
}
printf("Buffer :->%s\n",buffer);
snprintf(bufferText, sizeof(bufferText), "FROM THREAD :%d Recieved **%s** \n", thread_id,buffer);
printf("%s\n",bufferText);
n = write(sock,bufferText,strlen(bufferText)+1);
hashTable[key] = buffer;
bitmap[key] = 1;
snprintf(bufferText, sizeof(bufferText), "FROM THREAD :%d Ok: updated\n", thread_id);
n = write(sock,bufferText,strlen(bufferText)+1);
}
}
else if(strncmp(buffer,"delete",6)==0)
{
long long int key;
bool present = true;
snprintf(bufferText, sizeof(bufferText), "FROM THREAD :%d Recieved **DELETE** command\n", thread_id);
n = write(sock,bufferText,strlen(bufferText)+1);
bzero(buffer,256);
n = read(sock,buffer,255);
sscanf(buffer, "%lld", &key);
printf("%s size--->%d\n",buffer,n-1);
if(bitmap.count(key)==1)
snprintf(bufferText, sizeof(bufferText), "FROM THREAD :%d Recieved **%s** \n", thread_id,buffer);
else{
snprintf(bufferText, sizeof(bufferText), "not\n");
present = false;
}
printf("%s\n",bufferText);
n = write(sock,bufferText,strlen(bufferText)+1);
if(present){
free(hashTable[key]);
hashTable.erase(key);
bitmap.erase(key);
snprintf(bufferText, sizeof(bufferText), "FROM THREAD :%d Ok: Deleted\n", thread_id);
n = write(sock,bufferText,strlen(bufferText)+1);
}
}
}
// ***************** Locks and Unlocks Methods*********************/
void readLock(int thread_id)
{
pthread_mutex_lock(&Pmutex); // mutex lock to access buffer
while(writer_present || writer_waiting>0){
// If buffer gets full time for producer to rest
printf("Thread %d goes to sleep: Reading\n",thread_id);
pthread_cond_wait(&reader_can_enter, &Pmutex);
printf("Thread %d start executing again; Reading\n",thread_id);
}
readcount+=1;
pthread_mutex_unlock(&Pmutex);
}
void readUnLock(int thread_id)
{
pthread_mutex_lock(&Pmutex); // mutex lock to access buffer
readcount-=1;
if(readcount==0)
pthread_cond_broadcast(&writer_can_enter);
pthread_mutex_unlock(&Pmutex);
}
void writeLock(int thread_id)
{
pthread_mutex_lock(&Pmutex); // mutex lock to access buffer
writer_waiting+=1;
while(writer_present || readcount>0){
// If buffer gets full time for producer to rest
printf("Thread %d goes to sleep: Modifying\n",thread_id);
pthread_cond_wait(&writer_can_enter, &Pmutex);
printf("Thread %d start executing again; Modifying\n",thread_id);
}
writer_waiting-=1;
writer_present = true;
pthread_mutex_unlock(&Pmutex);
}
void writeUnLock(int thread_id)
{
pthread_mutex_lock(&Pmutex); // mutex lock to access buffer
writer_present = false;
if(writer_waiting==0)
pthread_cond_broadcast(&reader_can_enter);
else
pthread_cond_broadcast(&writer_can_enter);
pthread_mutex_unlock(&Pmutex);
}
//-------------- Query Processing --------------------------------//
void doprocessing (int sock, int thread_id) {
int n;
char buffer[256];
bzero(buffer,256);
int tokenCount = 0;
while(1){
n = read(sock,buffer,255);
tokenCount+=1;
char bufferText[1024];
//snprintf(bufferText, sizeof(bufferText), "I got your message from thread %d\n", thread_id);
// For disconnecting the active action /////////////
if(strncmp(buffer,"bye",3)==0)
{
printf("%s from client %d\n","Connnection closed",(int)(sock-3));
n = write(sock,"Connection Terminated\n",100);
break;
}
if (n < 0) {
perror("ERROR reading from socket");
break;
}
printf("Thread_id :%d From Client :%d Here is the message: %s\n",thread_id,(int)(sock-3),buffer);
//n = write(sock,bufferText,100);
if(strncmp(buffer,"create",6)==0 || strncmp(buffer,"delete",6)==0 || strncmp(buffer,"update",6)==0)
{
writeLock(thread_id);
modifyInstruction(buffer, sock, thread_id);
writeUnLock(thread_id);
}
else if(strncmp(buffer,"read",4)==0)
{
readLock(thread_id);
readInstruction(buffer,sock,thread_id);
readUnLock(thread_id);
}
}
close(sock);
}
//
// Networking Ends here
// Buffer insertion and deletion is defined here
void insert(int value)
{
rear=(rear+1)%MAXBUFFERSIZE;
buffer[rear] = value;
// printf("Insert: front = %d, rear = %d, buffer[rear]=%d\n",front,rear,buffer[rear]);
queue_count+=1;
}
int release()
{
written+=1;
int data = buffer[front];
front = (front+1)%MAXBUFFERSIZE;
// printf("Release: front = %d, rear = %d, buffer[front-1]=%d\n",front,rear,data);
queue_count-=1;
return data;
}
// Clean up routine is presented however it is incomplete
void cleanup_routine()
{
pthread_mutex_destroy(&mutex); /* Free up the_mutex */
pthread_cond_destroy(&condc); /* Free up consumer condition variable */
pthread_cond_destroy(&condp); /* Free up producer condition variable */
printf("\n\n**************DONE CLEANING RESOURCES******************\n");
pid_t pid = getpid();
printf("**************DONE COLLECTING THREADS******************\n");
kill(pid,SIGTERM);
}
/* producer stores the incomming connection in the buffer so that consumer thread cleanup_routine
act upon it */
void* producer(void *ptr) {
int newsockfd;
int thread_id = *((int *)ptr); // for thread id
while(1){
pthread_mutex_lock(&mutex); // mutex lock to access buffer
while(queue_count==MAXBUFFERSIZE){
// If buffer gets full time for producer to rest
printf("Producer goes to sleep\n");
pthread_cond_wait(&condp, &mutex);
printf("Producer start executing\n");
}
pthread_mutex_unlock(&mutex);
// // inserts the number in buffer
// if(written==MAXITERATIONS-1){
// pthread_cond_broadcast(&condc);
// pthread_mutex_unlock(&mutex);
// break;
// }
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) {
perror("ERROR on accept");
exit(1);
}
pthread_mutex_lock(&mutex);
insert(newsockfd);
printf("Producer: Written %d Current Buffer size %d\n",newsockfd,queue_count);
//close(newsockfd);
// broadcast signal for the consumer to print number
// while(queue_count==2){
// pthread_cond_broadcast(&condc);
// pthread_mutex_unlock(&mutex);
// }
pthread_cond_broadcast(&condc);
pthread_mutex_unlock(&mutex);
// Optional sleep for the master, so that worker can get extra time for
// processing
struct timespec delay;
delay.tv_sec = 0.2;
delay.tv_nsec = SLEEP_NANOSEC_PROD;
nanosleep(&delay, NULL);
}
// printf("Here Producer\n");
pthread_cond_broadcast(&condc);
pthread_cond_broadcast(&condc);
pthread_cond_broadcast(&condc);
pthread_cond_broadcast(&condc);
numT+=1;
if(numT==5)
{
cleanup_routine();
}
//pthread_exit(NULL);
}
/* pops the socket from the queue and do the necessary operations on the request of user*/
void* consumer(void *ptr) {
int newsockfd,data;
int thread_id = *((int *)ptr);
printf("Thread comes to play id: %d\n",thread_id);
while(1){
pthread_mutex_lock(&mutex);
while (queue_count==0){
printf("Thread %d goes to sleep\n",thread_id);
pthread_cond_wait(&condc, &mutex);
printf("Thread %d starts executing\n",thread_id);
}
// if(written==MAXITERATIONS-1){
// pthread_cond_signal(&condp);
// pthread_cond_broadcast(&condc);
// pthread_mutex_unlock(&mutex);
// break;
// }
newsockfd = release();
printf("Thread %d Consumed %d Current Buffer size %d\n",thread_id,newsockfd,queue_count);
// doprocessing(newsockfd,thread_id); //{ when nothing works do this}
pthread_cond_broadcast(&condc);
pthread_cond_signal(&condp);
pthread_mutex_unlock(&mutex);
doprocessing(newsockfd,thread_id);
// optional sleep to allow workers to run
struct timespec delay;
delay.tv_sec = 0;
delay.tv_nsec = SLEEP_NANOSEC_CONS;
//nanosleep(&delay, NULL);
}
// printf("Here Thread %d\n",thread_id);
pthread_cond_broadcast(&condc);
pthread_cond_broadcast(&condc);
pthread_cond_broadcast(&condc);
pthread_cond_broadcast(&condc);
pthread_cond_signal(&condp);
numT+=1;
if(numT==5)
{
cleanup_routine();
}
//pthread_exit(NULL);
}