]> git.meshlink.io Git - meshlink/blobdiff - examples/manynodes.c
json export of node and edge structure
[meshlink] / examples / manynodes.c
index 18299d9bb5fe9f60d1896d9e75a81d04f7958325..3b617e4e5b517d7af0a0c33eeccf5cf249afa130 100644 (file)
@@ -7,6 +7,11 @@
 #include <linux/limits.h>
 
 #include "../src/meshlink.h"
+#include "../src/devtools.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
 
 static int n = 10;
 static meshlink_handle_t **mesh;
@@ -37,11 +42,9 @@ static void testmesh () {
                                printf("%zu known nodes:\n", nnodes);
                                for(int i = 0; i < nnodes; i++) {
                                        //printf(" %s\n", nodes[i]->name);
-                                       if (nindex != i) {
                                                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));
                                                }
-                                       }
                                }
 
                        }
@@ -64,6 +67,40 @@ static void linkmesh() {
        }
 }
 
+static bool exportmeshgraph(const char* path)
+{
+       struct stat ps;
+       int psr = stat(path, &ps);
+
+       if(psr == 0 || errno != ENOENT)
+       {
+               if(psr == -1)
+                       { perror("stat"); }
+               else
+                       { fprintf(stderr, "%s exists already\n", path); }
+
+               return false;
+       }
+
+       FILE* stream = fopen(path, "w");
+
+       if(!stream)
+       {
+               perror("stream");
+               return false;
+       }
+
+       if(!devtool_export_json_all_edges_state(mesh[0], stream))
+       {
+               fclose(stream);
+               fprintf(stderr, "could not export graph\n");
+               return false;
+       }
+
+       fclose(stream);
+       return true;
+}
+
 static void parse_command(char *buf) {
        char *arg = strchr(buf, ' ');
        if(arg)
@@ -133,6 +170,8 @@ static void parse_command(char *buf) {
                }
        } else if(!strcasecmp(buf, "link")) {
                linkmesh();
+       } else if(!strcasecmp(buf, "eg")) {
+               exportmeshgraph(arg);
        } else if(!strcasecmp(buf, "test")) {
                testmesh();
        } else if(!strcasecmp(buf, "quit")) {
@@ -147,6 +186,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"
+                       "/eg <path>            Export graph as json file.\n"
                        "/test                 Test functionality sending some data to all nodes\n"
                        "/quit                 Exit this program.\n"
                        );
@@ -234,7 +274,7 @@ int main(int argc, char *argv[]) {
 
        mesh = calloc(n, sizeof *mesh);
 
-       meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
+       meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message);
        mkdir(basebase, 0750);
 
        char filename[PATH_MAX];
@@ -243,7 +283,7 @@ int main(int argc, char *argv[]) {
                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", STATIONARY);
+               mesh[i] = meshlink_open(filename, nodename, "manynodes", i%_DEV_CLASS_MAX);
                meshlink_set_log_cb(mesh[i], MESHLINK_INFO, log_message);
                if(itsnew)
                        meshlink_add_address(mesh[i], "localhost");