]> git.meshlink.io Git - meshlink-tiny/blob - examples/channels.c
Add a metering test.
[meshlink-tiny] / examples / channels.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <strings.h>
5 #include "../src/meshlink-tiny.h"
6
7 #define CHAT_PORT 531
8
9 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
10         (void) mesh;
11
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",
18         };
19         fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text);
20 }
21
22 static void channel_receive(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
23         if(!len) {
24                 if(meshlink_errno) {
25                         fprintf(stderr, "Error while reading data from %s: %s\n", channel->node->name, meshlink_strerror(meshlink_errno));
26                 } else {
27                         fprintf(stderr, "Chat connection closed by %s\n", channel->node->name);
28                 }
29
30                 channel->node->priv = NULL;
31                 meshlink_channel_close(mesh, channel);
32                 return;
33         }
34
35         // TODO: we now have TCP semantics, don't expect exactly one message per receive call.
36
37         printf("%s says: ", channel->node->name);
38         fwrite(data, len, 1, stdout);
39         fputc('\n', stdout);
40 }
41
42 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
43         (void)data;
44         (void)len;
45
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);
49                 return false;
50         }
51
52         fprintf(stderr, "Accepted incoming channel from '%s'\n", channel->node->name);
53
54         // Remember the channel
55         channel->node->priv = channel;
56
57         // Set the receive callback
58         meshlink_set_channel_receive_cb(mesh, channel, channel_receive);
59
60         // Accept this channel
61         return true;
62 }
63
64 static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
65         (void)mesh;
66
67         if(reachable) {
68                 printf("%s joined.\n", node->name);
69         } else {
70                 printf("%s left.\n", node->name);
71         }
72 }
73
74 static void parse_command(meshlink_handle_t *mesh, char *buf) {
75         char *arg = strchr(buf, ' ');
76
77         if(arg) {
78                 *arg++ = 0;
79         }
80
81         if(!strcasecmp(buf, "join")) {
82                 if(!arg) {
83                         fprintf(stderr, "/join requires an argument!\n");
84                         return;
85                 }
86
87                 meshlink_stop(mesh);
88
89                 if(!meshlink_join(mesh, arg)) {
90                         fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
91                 } else {
92                         fprintf(stderr, "Invitation accepted!\n");
93                 }
94
95                 if(!meshlink_start(mesh)) {
96                         fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
97                         exit(1);
98                 }
99         } else if(!strcasecmp(buf, "quit")) {
100                 printf("Bye!\n");
101                 fclose(stdin);
102         } else if(!strcasecmp(buf, "help")) {
103                 printf(
104                         "<name>: <message>     Send a message to the given node.\n"
105                         "                      Subsequent messages don't need the <name>: prefix.\n"
106                         "/join <invitation>    Join an existing mesh using an invitation.\n"
107                         "/kick <name>          Blacklist the given node.\n"
108                         "/who [<name>]         List all nodes or show information about the given node.\n"
109                         "/quit                 Exit this program.\n"
110                 );
111         } else {
112                 fprintf(stderr, "Unknown command '/%s'\n", buf);
113         }
114 }
115
116 static void parse_input(meshlink_handle_t *mesh, char *buf) {
117         static meshlink_node_t *destination;
118         size_t len;
119
120         if(!buf) {
121                 return;
122         }
123
124         // Remove newline.
125
126         len = strlen(buf);
127
128         if(len && buf[len - 1] == '\n') {
129                 buf[--len] = 0;
130         }
131
132         if(len && buf[len - 1] == '\r') {
133                 buf[--len] = 0;
134         }
135
136         // Ignore empty lines.
137
138         if(!len) {
139                 return;
140         }
141
142         // Commands start with '/'
143
144         if(*buf == '/') {
145                 parse_command(mesh, buf + 1);
146                 return;
147         }
148
149         // Lines in the form "name: message..." set the destination node.
150
151         char *msg = buf;
152         char *colon = strchr(buf, ':');
153
154         if(colon) {
155                 *colon = 0;
156                 msg = colon + 1;
157
158                 if(*msg == ' ') {
159                         msg++;
160                 }
161
162                 destination = meshlink_get_node(mesh, buf);
163
164                 if(!destination) {
165                         fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
166                         return;
167                 }
168         }
169
170         if(!destination) {
171                 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
172                 return;
173         }
174
175         // We want to have one channel per node.
176         // We keep the pointer to the meshlink_channel_t in the priv field of that node.
177         meshlink_channel_t *channel = destination->priv;
178
179         if(!channel) {
180                 fprintf(stderr, "Opening chat channel to '%s'\n", destination->name);
181                 channel = meshlink_channel_open_ex(mesh, destination, CHAT_PORT, channel_receive, NULL, 0, MESHLINK_CHANNEL_UDP);
182
183                 if(!channel) {
184                         fprintf(stderr, "Could not create channel to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
185                         return;
186                 }
187
188                 destination->priv = channel;
189         }
190
191         if(!meshlink_channel_send(mesh, channel, msg, strlen(msg))) {
192                 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
193                 return;
194         }
195
196         printf("Message sent to '%s'.\n", destination->name);
197 }
198
199 int main(int argc, char *argv[]) {
200         const char *confbase = ".chat";
201         const char *nick = NULL;
202         char buf[1024];
203
204         if(argc > 1) {
205                 confbase = argv[1];
206         }
207
208         if(argc > 2) {
209                 nick = argv[2];
210         }
211
212         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
213
214         meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY);
215
216         if(!mesh) {
217                 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
218                 return 1;
219         }
220
221         meshlink_set_node_status_cb(mesh, node_status);
222         meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message);
223
224         // Set the channel accept callback. This implicitly turns on channels for all nodes.
225         // This replaces the call to meshlink_set_receive_cb().
226         meshlink_set_channel_accept_cb(mesh, channel_accept);
227
228         if(!meshlink_start(mesh)) {
229                 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
230                 return 1;
231         }
232
233         printf("Chat started.\nType /help for a list of commands.\n");
234
235         while(fgets(buf, sizeof(buf), stdin)) {
236                 parse_input(mesh, buf);
237         }
238
239         printf("Chat stopping.\n");
240
241         meshlink_stop(mesh);
242         meshlink_close(mesh);
243
244         return 0;
245 }