-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbuffer.c
362 lines (311 loc) · 8.81 KB
/
sbuffer.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <time.h>
#include <math.h>
#include <inttypes.h>
char buffer[64000];
int dstart = 1;
int dend = 1;
int isempty = 1;
struct cirNode
{
struct cirNode * next;
int firstSeq;
int lastSeq;
int data;
int size;
int filled;
};
struct cirNode * head;
struct cirNode * newCirNode()
{
struct cirNode * cir = (struct cirNode *) malloc(sizeof(struct cirNode));
cir->next = NULL;
cir->data = 0;
cir->size = 0;
cir->filled = 0;
return cir;
}
void initHead()
{
head = newCirNode();
head->next = head;
head->data = 0;
head->size = 64000;
head->filled = 0;
head->firstSeq = 1;
head->lastSeq = 64;
}
void initialize() {
initHead();
}
void freeCirNode(struct cirNode * cir)
{
free(cir);
}
int validbuffer(struct cirNode* cir)
{
}
// Prints list (for troubleshooting)
void printList() {
struct cirNode * current = head;
if (current == NULL) {
printf("No List.\n");
} else {
printf("dstart:\t%d\n", dstart);
printf("dend:\t%d\n", dend);
while (current->next != head) {
printf("_________________\n");
printf("Filled:\t\t%d\n", current->filled);
printf("FirstSeq:\t%d\n", current->firstSeq);
printf("LastSeq:\t%d\n", current->lastSeq);
printf("Data:\t\t%d\n", current->data);
printf("Size:\t\t%d\n", current->size);
printf("NextSeq:\t%d\n", current->next->firstSeq);
printf("_________________\n");
current = current->next;
}
printf("_________________\n");
printf("Filled:\t\t%d\n", current->filled);
printf("FirstSeq:\t%d\n", current->firstSeq);
printf("LastSeq:\t%d\n", current->lastSeq);
printf("Data:\t\t%d\n", current->data);
printf("Size:\t\t%d\n", current->size);
printf("NextSeq:\t%d (HEAD)\n", current->next->firstSeq);
printf("_________________\n");
}
}
// Goes through list and merges when possible
void mergeList() {
struct cirNode * current = head;
while (current->next != head && current!= NULL) {
while(current->lastSeq == (current->next->firstSeq - 1)
&& current->filled == current->next->filled) {
// Merge
printf("Merging %d & %d.\n", current->lastSeq, current->next->firstSeq);
current->lastSeq = current->next->lastSeq;
current->size = current->size + current->next->size;
current->next = current->next->next;
}
while (current->filled == 0 && current->next->filled == 0
&& dstart != current->firstSeq && dstart != current->next->firstSeq) {
// Merge
printf("Merging %d & %d.\n", current->lastSeq, current->next->firstSeq);
current->lastSeq = 2147483647; // Max int value
current->size = current->size + current->next->size;
current->next = current->next->next;
}
current = current->next;
}
// reset size if only one empty node
/*if (head->next == head && head->filled ==0) {
head->size = 64000;
head->data = 0;
}*/
}
int addData(int seqnum, int size ,char * data)
{
// Merge List to ensure it is proper
mergeList();
struct cirNode * n = head;
int e = 1;
while(n->next != head)
{
e++;
n = n->next;
}
fprintf(stderr,"nums= %d",e);
struct cirNode * current = head;
do{
// Check for entry that already exists
if (current->firstSeq == seqnum && current->filled == 1) {
// Already have data!
// TODO - Return something
printf("Duplicate seqnum");
return 1;
} else if (seqnum > (dstart+63)) {
perror("Seqnum out of range!");
break;
}
// current is empty
if (current->filled == 0) {
// data fills beginning of hole
if (current->firstSeq == seqnum) {
// data will not fill hole
if (size < current->size) {
// create new node to insert
struct cirNode * node = newCirNode();
node->size = current->size - size;
node->data = current->data + size;
node->firstSeq = seqnum + 1;
node->lastSeq = current->lastSeq;
// Update current
current->size = size;
current->filled = 1;
current->lastSeq = seqnum;
node->next = current->next;
current->next = node;
// data fills hole
} else if (size == current->size) {
// Update Current
current->filled = 1;
current->firstSeq = seqnum;
current->lastSeq = seqnum;
}
// data fills middle of a hole
} else if (current->firstSeq < seqnum
&& current->lastSeq > seqnum) {
// create two new nodes
struct cirNode * nodeA = newCirNode(); // Contains new data
struct cirNode * nodeB = newCirNode(); // Contains remaining hole
// Set filled
nodeA->filled = 1;
nodeB->filled = 0;
// Set seq numbers
nodeA->firstSeq = seqnum;
nodeA->lastSeq = seqnum;
nodeB->firstSeq = seqnum + 1;
nodeB->lastSeq = current->lastSeq;
current->lastSeq = seqnum - 1;
// Set Sizes (ASSUMING PACKET SIZE OF 1000)
nodeA->size = size;
int temp = current->size;
current->size = ((current->lastSeq - current->firstSeq + 1) * 1000);
nodeB->size = temp - (current->size + nodeA->size);
// set data values
// nodeA wraparound
if ((current->data + current->size) > 64000) {
nodeA->data =(current->data + current->size) - 64000;
// nodeA nonwrap
} else {
nodeA->data = current->data + current->size;
}
// nodeB wraparound
if ((nodeA->data + nodeA->size) > 64000) {
nodeB->data =(nodeA->data + nodeA->size) - 64000;
// nodeB nonwrap
} else {
nodeB->data = nodeA->data + nodeA->size;
}
// Set Order
nodeB->next = current->next;
current->next = nodeA;
nodeA->next = nodeB;
current = nodeA;
// data fills end of a hole
} else if (current->firstSeq < seqnum
&& current->lastSeq == seqnum) {
// create new node to insert
struct cirNode * node = newCirNode();
node->size = size;
node->firstSeq = seqnum;
node->lastSeq = seqnum;
node->next = current->next;
node->filled = 1;
// set data value
// node wraparound
if ((current->data + current->size - size)> 64000) {
node->data = (current->data + current->size - size) - 64000;
// no wrap
} else {
node->data = current->data + current->size - size;
}
// Update Current
current->next = node;
current->size = current->size - size;
current->lastSeq = seqnum - 1;
current = node;
}
// write data to buffer if node was created
if (current->filled == 1) {
// wraparound
if ((current->data + current->size) > 64000) {
memcpy(buffer + current->data, data, 64000 - current->data);
memcpy(buffer, data + (64000 - current->data), current->size - (64000 - current->data));
// no wrap
} else {
memcpy(buffer + current->data, data, current->size);
}
// Update dvalues
if (seqnum > dend) {
dend = seqnum;
}
// Merge
mergeList();
// End
return 1;
}
}
// Move Current
current = current->next;
}
while(current != head);
printf("Unable to Insert data!\n");
return 0;
}
// gets size of first node (if it contains data)
int getSize() {
// check to see if head has data
if (head->filled == 0) {
return 0;
}
return head->size;
}
// sets *d equal to head data and free's node... returns size
int requestData(int size, char * d)
{ struct cirNode * current = head;
if (current->filled == 1 && current->firstSeq == dstart) {
// Wants more or equal data of current
if (current->size <= size) {
int returnSize = current->size;
// check wrapround
if(current->size + current->data > 64000)
{
memcpy(d, (buffer + current->data), (64000-(current->data)));
memcpy(d + (64000-(current->data)), buffer, current->size - (64000-current->data));
}
// no wraparound
else
{
memcpy(d, buffer+(current->data), current->size);
}
// set filled to 0;
current->filled = 0;
// reset head and dvalue
dstart = current->lastSeq + 1;
head = head->next;
// merge
mergeList();
return(returnSize);
// Current has more data then what is being requested
} else {
// check wrapround
if(current->size + current->data > 64000)
{
memcpy(d, (buffer + current->data), (64000-(current->data)));
memcpy(d + (64000-(current->data)), buffer, size - (64000-current->data));
}
// no wraparound
else
{
memcpy(d, buffer+(current->data), size);
}
// Adjust data and size
current->data = current->data + size;
current->size = current->size - size;
return(size);
}
} else {
d = NULL;
}
return 0;
}