]> git.meshlink.io Git - meshlink/blobdiff - examples/chatpp.cc
Never automatically try to bind to ports >= 32768.
[meshlink] / examples / chatpp.cc
index e500ad2733547b63fcd29ef46293061baafc5767..294ca866f602fb692eb6e37c39b8c817a07fd950 100644 (file)
@@ -4,30 +4,30 @@
 #include <strings.h>
 #include "../src/meshlink++.h"
 
-class ChatMesh : public meshlink::mesh
-{
+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);
-               }
+       }
 
        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;
                }
-               
+
                printf("%s says: %s\n", source->name, msg);
        }
 
        void node_status(meshlink::node *node, bool reachable) {
-               if(reachable)
+               if(reachable) {
                        printf("%s joined.\n", node->name);
-               else
+               } else {
                        printf("%s left.\n", node->name);
+               }
        }
 };
 
@@ -36,8 +36,10 @@ static size_t nnodes;
 
 static void parse_command(meshlink::mesh *mesh, char *buf) {
        char *arg = strchr(buf, ' ');
-       if(arg)
+
+       if(arg) {
                *arg++ = 0;
+       }
 
        if(!strcasecmp(buf, "invite")) {
                char *invitation;
@@ -47,7 +49,8 @@ static void parse_command(meshlink::mesh *mesh, char *buf) {
                        return;
                }
 
-               invitation = mesh->invite(arg);
+               invitation = mesh->invite(NULL, arg);
+
                if(!invitation) {
                        fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink::strerror());
                        return;
@@ -61,10 +64,18 @@ static void parse_command(meshlink::mesh *mesh, char *buf) {
                        return;
                }
 
-               if(!mesh->join(arg))
+               mesh->stop();
+
+               if(!mesh->join(arg)) {
                        fprintf(stderr, "Could not join using invitation: %s\n", meshlink::strerror());
-               else
+               } 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");
@@ -72,6 +83,7 @@ static void parse_command(meshlink::mesh *mesh, char *buf) {
                }
 
                meshlink::node *node = mesh->get_node(arg);
+
                if(!node) {
                        fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
                        return;
@@ -83,16 +95,21 @@ static void parse_command(meshlink::mesh *mesh, char *buf) {
        } else if(!strcasecmp(buf, "who")) {
                if(!arg) {
                        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("%lu known nodes:", (unsigned long)nnodes);
+
+                               for(size_t i = 0; i < nnodes; i++) {
                                        printf(" %s", nodes[i]->name);
+                               }
+
                                printf("\n");
                        }
                } else {
                        meshlink::node *node = mesh->get_node(arg);
+
                        if(!node) {
                                fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
                        } else {
@@ -104,14 +121,14 @@ static void parse_command(meshlink::mesh *mesh, char *buf) {
                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"
-                       );
+                       "<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);
        }
@@ -121,28 +138,33 @@ static void parse_input(meshlink::mesh *mesh, char *buf) {
        static meshlink::node *destination;
        size_t len;
 
-       if(!buf)
+       if(!buf) {
                return;
+       }
 
        // Remove newline.
 
        len = strlen(buf);
 
-       if(len && buf[len - 1] == '\n')
+       if(len && buf[len - 1] == '\n') {
                buf[--len] = 0;
+       }
 
-       if(len && buf[len - 1] == '\r')
+       if(len && buf[len - 1] == '\r') {
                buf[--len] = 0;
+       }
 
        // Ignore empty lines.
 
-       if(!len)
+       if(!len) {
                return;
+       }
 
        // Commands start with '/'
 
-       if(*buf == '/')
+       if(*buf == '/') {
                return parse_command(mesh, buf + 1);
+       }
 
        // Lines in the form "name: message..." set the destination node.
 
@@ -152,10 +174,13 @@ static void parse_input(meshlink::mesh *mesh, char *buf) {
        if(colon) {
                *colon = 0;
                msg = colon + 1;
-               if(*msg == ' ')
+
+               if(*msg == ' ') {
                        msg++;
+               }
 
                destination = mesh->get_node(buf);
+
                if(!destination) {
                        fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink::strerror());
                        return;
@@ -181,33 +206,37 @@ int main(int argc, char *argv[]) {
        const char *nick = NULL;
        char buf[1024];
 
-       if(argc > 1)
+       if(argc > 1) {
                confbase = argv[1];
+       }
 
-       if(argc > 2)
+       if(argc > 2) {
                nick = argv[2];
+       }
 
-       ChatMesh* mesh = meshlink::open<ChatMesh>(confbase, nick, "chatpp", STATIONARY);
+       ChatMesh mesh;
+       mesh.open(confbase, nick, "chatpp", DEV_CLASS_STATIONARY);
 
-       if(!mesh) {
+       if(!mesh.isOpen()) {
                fprintf(stderr, "Could not open MeshLink: %s\n", meshlink::strerror());
                return 1;
        }
 
-       if(!mesh->start()) {
+       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;
 }