#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);
+ }
}
};
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;
return;
}
- invitation = mesh->invite(arg);
+ invitation = mesh->invite(NULL, arg);
+
if(!invitation) {
fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink::strerror());
return;
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");
}
meshlink::node *node = mesh->get_node(arg);
+
if(!node) {
fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
return;
} 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 {
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);
}
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.
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;
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", DEV_CLASS_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;
}