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 check_sync_flag(struct sync_flag *s) {
20 pthread_mutex_lock(&s->mutex);
22 pthread_mutex_unlock(&s->mutex);
26 bool wait_sync_flag(struct sync_flag *s, int seconds) {
27 struct timespec timeout;
28 clock_gettime(CLOCK_REALTIME, &timeout);
29 timeout.tv_sec += seconds;
31 pthread_mutex_lock(&s->mutex);
34 if(!pthread_cond_timedwait(&s->cond, &s->mutex, &timeout) || errno != EINTR) {
39 pthread_mutex_unlock(&s->mutex);
44 void link_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b) {
45 // Import and export both side's data
47 meshlink_add_address(a, "localhost");
48 meshlink_add_address(b, "localhost");
50 char *data = meshlink_export(a);
52 assert(meshlink_import(b, data));
55 data = meshlink_export(b);
57 assert(meshlink_import(a, data));
61 void open_meshlink_pair(meshlink_handle_t **pa, meshlink_handle_t **pb, const char *prefix) {
62 // Create two new MeshLink instances
66 char *a_name, *b_name;
70 ret_val = asprintf(&a_name, "%s_conf.1", prefix);
73 ret_val = asprintf(&b_name, "%s_conf.2", prefix);
76 meshlink_handle_t *a = meshlink_open(a_name, "a", prefix, DEV_CLASS_BACKBONE);
79 meshlink_handle_t *b = meshlink_open(b_name, "b", prefix, DEV_CLASS_BACKBONE);
82 meshlink_enable_discovery(a, false);
83 meshlink_enable_discovery(b, false);
85 link_meshlink_pair(a, b);
91 // Don't poll in the application thread, use a condition variable to signal when the peer is online.
92 static void pair_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
95 set_sync_flag(mesh->priv, reachable);
98 void start_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b) {
99 struct sync_flag pair_status = {.flag = false};
101 a->priv = &pair_status;
102 meshlink_set_node_status_cb(a, pair_status_cb);
107 assert(wait_sync_flag(&pair_status, 5));
109 meshlink_set_node_status_cb(a, NULL);
113 void stop_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b) {
118 void close_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b, const char *prefix) {
125 char *a_name, *b_name;
127 ret_val = asprintf(&a_name, "%s_conf.1", prefix);
129 assert(meshlink_destroy(a_name));
131 ret_val = asprintf(&b_name, "%s_conf.2", prefix);
133 assert(meshlink_destroy(b_name));