Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit ea3e119

Browse files
author
Yu Wei Wu
committed
feat(serialize): Add serializer for send mam message
1 parent 53f23fe commit ea3e119

File tree

8 files changed

+129
-50
lines changed

8 files changed

+129
-50
lines changed

accelerator/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ cc_library(
2828
":ta_errors",
2929
"//serializer",
3030
"@entangled//common/trinary:trit_tryte",
31-
"@entangled//common/trinary:tryte_ascii",
3231
"@entangled//mam/api",
3332
],
3433
)

response/ta_send_mam.c

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,36 @@
22

33
send_mam_res_t* send_mam_res_new() {
44
send_mam_res_t* res = (send_mam_res_t*)malloc(sizeof(send_mam_res_t));
5-
if (res) {
6-
res->bundle_hash = NULL;
7-
res->channel_id = NULL;
8-
}
95

106
return res;
117
}
128

139
status_t send_mam_res_set_bundle_hash(send_mam_res_t* res,
1410
const tryte_t* bundle_hash) {
15-
if (res->bundle_hash || !bundle_hash) {
11+
if (!bundle_hash || !res) {
1612
return SC_RES_NULL;
1713
}
1814

19-
size_t bundle_hash_size = NUM_TRYTES_ADDRESS * sizeof(char);
20-
res->bundle_hash = (char*)malloc(bundle_hash_size);
21-
if (!res->bundle_hash) {
22-
return SC_RES_OOM;
23-
}
24-
25-
memcpy(res->bundle_hash, bundle_hash, bundle_hash_size);
15+
memcpy(res->bundle_hash, bundle_hash, NUM_TRYTES_HASH);
16+
res->bundle_hash[NUM_TRYTES_HASH] = '\0';
2617
return SC_OK;
2718
}
2819

2920
status_t send_mam_res_set_channel_id(send_mam_res_t* res,
3021
const tryte_t* channel_id) {
31-
if (res->channel_id || !channel_id) {
22+
if (!channel_id || !res) {
3223
return SC_RES_NULL;
3324
}
3425

35-
size_t channel_id_size = NUM_TRYTES_ADDRESS * sizeof(char);
36-
res->channel_id = (char*)malloc(channel_id_size);
37-
if (!res->channel_id) {
38-
return SC_RES_OOM;
39-
}
40-
41-
memcpy(res->channel_id, channel_id, channel_id_size);
26+
memcpy(res->channel_id, channel_id, NUM_TRYTES_HASH);
27+
res->channel_id[NUM_TRYTES_HASH] = '\0';
4228
return SC_OK;
4329
}
4430

4531
void send_mam_res_free(send_mam_res_t** res) {
4632
if (!res || !(*res)) {
4733
return;
4834
}
49-
if ((*res)->bundle_hash) {
50-
free((*res)->bundle_hash);
51-
(*res)->bundle_hash = NULL;
52-
}
53-
if ((*res)->channel_id) {
54-
free((*res)->channel_id);
55-
(*res)->channel_id = NULL;
56-
}
5735

5836
free(*res);
5937
*res = NULL;

response/ta_send_mam.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ extern "C" {
1515
/** struct of ta_send_mam_res_t */
1616
typedef struct send_mam_res_s {
1717
/** ascii string bundle hash */
18-
char* bundle_hash;
18+
char bundle_hash[NUM_TRYTES_HASH + 1];
1919
/** ascii string channel id */
20-
char* channel_id;
20+
char channel_id[NUM_TRYTES_HASH + 1];
2121
} send_mam_res_t;
2222

2323
/**

serializer/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ cc_library(
1111
"//response",
1212
"@cJSON",
1313
"@entangled//cclient/types",
14+
"@entangled//common/trinary:tryte_ascii",
1415
],
1516
)

serializer/serializer.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,56 @@ status_t receive_mam_message_serialize(char** obj, const char** res) {
407407
cJSON_Delete(json_root);
408408
return ret;
409409
}
410+
411+
status_t send_mam_res_serialize(char** obj, const send_mam_res_t* const res) {
412+
status_t ret = SC_OK;
413+
cJSON* json_root = cJSON_CreateObject();
414+
if (json_root == NULL) {
415+
ret = SC_SERIALIZER_JSON_CREATE;
416+
goto done;
417+
}
418+
419+
cJSON_AddStringToObject(json_root, "channel", res->channel_id);
420+
421+
cJSON_AddStringToObject(json_root, "bundle_hash", res->bundle_hash);
422+
423+
*obj = cJSON_PrintUnformatted(json_root);
424+
if (*obj == NULL) {
425+
ret = SC_SERIALIZER_JSON_PARSE;
426+
goto done;
427+
}
428+
429+
done:
430+
cJSON_Delete(json_root);
431+
return ret;
432+
}
433+
434+
status_t send_mam_req_deserialize(const char* const obj, send_mam_req_t* req) {
435+
if (obj == NULL) {
436+
return SC_SERIALIZER_NULL;
437+
}
438+
cJSON* json_obj = cJSON_Parse(obj);
439+
cJSON* json_result = NULL;
440+
status_t ret = SC_OK;
441+
442+
if (json_obj == NULL) {
443+
ret = SC_SERIALIZER_JSON_PARSE;
444+
goto done;
445+
}
446+
447+
json_result = cJSON_GetObjectItemCaseSensitive(json_obj, "message");
448+
if (json_result != NULL) {
449+
size_t payload_size = strlen(json_result->valuestring) * 2;
450+
tryte_t* payload_trytes = (tryte_t*)malloc(payload_size * sizeof(tryte_t));
451+
ascii_to_trytes(json_result->valuestring, payload_trytes);
452+
453+
req->payload_trytes = payload_trytes;
454+
req->payload_trytes_size = payload_size;
455+
} else {
456+
ret = SC_SERIALIZER_NULL;
457+
}
458+
459+
done:
460+
cJSON_Delete(json_obj);
461+
return ret;
462+
}

serializer/serializer.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "accelerator/errors.h"
77
#include "cJSON.h"
88
#include "cclient/types/types.h"
9+
#include "common/trinary/tryte_ascii.h"
910
#include "request/request.h"
1011
#include "response/response.h"
1112

@@ -122,6 +123,30 @@ status_t ta_find_transactions_obj_res_serialize(
122123
* - non-zero on error
123124
*/
124125
status_t receive_mam_message_serialize(char** obj, const char** res);
126+
127+
/**
128+
* @brief Serialze type of send_mam_res_t to JSON string
129+
*
130+
* @param[out] obj send mam response object in JSON
131+
* @param[in] res Response data in type of send_mam_res_t
132+
*
133+
* @return
134+
* - SC_OK on success
135+
* - non-zero on error
136+
*/
137+
status_t send_mam_res_serialize(char** obj, const send_mam_res_t* const res);
138+
139+
/**
140+
* @brief Deserialze JSON string to type of send_mam_req_t
141+
*
142+
* @param[in] obj Input values in JSON
143+
* @param[out] req Request data in type of send_mam_req_t
144+
*
145+
* @return
146+
* - SC_OK on success
147+
* - non-zero on error
148+
*/
149+
status_t send_mam_req_deserialize(const char* const obj, send_mam_req_t* req);
125150
#ifdef __cplusplus
126151
}
127152
#endif

tests/driver.c

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -149,30 +149,16 @@ void test_find_transactions_obj_by_tag(void) {
149149

150150
void test_send_mam_message(void) {
151151
double sum = 0;
152-
153-
char* bundle_hash_result;
154-
char* channel_id_result;
152+
const char* json = "{\"message\":\"" TEST_PAYLOAD "\"}";
153+
char* json_result;
155154

156155
for (size_t count = 0; count < TEST_COUNT; count++) {
157156
test_time_start(&start_time);
158157
TEST_ASSERT_EQUAL_INT32(
159-
SC_OK,
160-
api_mam_send_message(&ta_core.tangle, &ta_core.service, TEST_PAYLOAD,
161-
&bundle_hash_result, &channel_id_result));
158+
SC_OK, api_mam_send_message(&ta_core.tangle, &ta_core.service, json,
159+
&json_result));
162160
test_time_end(&start_time, &end_time, &sum);
163-
164-
for (size_t i = 0; i < FLEX_TRIT_SIZE_243; i++) {
165-
printf("%c", channel_id_result[i]);
166-
}
167-
printf("\n");
168-
printf("Bundle: ");
169-
for (size_t i = 0; i < FLEX_TRIT_SIZE_243; i++) {
170-
printf("%c", bundle_hash_result[i]);
171-
}
172-
printf("\n");
173-
174-
free(bundle_hash_result);
175-
free(channel_id_result);
161+
free(json_result);
176162
}
177163
printf("Average time of receive_mam_message: %lf\n", sum / TEST_COUNT);
178164
}

tests/test_serializer.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,41 @@ void test_serialize_ta_find_transactions_obj_by_tag(void) {
251251
transaction_free(txn);
252252
free(json_result);
253253
}
254+
255+
void test_serialize_send_mam_message(void) {
256+
const char* json = "{\"channel\":\"" TRYTES_81_1
257+
"\","
258+
"\"bundle_hash\":\"" TRYTES_81_2 "\"}";
259+
char* json_result;
260+
send_mam_res_t* res = send_mam_res_new();
261+
262+
send_mam_res_set_bundle_hash(res, (tryte_t*)TRYTES_81_2);
263+
send_mam_res_set_channel_id(res, (tryte_t*)TRYTES_81_1);
264+
265+
send_mam_res_serialize(&json_result, res);
266+
TEST_ASSERT_EQUAL_STRING(json, json_result);
267+
268+
free(json_result);
269+
send_mam_res_free(&res);
270+
}
271+
272+
void test_deserialize_send_mam_message(void) {
273+
const char* json = "{\"message\":\"" TEST_PAYLOAD "\"}";
274+
send_mam_req_t* req = send_mam_req_new();
275+
276+
send_mam_req_deserialize(json, req);
277+
278+
size_t payload_size = strlen(TEST_PAYLOAD) * 2;
279+
tryte_t* payload_trytes = (tryte_t*)malloc(payload_size * sizeof(tryte_t));
280+
ascii_to_trytes(TEST_PAYLOAD, payload_trytes);
281+
282+
TEST_ASSERT_EQUAL_UINT(payload_size, req->payload_trytes_size);
283+
TEST_ASSERT_EQUAL_MEMORY(payload_trytes, req->payload_trytes, payload_size);
284+
285+
free(payload_trytes);
286+
send_mam_req_free(&req);
287+
}
288+
254289
int main(void) {
255290
UNITY_BEGIN();
256291

@@ -260,5 +295,7 @@ int main(void) {
260295
RUN_TEST(test_serialize_ta_get_transaction_object);
261296
RUN_TEST(test_serialize_ta_find_transactions_by_tag);
262297
RUN_TEST(test_serialize_ta_find_transactions_obj_by_tag);
298+
RUN_TEST(test_serialize_send_mam_message);
299+
RUN_TEST(test_deserialize_send_mam_message);
263300
return UNITY_END();
264301
}

0 commit comments

Comments
 (0)