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