-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtuple_space.h
More file actions
422 lines (359 loc) · 13 KB
/
tuple_space.h
File metadata and controls
422 lines (359 loc) · 13 KB
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
#ifndef TUPLE_SPACE_H
#define TUPLE_SPACE_H
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define ALP_OUT 0x01u // 0000 0001
#define ALP_IN 0x02u // 0000 0010
#define ALP_INP 0x04u // 0000 0100
#define ALP_RD 0x08u // 0000 1000
#define ALP_RDP 0x10u // 0001 0000
#define ALP_ACK 0x20u // 0010 0000
#define ALP_HELLO 0x40u // 0100 0000
#define ALP_ERROR 0x80u // 1000 0000
#define ALP_SUCCESS 0x30u // 0011 0000
#define TS_INT 0x01
#define TS_FLOAT 0x02
#define TS_STRING 0x04
#define INT_FIELD_SIZE 2
#define FLOAT_FIELD_SIZE 4
#define YES 1
#define NO 0
#define TS_SUCCESS 1
#define TS_FAIL 0
#define MAX_TUPLES 50
#define MAX_RETRIES 3
#define TYPE_FIELD_SIZE 1 //byte
#define IS_ACTUAL_FIELD_SIZE 1 //byte
#define OP_TYPE_FIELD_SIZE 1 //byte
#define STRING_FIELD_LENGTH_SIZE 1 //byte
#define MSG_LENGTH_FIELD_SIZE 1 //bytes
#define NUM_FIELD_SIZE 1 //byte
#define TUPLE_SIZE 253 //max tuple length in bytes
#define ALP_MESSAGE_SIZE 255 //max ALP message length in bytes
typedef struct {
uint8_t length;
char* value;
} StringField;
// Tuple field structure: | TYPE | DATA |
typedef struct {
uint8_t is_actual; // YES or NO
uint8_t type; // TS_INT, TS_FLOAT or TS_STRING
union {
int16_t int_field;
float float_field;
StringField string_field;
} data;
} Field;
// Structure for Tuple | Nr. of fields (1B) | Field 1 | Field 2 | ... | Field n |
typedef struct {
uint8_t num_fields;
Field *fields;
} Tuple;
void printTuple(Tuple *tuple) {
if (tuple == NULL) {
printf("Invalid tuple pointer\n");
return;
}
printf("Printing Tuple with %d fields:\n", tuple->num_fields);
printf("(");
for (int i = 0; i < tuple->num_fields; ++i) {
switch (tuple->fields[i].type) {
case TS_INT:
printf("%d", tuple->fields[i].data.int_field);
break;
case TS_FLOAT:
printf("%f", tuple->fields[i].data.float_field);
break;
case TS_STRING:
printf("%s", tuple->fields[i].data.string_field.value);
break;
default:
printf("Unknown type");
break;
}
if (i != tuple->num_fields - 1) {
printf(", ");
}
}
printf(")\n");
}
char* tupleToString(Tuple* tuple) {
if (tuple == NULL) {
return NULL;
}
int bufferSize = 2; // Initial size for '(' and null terminator
for (int i = 0; i < tuple->num_fields; i++) {
switch (tuple->fields[i].type) {
case TS_INT:
bufferSize += INT_FIELD_SIZE;
break;
case TS_FLOAT:
bufferSize += FLOAT_FIELD_SIZE;
break;
case TS_STRING:
bufferSize += tuple->fields[i].data.string_field.length + 3; // +3 for '(' and ')' and null terminator
break;
default:
break;
}
if (i != tuple->num_fields - 1) {
bufferSize += 2; // Add 2 for comma and space
}
}
char* buffer = (char*)malloc(bufferSize);
if (buffer == NULL) {
return NULL;
}
int offset = sprintf(buffer, "("); // Start with '('
for (int i = 0; i < tuple->num_fields; i++) {
if (i != 0) {
offset += sprintf(buffer + offset, ", ");
}
switch (tuple->fields[i].type) {
case TS_INT:
offset += sprintf(buffer + offset, "%d", tuple->fields[i].data.int_field);
break;
case TS_FLOAT:
offset += sprintf(buffer + offset, "%.6f", tuple->fields[i].data.float_field);
break;
case TS_STRING:
offset += sprintf(buffer + offset, "%.*s", tuple->fields[i].data.string_field.length, tuple->fields[i].data.string_field.value);
break;
default:
break;
}
}
sprintf(buffer + offset, ")");
return buffer;
}
/*
char* tupleToString(Tuple *tuple) {
if (tuple == NULL) {
return NULL;
}
int bufferSize = 0;
for (int i = 0; i < tuple->num_fields; i++) {
switch (tuple->fields[i].type) {
case TS_INT:
bufferSize += INT_FIELD_SIZE + 3;
break;
case TS_FLOAT:
bufferSize += FLOAT_FIELD_SIZE + 3;
break;
case TS_STRING:
bufferSize += tuple->fields[i].data.string_field.length + 3;
break;
default:
break;
}
}
char* buffer = (char*)malloc(bufferSize + 1);
if (buffer == NULL) {
return NULL;
}
int offset = 0;
for (int i = 0; i < tuple->num_fields; i++) {
if (i != 0) {
buffer[offset++] = ',';
buffer[offset++] = ' ';
}
switch (tuple->fields[i].type) {
case TS_INT:
sprintf(buffer + offset, "(%d)", tuple->fields[i].data.int_field);
offset += INT_FIELD_SIZE + 2;
break;
case TS_FLOAT:
sprintf(buffer + offset, "(%f)", tuple->fields[i].data.float_field);
offset += FLOAT_FIELD_SIZE + 2;
break;
case TS_STRING:
buffer[offset++] = '(';
strncpy(buffer + offset, tuple->fields[i].data.string_field.value, tuple->fields[i].data.string_field.length);
offset += tuple->fields[i].data.string_field.length;
buffer[offset++] = ')';
break;
default:
break;
}
}
buffer[bufferSize] = '\0';
return buffer;
}
*/
// ALP message structure
typedef struct {
uint8_t op_type;
uint16_t msg_len;
Tuple *tuple;
} ALPMessage;
uint16_t serialize_tuple(Tuple *tuple, uint8_t *buffer, uint8_t op_type) {
uint8_t *ptr = buffer;
uint16_t length = 1; //tuple length in bytes, always starts with num_fields
memcpy(ptr, &tuple->num_fields, NUM_FIELD_SIZE);
ptr += NUM_FIELD_SIZE;
for (int i = 0; i < tuple->num_fields; i++) {
length += IS_ACTUAL_FIELD_SIZE;
memcpy(ptr, &tuple->fields[i].is_actual, IS_ACTUAL_FIELD_SIZE);
ptr += IS_ACTUAL_FIELD_SIZE;
*(ptr++) = tuple->fields[i].type;
length += 1;
length += TYPE_FIELD_SIZE;
if (tuple->fields[i].type == TS_INT) {
length += INT_FIELD_SIZE;
memcpy(ptr, &tuple->fields[i].data.int_field, sizeof(tuple->fields[i].data.int_field));
ptr += INT_FIELD_SIZE;
} else if (tuple->fields[i].type == TS_FLOAT) {
length += FLOAT_FIELD_SIZE;
memcpy(ptr, &tuple->fields[i].data.float_field, sizeof(tuple->fields[i].data.float_field));
ptr += FLOAT_FIELD_SIZE;
} else if (tuple->fields[i].type == TS_STRING) {
uint8_t field_length = tuple->fields[i].data.string_field.length;
*(ptr++) = field_length;
length += 1;
strcpy((char *)ptr, tuple->fields[i].data.string_field.value);
ptr += field_length;
length += field_length;
}
}
// Move the buffer content by 2 bytes
memmove(buffer + 2, buffer, length);
memcpy(buffer, &op_type, 1);
memcpy(buffer + 1, &length, 1);
return length + 2;
}
uint16_t serialize_alp_message(ALPMessage *alp_message, uint8_t *buffer) {
uint8_t *ptr = buffer;
Tuple *tuple = alp_message->tuple;
uint16_t length = OP_TYPE_FIELD_SIZE; //alp message length in bytes, always starts with op_type
memcpy(ptr, &alp_message->op_type, OP_TYPE_FIELD_SIZE);
ptr += OP_TYPE_FIELD_SIZE;
length += MSG_LENGTH_FIELD_SIZE;
memcpy(ptr, &alp_message->msg_len, MSG_LENGTH_FIELD_SIZE);
ptr += MSG_LENGTH_FIELD_SIZE;
if (tuple != NULL) {
length += NUM_FIELD_SIZE; //tuple length in bytes, always starts with num_fields
memcpy(ptr, &tuple->num_fields, NUM_FIELD_SIZE);
ptr += NUM_FIELD_SIZE;
for (int i = 0; i < tuple->num_fields; i++) {
length += IS_ACTUAL_FIELD_SIZE;
memcpy(ptr, &tuple->fields[i].is_actual, IS_ACTUAL_FIELD_SIZE);
ptr += IS_ACTUAL_FIELD_SIZE;
length += TYPE_FIELD_SIZE;
memcpy(ptr, &tuple->fields[i].type, TYPE_FIELD_SIZE);
ptr += TYPE_FIELD_SIZE;
if (tuple->fields[i].type == TS_INT) {
length += INT_FIELD_SIZE;
memcpy(ptr, &tuple->fields[i].data.int_field, sizeof(tuple->fields[i].data.int_field));
ptr += INT_FIELD_SIZE;
} else if (tuple->fields[i].type == TS_FLOAT) {
length += FLOAT_FIELD_SIZE;
memcpy(ptr, &tuple->fields[i].data.float_field, sizeof(tuple->fields[i].data.float_field));
ptr += FLOAT_FIELD_SIZE;
} else if (tuple->fields[i].type == TS_STRING) {
uint8_t field_length = tuple->fields[i].data.string_field.length;
*(ptr++) = field_length;
length += 1;
memcpy(ptr, tuple->fields[i].data.string_field.value, field_length);
ptr += field_length;
length += field_length;
}
}
}
return length;
}
void deserialize_alp_message(uint8_t *buffer, ALPMessage *alp_message) {
uint8_t *ptr = buffer;
Tuple *tuple = (Tuple *)malloc(sizeof(Tuple));
alp_message->op_type = *(uint8_t *)ptr;
ptr += OP_TYPE_FIELD_SIZE;
alp_message->msg_len = *(uint16_t *)ptr;
ptr += MSG_LENGTH_FIELD_SIZE;
if (tuple != NULL) {
tuple->num_fields = *(uint8_t *)ptr;
ptr += NUM_FIELD_SIZE;
tuple->fields = (Field *)malloc(tuple->num_fields * sizeof(Field));
for (int i = 0; i < tuple->num_fields; i++) {
tuple->fields[i].is_actual = *(uint8_t *)ptr;
ptr += IS_ACTUAL_FIELD_SIZE;
tuple->fields[i].type = *(uint8_t *)ptr;
ptr += TYPE_FIELD_SIZE;
if (tuple->fields[i].type == TS_INT) {
tuple->fields[i].data.int_field = *(uint16_t *)ptr;
ptr += INT_FIELD_SIZE;
} else if (tuple->fields[i].type == TS_FLOAT) {
tuple->fields[i].data.float_field = *(float *)ptr;
ptr += FLOAT_FIELD_SIZE;
} else if (tuple->fields[i].type == TS_STRING) {
uint8_t field_length = *(uint8_t *)ptr;
tuple->fields[i].data.string_field.length = field_length;
ptr += STRING_FIELD_LENGTH_SIZE;
tuple->fields[i].data.string_field.value = (char *)malloc(field_length);
strncpy(tuple->fields[i].data.string_field.value, (char *)ptr, field_length);
ptr += field_length;
}
}
}
alp_message->tuple = tuple;
}
void deserialize_tuple(uint8_t *buffer, Tuple *tuple, ALPMessage *alp_message) {
uint8_t *ptr = buffer;
alp_message->op_type = *(uint8_t *)ptr;
ptr += OP_TYPE_FIELD_SIZE;
tuple->num_fields = *(uint8_t *)ptr;
ptr += NUM_FIELD_SIZE;
tuple->fields = (Field *)malloc(tuple->num_fields * sizeof(Field));
for (int i = 0; i < tuple->num_fields; i++) {
tuple->fields[i].type = *(uint8_t *)ptr;
ptr += 1;
if (tuple->fields[i].type == TS_INT) {
tuple->fields[i].data.int_field = *(uint16_t *)ptr;
ptr += INT_FIELD_SIZE;
} else if (tuple->fields[i].type == TS_FLOAT) {
tuple->fields[i].data.float_field = *(float *)ptr;
ptr += FLOAT_FIELD_SIZE;
} else if (tuple->fields[i].type == TS_STRING) {
uint8_t field_length = *(uint8_t *)ptr; // Read the length of the string field
tuple->fields[i].data.string_field.length = field_length;
ptr += 1;
tuple->fields[i].data.string_field.value = (char *)malloc(field_length + 1);
strncpy(tuple->fields[i].data.string_field.value, (char *)ptr, field_length);
tuple->fields[i].data.string_field.value[field_length] = '\0';
ptr += field_length;
}
}
}
void freeALPMessage(ALPMessage *alp_message) {
if (alp_message != NULL) {
if (alp_message->tuple != NULL) {
for (int i = 0; i < alp_message->tuple->num_fields; ++i) {
if (alp_message->tuple->fields[i].type == TS_STRING) {
free(alp_message->tuple->fields[i].data.string_field.value);
}
}
free(alp_message->tuple->fields);
free(alp_message->tuple);
}
free(alp_message);
}
}
void free_tuple(Tuple *tuple) {
for (int i = 0; i < tuple->num_fields; ++i) {
if (tuple->fields[i].type == TS_STRING) {
free(tuple->fields[i].data.string_field.value);
}
}
free(tuple->fields);
free(tuple);
}
void printBufferInBinary(uint8_t *buffer, uint16_t length) {
for (int i = 0; i < length; i++) {
for (int j = 7; j >= 0; j--) {
printf("%d", (buffer[i] >> j) & 1);
}
printf(" ");
}
printf("\n");
}
#endif