From 1d91eecd5611005de957d4821b8bb205000ed57f Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sat, 19 Apr 2014 15:39:06 +0200 Subject: [PATCH] Add an example chat program. This one doesn't compile yet, but it illustrates how we want to use MeshLink in a more realistic application. --- examples/Makefile.am | 5 +- examples/chat.c | 170 +++++++++++++++++++++++++++++++++++++++++++ src/meshlink.h | 52 +++++++++++-- 3 files changed, 218 insertions(+), 9 deletions(-) create mode 100644 examples/chat.c diff --git a/examples/Makefile.am b/examples/Makefile.am index ca050a8c..ce4cb077 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,6 +1,9 @@ -bin_PROGRAMS = meshlinkapp +bin_PROGRAMS = meshlinkapp chat AM_CPPFLAGS = -I../src meshlinkapp_SOURCES = meshlinkapp.c meshlinkapp_LDADD = ../src/libmeshlink.la + +meshlinkapp_SOURCES = chat.c +meshlinkapp_LDADD = ../src/libmeshlink.la diff --git a/examples/chat.c b/examples/chat.c new file mode 100644 index 00000000..84e3dadb --- /dev/null +++ b/examples/chat.c @@ -0,0 +1,170 @@ +#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]) { + fprintf(stderr, "Received invalid data from %s\n", source->name); + return; + } + + printf("%s says: %s\n", source->name, data); +} + +static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { + if(reachable) + printf("%s joined.\n", node->name); + else + printf("%s left.\n", node->name); +} + +static void parse_command(meshlink_handle_t *mesh, char *buf) { + char *arg = strchr(buf, ' '); + if(arg) + *arg++ = 0; + + if(!strcasecmp(buf, "invite")) { + char *invitation; + + if(!arg) { + fprintf(stderr, "/invite requires an argument!\n"); + return; + } + + invitation = meshlink_invite(mesh, arg); + if(!invitation) { + fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_errstr); + return; + } + + printf("Invitation for %s: %s\n", arg, invitation); + free(invitation); + } else if(!strcasecmp(buf, "join")) { + if(!arg) { + fprintf(stderr, "/join requires an argument!\n"); + return; + } + + if(!meshlink_join(mesh, arg)) + fprintf(stderr, "Could not join using invitation: %s\n", meshlink_errstr); + else + fprintf(stderr, "Invitation accepted!\n"); + } else if(!strcasecmp(buf, "kick")) { + if(!arg) { + fprintf(stderr, "/kick requires an argument!\n"); + return; + } + + meshlink_node_t *node = meshlink_get_node(mesh, arg); + if(!node) { + fprintf(stderr, "Unknown node '%s'\n", arg); + return; + } + + meshlink_blacklist(mesh, node); + + printf("Node '%s' blacklisted.\n", arg); + } else if(!strcasecmp(buf, "quit")) { + printf("Bye!\n"); + fclose(stdin); + } else { + fprintf(stderr, "Unknown command '/%s'\n", buf); + } +} + +static void parse_input(meshlink_handle_t *mesh, char *buf) { + static meshlink_node_t *destination; + size_t len; + + if(!buf) + return; + + // Remove newline. + + len = strlen(buf); + + if(len && buf[len - 1] == '\n') + buf[--len] = 0; + + if(len && buf[len - 1] == '\r') + buf[--len] = 0; + + // Ignore empty lines. + + if(!len) + return; + + // Commands start with '/' + + if(*buf == '/') + return parse_command(mesh, buf + 1); + + // Lines in the form "name: message..." set the destination node. + + char *msg = buf; + char *colon = strchr(buf, ':'); + + if(colon) { + *colon = 0; + msg = colon + 1; + if(*msg == ' ') + msg++; + + destination = meshlink_get_node(mesh, buf); + if(!destination) { + fprintf(stderr, "Unknown node '%s'\n", buf); + return; + } + } + + if(!destination) { + fprintf(stderr, "Who are you talking to? Write 'name: message...'\n"); + return; + } + + if(!meshlink_send(mesh, destination, msg, strlen(msg) + 1)) { + fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_errstr); + return; + } + + printf("Message sent to '%s'.\n", destination->name); +} + +int main(int argc, char *argv[]) { + const char *confbase = ".chat"; + const char *nick = NULL; + char buf[1024]; + + if(argc > 1) + confbase = argv[1]; + + if(argc > 2) + nick = argv[2]; + + mesh = meshlink_open(confbase, nick); + if(!mesh) { + fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_errstr); + return 1; + } + + meshlink_set_receive_cb(receive); + meshlink_set_node_status_cb(node_changed); + + if(!meshlink_start(mesh)) { + fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_errstr); + return 1; + } + + printf("Chat started.\n"); + + while(fgets(buf, sizeof buf, stdin)) + parse_input(mesh, buf); + + printf("Chat stopping.\n"); + + meshlink_stop(mesh); + meshlink_close(mesh); + + return 0; +} diff --git a/src/meshlink.h b/src/meshlink.h index 9ed90379..ca78afd0 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -22,11 +22,38 @@ #include #include +#ifndef MESHLINK_INTERNAL_H + /// A handle for an instance of MeshLink. typedef struct meshlink_handle meshlink_handle_t; /// A handle for a MeshLink node. -typedef struct meshlink_node meshlink_node_t; +typedef struct meshlink_node { + const char *name; +} meshlink_node_t; + +#endif // MESHLINK_INTERNAL_H + +/// Code of most recent error encountered. +typedef enum { + MESHLINK_OK, // Everything is fine + MESHLINK_ENOMEM, // Out of memory + MESHLINK_ENOENT, // Node is not known +} meshlink_errno_t; + +extern meshlink_errno_t meshlink_errno; + +/// Textual representation of most recent error encountered. +const char *meshlink_errstr; + +/// Get the text for the given MeshLink error code. +/** This function returns a pointer to the string containing the description of the given error code. + * + * @param errno An error code returned by MeshLink. + * + * @return A pointer to a string containing the description of the error code. + */ +extern const char *meshlink_strerror(meshlink_errno_t errno); /// Initialize MeshLink's configuration directory. /** This function causes MeshLink to initialize its configuration directory, @@ -37,28 +64,37 @@ typedef struct meshlink_node meshlink_node_t; * * @param confbase The directory in which MeshLink will store its configuration files. * @param name The name which this instance of the application will use in the mesh. + * * @return This function will return true if MeshLink has succesfully set up its configuration files, false otherwise. */ -extern bool meshlink_setup(const char *confbase, const char *name); +extern meshlink_handle_t *meshlink_open(const char *confbase, const char *name); /// Start MeshLink. /** This function causes MeshLink to create a new thread, which will * handle all network I/O. * * @param confbase The directory in which MeshLink will store its configuration files. - * @return A handle which represents this instance of MeshLink, - * or NULL in case of an error. + * + * @return This function will return true if MeshLink has succesfully started its thread, false otherwise. */ -extern meshlink_handle_t *meshlink_start(const char *confbase); +extern bool meshlink_start(meshlink_handle_t *handle); /// Stop MeshLink. /** This function causes MeshLink to disconnect from all other nodes, - * and shuts down its own thread. Afterwards, the handle and any - * pointers to a struct meshlink_node are invalid. + * and shuts down its own thread. + * + * @param handle A handle which represents an instance of MeshLink. + */ +extern void meshlink_stop(meshlink_handle_t *handle); + +/// Close the MeshLink handle. +/** This function calls meshlink_stop() if necessary, + * and frees all memory allocated by MeshLink. + * Afterwards, the handle and any pointers to a struct meshlink_node are invalid. * * @param handle A handle which represents an instance of MeshLink. */ -extern void meshlink_stop(meshlink_handle *handle); +extern void meshlink_close(meshlink_handle_t *handle); /// A callback for receiving data from the mesh. /** @param handle A handle which represents an instance of MeshLink. -- 2.39.5