-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularBuffer.cpp
133 lines (109 loc) · 3.33 KB
/
CircularBuffer.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
#include "CircularBuffer.h"
CircularBuffer::CircularBuffer(int maxslots)
{
size = maxslots;
buffer = new bufferMessage[maxslots];
nextFreeIndex = 0;
messages = 0;
pthread_mutex_init(&insertMutex, NULL);
pthread_mutex_init(&popMessagesMutex, NULL);
pthread_cond_init (&bufferFullCondition, NULL);
pthread_cond_init (&bufferNotFullCondition, NULL);
}
void CircularBuffer::insertMessage(int threadID, int socket, int priority, int photoID)
{
pthread_mutex_lock(&insertMutex);
while(messages == size)
pthread_cond_wait(&bufferNotFullCondition, &insertMutex);
bufferMessage *newMessage = new bufferMessage;
newMessage->threadID = threadID;
newMessage->socket = socket;
newMessage->priority = priority;
newMessage->photoID = photoID;
buffer[nextFreeIndex] = *newMessage;
nextFreeIndex = (nextFreeIndex+1) % size;
messages++;
if(messages == size)
{
// signal dispatcher that the buffer is full
while(messages == size)
pthread_cond_signal(&bufferFullCondition);
}
else
{
// signal the next insert message
pthread_cond_signal(&bufferNotFullCondition);
}
pthread_mutex_unlock(&insertMutex);
}
int CircularBuffer::numberOfMessages()
{
return messages;
}
void CircularBuffer::popMessages()
{
pthread_mutex_lock(&popMessagesMutex);
if(messages != size)
pthread_cond_wait(&bufferFullCondition, &popMessagesMutex);
vector<bufferMessage> *messageBuffer = new vector<bufferMessage>;
for(int i=0; i<messages; i++)
{
messageBuffer->push_back(buffer[i%size]);
}
stable_sort(messageBuffer->begin(), messageBuffer->end(), CircularBuffer::messageSorter);
for(std::vector<bufferMessage>::iterator it = messageBuffer->begin(); it != messageBuffer->end(); ++it)
{
bufferMessage message = *it;
char photoLocation[80];
sprintf(photoLocation, "./images/sw%d.ppm", message.photoID + 1);
unsigned char* buff = bufferFromImage(photoLocation);
write(message.socket, buff, IMG_SIZE);
}
messages = 0;
while(messages == 0)
pthread_cond_signal(&bufferNotFullCondition);
pthread_mutex_unlock(&popMessagesMutex);
//return newBuffer;
}
void CircularBuffer::printMessages()
{
for(int i=0; i< messages; i++)
{
bufferMessage b = buffer[i%size];
printf("Socket: %d Priority: %d Photo ID: %d\n", b.socket, b.priority, b.photoID);
}
}
bool CircularBuffer::messageSorter(bufferMessage const& lhs, bufferMessage const& rhs)
{
return lhs.priority > rhs.priority;
}
unsigned char* CircularBuffer::bufferFromImage(char *photoLocation)
{
pixel** pixarray;
FILE *fp;
int cols, rows;
pixval maxval;
register int x, y;
unsigned char *buf=NULL;
if ((fp = fopen (photoLocation,"r")) == NULL) {
fprintf (stderr, "%s: Can't open input file:\n %s.\n", photoLocation);
exit (1);
}
pixarray = ppm_readppm (fp, &cols, &rows, &maxval);
fclose (fp);
if (buf == NULL)
buf = (unsigned char *)malloc(IMG_SIZE);
/* Use this double 'for loop' in the consumer/client threads.
* You'll need to modify this code to read from a socket into
* a data structure called "pixarray" first.
*/
for (y = 0; y < rows; y++) {
for (x = 0; x < cols; x++) {
buf[(y*cols+x)*3+0] = PPM_GETR(pixarray[rows-y-1][x]);
buf[(y*cols+x)*3+1] = PPM_GETG(pixarray[rows-y-1][x]);
buf[(y*cols+x)*3+2] = PPM_GETB(pixarray[rows-y-1][x]);
}
}
ppm_freearray (pixarray, rows);
return buf;
}