11 void set_sync_flag(struct sync_flag *s, bool value) {
12 pthread_mutex_lock(&s->mutex);
14 pthread_cond_broadcast(&s->cond);
15 pthread_mutex_unlock(&s->mutex);
18 bool wait_sync_flag(struct sync_flag *s, int seconds) {
19 struct timespec timeout;
20 clock_gettime(CLOCK_REALTIME, &timeout);
21 timeout.tv_sec += seconds;
24 if(!pthread_cond_timedwait(&s->cond, &s->mutex, &timeout) || errno != EINTR) {
31 void open_meshlink_pair(meshlink_handle_t **pa, meshlink_handle_t **pb, const char *prefix) {
32 // Create two new MeshLink instances
36 char *a_name, *b_name;
38 asprintf(&a_name, "%s_conf.1", prefix);
41 asprintf(&b_name, "%s_conf.2", prefix);
44 meshlink_handle_t *a = meshlink_open(a_name, "a", prefix, DEV_CLASS_BACKBONE);
47 meshlink_handle_t *b = meshlink_open(b_name, "b", prefix, DEV_CLASS_BACKBONE);
50 meshlink_enable_discovery(a, false);
51 meshlink_enable_discovery(b, false);
53 // Import and export both side's data
55 meshlink_add_address(a, "localhost");
57 char *data = meshlink_export(a);
59 assert(meshlink_import(b, data));
62 data = meshlink_export(b);
64 assert(meshlink_import(a, data));
71 // Don't poll in the application thread, use a condition variable to signal when the peer is online.
72 static void pair_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
75 set_sync_flag(mesh->priv, reachable);
78 void start_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b) {
79 struct sync_flag pair_status = {.flag = false};
81 a->priv = &pair_status;
82 meshlink_set_node_status_cb(a, pair_status_cb);
84 pthread_mutex_lock(&pair_status.mutex);
89 assert(wait_sync_flag(&pair_status, 5));
91 pthread_mutex_unlock(&pair_status.mutex);
93 meshlink_set_node_status_cb(a, NULL);
97 void stop_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b) {
102 void close_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b, const char *prefix) {
107 char *a_name, *b_name;
109 asprintf(&a_name, "%s_conf.1", prefix);
111 assert(meshlink_destroy(a_name));
113 asprintf(&b_name, "%s_conf.2", prefix);
115 assert(meshlink_destroy(b_name));