]> git.meshlink.io Git - meshlink/blob - examples/chatpp.cc
manynodes example: trivial, change debug levels
[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                 if(!mesh->join(arg))
65                         fprintf(stderr, "Could not join using invitation: %s\n", meshlink::strerror());
66                 else
67                         fprintf(stderr, "Invitation accepted!\n");
68         } else if(!strcasecmp(buf, "kick")) {
69                 if(!arg) {
70                         fprintf(stderr, "/kick requires an argument!\n");
71                         return;
72                 }
73
74                 meshlink::node *node = mesh->get_node(arg);
75                 if(!node) {
76                         fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
77                         return;
78                 }
79
80                 mesh->blacklist(node);
81
82                 printf("Node '%s' blacklisted.\n", arg);
83         } else if(!strcasecmp(buf, "who")) {
84                 if(!arg) {
85                         nodes = mesh->get_all_nodes(nodes, &nnodes);
86                         if(!nodes) {
87                                 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink::strerror());
88                         } else {
89                                 printf("%zu known nodes:", nnodes);
90                                 for(size_t i = 0; i < nnodes; i++)
91                                         printf(" %s", nodes[i]->name);
92                                 printf("\n");
93                         }
94                 } else {
95                         meshlink::node *node = mesh->get_node(arg);
96                         if(!node) {
97                                 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
98                         } else {
99                                 printf("Node %s found\n", arg);
100                         }
101                 }
102         } else if(!strcasecmp(buf, "quit")) {
103                 printf("Bye!\n");
104                 fclose(stdin);
105         } else if(!strcasecmp(buf, "help")) {
106                 printf(
107                         "<name>: <message>     Send a message to the given node.\n"
108                         "                      Subsequent messages don't need the <name>: prefix.\n"
109                         "/invite <name>        Create an invitation for a new node.\n"
110                         "/join <invitation>    Join an existing mesh using an invitation.\n"
111                         "/kick <name>          Blacklist the given node.\n"
112                         "/who [<name>]         List all nodes or show information about the given node.\n"
113                         "/quit                 Exit this program.\n"
114                         );
115         } else {
116                 fprintf(stderr, "Unknown command '/%s'\n", buf);
117         }
118 }
119
120 static void parse_input(meshlink::mesh *mesh, char *buf) {
121         static meshlink::node *destination;
122         size_t len;
123
124         if(!buf)
125                 return;
126
127         // Remove newline.
128
129         len = strlen(buf);
130
131         if(len && buf[len - 1] == '\n')
132                 buf[--len] = 0;
133
134         if(len && buf[len - 1] == '\r')
135                 buf[--len] = 0;
136
137         // Ignore empty lines.
138
139         if(!len)
140                 return;
141
142         // Commands start with '/'
143
144         if(*buf == '/')
145                 return parse_command(mesh, buf + 1);
146
147         // Lines in the form "name: message..." set the destination node.
148
149         char *msg = buf;
150         char *colon = strchr(buf, ':');
151
152         if(colon) {
153                 *colon = 0;
154                 msg = colon + 1;
155                 if(*msg == ' ')
156                         msg++;
157
158                 destination = mesh->get_node(buf);
159                 if(!destination) {
160                         fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink::strerror());
161                         return;
162                 }
163         }
164
165         if(!destination) {
166                 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
167                 return;
168         }
169
170         if(!mesh->send(destination, msg, strlen(msg) + 1)) {
171                 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink::strerror());
172                 return;
173         }
174
175         printf("Message sent to '%s'.\n", destination->name);
176 }
177
178
179 int main(int argc, char *argv[]) {
180         const char *confbase = ".chat";
181         const char *nick = NULL;
182         char buf[1024];
183
184         if(argc > 1)
185                 confbase = argv[1];
186
187         if(argc > 2)
188                 nick = argv[2];
189
190         ChatMesh* mesh = meshlink::open<ChatMesh>(confbase, nick, "chatpp", STATIONARY);
191
192         if(!mesh) {
193                 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink::strerror());
194                 return 1;
195         }
196
197         if(!mesh->start()) {
198                 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink::strerror());
199                 return 1;
200         }
201
202         printf("Chat started.\nType /help for a list of commands.\n");
203
204         while(fgets(buf, sizeof buf, stdin))
205                 parse_input(mesh, buf);
206
207         printf("Chat stopping.\n");
208
209         mesh->stop();
210         meshlink::close(mesh);
211
212         return 0;
213 }