-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample_provider.c
More file actions
105 lines (83 loc) · 2.51 KB
/
example_provider.c
File metadata and controls
105 lines (83 loc) · 2.51 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
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "UID_message.h"
#include "UID_dispatch.h"
// example of user-defined method
void user_33(char *param, char *result, size_t size)
{
snprintf(result, size, "you request: <%s>", param);
}
int perform_request(uint8_t *buffer, size_t size, uint8_t *response, size_t *rsize, UID_ServerChannelCtx *channel_ctx)
{
int ret;
int method;
int64_t sID;
char params[1024];
char result[1024] = {0};
int error;
// parse the request
ret = UID_parseReqMsg(buffer, size, &method, params, sizeof(params), &sID);
if (ret) return ret;
// check the contract for permission
if(UID_checkPermission(method, channel_ctx->contract.profile)) {
if (UID_RPC_RESERVED > method) {
// Uniquid method. call UID_performRequest
error = UID_performRequest(method, params, result, sizeof(result));
}
else {
// user method.
switch(method) {
case 33:
user_33(params, result, sizeof(result));
error = 0;
break;
default:
error = UID_DISPATCH_NOTEXISTENT;
break;
}
}
}
else {
// no permission for the method
error = UID_DISPATCH_NOPERMISSION;
}
// format the response message
ret = UID_formatRespMsg(&(channel_ctx->contract.path), result, error, sID, response, rsize);
if (ret) return ret;
return UID_MSG_OK;
}
/**
* thread implementing a Service Provider
*/
void* service_provider(void *arg)
{
(void)arg;
int ret;
// Provider infinite loop
while(1)
{
uint8_t msg[1024] = {0};
size_t size = 0;
// < Wait_for_Msg_from_user(msg, &size) >
// create the contest for the communication (contract, identities of the peers, etc)
UID_ServerChannelCtx sctx;
uint8_t sbuffer[1024];
size_t ssize = sizeof(sbuffer);
ret = UID_accept_channel(msg, size, &sctx, sbuffer, &ssize);
if ( UID_MSG_OK != ret) {
// < manage_error(ret) >
continue;
}
// perform the request
uint8_t response[1024];
size_t respsize = sizeof(response);
if ( UID_MSG_OK != (ret = perform_request(sbuffer, ssize, response, &respsize, &sctx))) {
// < manage_error(ret) >
continue;
}
// < Send_Msg_to_user(response, respsize - 1) >
UID_closeServerChannel(&sctx);
}
return NULL;
}