]> git.meshlink.io Git - meshlink-tiny/blob - examples/chatpp.cc
Add a metering test.
[meshlink-tiny] / examples / chatpp.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <strings.h>
5 #include "../src/meshlink-tiny++.h"
6
7 class ChatMesh : public meshlink::mesh {
8 public:
9         void log(meshlink::log_level_t level, const char *text) {
10                 const char *levelstr[] = {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"};
11                 fprintf(stderr, "%s: %s\n", levelstr[level], text);
12         }
13
14         void receive(meshlink::node *source, const void *data, size_t len) {
15                 const char *msg = (const char *)data;
16
17                 if(!len || msg[len - 1]) {
18                         fprintf(stderr, "Received invalid data from %s\n", source->name);
19                         return;
20                 }
21
22                 printf("%s says: %s\n", source->name, msg);
23         }
24
25         void node_status(meshlink::node *node, bool reachable) {
26                 if(reachable) {
27                         printf("%s joined.\n", node->name);
28                 } else {
29                         printf("%s left.\n", node->name);
30                 }
31         }
32 };
33
34 static meshlink::node **nodes;
35 static size_t nnodes;
36
37 static void parse_command(meshlink::mesh *mesh, char *buf) {
38         char *arg = strchr(buf, ' ');
39
40         if(arg) {
41                 *arg++ = 0;
42         }
43
44         if(!strcasecmp(buf, "join")) {
45                 if(!arg) {
46                         fprintf(stderr, "/join requires an argument!\n");
47                         return;
48                 }
49
50                 mesh->stop();
51
52                 if(!mesh->join(arg)) {
53                         fprintf(stderr, "Could not join using invitation: %s\n", meshlink::strerror());
54                 } else {
55                         fprintf(stderr, "Invitation accepted!\n");
56                 }
57
58                 if(!mesh->start()) {
59                         fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink::strerror());
60                         exit(1);
61                 }
62         } else if(!strcasecmp(buf, "who")) {
63                 if(!arg) {
64                         nodes = mesh->get_all_nodes(nodes, &nnodes);
65
66                         if(!nodes) {
67                                 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink::strerror());
68                         } else {
69                                 printf("%lu known nodes:", (unsigned long)nnodes);
70
71                                 for(size_t i = 0; i < nnodes; i++) {
72                                         printf(" %s", nodes[i]->name);
73                                 }
74
75                                 printf("\n");
76                         }
77                 } else {
78                         meshlink::node *node = mesh->get_node(arg);
79
80                         if(!node) {
81                                 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
82                         } else {
83                                 printf("Node %s found\n", arg);
84                         }
85                 }
86         } else if(!strcasecmp(buf, "quit")) {
87                 printf("Bye!\n");
88                 fclose(stdin);
89         } else if(!strcasecmp(buf, "help")) {
90                 printf(
91                         "<name>: <message>     Send a message to the given node.\n"
92                         "                      Subsequent messages don't need the <name>: prefix.\n"
93                         "/join <invitation>    Join an existing mesh using an invitation.\n"
94                         "/kick <name>          Blacklist the given node.\n"
95                         "/who [<name>]         List all nodes or show information about the given node.\n"
96                         "/quit                 Exit this program.\n"
97                 );
98         } else {
99                 fprintf(stderr, "Unknown command '/%s'\n", buf);
100         }
101 }
102
103 static void parse_input(meshlink::mesh *mesh, char *buf) {
104         static meshlink::node *destination;
105         size_t len;
106
107         if(!buf) {
108                 return;
109         }
110
111         // Remove newline.
112
113         len = strlen(buf);
114
115         if(len && buf[len - 1] == '\n') {
116                 buf[--len] = 0;
117         }
118
119         if(len && buf[len - 1] == '\r') {
120                 buf[--len] = 0;
121         }
122
123         // Ignore empty lines.
124
125         if(!len) {
126                 return;
127         }
128
129         // Commands start with '/'
130
131         if(*buf == '/') {
132                 return parse_command(mesh, buf + 1);
133         }
134
135         // Lines in the form "name: message..." set the destination node.
136
137         char *msg = buf;
138         char *colon = strchr(buf, ':');
139
140         if(colon) {
141                 *colon = 0;
142                 msg = colon + 1;
143
144                 if(*msg == ' ') {
145                         msg++;
146                 }
147
148                 destination = mesh->get_node(buf);
149
150                 if(!destination) {
151                         fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink::strerror());
152                         return;
153                 }
154         }
155
156         if(!destination) {
157                 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
158                 return;
159         }
160
161         if(!mesh->send(destination, msg, strlen(msg) + 1)) {
162                 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink::strerror());
163                 return;
164         }
165
166         printf("Message sent to '%s'.\n", destination->name);
167 }
168
169
170 int main(int argc, char *argv[]) {
171         const char *confbase = ".chat";
172         const char *nick = NULL;
173         char buf[1024];
174
175         if(argc > 1) {
176                 confbase = argv[1];
177         }
178
179         if(argc > 2) {
180                 nick = argv[2];
181         }
182
183         ChatMesh mesh;
184         mesh.open(confbase, nick, "chatpp", DEV_CLASS_STATIONARY);
185
186         if(!mesh.isOpen()) {
187                 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink::strerror());
188                 return 1;
189         }
190
191         if(!mesh.start()) {
192                 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink::strerror());
193                 return 1;
194         }
195
196         printf("Chat started.\nType /help for a list of commands.\n");
197
198         while(fgets(buf, sizeof(buf), stdin)) {
199                 parse_input(&mesh, buf);
200         }
201
202         printf("Chat stopping.\n");
203
204         mesh.stop();
205         mesh.close();
206
207         return 0;
208 }