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);
21 void set_sync_flag(struct sync_flag *s, bool value) {
22 assert(pthread_mutex_lock(&s->mutex) == 0);
24 assert(pthread_cond_broadcast(&s->cond) == 0);
25 assert(pthread_mutex_unlock(&s->mutex) == 0);
28 bool check_sync_flag(struct sync_flag *s) {
30 assert(pthread_mutex_lock(&s->mutex) == 0);
32 assert(pthread_mutex_unlock(&s->mutex) == 0);
36 bool wait_sync_flag(struct sync_flag *s, int seconds) {
37 struct timespec timeout;
38 clock_gettime(CLOCK_REALTIME, &timeout);
39 timeout.tv_sec += seconds;
41 assert(pthread_mutex_lock(&s->mutex) == 0);
44 if(!pthread_cond_timedwait(&s->cond, &s->mutex, &timeout) || errno != EINTR) {
49 assert(pthread_mutex_unlock(&s->mutex) == 0);
54 void link_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b) {
55 // Import and export both side's data
57 assert(meshlink_set_canonical_address(a, meshlink_get_self(a), "localhost", NULL));
58 assert(meshlink_set_canonical_address(b, meshlink_get_self(b), "localhost", NULL));
60 char *data = meshlink_export(a);
62 assert(meshlink_import(b, data));
65 data = meshlink_export(b);
67 assert(meshlink_import(a, data));
71 void open_meshlink_pair(meshlink_handle_t **pa, meshlink_handle_t **pb, const char *prefix) {
72 // Create two new MeshLink instances
76 char *a_name, *b_name;
78 assert(asprintf(&a_name, "%s_conf.1", prefix) > 0);
81 assert(asprintf(&b_name, "%s_conf.2", prefix) > 0);
84 assert(meshlink_destroy(a_name));
85 assert(meshlink_destroy(b_name));
87 meshlink_handle_t *a = meshlink_open(a_name, "a", prefix, DEV_CLASS_BACKBONE);
90 meshlink_handle_t *b = meshlink_open(b_name, "b", prefix, DEV_CLASS_BACKBONE);
96 meshlink_enable_discovery(a, false);
97 meshlink_enable_discovery(b, false);
99 link_meshlink_pair(a, b);
105 // Don't poll in the application thread, use a condition variable to signal when the peer is online.
106 static void pair_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
110 set_sync_flag(mesh->priv, true);
114 void start_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b) {
115 struct sync_flag pair_status = {.flag = false};
116 init_sync_flag(&pair_status);
118 a->priv = &pair_status;
119 meshlink_set_node_status_cb(a, pair_status_cb);
121 assert(meshlink_start(a));
122 assert(meshlink_start(b));
124 assert(wait_sync_flag(&pair_status, 5));
126 meshlink_set_node_status_cb(a, NULL);
130 void stop_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b) {
135 void close_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b) {
140 void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
141 static const char *levelstr[] = {
142 [MESHLINK_DEBUG] = "DEBUG",
143 [MESHLINK_INFO] = "INFO",
144 [MESHLINK_WARNING] = "WARNING",
145 [MESHLINK_ERROR] = "ERROR",
146 [MESHLINK_CRITICAL] = "CRITICAL",
149 static struct timespec ts0;
152 clock_gettime(CLOCK_MONOTONIC, &ts);
154 if(ts0.tv_sec == 0) {
158 float diff = (ts.tv_sec - ts0.tv_sec) + (ts.tv_nsec - ts0.tv_nsec) * 1e-9;
160 fprintf(stderr, "%7.3f (%s) [%s] %s\n",
162 mesh ? mesh->name : "",