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

Commit 894f4d7

Browse files
authored
Merge pull request #139 from wusyong/send_mam_serialize
feat(serializer): Add send_mam_message (de)serializer
2 parents bfc2b6a + fea96de commit 894f4d7

File tree

18 files changed

+191
-128
lines changed

18 files changed

+191
-128
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
)

accelerator/apis.c

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,7 @@ status_t api_receive_mam_message(const iota_client_service_t* const service,
223223

224224
status_t api_mam_send_message(const iota_config_t* const tangle,
225225
const iota_client_service_t* const service,
226-
char const* const payload,
227-
char** bundle_hash_result,
228-
char** channel_id_result) {
226+
char const* const payload, char** json_result) {
229227
status_t ret = SC_OK;
230228
mam_api_t mam;
231229
const bool last_packet = true;
@@ -234,24 +232,12 @@ status_t api_mam_send_message(const iota_config_t* const tangle,
234232
bundle_transactions_new(&bundle);
235233
tryte_t channel_id[MAM_CHANNEL_ID_SIZE];
236234
trit_t msg_id[MAM_MSG_ID_SIZE];
235+
send_mam_req_t* req = send_mam_req_new();
236+
send_mam_res_t* res = send_mam_res_new();
237237

238-
size_t payload_size = strlen(payload) * 2;
239-
if ((payload_size == 0) || ((payload_size * 3) > SIZE_MAX)) {
240-
return SC_MAM_OOM;
241-
}
242-
tryte_t* payload_trytes = (tryte_t*)malloc(payload_size * sizeof(tryte_t));
243-
if (!payload_trytes) {
244-
return SC_MAM_OOM;
245-
}
246-
ascii_to_trytes(payload, payload_trytes);
247-
248-
*bundle_hash_result = (tryte_t*)malloc(sizeof(tryte_t) * NUM_TRYTES_ADDRESS);
249-
if (!(*bundle_hash_result)) {
250-
return SC_MAM_OOM;
251-
}
252-
*channel_id_result = (tryte_t*)malloc(sizeof(tryte_t) * NUM_TRYTES_ADDRESS);
253-
if (!(*channel_id_result)) {
254-
return SC_MAM_OOM;
238+
ret = send_mam_req_deserialize(payload, req);
239+
if (ret) {
240+
goto done;
255241
}
256242

257243
// Creating MAM API
@@ -281,21 +267,23 @@ status_t api_mam_send_message(const iota_config_t* const tangle,
281267
ret = SC_MAM_FAILED_WRITE;
282268
goto done;
283269
}
284-
if (mam_api_bundle_write_packet(&mam, msg_id, payload_trytes, payload_size, 0,
285-
last_packet, bundle) != RC_OK) {
270+
if (mam_api_bundle_write_packet(&mam, msg_id, req->payload_trytes,
271+
req->payload_trytes_size, 0, last_packet,
272+
bundle) != RC_OK) {
286273
ret = SC_MAM_FAILED_WRITE;
287274
goto done;
288275
}
289-
memcpy(*channel_id_result, channel_id, sizeof(tryte_t) * NUM_TRYTES_ADDRESS);
276+
send_mam_res_set_channel_id(res, channel_id);
290277

291278
// Sending bundle
292279
if (ta_send_bundle(tangle, service, bundle) != SC_OK) {
293280
ret = SC_MAM_FAILED_RESPONSE;
294281
goto done;
295282
}
296-
memcpy(*bundle_hash_result,
297-
((iota_transaction_t*)utarray_front(bundle))->essence.bundle,
298-
sizeof(tryte_t) * NUM_TRYTES_ADDRESS);
283+
send_mam_res_set_bundle_hash(
284+
res, transaction_bundle((iota_transaction_t*)utarray_front(bundle)));
285+
286+
ret = send_mam_res_serialize(json_result, res);
299287

300288
done:
301289
// Destroying MAM API
@@ -304,9 +292,10 @@ status_t api_mam_send_message(const iota_config_t* const tangle,
304292
ret = SC_MAM_FAILED_DESTROYED;
305293
}
306294
}
307-
free(payload_trytes);
308295
mam_psk_t_set_free(&psks);
309296
bundle_transactions_free(&bundle);
297+
send_mam_req_free(&req);
298+
send_mam_res_free(&res);
310299
return ret;
311300
}
312301

accelerator/apis.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "accelerator/errors.h"
66
#include "cclient/types/types.h"
77
#include "common/trinary/trit_tryte.h"
8-
#include "common/trinary/tryte_ascii.h"
98
#include "mam/api/api.h"
109
#include "mam/mam/mam_channel_t_set.h"
1110
#include "serializer/serializer.h"
@@ -102,18 +101,15 @@ status_t api_receive_mam_message(const iota_client_service_t* const service,
102101
* @param[in] tangle IOTA API parameter configurations
103102
* @param[in] service IRI node end point service
104103
* @param[in] payload message to send undecoded ascii string.
105-
* @param[out] bundle_hashes_result the bundle hash of sent message
106-
* @param[out] channel_id_result the channel id the sent message to
104+
* @param[out] json_result Result containing channel id and bundle hash
107105
*
108106
* @return
109107
* - SC_OK on success
110108
* - non-zero on error
111109
*/
112110
status_t api_mam_send_message(const iota_config_t* const tangle,
113111
const iota_client_service_t* const service,
114-
char const* const payload,
115-
char** bundle_hash_result,
116-
char** channel_id_result);
112+
char const* const payload, char** json_result);
117113

118114
/**
119115
* @brief Send transfer to tangle.

accelerator/server.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ int main(int, char const**) {
8787
.post([&](served::response& res, const served::request& req) {
8888
status_t ret = SC_OK;
8989
char* json_result;
90-
char* chid_result;
9190

9291
if (req.header("content-type").find("application/json") ==
9392
std::string::npos) {
@@ -100,7 +99,7 @@ int main(int, char const**) {
10099
cJSON_Delete(json_obj);
101100
} else {
102101
api_mam_send_message(&ta_core.tangle, &ta_core.service,
103-
req.body().c_str(), &json_result, &chid_result);
102+
req.body().c_str(), &json_result);
104103
ret = set_response_content(ret, &json_result);
105104
res.set_status(ret);
106105
}

request/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ cc_library(
99
include_prefix = "request",
1010
visibility = ["//visibility:public"],
1111
deps = [
12-
"@entangled//cclient/types",
1312
"//accelerator:ta_errors",
13+
"@entangled//cclient/types",
1414
],
1515
)

request/request.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef REQUEST_REQUEST_H_
22
#define REQUEST_REQUEST_H_
33

4-
#include "request/ta_send_mam_req.h"
4+
#include "request/ta_send_mam.h"
55
#include "request/ta_send_transfer.h"
66

77
/**

request/ta_send_mam_req.c renamed to request/ta_send_mam.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "ta_send_mam_req.h"
1+
#include "ta_send_mam.h"
22

33
send_mam_req_t* send_mam_req_new() {
44
send_mam_req_t* req = (send_mam_req_t*)malloc(sizeof(send_mam_req_t));

request/ta_send_mam_req.h renamed to request/ta_send_mam.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef REQUEST_TA_SEND_MAM_REQ_H_
2-
#define REQUEST_TA_SEND_MAM_REQ_H_
1+
#ifndef REQUEST_TA_SEND_MAM_H_
2+
#define REQUEST_TA_SEND_MAM_H_
33

44
#include "accelerator/errors.h"
55
#include "cclient/types/types.h"
@@ -21,4 +21,4 @@ void send_mam_req_free(send_mam_req_t** req);
2121
}
2222
#endif
2323

24-
#endif // REQUEST_TA_SEND_MAM_REQ_H_
24+
#endif // REQUEST_TA_SEND_MAM_H_

response/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ cc_library(
99
include_prefix = "response",
1010
visibility = ["//visibility:public"],
1111
deps = [
12+
"//accelerator:ta_errors",
1213
"@entangled//cclient/types",
1314
"@entangled//common/model:transaction",
14-
"//accelerator:ta_errors",
1515
],
1616
)

response/response.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "response/ta_generate_address.h"
77
#include "response/ta_get_tips.h"
88
#include "response/ta_get_transaction_object.h"
9-
#include "response/ta_send_mam_res.h"
9+
#include "response/ta_send_mam.h"
1010
#include "response/ta_send_transfer.h"
1111

1212
/**

0 commit comments

Comments
 (0)