-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.txt
More file actions
147 lines (121 loc) · 4.06 KB
/
example.txt
File metadata and controls
147 lines (121 loc) · 4.06 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
Do ogarnięcia: template'y, sprawdzanie zwracanych wartości, (ALP można wzorować na RTP), network byte order,
endianness!, ACK wiadomości dla niezawodności, printowanie wiadomości diagn. na serwerze,
(unikalne ID aplikacji w krotkach, żeby móc uruchomić 2 różne aplikacje na raz)
-------------DO POPRAWY-----------------------------------------
/*
* Payload UDP: 1472 bajty, nagłówek ALP: 3 bajty, krotka 1469 bajtów
*
* Format wiadomości ALP:
***| Typ operacji (1 bajt) | Długość wiadomości (1 bajt) | Krotka (n bajtów, max 128) | <--- tuple_space.h
*
* Typ operacji:
* 0x01 - OUT
* 0x02 - IN
* 0x03 - INP
* 0x04 - RD
* 0x05 - RDP
* 0x06 - ACK - potwierdzenie odebrania pakietu
*
* Długość krotki jest wyrażona w bajtach i obejmuje całą krotkę.
*
* Krotka jest ciągiem bajtów o określonej długości. Format krotki: Pierwsze pole to zawsze string(max 128 bajtów), kolejne int lub float (do wykorzystania sizeof!),
łącznie krotka <= 1469 bajtów
* in(adam, 123, ?float) '\0'
*/
#define ALP_OUT 0x01 // 0000 0001
#define ALP_IN 0x02 // 0000 0010
#define ALP_INP 0x04 // 0000 0100
#define ALP_RD 0x08 // 0000 1000
#define ALP_RDP 0x10 // 0001 0000
#define ALP_ACK 0x20 // 0010 0000
typedef struct {
uint8_t op_type;
uint16_t msg_len;
uint8_t *tuple;
} ALPMessage;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Definicja typów danych
#define TYPE_INT 0x01 // 0000 0001
#define TYPE_FLOAT 0x02 // 0000 0010
#define TYPE_STRING 0x04 // 0000 0100
#define INT_SIZE sizeof(int)
#define FLOAT_SIZE sizeof(float)
#define STRING_SIZE 1 // 1 byte
// Struktura dla pola | TYP | ZAWARTOSC | int or float (or string*)
typedef struct {
uint8_t data_type;
void *data;
} Field;
// Struktura dla krotki (string, ...)
typedef struct {
uint8_t num_fields;
Field **fields;
} Tuple;
// Funkcje do tworzenia i usuwania krotek i pól
Tuple *create_tuple(uint8_t num_fields) {
Tuple *tuple = malloc(sizeof(Tuple));
tuple->num_fields = num_fields;
tuple->fields = malloc(num_fields * sizeof(Field *));
return tuple;
}
void delete_tuple(Tuple *tuple) {
for (int i = 0; i < tuple->num_fields; i++) {
delete_field(tuple->fields[i]);
}
free(tuple->fields);
free(tuple);
}
Field *create_field(uint8_t data_type, void *data) {
Field *field = malloc(sizeof(Field));
field->data_type = data_type;
field->data = data;
return field;
}
void delete_field(Field *field) {
free(field->data);
free(field);
}
// Funkcje do serializacji i deserializacji
uint8_t *serialize_tuple(Tuple *tuple, size_t *length) {
// Oblicz długość bufora
*length = sizeof(tuple->num_fields);
for (int i = 0; i < tuple->num_fields; i++) {
*length += sizeof(tuple->fields[i]->data_type) + sizeof(tuple->fields[i]->data);
}
// Utwórz bufor
uint8_t *buffer = malloc(*length);
uint8_t *ptr = buffer;
// Zapisz liczbę pól
memcpy(ptr, &tuple->num_fields, sizeof(tuple->num_fields));
ptr += sizeof(tuple->num_fields);
// Zapisz pola
for (int i = 0; i < tuple->num_fields; i++) {
memcpy(ptr, &tuple->fields[i]->data_type, sizeof(tuple->fields[i]->data_type));
ptr += sizeof(tuple->fields[i]->data_type);
memcpy(ptr, tuple->fields[i]->data, sizeof(tuple->fields[i]->data));
ptr += sizeof(tuple->fields[i]->data);
}
return buffer;
}
Tuple *deserialize_tuple(uint8_t *buffer, size_t length) {
uint8_t *ptr = buffer;
// Odczytaj liczbę pól
uint8_t num_fields;
memcpy(&num_fields, ptr, sizeof(num_fields));
ptr += sizeof(num_fields);
// Utwórz krotkę
Tuple *tuple = create_tuple(num_fields);
// Odczytaj pola
for (int i = 0; i < num_fields; i++) {
uint8_t data_type;
memcpy(&data_type, ptr, sizeof(data_type));
ptr += sizeof(data_type);
void *data = malloc(sizeof(data_type));
memcpy(data, ptr, sizeof(data_type));
ptr += sizeof(data_type);
tuple->fields[i] = create_field(data_type, data);
}
return tuple;
}