5 #include "../src/meshlink.h"
9 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
12 static const char *levelstr[] = {
13 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
14 [MESHLINK_INFO] = "\x1b[32mINFO",
15 [MESHLINK_WARNING] = "\x1b[33mWARNING",
16 [MESHLINK_ERROR] = "\x1b[31mERROR",
17 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
19 fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text);
22 static void channel_receive(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
25 fprintf(stderr, "Error while reading data from %s: %s\n", channel->node->name, meshlink_strerror(meshlink_errno));
27 fprintf(stderr, "Chat connection closed by %s\n", channel->node->name);
30 channel->node->priv = NULL;
31 meshlink_channel_close(mesh, channel);
35 // TODO: we now have TCP semantics, don't expect exactly one message per receive call.
37 printf("%s says: ", channel->node->name);
38 fwrite(data, len, 1, stdout);
42 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
46 // Only accept connections to the chat port
47 if(port != CHAT_PORT) {
48 fprintf(stderr, "Rejected incoming channel from '%s' to port %u\n", channel->node->name, port);
52 fprintf(stderr, "Accepted incoming channel from '%s'\n", channel->node->name);
54 // Remember the channel
55 channel->node->priv = channel;
57 // Set the receive callback
58 meshlink_set_channel_receive_cb(mesh, channel, channel_receive);
60 // Accept this channel
64 static void channel_poll(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
67 fprintf(stderr, "Channel to '%s' connected\n", channel->node->name);
68 meshlink_set_channel_poll_cb(mesh, channel, NULL);
71 static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
75 printf("%s joined.\n", node->name);
77 printf("%s left.\n", node->name);
81 static meshlink_node_t **nodes;
84 static void parse_command(meshlink_handle_t *mesh, char *buf) {
85 char *arg = strchr(buf, ' ');
91 if(!strcasecmp(buf, "invite")) {
95 fprintf(stderr, "/invite requires an argument!\n");
99 invitation = meshlink_invite(mesh, NULL, arg);
102 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
106 printf("Invitation for %s: %s\n", arg, invitation);
108 } else if(!strcasecmp(buf, "join")) {
110 fprintf(stderr, "/join requires an argument!\n");
116 if(!meshlink_join(mesh, arg)) {
117 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
119 fprintf(stderr, "Invitation accepted!\n");
122 if(!meshlink_start(mesh)) {
123 fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
126 } else if(!strcasecmp(buf, "kick")) {
128 fprintf(stderr, "/kick requires an argument!\n");
132 meshlink_node_t *node = meshlink_get_node(mesh, arg);
135 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
139 meshlink_blacklist(mesh, node);
141 printf("Node '%s' blacklisted.\n", arg);
142 } else if(!strcasecmp(buf, "who")) {
144 nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes);
147 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
149 printf("%zu known nodes:", nnodes);
151 for(size_t i = 0; i < nnodes; i++) {
152 printf(" %s", nodes[i]->name);
158 meshlink_node_t *node = meshlink_get_node(mesh, arg);
161 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
163 printf("Node %s found\n", arg);
166 } else if(!strcasecmp(buf, "quit")) {
169 } else if(!strcasecmp(buf, "help")) {
171 "<name>: <message> Send a message to the given node.\n"
172 " Subsequent messages don't need the <name>: prefix.\n"
173 "/invite <name> Create an invitation for a new node.\n"
174 "/join <invitation> Join an existing mesh using an invitation.\n"
175 "/kick <name> Blacklist the given node.\n"
176 "/who [<name>] List all nodes or show information about the given node.\n"
177 "/quit Exit this program.\n"
180 fprintf(stderr, "Unknown command '/%s'\n", buf);
184 static void parse_input(meshlink_handle_t *mesh, char *buf) {
185 static meshlink_node_t *destination;
196 if(len && buf[len - 1] == '\n') {
200 if(len && buf[len - 1] == '\r') {
204 // Ignore empty lines.
210 // Commands start with '/'
213 parse_command(mesh, buf + 1);
217 // Lines in the form "name: message..." set the destination node.
220 char *colon = strchr(buf, ':');
230 destination = meshlink_get_node(mesh, buf);
233 fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
239 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
243 // We want to have one channel per node.
244 // We keep the pointer to the meshlink_channel_t in the priv field of that node.
245 meshlink_channel_t *channel = destination->priv;
248 fprintf(stderr, "Opening chat channel to '%s'\n", destination->name);
249 channel = meshlink_channel_open(mesh, destination, CHAT_PORT, channel_receive, NULL, 0);
252 fprintf(stderr, "Could not create channel to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
256 destination->priv = channel;
257 meshlink_set_channel_poll_cb(mesh, channel, channel_poll);
260 if(!meshlink_channel_send(mesh, channel, msg, strlen(msg))) {
261 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
265 printf("Message sent to '%s'.\n", destination->name);
268 int main(int argc, char *argv[]) {
269 const char *confbase = ".chat";
270 const char *nick = NULL;
281 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
283 meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY);
286 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
290 meshlink_set_node_status_cb(mesh, node_status);
291 meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message);
293 // Set the channel accept callback. This implicitly turns on channels for all nodes.
294 // This replaces the call to meshlink_set_receive_cb().
295 meshlink_set_channel_accept_cb(mesh, channel_accept);
297 if(!meshlink_start(mesh)) {
298 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
302 printf("Chat started.\nType /help for a list of commands.\n");
304 while(fgets(buf, sizeof(buf), stdin)) {
305 parse_input(mesh, buf);
308 printf("Chat stopping.\n");
311 meshlink_close(mesh);