-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.c
772 lines (642 loc) · 20.9 KB
/
message.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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
//
// message.c
// SD15-Product
//
// Created by Grupo SD015 on 07/10/14.
// Copyright (c) 2014 Grupo SD015. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "message.h"
#include "message-private.h"
#include "tuple.h"
#include "tuple-private.h"
#include "entry-private.h"
#include "list-private.h"
#include <assert.h>
#include "general_utils.h"
#include "inet.h"
/*
* Creates a message_t
*/
struct message_t * message_create () {
struct message_t * new_message = (struct message_t*) malloc ( sizeof(struct message_t) );
return new_message;
}
/*
* Creates a message with the given attributes.
* (Atualizado para Projeto 5)
*/
struct message_t * message_create_with ( int opcode, int content_type, void * element) {
struct message_t * new_message = message_create();
//sets the content of the message
if ( new_message != NULL ) {
new_message->opcode = opcode;
new_message->c_type = content_type;
//depending on the content_type sets the content
switch (content_type) {
case CT_ENTRY:
new_message->content.entry = element;
break;
case CT_TUPLE:
new_message->content.tuple = element;
break;
case CT_RESULT:
new_message->content.result = * ((int *) element);
break;
// * (Atualizado para Projeto 5) - TOKEN
case CT_SFAILURE:
case CT_SRUNNING:
case CT_INVCMD:
new_message->content.token = element;
break;
default:
break;
}
}
return new_message;
}
/*
* Creates an array of msg_num messages
*/
struct message_t ** message_create_set ( int msg_num ) {
struct message_t ** msg_set = malloc (msg_num * sizeof(struct message_t *) );
return msg_set;
}
/*
* Frees num messages from message_set
*/
void free_message_set(struct message_t ** message_set, int num) {
if ( message_set != NULL ) {
int i =0;
for ( i= 0; i<num; i++) {
free_message2(message_set[i], NO );
}
}
}
/*
* Returns the tuple_t from the message or NULL if msg is NULL.
* Assumes the invoker knows what he is doing, ie., that
* the message contains a tuple as its content.
*/
struct tuple_t * tuple_from_message(struct message_t * msg ) {
return msg == NULL ? NULL : msg->content.tuple;
}
/*
* Returns the size of the given message in bytes.
*/
int message_size_bytes ( struct message_t * msg ) {
return OPCODE_SIZE + C_TYPE_SIZE + message_content_size_bytes(msg);
}
/*
* Returns the size in bytes of the message's content.
* (Atualizado para Projeto 5)
*/
int message_content_size_bytes ( struct message_t * msg ) {
if ( msg == NULL) {
puts ("message_content_size_bytes but msg is NULL");
return FAILED;
}
//the content size in bytes
int content_size_bytes = 0;
if ( msg->c_type == CT_TUPLE ) {
content_size_bytes = tuple_size_bytes(msg->content.tuple);
}
else if ( msg->c_type == CT_ENTRY ) {
content_size_bytes = entry_size_bytes(msg->content.entry);
}
else if ( msg->c_type == CT_RESULT ) {
content_size_bytes = RESULT_SIZE;
}
else if (msg->c_type == CT_SFAILURE || msg->c_type == CT_SRUNNING || msg->c_type == CT_INVCMD){
content_size_bytes = token_as_serialized_size(msg->content.token);
}
else {
printf("Unrecognized message content type : value is %d\n", msg->c_type);
content_size_bytes = FAILED;
}
return content_size_bytes;
}
/*
* Serializes content of a given message_t
* (Atualizado para Projeto 5)(CT_SFAILURE || CT_SRUNNING)
*/
int message_serialize_content ( struct message_t * message, char ** buffer ) {
int buffer_size = 0;
if ( message->c_type == CT_TUPLE ) {
buffer_size = tuple_serialize(message->content.tuple, buffer);
}
else if ( message->c_type == CT_ENTRY ) {
buffer_size = entry_serialize(message->content.entry, buffer);
}
else if ( message->c_type == CT_RESULT ) {
buffer[0] = (char*) malloc(RESULT_SIZE );
int result_to_network = htonl(message->content.result);
memcpy(buffer[0], &result_to_network, RESULT_SIZE);
buffer_size = RESULT_SIZE;
}
else if (message->c_type == CT_SFAILURE
|| message->c_type == CT_SRUNNING
|| message->c_type == CT_INVCMD )
{
buffer_size = token_serialize(message->content.token, buffer);
}
else {
printf("message_serialize_content : invalide C_TYPE\n");
buffer_size=FAILED;
}
return buffer_size;
}
/* Converte o conteúdo de uma message_t num char*, retornando o tamanho do
* buffer alocado para a mensagem serializada como um array de
* bytes, ou FAILED em caso de erro.
*
* A mensagem serializada deve ter o seguinte formato:
*
* OPCODE C_TYPE
* [2 bytes] [2 bytes]
*
* a partir daí, o formato difere para cada c_type:
*
* ct_type dados
* TUPLE DIMENSION ELEMENTSIZE ELEMENTDATA ...
* [4 bytes] [4 bytes] [ES bytes]
*
* ENTRY TIMESTAMP DIMENSION ELEMENTSIZE ELEMENTDATA
* [8 bytes] [4 bytes] [4 bytes] [ES bytes] ...
*
* RESULT RESULT
* [4 bytes]
*
* TOKEN DIMENSION TOKENDATA
* [4 BYTES] [TD BYTES]
*
*/
int message_to_buffer(struct message_t *msg, char **msg_buf) {
if ( msg == NULL )
return FAILED;
//gets the memory amount needed to be alloced
int msg_buffer_size = message_size_bytes ( msg );
//allocs the memory
*msg_buf = (char*) malloc( msg_buffer_size );
//offset
int offset = 0;
//1. adds the opcode to the buffer
int opcode_to_network = htons(msg->opcode);
memcpy(msg_buf[0]+offset, &opcode_to_network, OPCODE_SIZE);
//moves offset
offset+=OPCODE_SIZE;
//2. adds the content type code
int ctype_to_network = htons(msg->c_type);
memcpy(msg_buf[0]+offset, &ctype_to_network, C_TYPE_SIZE);
//moves the offset
offset+=C_TYPE_SIZE;
//buffer to serialize the message content
char * message_serialized_content = NULL;
// serializes the content message
int message_serialized_content_size = message_serialize_content ( msg, &message_serialized_content);
if ( message_serialized_content_size == FAILED || message_serialized_content == NULL) {
return FAILED;
}
//adds the content into the buffer
memcpy(msg_buf[0]+offset, message_serialized_content, message_serialized_content_size);
//frees it
free(message_serialized_content);
return msg_buffer_size;
}
/*
* Transforma uma mensagem em buffer para uma struct message_t*
* (Atualizado para Projeto 5)
*/
struct message_t *buffer_to_message(char *msg_buf, int msg_size) {
// OP_CODE
int offset = 0;
//gets the opcode
int opcode_network = 0;
memcpy(&opcode_network, msg_buf+offset, OPCODE_SIZE);
//gets to host
int opcode_host = ntohs(opcode_network);
//moves offset
offset+=OPCODE_SIZE;
//sets it
//same to c_type
//gets the opcode
int ctype_network = 0;
memcpy(&ctype_network, msg_buf+offset, C_TYPE_SIZE);
//gets to host
int ctype_host = ntohs(ctype_network);
//moves offset
offset+=C_TYPE_SIZE;
//sets it
void * message_content = NULL;
switch (ctype_host) {
case CT_TUPLE:
message_content = tuple_deserialize(msg_buf+offset, msg_size-offset);
break;
case CT_ENTRY:
message_content = entry_deserialize(msg_buf+offset, msg_size-offset);
break;
case CT_RESULT:
{
int result_network = 0;
memcpy(&result_network, msg_buf+offset, RESULT_SIZE);
//gets to host
int result_host = ntohl(result_network);
message_content = &result_host;
break;
}
case CT_SRUNNING:
case CT_SFAILURE:
case CT_INVCMD:
{
message_content = token_deserialize (msg_buf + offset, msg_size-offset);
break;
}
default:
break;
}
if ( message_content == NULL ) {
printf("message_content == NULL\n");
return NULL;
}
//finally creates the message with all its components
struct message_t * message = message_create_with(opcode_host, ctype_host, message_content);
return message != NULL ? message : NULL;
}
/*
* Liberta a memoria alocada na função buffer_to_message
*/
void free_message(struct message_t *message) {
free_message2(message, YES);
}
/*
* More flexible free_message function that gets the option free_content (YES/NO).
* (Atualizado para Projeto 5)
*/
void free_message2(struct message_t * message, int free_content) {
if ( message == NULL)
return;
if ( free_content ) {
if ( message->c_type == CT_TUPLE ) {
tuple_destroy(message->content.tuple);
}
else if ( message->c_type == CT_ENTRY ) {
entry_destroy(message->content.entry);
}
// * (Atualizado para Projeto 5)
else if (message->c_type == CT_SFAILURE || message->c_type == CT_SRUNNING){
free(message->content.token);
}
}
free(message);
}
/*
* Creates an error message
*/
struct message_t * message_of_error () {
int taskFailedFlag = FAILED;
return message_create_with(OC_ERROR, CT_RESULT, &taskFailedFlag);
}
/*
* Verifies if message has error code or is NULL
*/
int message_error (struct message_t* msg){
return msg != NULL && msg->opcode == OC_ERROR;
}
/*
* Verifies if message is a report message
*/
int message_report (struct message_t* msg) {
return msg != NULL && msg->opcode == OC_REPORT;
}
int message_update_request (struct message_t* msg) {
return msg != NULL && msg->opcode == OC_UPDATE;
}
/*
* Checks if response_msg means success upon the request_msg. YES or NO
*/
int response_with_success ( struct message_t* request_msg, struct message_t* response_msg){
int opcode_req = request_msg->opcode;
int opcode_resp = response_msg->opcode;
if (message_error(response_msg)) {
puts(" (received message is an error message)");
return NO;
}
else if (message_report(response_msg)) {
puts(" (received message is an report message)");
return NO;
}
else if ( opcode_resp != (opcode_req+1) ) {
printf(" (received message has not the expected opcode - sent %d - got %d)\n", opcode_req, opcode_resp);
return NO;
}
return YES;
}
int find_opcode_as_int(const char *input ){
char * input_dup = strdup(input);
char *user_task = strdup(strtok(input_dup," "));
int opcode = atoi(user_task);
free(input_dup);
free(user_task);
return opcode;
}
/*
* Find opcode form user input
*/
int find_opcode_as_string(const char *input ){
char * input_dup = strdup(input);
char *user_task = strdup(strtok(input_dup," "));
char *out = "out";
char *in = "in";
char *in_all = "in_all";
char *copy = "copy";
char *copy_all = "copy_all";
char *size = "size\n";
char *quit = "quit\n";
if (strcasecmp(user_task, out) == 0) {
free(user_task);
free(input_dup);
return OC_OUT;
}
else if (strcasecmp(user_task, in) == 0) {
free(user_task);
free(input_dup);
return OC_IN;
}
else if (strcasecmp(user_task, in_all) == 0) {
free(user_task);
free(input_dup);
return OC_IN_ALL;
}
else if (strcasecmp(user_task, copy) == 0) {
free(user_task);
free(input_dup);
return OC_COPY;
}
else if (strcasecmp(user_task, copy_all) == 0) {
free(user_task);
free(input_dup);
return OC_COPY_ALL;
}
else if (strcasecmp(user_task, size) == 0) {
free(user_task);
free(input_dup);
return OC_SIZE;
}
else if (strcasecmp(user_task, quit) == 0 ) {
free(user_task);
free(input_dup);
return OC_QUIT;
}
else {
//if none was it...
free(user_task);
free(input_dup);
return OC_DOESNT_EXIST;
}
}
int find_ctype (const char * input ) {
char * input_dup = strndup(input, strlen(input));
strtok(input_dup," ");
char * ctype_s = strtok(NULL," ");
return atoi(ctype_s);
}
/*
* Assigns CTCODE according with OPCODE
*/
int assign_ctype (int opcode, int callFromServer ){
int ctcode = FAILED;
switch (opcode) {
/* all operations that envolves finding elements from table */
case OC_IN:
case OC_COPY:
case OC_IN_ALL:
case OC_COPY_ALL:
{
ctcode = CT_TUPLE;
break;
}
case OC_OUT:
ctcode = callFromServer ? CT_ENTRY : CT_TUPLE;
break;
/* operation that envolves returning a value */
case OC_SIZE:
{
ctcode = CT_RESULT;
break;
}
default:
ctcode = FAILED;
break;
}
return ctcode;
}
char * message_to_string ( struct message_t * msg ) {
char * msg_str = NULL;
if ( message_opcode_setter(msg) ) {
if ( msg->c_type == CT_TUPLE) {
msg_str = malloc(OPCODE_SIZE+1 + C_TYPE_SIZE+1 + tuple_size_as_string(msg->content.tuple)+5);
sprintf(msg_str, "%hu %hu %s", msg->opcode, msg->c_type, tuple_to_string(msg->content.tuple));
}
else if ( msg->c_type == CT_ENTRY ) {
msg_str = malloc(OPCODE_SIZE+1 + C_TYPE_SIZE+1 + TIMESTAMP_SIZE+1 + tuple_size_as_string(msg->content.entry->value)+5);
sprintf(msg_str, "%hu %hu %llu %s", msg->opcode, msg->c_type, msg->content.entry->timestamp, tuple_to_string(msg->content.entry->value));
}
}
else if ( message_opcode_taker(msg) ) {
msg_str = malloc(OPCODE_SIZE+1 + C_TYPE_SIZE+1 + tuple_size_as_string(msg->content.tuple)+5);
sprintf(msg_str, "%hu %hu %s", msg->opcode, msg->c_type, tuple_to_string(msg->content.tuple));
}
return msg_str;
}
/*
* Returns a message_t * built from the command string.
* Assumes the command is valid.
*/
struct message_t * command_to_message (const char * command) {
//get opcode
int opcode = find_opcode_as_string(command);
//get ctype
int ctype = find_ctype(command);
void * message_content = NULL;
if ( ctype == CT_TUPLE ) {
message_content = create_tuple_from_input (command);
}
else if ( ctype == CT_ENTRY ) {
message_content = entry_create_from_string(command);
}
else if ( ctype == CT_RESULT ) {
int resultValue = 0;
message_content = &resultValue;
}
//create and return message
struct message_t * message = message_create_with(opcode, ctype, message_content);
return message;
}
/*
* Prints a given message.
* (Atualizado para Projeto 5)
*/
void message_print ( struct message_t * msg ) {
if ( msg == NULL )
printf(" [null message] ");
else {
if ( msg->c_type == CT_TUPLE ) {
printf(" [%hd , %hd , ", msg->opcode, msg->c_type );
tuple_print(msg->content.tuple);
printf(" ] ");
}
else if ( msg->c_type == CT_ENTRY ) {
printf(" [%hd , %hd , <%llu , ", msg->opcode, msg->c_type, msg->content.entry->timestamp);
tuple_print(msg->content.entry->value);
printf("> ] ");
}
// * (Atualizado para Projeto 5)
else if (msg->c_type == CT_SFAILURE || msg->c_type == CT_SRUNNING || msg->c_type == CT_INVCMD ) {
printf (" [ %hd , %hd , %s ]", msg->opcode, msg->c_type, msg->content.token);
}
else if ( msg->c_type == CT_RESULT ) {
printf(" [%hd , %hd , %d ] ", msg->opcode, msg->c_type, msg->content.result );
}
}
}
/*
* Check if message is writer (Criado para Projeto 5)
*/
int message_is_writer(struct message_t *msg) {
return message_opcode_taker(msg) || message_opcode_setter(msg);
}
/*
* Check if message is reader (Criado para Projeto 5)
*/
int message_is_reader(struct message_t *msg) {
return ! message_is_writer(msg);
}
/*
* Check if message has opcode report (Criado para Projeto 5)
*/
int message_opcode_report (struct message_t * msg){
return msg != NULL && msg->opcode == OC_REPORT;
}
/*
* Check is message has opcode setter
*/
int message_opcode_setter (struct message_t * msg ) {
return msg != NULL && msg->opcode == OC_OUT;
}
/*
* Check is message has opcode getter
*/
int message_opcode_getter ( struct message_t * msg) {
return msg != NULL && (msg->opcode == OC_IN || msg->opcode == OC_IN_ALL
|| msg->opcode == OC_COPY || msg->opcode == OC_COPY_ALL
|| msg->opcode == OC_UPDATE );
}
/*
* Checks if message has opcode taker, ie, a getter that takes on reading.
*/
int message_opcode_taker( struct message_t * msg ) {
return msg != NULL && (msg->opcode == OC_IN || msg->opcode == OC_IN_ALL );
}
/*
* Check is message has opcode size
*/
int message_opcode_size (struct message_t * msg ) {
return msg != NULL && msg->opcode == OC_SIZE;
}
/*
* Checks if the msg's opcode is valid/exists. YES or NO
* (Atualizado para projeto 5)
*/
int message_valid_opcode ( struct message_t * msg ) {
return msg != NULL &&
( message_opcode_setter(msg) || message_opcode_getter(msg) || message_opcode_size(msg) || message_opcode_report(msg) );
}
/*
* Returns the size (bytes) of a given token (Criado para Projeto 5)
*/
int token_size_bytes (char* token) {
return token == NULL ? 0 : (int) strlen(token);
}
int token_as_serialized_size(char* token) {
return TOKEN_STRING_SIZE + token_size_bytes(token);
}
/*
* Serializes a given token
*/
int token_serialize(char* token, char **buffer) {
if ( token == NULL)
return FAILED;
//1. Alocar memória para token (n bytes para alocar)
int size_of_token = token_size_bytes(token);
int serialized_token_size = token_as_serialized_size(token);
//aloca tamanho do token + TOKEN_STRING_SIZE
*buffer = malloc(serialized_token_size);
//para gerir preenchimento do buffer
int offset = 0;
//2. Insere dimensão do TOKEN
//2.1 Tamanho do token para formato rede
int size_of_token_htonl = htonl(size_of_token); //
//2.2 Copia o tamanho do TOKEN para o buffer
memcpy(((*buffer)+offset), &size_of_token_htonl, TOKEN_STRING_SIZE);
//2.3 Atualiza offset
offset+=TOKEN_STRING_SIZE;
//3. Copia o TOKEN para o buffer
memcpy((buffer[0]+offset), token, size_of_token);
//3.1 Atualiza offset
offset+=size_of_token;
//4.1 Verifica se o que recebeu é maior do que a memória disponivel
if ( offset > serialized_token_size) {
free (*buffer);
return FAILED;
}
//5. Devolve o tamanho do buffer
return serialized_token_size;
}
/*
* Deserializes a given token (Criado para Projeto 5)
*/
char* token_deserialize(char* buffer, int size) {
if ( buffer == NULL ) {
puts("token_deserialize > buffer == NULL ");
return NULL;
}
//para gerir preenchimento do buffer
int offset = 0;
//1. obtem o tamanho do TOKEN a ler
// [TOKEN_DIM][TOKEN]
int size_of_token_from_buffer = 0;
//1.1 copia para size_of_token o tamanho do token a deserializar
memcpy(&size_of_token_from_buffer, buffer+offset, TOKEN_STRING_SIZE);
//1.2 converte de formato de rede o tamanho do token
int size_of_token = ntohl(size_of_token_from_buffer);
//1.3 Atualiza o offset
offset+=TOKEN_STRING_SIZE;
//2. Aloca memória para receber o TOKEN
char * token_rcvd = (char*) malloc(size_of_token);
//2.1 copia para token_rcvd o que recebe do buffer
memcpy(token_rcvd, (buffer+offset), size_of_token);
token_rcvd[size_of_token] = '\0';
offset+= size_of_token;
//2.2 Verifica se o que recebeu é maior do que a memória disponivel
if ( offset > size) {
puts("!token_deserialize > if ( offset > size) ");
free (token_rcvd);
return NULL;
}
//devolve o token
return token_rcvd;
}
long long swap_bytes_64(long long number) {
long long new_number;
new_number = ((number & 0x00000000000000FF) << 56 |
(number & 0x000000000000FF00) << 40 |
(number & 0x0000000000FF0000) << 24 |
(number & 0x00000000FF000000) << 8 |
(number & 0x000000FF00000000) >> 8 |
(number & 0x0000FF0000000000) >> 24 |
(number & 0x00FF000000000000) >> 40 |
(number & 0xFF00000000000000) >> 56);
return new_number;
}