-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buffer.c
83 lines (64 loc) · 2.03 KB
/
Buffer.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
#include <stdio.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <signal.h>
#include <string.h>
#include <arpa/inet.h>
#include <error.h>
#include <pthread.h>
#include "Buffer.h"
int isFull(struct buffer* bpool, int bsize){
if( (bpool->start == bpool->end + 1) || (bpool->start == 0 && bpool->end == bsize-1)) {
return 1;
}
return 0;
}
int isEmpty(struct buffer* bpool){
if(bpool->start == -1){
return 1;
}
return 0;
}
void place(struct buffer* bpool, struct buffer_item bp, int bsize) { //Placing an item in the buffer
pthread_mutex_lock(&mtx);
while (bpool->counter >= bsize) {
printf("Found Buffer Full !! \n");
pthread_cond_wait(&cond_nonfull, &mtx);
}
bpool->end = (bpool->end + 1) % bsize;
bpool->buff_array[bpool->end].IP=malloc(strlen(bp.IP)+1);
strcpy(bpool->buff_array[bpool->end].IP,bp.IP );
bpool->buff_array[bpool->end].port=bp.port;
if(bp.pathname!=NULL && bp.version!=-1){
bpool->buff_array[bpool->end].pathname=malloc(strlen(bp.pathname)+1);
strcpy(bpool->buff_array[bpool->end].pathname,bp.pathname);
bpool->buff_array[bpool->end].version=bp.version;
}
bpool->counter++;
pthread_mutex_unlock(&mtx);
}
struct buffer_item obtain(struct buffer* bpool, int bsize) {//Removing an item from the buffer
struct buffer_item ret;
pthread_mutex_lock(&mtx);
while (bpool->counter <= 0) {
printf("Found Buffer Empty !! \n");
pthread_cond_wait(&cond_nonempty, &mtx);
}
ret = bpool->buff_array[bpool->start];
bpool->buff_array[bpool->start].IP=NULL;
free(bpool->buff_array[bpool->start].IP);
bpool->buff_array[bpool->start].port=0;
bpool->buff_array[bpool->start].pathname=NULL;
free(bpool->buff_array[bpool->start].pathname);
bpool->buff_array[bpool->start].version=-1;
bpool->start = (bpool->start + 1) % bsize;
bpool->counter--;
pthread_mutex_unlock(&mtx);
return ret;
}