X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=examples%2Fchat.c;h=6ffebb865ceaec0404df278beca4bb6002b803bc;hb=adbdea46d4b7364720b6b6a25fe45137494abbfc;hp=70d17d7d917bf6515aaf98d1fb97bea2f969df22;hpb=be457ad7e4857ac3b0cbb7e64c0ccd0e44ba21e3;p=meshlink diff --git a/examples/chat.c b/examples/chat.c index 70d17d7d..6ffebb86 100644 --- a/examples/chat.c +++ b/examples/chat.c @@ -27,6 +27,9 @@ static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool rea printf("%s left.\n", node->name); } +static meshlink_node_t **nodes; +static size_t nnodes; + static void parse_command(meshlink_handle_t *mesh, char *buf) { char *arg = strchr(buf, ' '); if(arg) @@ -42,7 +45,7 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { invitation = meshlink_invite(mesh, arg); if(!invitation) { - fprintf(stderr, "Could not invite '%s': %s\n", arg, mesh->errstr); + fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); return; } @@ -53,11 +56,16 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { fprintf(stderr, "/join requires an argument!\n"); return; } - + meshlink_stop(mesh); if(!meshlink_join(mesh, arg)) - fprintf(stderr, "Could not join using invitation: %s\n", mesh->errstr); - else + fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno)); + else { fprintf(stderr, "Invitation accepted!\n"); + if(!meshlink_start(mesh)) { + fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno)); + return; + } + } } else if(!strcasecmp(buf, "kick")) { if(!arg) { fprintf(stderr, "/kick requires an argument!\n"); @@ -66,7 +74,7 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { meshlink_node_t *node = meshlink_get_node(mesh, arg); if(!node) { - fprintf(stderr, "Unknown node '%s'\n", arg); + fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); return; } @@ -75,22 +83,19 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { printf("Node '%s' blacklisted.\n", arg); } else if(!strcasecmp(buf, "who")) { if(!arg) { - meshlink_node_t *nodes[100]; - size_t n = meshlink_get_all_nodes(mesh, nodes, 100); - if(!n) { - fprintf(stderr, "No nodes known!\n"); + nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes); + if(!nnodes) { + fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno)); } else { - printf("Known nodes:"); - for(int i = 0; i < n && i < 100; i++) + printf("%zu known nodes:", nnodes); + for(int i = 0; i < nnodes; i++) printf(" %s", nodes[i]->name); - if(n > 100) - printf(" (and %zu more)", n - 100); printf("\n"); } } else { meshlink_node_t *node = meshlink_get_node(mesh, arg); if(!node) { - fprintf(stderr, "Unknown node '%s'\n", arg); + fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); } else { printf("Node %s found\n", arg); } @@ -153,7 +158,7 @@ static void parse_input(meshlink_handle_t *mesh, char *buf) { destination = meshlink_get_node(mesh, buf); if(!destination) { - fprintf(stderr, "Unknown node '%s'\n", buf); + fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno)); return; } } @@ -164,7 +169,7 @@ static void parse_input(meshlink_handle_t *mesh, char *buf) { } if(!meshlink_send(mesh, destination, msg, strlen(msg) + 1)) { - fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, mesh->errstr); + fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno)); return; } @@ -184,7 +189,7 @@ int main(int argc, char *argv[]) { meshlink_handle_t *mesh = meshlink_open(confbase, nick); if(!mesh) { - fprintf(stderr, "Could not open MeshLink!\n"); + fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno)); return 1; } @@ -193,7 +198,7 @@ int main(int argc, char *argv[]) { meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message); if(!meshlink_start(mesh)) { - fprintf(stderr, "Could not start MeshLink: %s\n", mesh->errstr); + fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno)); return 1; }