-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample_user.c
More file actions
65 lines (47 loc) · 1.63 KB
/
example_user.c
File metadata and controls
65 lines (47 loc) · 1.63 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
#include <string.h>
#include "UID_message.h"
int parse_result(uint8_t *buffer, size_t size, UID_ClientChannelCtx *ctx, char *res, size_t rsize, int64_t id)
{
BTC_Address sender;
int error = 0;
int64_t sID;
int ret = UID_parseRespMsg(buffer, size, sender, sizeof(sender), &error, res, rsize, &sID);
if ( ret ) return ret;
if (error) return UID_MSG_RPC_ERROR | error;
if (strcmp(sender, ctx->contract.serviceProviderAddress)) return UID_MSG_INVALID_SENDER;
if (sID != id) return UID_MSG_ID_MISMATCH;
return 0;
}
void RPC_request(void)
{
int ret;
int method = 31;
// create the contest for the communication (contract, identities of the peers, etc)
UID_ClientChannelCtx ctx;
if ( UID_MSG_OK != (ret = UID_createChannel("Machine name", &ctx)) ) {
// < manage_error(ret) >
return;
}
// format the message
uint8_t buffer[1024];
size_t size = sizeof(buffer);
int64_t id;
if ( UID_MSG_OK != (ret = UID_formatReqMsg(&ctx.contract.path, method, "parameter to the method", buffer, &size, &id)) ) {
// < manage_error(ret) >
return;
}
// < Send_Msg_to_provider(buffer, size) >
uint8_t msg[1024] = {0};
size = sizeof(msg);
// < Wait_for_Msg_from_provider(msg, &size) >
// parse the received message
char result[1024] = "";
if ( UID_MSG_OK != (ret = parse_result(msg, size, &ctx, result, sizeof(result), id))) {
// < manage_error(ret) >
return;
}
// now you have the <result> from the execution on the provider
// of the requested method: you can use it as you need
// close the channel
UID_closeChannel(&ctx);
}