16 void init_sync_flag(struct sync_flag *s) {
17 assert(pthread_mutex_init(&s->mutex, NULL) == 0);
18 assert(pthread_cond_init(&s->cond, NULL) == 0);
22 void set_sync_flag(struct sync_flag *s, bool value) {
23 assert(pthread_mutex_lock(&s->mutex) == 0);
25 assert(pthread_cond_broadcast(&s->cond) == 0);
26 assert(pthread_mutex_unlock(&s->mutex) == 0);
29 void reset_sync_flag(struct sync_flag *s) {
30 assert(pthread_mutex_lock(&s->mutex) == 0);
32 assert(pthread_mutex_unlock(&s->mutex) == 0);
35 bool check_sync_flag(struct sync_flag *s) {
37 assert(pthread_mutex_lock(&s->mutex) == 0);
39 assert(pthread_mutex_unlock(&s->mutex) == 0);
43 bool wait_sync_flag(struct sync_flag *s, int seconds) {
44 struct timespec timeout;
45 clock_gettime(CLOCK_REALTIME, &timeout);
46 timeout.tv_sec += seconds;
48 assert(pthread_mutex_lock(&s->mutex) == 0);
51 if(!pthread_cond_timedwait(&s->cond, &s->mutex, &timeout) || errno != EINTR) {
56 assert(pthread_mutex_unlock(&s->mutex) == 0);
61 void link_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b) {
62 // Import and export both side's data
64 assert(meshlink_set_canonical_address(a, meshlink_get_self(a), "localhost", NULL));
65 assert(meshlink_set_canonical_address(b, meshlink_get_self(b), "localhost", NULL));
67 char *data = meshlink_export(a);
69 assert(meshlink_import(b, data));
72 data = meshlink_export(b);
74 assert(meshlink_import(a, data));
78 void open_meshlink_pair(meshlink_handle_t **pa, meshlink_handle_t **pb, const char *prefix) {
79 // Create two new MeshLink instances
83 char *a_name, *b_name;
85 assert(asprintf(&a_name, "%s_conf.1", prefix) > 0);
88 assert(asprintf(&b_name, "%s_conf.2", prefix) > 0);
91 assert(meshlink_destroy(a_name));
92 assert(meshlink_destroy(b_name));
94 meshlink_handle_t *a = meshlink_open(a_name, "a", prefix, DEV_CLASS_BACKBONE);
97 meshlink_handle_t *b = meshlink_open(b_name, "b", prefix, DEV_CLASS_BACKBONE);
103 meshlink_enable_discovery(a, false);
104 meshlink_enable_discovery(b, false);
106 link_meshlink_pair(a, b);
112 void open_meshlink_pair_ephemeral(meshlink_handle_t **pa, meshlink_handle_t **pb, const char *prefix) {
113 // Create two new MeshLink instances
117 meshlink_handle_t *a = meshlink_open_ephemeral("a", prefix, DEV_CLASS_BACKBONE);
118 meshlink_handle_t *b = meshlink_open_ephemeral("b", prefix, DEV_CLASS_BACKBONE);
123 meshlink_enable_discovery(a, false);
124 meshlink_enable_discovery(b, false);
126 link_meshlink_pair(a, b);
132 // Don't poll in the application thread, use a condition variable to signal when the peer is online.
133 static void pair_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
136 if(reachable && meshlink_get_self(mesh) != node) {
137 set_sync_flag(mesh->priv, true);
141 void start_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b) {
142 struct sync_flag pair_status = {.flag = false};
143 init_sync_flag(&pair_status);
145 a->priv = &pair_status;
146 meshlink_set_node_status_cb(a, pair_status_cb);
148 assert(meshlink_start(a));
149 assert(meshlink_start(b));
151 assert(wait_sync_flag(&pair_status, 5));
153 meshlink_set_node_status_cb(a, NULL);
157 void stop_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b) {
162 void close_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b) {
167 void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
168 static const char *levelstr[] = {
169 [MESHLINK_DEBUG] = "DEBUG",
170 [MESHLINK_INFO] = "INFO",
171 [MESHLINK_WARNING] = "WARNING",
172 [MESHLINK_ERROR] = "ERROR",
173 [MESHLINK_CRITICAL] = "CRITICAL",
176 static struct timespec ts0;
179 clock_gettime(CLOCK_MONOTONIC, &ts);
181 if(ts0.tv_sec == 0) {
185 float diff = (ts.tv_sec - ts0.tv_sec) + (ts.tv_nsec - ts0.tv_nsec) * 1e-9;
187 fprintf(stderr, "%7.3f (%s) [%s] %s\n",
189 mesh ? mesh->name : "",