X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=examples%2Fchat.c;h=c6d67fb5c1ba2c111d4c814fdc1971699dc85ccc;hb=a1ca2b23296707172f0e315a1edc186eaccf5551;hp=84e3dadb6930ffc128ed6254739298b0f691102a;hpb=1d91eecd5611005de957d4821b8bb205000ed57f;p=meshlink diff --git a/examples/chat.c b/examples/chat.c index 84e3dadb..c6d67fb5 100644 --- a/examples/chat.c +++ b/examples/chat.c @@ -1,15 +1,29 @@ #include #include #include +#include #include "../src/meshlink.h" -static void receive(meshlink_handle_t *mesh, meshlink_node_t *source, const char *data, size_t len) { - if(!len || data[len - 1]) { +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:\x1b[0m %s\n", levelstr[level], text); +} + +static void receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) { + const char *msg = data; + + if(!len || msg[len - 1]) { fprintf(stderr, "Received invalid data from %s\n", source->name); return; } - printf("%s says: %s\n", source->name, data); + printf("%s says: %s\n", source->name, msg); } static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { @@ -19,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) @@ -34,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, meshlink_errstr); + fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); return; } @@ -45,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", meshlink_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"); @@ -58,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); } @@ -113,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; } } @@ -124,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, meshlink_errstr); + fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno)); return; } @@ -142,21 +193,24 @@ int main(int argc, char *argv[]) { if(argc > 2) nick = argv[2]; - 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: %s\n", meshlink_errstr); + fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno)); return 1; } - meshlink_set_receive_cb(receive); - meshlink_set_node_status_cb(node_changed); + meshlink_set_receive_cb(mesh, receive); + meshlink_set_node_status_cb(mesh, node_status); + meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message); if(!meshlink_start(mesh)) { - fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_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);