X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=examples%2Fchat.c;h=4d65d10eb04fd2ef0de3611b60537bc6b1ad4525;hb=196806f90f40fcc0cf727abd4bed6bc5aefff5ff;hp=ddb43bf7c477aa6eee66718e37d8208ea539de14;hpb=6d1ac53f5c34ece4c7a82efb432a8e1d81fcff0d;p=meshlink diff --git a/examples/chat.c b/examples/chat.c index ddb43bf7..4d65d10e 100644 --- a/examples/chat.c +++ b/examples/chat.c @@ -5,8 +5,14 @@ #include "../src/meshlink.h" static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) { - const char *levelstr[] = {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"}; - fprintf(stderr, "%s: %s\n", levelstr[level], 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:\x1b[0m %s\n", levelstr[level], text); } static void receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) { @@ -59,12 +65,11 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { meshlink_stop(mesh); if(!meshlink_join(mesh, arg)) fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno)); - else { + else fprintf(stderr, "Invitation accepted!\n"); - if(!meshlink_start(mesh)) { - fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno)); - return; - } + if(!meshlink_start(mesh)) { + fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno)); + exit(1); } } else if(!strcasecmp(buf, "kick")) { if(!arg) { @@ -84,9 +89,9 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { } else if(!strcasecmp(buf, "who")) { if(!arg) { nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes); - if(!nnodes) { + if(!nnodes) fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno)); - } else { + else { printf("%zu known nodes:", nnodes); for(int i = 0; i < nnodes; i++) printf(" %s", nodes[i]->name); @@ -94,28 +99,26 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { } } else { meshlink_node_t *node = meshlink_get_node(mesh, arg); - if(!node) { + if(!node) fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); - } else { + 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 { + ": 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); - } } static void parse_input(meshlink_handle_t *mesh, char *buf) { @@ -187,7 +190,9 @@ int main(int argc, char *argv[]) { if(argc > 2) nick = argv[2]; - meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat"); + meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message); + + meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY); if(!mesh) { fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno)); return 1;