9 static volatile bool duplicate_detected;
11 static void handle_duplicate(meshlink_handle_t *mesh, meshlink_node_t *node) {
12 meshlink_node_t *self = meshlink_get_self(mesh);
13 fprintf(stderr, "%s: detected duplicate node %s\n", self->name, node->name);
14 duplicate_detected = true;
15 meshlink_blacklist(mesh, node);
19 // Open meshlink instances
21 static const char *name[4] = {"foo", "bar", "baz", "foo"};
22 meshlink_handle_t *mesh[4];
24 for(int i = 0; i < 4; i++) {
26 snprintf(dirname, sizeof dirname, "duplicate_conf.%d", i);
28 mesh[i] = meshlink_open(dirname, name[i], "duplicate", DEV_CLASS_BACKBONE);
31 fprintf(stderr, "Could not initialize configuration for node %d\n", i);
35 meshlink_add_address(mesh[i], "localhost");
36 meshlink_enable_discovery(mesh[i], false);
38 meshlink_set_node_duplicate_cb(mesh[i], handle_duplicate);
41 // Link them in a chain
45 for(int i = 0; i < 4; i++) {
46 data[i] = meshlink_export(mesh[i]);
49 for(int i = 0; i < 3; i++) {
50 meshlink_import(mesh[i], data[i + 1]);
51 meshlink_import(mesh[i + 1], data[i]);
54 for(int i = 0; i < 4; i++) {
60 for(int i = 0; i < 4; i++) {
61 if(!meshlink_start(mesh[i])) {
62 fprintf(stderr, "Could not start mesh %d\n", i);
67 // Wait for the duplicate node to be detected
69 for(int i = 0; i < 20; i++) {
72 if(duplicate_detected) {
77 if(!duplicate_detected) {
78 fprintf(stderr, "Failed to detect duplicate node after 20 seconds\n");
84 for(int i = 0; i < 4; i++) {
85 meshlink_close(mesh[i]);