]> git.meshlink.io Git - meshlink/blobdiff - examples/chatpp.cc
Don't install the example programs.
[meshlink] / examples / chatpp.cc
index 864a3e809b7258d20f8dcc7dcf561d3bb4c1d4aa..b5fe44e63b27367fd8c9a7a5663996cf2e20ebba 100644 (file)
@@ -4,28 +4,34 @@
 #include <strings.h>
 #include "../src/meshlink++.h"
 
-static void log_message(meshlink::mesh *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);
-}
+class ChatMesh : public meshlink::mesh {
+public:
+       void log(meshlink::log_level_t level, const char *text) {
+               const char *levelstr[] = {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"};
+               fprintf(stderr, "%s: %s\n", levelstr[level], text);
+       }
 
-static void receive(meshlink::mesh *mesh, meshlink::node *source, const void *data, size_t len) {
-       const char *msg = (const char *)data;
+       void receive(meshlink::node *source, const void *data, size_t len) {
+               const char *msg = (const char *)data;
 
-       if(!len || msg[len - 1]) {
-               fprintf(stderr, "Received invalid data from %s\n", source->name);
-               return;
+               if(!len || msg[len - 1]) {
+                       fprintf(stderr, "Received invalid data from %s\n", source->name);
+                       return;
+               }
+
+               printf("%s says: %s\n", source->name, msg);
        }
 
-       printf("%s says: %s\n", source->name, msg);
-}
+       void node_status(meshlink::node *node, bool reachable) {
+               if(reachable)
+                       printf("%s joined.\n", node->name);
+               else
+                       printf("%s left.\n", node->name);
+       }
+};
 
-static void node_status(meshlink::mesh *mesh, meshlink::node *node, bool reachable) {
-       if(reachable)
-               printf("%s joined.\n", node->name);
-       else
-               printf("%s left.\n", node->name);
-}
+static meshlink::node **nodes;
+static size_t nnodes;
 
 static void parse_command(meshlink::mesh *mesh, char *buf) {
        char *arg = strchr(buf, ' ');
@@ -42,7 +48,7 @@ static void parse_command(meshlink::mesh *mesh, char *buf) {
 
                invitation = mesh->invite(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());
                        return;
                }
 
@@ -54,10 +60,17 @@ static void parse_command(meshlink::mesh *mesh, char *buf) {
                        return;
                }
 
+               mesh->stop();
+
                if(!mesh->join(arg))
-                       fprintf(stderr, "Could not join using invitation: %s\n", mesh->errstr);
+                       fprintf(stderr, "Could not join using invitation: %s\n", meshlink::strerror());
                else
                        fprintf(stderr, "Invitation accepted!\n");
+
+               if(!mesh->start()) {
+                       fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink::strerror());
+                       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::mesh *mesh, char *buf) {
 
                meshlink::node *node = mesh->get_node(arg);
                if(!node) {
-                       fprintf(stderr, "Unknown node '%s'\n", arg);
+                       fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
                        return;
                }
 
@@ -75,42 +88,37 @@ static void parse_command(meshlink::mesh *mesh, char *buf) {
                printf("Node '%s' blacklisted.\n", arg);
        } else if(!strcasecmp(buf, "who")) {
                if(!arg) {
-                       meshlink::node *nodes[100];
-                       size_t n = mesh->get_all_nodes(nodes, 100);
-                       if(!n) {
-                               fprintf(stderr, "No nodes known!\n");
-                       } else {
-                               printf("Known nodes:");
-                               for(int i = 0; i < n && i < 100; i++)
+                       nodes = mesh->get_all_nodes(nodes, &nnodes);
+                       if(!nodes)
+                               fprintf(stderr, "Could not get list of nodes: %s\n", meshlink::strerror());
+                       else {
+                               printf("%zu known nodes:", nnodes);
+                               for(size_t i = 0; i < nnodes; i++)
                                        printf(" %s", nodes[i]->name);
-                               if(n > 100)
-                                       printf(" (and %zu more)", n - 100);
                                printf("\n");
                        }
                } else {
                        meshlink::node *node = mesh->get_node(arg);
-                       if(!node) {
-                               fprintf(stderr, "Unknown node '%s'\n", arg);
-                       } else {
+                       if(!node)
+                               fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
+                       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::mesh *mesh, char *buf) {
@@ -153,7 +161,7 @@ static void parse_input(meshlink::mesh *mesh, char *buf) {
 
                destination = mesh->get_node(buf);
                if(!destination) {
-                       fprintf(stderr, "Unknown node '%s'\n", buf);
+                       fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink::strerror());
                        return;
                }
        }
@@ -164,13 +172,14 @@ static void parse_input(meshlink::mesh *mesh, char *buf) {
        }
 
        if(!mesh->send(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());
                return;
        }
 
        printf("Message sent to '%s'.\n", destination->name);
 }
 
+
 int main(int argc, char *argv[]) {
        const char *confbase = ".chat";
        const char *nick = NULL;
@@ -182,30 +191,28 @@ int main(int argc, char *argv[]) {
        if(argc > 2)
                nick = argv[2];
 
-       meshlink::mesh *mesh = meshlink::open(confbase, nick);
-       if(!mesh) {
-               fprintf(stderr, "Could not open MeshLink!\n");
+       ChatMesh mesh;
+       mesh.open(confbase, nick, "chatpp", DEV_CLASS_STATIONARY);
+
+       if(!mesh.isOpen()) {
+               fprintf(stderr, "Could not open MeshLink: %s\n", meshlink::strerror());
                return 1;
        }
 
-       mesh->set_receive_cb(receive);
-       mesh->set_node_status_cb(node_status);
-       mesh->set_log_cb(MESHLINK_INFO, log_message);
-
-       if(!mesh->start()) {
-               fprintf(stderr, "Could not start MeshLink: %s\n", mesh->errstr);
+       if(!mesh.start()) {
+               fprintf(stderr, "Could not start MeshLink: %s\n", meshlink::strerror());
                return 1;
        }
 
        printf("Chat started.\nType /help for a list of commands.\n");
 
-       while(fgets(buf, sizeof buf, stdin))
-               parse_input(mesh, buf);
+       while(fgets(buf, sizeof(buf), stdin))
+               parse_input(&mesh, buf);
 
        printf("Chat stopping.\n");
 
-       mesh->stop();
-       meshlink::close(mesh);
+       mesh.stop();
+       mesh.close();
 
        return 0;
 }