]> git.meshlink.io Git - meshlink/blobdiff - examples/manynodes.c
manynodes example: committed /test command to test if it is possible to succefully...
[meshlink] / examples / manynodes.c
index 3fa88c3b26decc143450a2abccbfad8b47c7a6a5..2c154f94b8bbd73baa4b1d99f8dd6deae0063d96 100644 (file)
@@ -14,6 +14,27 @@ static meshlink_handle_t **mesh;
 static meshlink_node_t **nodes;
 static size_t nnodes;
 
+//Test mesh sending data
+static void testmesh () {
+
+       for(int nindex = 0; nindex < n; nindex++) {
+
+                       nodes = meshlink_get_all_nodes(mesh[nindex], nodes, &nnodes);
+                       if(!nodes) {
+                               fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
+                       } else {
+                               printf("%zu known nodes:", nnodes);
+                               for(int i = 0; i < nnodes; i++) {
+                                       printf(" %s", nodes[i]->name);
+                                       if(!meshlink_send(mesh[nindex], nodes[i], "magic", strlen("magic") + 1)) {
+               fprintf(stderr, "Could not send message to '%s': %s\n", nodes[i]->name, meshlink_strerror(meshlink_errno));
+       }
+       }
+
+                       }
+
+       }
+}
 // Make all nodes know about each other by importing each others public keys and addresses.
 static void linkmesh() {
        for(int i = 0; i < n; i++) {
@@ -56,11 +77,13 @@ static void parse_command(char *buf) {
                        fprintf(stderr, "/join requires an argument!\n");
                        return;
                }
-
+               meshlink_stop(mesh[0]);
                if(!meshlink_join(mesh[0], arg))
                        fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
-               else
+               else {
                        fprintf(stderr, "Invitation accepted!\n");
+                       meshlink_start(mesh[0]);
+               }
        } else if(!strcasecmp(buf, "kick")) {
                if(!arg) {
                        fprintf(stderr, "/kick requires an argument!\n");
@@ -97,6 +120,8 @@ static void parse_command(char *buf) {
                }
        } else if(!strcasecmp(buf, "link")) {
                linkmesh();
+       } else if(!strcasecmp(buf, "test")) {
+               testmesh();
        } else if(!strcasecmp(buf, "quit")) {
                printf("Bye!\n");
                fclose(stdin);
@@ -109,6 +134,7 @@ static void parse_command(char *buf) {
                        "/kick <name>          Blacklist the given node.\n"
                        "/who [<name>]         List all nodes or show information about the given node.\n"
                        "/link                 Link all nodes together.\n"
+                       "/test                 Test functionality sending some data to all nodes\n"
                        "/quit                 Exit this program.\n"
                        );
        } else {
@@ -176,19 +202,23 @@ static void parse_input(char *buf) {
 
 int main(int argc, char *argv[]) {
        const char *basebase = ".manynodes";
+       const char *namesprefix = "machine1";
        char buf[1024];
 
        if(argc > 1)
                n = atoi(argv[1]);
 
        if(n < 1) {
-               fprintf(stderr, "Usage: %s [number of local nodes] [confbase]\n", argv[0]);
+               fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames]\n", argv[0]);
                return 1;
        }
 
        if(argc > 2)
                basebase = argv[2];
 
+       if(argc > 3)
+               namesprefix = argv[3];
+
        mesh = calloc(n, sizeof *mesh);
 
        mkdir(basebase, 0750);
@@ -196,7 +226,7 @@ int main(int argc, char *argv[]) {
        char filename[PATH_MAX];
        char nodename[100];
        for(int i = 0; i < n; i++) {
-               snprintf(nodename, sizeof nodename, "node%d", i);
+               snprintf(nodename, sizeof nodename, "%snode%d", namesprefix,i);
                snprintf(filename, sizeof filename, "%s/%s", basebase, nodename);
                bool itsnew = access(filename, R_OK);
                mesh[i] = meshlink_open(filename, nodename, "manynodes");