]> git.meshlink.io Git - meshlink/blobdiff - examples/chat.c
Don't install the example programs.
[meshlink] / examples / chat.c
index 1c07e422d07a2fa7efb20c015d94db81246be21f..f5f41c936126c98393e1e53c1792d8e7267b2357 100644 (file)
@@ -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) {
@@ -27,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)
@@ -53,11 +62,15 @@ 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_strerror(meshlink_errno));
                else
                        fprintf(stderr, "Invitation accepted!\n");
+               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) {
                        fprintf(stderr, "/kick requires an argument!\n");
@@ -66,7 +79,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,42 +88,37 @@ 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");
-                       } else {
-                               printf("Known nodes:");
-                               for(int i = 0; i < n && i < 100; i++)
+                       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);
-                               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 {
+                       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(
-                       "<name>: <message>     Send a message to the given node.\n"
-                       "                      Subsequent messages don't need the <name>: prefix.\n"
-                       "/invite <name>        Create an invitation for a new node.\n"
-                       "/join <invitation>    Join an existing mesh using an invitation.\n"
-                       "/kick <name>          Blacklist the given node.\n"
-                       "/who [<name>]         List all nodes or show information about the given node.\n"
-                       "/quit                 Exit this program.\n"
-                       );
-       } else {
+                       "<name>: <message>     Send a message to the given node.\n"
+                       "                      Subsequent messages don't need the <name>: prefix.\n"
+                       "/invite <name>        Create an invitation for a new node.\n"
+                       "/join <invitation>    Join an existing mesh using an invitation.\n"
+                       "/kick <name>          Blacklist the given node.\n"
+                       "/who [<name>]         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) {
@@ -153,7 +161,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;
                }
        }
@@ -182,9 +190,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;
        }
 
@@ -199,7 +209,7 @@ int main(int argc, char *argv[]) {
 
        printf("Chat started.\nType /help for a list of commands.\n");
 
-       while(fgets(buf, sizeof buf, stdin))
+       while(fgets(buf, sizeof(buf), stdin))
                parse_input(mesh, buf);
 
        printf("Chat stopping.\n");