11 void set_sync_flag(struct sync_flag *s) {
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)
30 void open_meshlink_pair(meshlink_handle_t **pa, meshlink_handle_t **pb, const char *prefix) {
31 // Create two new MeshLink instances
35 char *a_name, *b_name;
37 asprintf(&a_name, "%s_conf.1", prefix);
40 asprintf(&b_name, "%s_conf.2", prefix);
43 meshlink_handle_t *a = meshlink_open(a_name, "a", prefix, DEV_CLASS_BACKBONE);
46 meshlink_handle_t *b = meshlink_open(b_name, "b", prefix, DEV_CLASS_BACKBONE);
49 meshlink_enable_discovery(a, false);
50 meshlink_enable_discovery(b, false);
52 // Import and export both side's data
54 meshlink_add_address(a, "localhost");
56 char *data = meshlink_export(a);
58 assert(meshlink_import(b, data));
61 data = meshlink_export(b);
63 assert(meshlink_import(a, data));
70 // Don't poll in the application thread, use a condition variable to signal when the peer is online.
71 static void pair_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
72 set_sync_flag(mesh->priv);
75 void start_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b) {
76 struct sync_flag pair_status = {};
78 a->priv = &pair_status;
79 meshlink_set_node_status_cb(a, pair_status_cb);
81 pthread_mutex_lock(&pair_status.mutex);
86 assert(wait_sync_flag(&pair_status, 5));
88 pthread_mutex_unlock(&pair_status.mutex);
90 meshlink_set_node_status_cb(a, NULL);
94 void stop_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b) {
99 void close_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b, const char *prefix) {
104 char *a_name, *b_name;
106 asprintf(&a_name, "%s_conf.1", prefix);
108 assert(meshlink_destroy(a_name));
110 asprintf(&b_name, "%s_conf.2", prefix);
112 assert(meshlink_destroy(b_name));