From 7d0d06e82233623ce90c70dac2803a7f299c673e Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Fri, 5 Sep 2014 23:14:40 +0200 Subject: [PATCH] Add an example chat program that uses the channels API. --- examples/channels.c | 274 ++++++++++++++++++++++++++++++++++++++++ src/meshlink.h | 11 +- src/meshlink_internal.h | 4 +- 3 files changed, 283 insertions(+), 6 deletions(-) create mode 100644 examples/channels.c diff --git a/examples/channels.c b/examples/channels.c new file mode 100644 index 00000000..3ad2742e --- /dev/null +++ b/examples/channels.c @@ -0,0 +1,274 @@ +#include +#include +#include +#include +#include "../src/meshlink.h" + +#define CHAT_PORT 531 + +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 channel_receive(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { + const char *msg = data; + + if(!len) { + if(meshlink_errno) + fprintf(stderr, "Error while reading data from %s: %s\n", channel->node->name, meshlink_strerror(meshlink_errno)); + else + fprintf(stderr, "Chat connection closed by %s\n", channel->node->name); + + channel->node->priv = NULL; + meshlink_channel_close(mesh, channel); + return; + } + + // TODO: we now have TCP semantics, don't expect exactly one message per receive call. + if(msg[len - 1]) { + fprintf(stderr, "Received invalid data from %s\n", channel->node->name); + return; + } + + printf("%s says: %s\n", channel->node->name, msg); +} + +static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) { + // Only accept connections to the chat port + if(port != CHAT_PORT) { + fprintf(stderr, "Rejected incoming channel from '%s' to port %u\n", channel->node->name, port); + return false; + } + + fprintf(stderr, "Accepted incoming channel from '%s'\n", channel->node->name); + + // Remember the channel + channel->node->priv = channel; + + // Set the receive callback + meshlink_set_channel_receive_cb(mesh, channel, channel_receive); + + // Accept this channel + return true; +} + +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 meshlink_node_t **nodes; +static size_t nnodes; + +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_strerror(meshlink_errno)); + 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; + } + 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 start MeshLink: %s\n", meshlink_strerror(meshlink_errno)); + return; + } + } + } 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, "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); + } +} + +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, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno)); + return; + } + } + + if(!destination) { + fprintf(stderr, "Who are you talking to? Write 'name: message...'\n"); + return; + } + + // We want to have one channel per node. + // We keep the pointer to the meshlink_channel_t in the priv field of that node. + meshlink_channel_t *channel = destination->priv; + + if(!channel) { + fprintf(stderr, "Opening chat channel to '%s'\n", destination->name); + channel = meshlink_channel_open(mesh, destination, CHAT_PORT, channel_receive, NULL, 0); + if(!channel) { + fprintf(stderr, "Could not create channel to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno)); + return; + } + destination->priv = channel; + } + + if(!meshlink_channel_send(mesh, channel, msg, len + 1)) { + fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno)); + 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]; + + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, 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_strerror(meshlink_errno)); + return 1; + } + + meshlink_set_node_status_cb(mesh, node_status); + meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message); + + // Set the channel accept callback. This implicitly turns on channels for all nodes. + // This replaces the call to meshlink_set_receive_cb(). + meshlink_set_channel_accept_cb(mesh, channel_accept); + + if(!meshlink_start(mesh)) { + fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno)); + return 1; + } + + printf("Chat started.\nType /help for a list of commands.\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 538c4a29..ff0562c4 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -88,8 +88,8 @@ extern __thread meshlink_errno_t meshlink_errno; #ifndef MESHLINK_INTERNAL_H struct meshlink_handle { - char *name; - void *priv; + char *name; ///< Textual name of ourself. It is stored in a nul-terminated C string, which is allocated by MeshLink. + void *priv; ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink. }; struct meshlink_node { @@ -98,6 +98,8 @@ struct meshlink_node { }; struct meshlink_channel { + struct meshlink_node *node; ///< Pointer to the peer of this channel. + void *priv; ///< Private pointer which may be set freely by the application, and is never used or modified by MeshLink. }; #endif // MESHLINK_INTERNAL_H @@ -254,6 +256,7 @@ typedef enum { * @param level An enum describing the severity level of the message. * @param text A pointer to a nul-terminated C string containing the textual log message. * This pointer is only valid for the duration of the callback. + * H * The application must not free() this pointer. * The application should strdup() the text if it has to be available outside the callback. */ @@ -495,8 +498,6 @@ extern void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node); * If the application rejects the incoming channel by returning false, * then this handle is invalid after the callback returns * (the callback does not need to call meshlink_channel_close() itself in this case). - * @param node The node from which this channel is being initiated. - * The pointer is guaranteed to be valid until meshlink_close() is called. * @param port The port number the peer wishes to connect to. * @param data A pointer to a buffer containing data already received, or NULL in case no data has been received yet. (Not yet used.) * The pointer is only valid during the lifetime of the callback. @@ -506,7 +507,7 @@ extern void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node); * @return This function should return true if the application accepts the incoming channel, false otherwise. * If returning false, the channel is invalid and may not be used anymore. */ -typedef bool (*meshlink_channel_accept_cb_t)(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_node_t *node, uint16_t port, const void *data, size_t len); +typedef bool (*meshlink_channel_accept_cb_t)(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len); /// A callback for receiving data from a channel. /** This function is called whenever data is received from a remote node on a channel. diff --git a/src/meshlink_internal.h b/src/meshlink_internal.h index 3099d7e2..a58ab440 100644 --- a/src/meshlink_internal.h +++ b/src/meshlink_internal.h @@ -153,8 +153,10 @@ struct meshlink_node { /// A channel. struct meshlink_channel { - struct utcp_connection *c; struct node_t *node; + void *priv; + + struct utcp_connection *c; meshlink_channel_receive_cb_t receive_cb; }; -- 2.39.5