X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=examples%2Fchat.c;h=c6d67fb5c1ba2c111d4c814fdc1971699dc85ccc;hb=1aa7ef75218e1b45969ee60ce81e0a9b73beb774;hp=a735cb1a0b2f6a05cfe9f4aab85c5fb03d9167bf;hpb=fcbee2208944dcd0ab93d5469ea5eeb490977c45;p=meshlink diff --git a/examples/chat.c b/examples/chat.c index a735cb1a..c6d67fb5 100644 --- a/examples/chat.c +++ b/examples/chat.c @@ -1,11 +1,18 @@ #include #include #include +#include #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) { @@ -26,6 +33,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) @@ -41,7 +51,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; } @@ -52,11 +62,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"); @@ -65,16 +80,45 @@ 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; } meshlink_blacklist(mesh, node); printf("Node '%s' blacklisted.\n", arg); + } else if(!strcasecmp(buf, "who")) { + if(!arg) { + 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("%zu known nodes:", nnodes); + for(int i = 0; i < nnodes; i++) + printf(" %s", nodes[i]->name); + printf("\n"); + } + } else { + meshlink_node_t *node = meshlink_get_node(mesh, arg); + if(!node) { + fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); + } 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); } @@ -120,7 +164,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; } } @@ -131,7 +175,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; } @@ -149,9 +193,11 @@ int main(int argc, char *argv[]) { if(argc > 2) nick = argv[2]; - meshlink_handle_t *mesh = meshlink_open(confbase, nick); + 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!\n"); + fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno)); return 1; } @@ -160,11 +206,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);