X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=examples%2Fmanynodes.c;h=3b617e4e5b517d7af0a0c33eeccf5cf249afa130;hb=31ab43d8a0691e773db6992fa3b52ca24f7f8db4;hp=3fa88c3b26decc143450a2abccbfad8b47c7a6a5;hpb=6d1ac53f5c34ece4c7a82efb432a8e1d81fcff0d;p=meshlink diff --git a/examples/manynodes.c b/examples/manynodes.c index 3fa88c3b..3b617e4e 100644 --- a/examples/manynodes.c +++ b/examples/manynodes.c @@ -7,13 +7,50 @@ #include #include "../src/meshlink.h" +#include "../src/devtools.h" -static int n = 100; +#include +#include +#include + +static int n = 10; static meshlink_handle_t **mesh; static meshlink_node_t **nodes; static size_t nnodes; +static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) { + const char *levelstr[] = { + [MESHLINK_DEBUG] = "\x1b[34mDEBUG", + [MESHLINK_INFO] = "\x1b[32mINFO", + [MESHLINK_WARNING] = "\x1b[33mWARNING", + [MESHLINK_ERROR] = "\x1b[31mERROR", + [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL", + }; + fprintf(stderr, "%s\t%s:\x1b[0m %s\n", mesh ? mesh->name : "global",levelstr[level], text); +} + +//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:\n", nnodes); + for(int i = 0; i < nnodes; i++) { + //printf(" %s\n", 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++) { @@ -30,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) @@ -56,11 +127,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"); @@ -92,11 +165,15 @@ static void parse_command(char *buf) { if(!node) { fprintf(stderr, "Unknown node '%s'\n", arg); } else { - printf("Node %s found\n", arg); + printf("Node %s found, pmtu %zd\n", arg, meshlink_get_pmtu(mesh[0], node)); } } } else if(!strcasecmp(buf, "link")) { linkmesh(); + } else if(!strcasecmp(buf, "eg")) { + exportmeshgraph(arg); + } else if(!strcasecmp(buf, "test")) { + testmesh(); } else if(!strcasecmp(buf, "quit")) { printf("Bye!\n"); fclose(stdin); @@ -109,6 +186,8 @@ static void parse_command(char *buf) { "/kick Blacklist the given node.\n" "/who [] List all nodes or show information about the given node.\n" "/link Link all nodes together.\n" + "/eg Export graph as json file.\n" + "/test Test functionality sending some data to all nodes\n" "/quit Exit this program.\n" ); } else { @@ -176,30 +255,36 @@ 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); + meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message); mkdir(basebase, 0750); 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"); + 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"); if(!mesh[i]) {