X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=examples%2Fchat.c;h=1c07e422d07a2fa7efb20c015d94db81246be21f;hb=3ba46c8dbefbb1dfc6776aa8b0107956f038a870;hp=a735cb1a0b2f6a05cfe9f4aab85c5fb03d9167bf;hpb=fcbee2208944dcd0ab93d5469ea5eeb490977c45;p=meshlink diff --git a/examples/chat.c b/examples/chat.c index a735cb1a..1c07e422 100644 --- a/examples/chat.c +++ b/examples/chat.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "../src/meshlink.h" static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) { @@ -41,7 +42,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; } @@ -54,7 +55,7 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { } if(!meshlink_join(mesh, arg)) - fprintf(stderr, "Could not join using invitation: %s\n", mesh->errstr); + fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno)); else fprintf(stderr, "Invitation accepted!\n"); } else if(!strcasecmp(buf, "kick")) { @@ -72,9 +73,41 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { meshlink_blacklist(mesh, node); 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"); + } else { + printf("Known nodes:"); + for(int i = 0; i < n && i < 100; 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); + } else { + printf("Node %s found\n", arg); + } + } } else if(!strcasecmp(buf, "quit")) { printf("Bye!\n"); fclose(stdin); + } else if(!strcasecmp(buf, "help")) { + printf( + ": Send a message to the given node.\n" + " Subsequent messages don't need the : prefix.\n" + "/invite Create an invitation for a new node.\n" + "/join Join an existing mesh using an invitation.\n" + "/kick Blacklist the given node.\n" + "/who [] List all nodes or show information about the given node.\n" + "/quit Exit this program.\n" + ); } else { fprintf(stderr, "Unknown command '/%s'\n", buf); } @@ -131,7 +164,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; } @@ -160,11 +193,11 @@ 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; } - printf("Chat started.\n"); + printf("Chat started.\nType /help for a list of commands.\n"); while(fgets(buf, sizeof buf, stdin)) parse_input(mesh, buf);