/
example.c
149 lines (128 loc) · 5.03 KB
/
example.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "drasyl.h"
#include "libdrasyl.h"
void console_logger(graal_isolatethread_t* thread, int level, unsigned long time, char* message) {
char level_marker[6];
switch (level) {
case DRASYL_LOG_INFO:
strcpy(level_marker, "INFO ");
break;
case DRASYL_LOG_WARN:
strcpy(level_marker, "WARN ");
break;
case DRASYL_LOG_ERROR:
strcpy(level_marker, "ERROR");
break;
default:
return;
}
printf("%ld %s : %s\n", time, level_marker, message);
}
void on_drasyl_event(graal_isolatethread_t* thread, drasyl_event_t* event) {
switch (event->event_code) {
case DRASYL_EVENT_NODE_UP:
printf("Node `%.64s` started.\n", event->node->identity->identity_public_key);
break;
case DRASYL_EVENT_NODE_DOWN:
printf("Node `%.64s` is shutting down.\n", event->node->identity->identity_public_key);
break;
case DRASYL_EVENT_NODE_ONLINE:
printf("Node `%.64s` is now online.\n", event->node->identity->identity_public_key);
break;
case DRASYL_EVENT_NODE_OFFLINE:
printf("Node `%.64s` is now offline.\n", event->node->identity->identity_public_key);
break;
case DRASYL_EVENT_NODE_UNRECOVERABLE_ERROR:
printf("Node `%.64s` failed to start.\n", event->node->identity->identity_public_key);
exit(1);
break;
case DRASYL_EVENT_NODE_NORMAL_TERMINATION:
printf("Node `%.64s` shut down.\n", event->node->identity->identity_public_key);
break;
case DRASYL_EVENT_PEER_DIRECT:
printf("Direct connection to peer `%.64s`.\n", event->peer->address);
break;
case DRASYL_EVENT_PEER_RELAY:
printf("Relayed connection to peer `%.64s`.\n", event->peer->address);
break;
case DRASYL_EVENT_LONG_TIME_ENCRYPTION:
printf("Long time encryption to peer `%.64s`.\n", event->peer->address);
break;
case DRASYL_EVENT_PERFECT_FORWARD_SECRECY_ENCRYPTION:
printf("Perfect forward secrecy encryption to peer `%.64s`.\n", event->peer->address);
break;
case DRASYL_EVENT_MESSAGE:
printf("Node received from peer `%.64s` message `%.*s`.\n", event->message_sender, event->message_payload_len, event->message_payload);
break;
case DRASYL_EVENT_INBOUND_EXCEPTION:
printf("Node faced error while receiving message.\n");
break;
default:
printf("Unknown event code received: %d\n", event->event_code);
}
}
int main(int argc, char **argv) {
graal_isolate_t *isolate = NULL;
graal_isolatethread_t *thread = NULL;
if (graal_create_isolate(NULL, &isolate, &thread) != 0) {
fprintf(stderr, "initialization error\n");
goto clean_up;
}
if (drasyl_set_logger(thread, &console_logger) != 0) {
fprintf(stderr, "could net set logger\n");
goto clean_up;
}
int version = drasyl_node_version(thread);
printf("drasyl node version: %i.%i.%i\n", (version >> 24) & 0xff, (version >> 16) & 0xff, (version >> 8) & 0xff);
char config[] = "my-node.conf";
if (drasyl_node_init(thread, config, sizeof(config), &on_drasyl_event) != DRASYL_SUCCESS) {
fprintf(stderr, "could not init node\n");
goto clean_up;
}
drasyl_identity_t *identity = calloc(1, sizeof(drasyl_identity_t));
if (drasyl_node_identity(thread, identity) != DRASYL_SUCCESS) {
fprintf(stderr, "could not retrieve node identity\n");
goto clean_up;
}
printf("My address: %.64s\n", identity->identity_public_key);
// try to free memory
free(identity);
if (drasyl_node_start(thread) != DRASYL_SUCCESS) {
fprintf(stderr, "could not start node\n");
goto clean_up;
}
printf("Wait for node to become online...\n");
while (!drasyl_node_is_online(thread)) {
drasyl_sleep(thread, 50);
}
char recipient[] = "78483253e5dbbe8f401dd1bd1ef0b6f1830c46e411f611dc93a664c1e44cc054";
char payload[] = "hello there";
if (drasyl_node_send(thread, recipient, payload, sizeof(payload)) != DRASYL_SUCCESS) {
fprintf(stderr, "could not send message\n");
goto clean_up;
}
drasyl_sleep(thread, 10000);
if (drasyl_node_stop(thread) != DRASYL_SUCCESS) {
fprintf(stderr, "could not stop node\n");
goto clean_up;
}
goto clean_up_2;
clean_up:
if (drasyl_shutdown_event_loop(thread) != DRASYL_SUCCESS) {
fprintf(stderr, "could not shutdown event loop\n");
graal_tear_down_isolate(thread);
return 1;
}
graal_tear_down_isolate(thread);
return 1;
clean_up_2:
if (drasyl_shutdown_event_loop(thread) != DRASYL_SUCCESS) {
fprintf(stderr, "could not shutdown event loop\n");
graal_tear_down_isolate(thread);
return 1;
}
graal_tear_down_isolate(thread);
return 0;
}