]> git.meshlink.io Git - meshlink/blobdiff - examples/meshlinkapp.c
Don't use assert() to check the results of pthread_*() calls.
[meshlink] / examples / meshlinkapp.c
index aa9b30c5570d0351c16d40af5df7c9a77d8a4338..67f996a4f91b75c3e036304a8dad4a29f7a8615a 100644 (file)
@@ -1,44 +1,60 @@
-#include <libmeshlink.h>
+#include "../src/logger.h"
+#include "../src/system.h"
+#include "../src/meshlink.h"
 
+static void handle_recv_data(meshlink_handle_t *mesh, meshlink_node_t *source, void *data, size_t len) {
+       (void)mesh;
 
-void handle_recv_data(void *data);
-void handle_recv_data(void *data) {
-printf("Data received is %s\n",data);
-
+       printf("Received %zu bytes from %s: %s\n", len, source->name, (char *)data);
 }
 
-int main(int argc , char **argv){
+int main(int argc, char **argv) {
+       const char *confbase = argc > 1 ? argv[1] : "/tmp/meshlink/";
+       const char *name = argc > 2 ? argv[2] : "foo";
+       const char *remotename = argc > 3 ? argv[3] : "bar";
 
-char *confbase = argc > 1 ? argv[1] : "/tmp/meshlink/";
-char *name = argc > 2 ? argv[2] : "foo";
-debug_level = 5;
+       meshlink_handle_t *myhandle;
 
-node_t* remotenode = new_node();
-char *remotename = argc > 3 ? argv[3] : "bar";
+       myhandle = meshlink_open(confbase, name, "meshlinkapp", DEV_CLASS_STATIONARY);
 
-//TODO: change this, calling a function that returns node_t
-remotenode->name = malloc(16);
-remotenode->name = remotename;
+       if(!myhandle) {
+               fprintf(stderr, "Could not open MeshLink: %s", meshlink_strerror(meshlink_errno));
+               return 1;
+       }
 
-tinc_setup(confbase, name);
-tinc_start(confbase);
+       //Register callback function for incoming data
+       meshlink_set_receive_cb(myhandle, (meshlink_receive_cb_t)handle_recv_data);
 
-//Register callback function for incoming data
-recvdata_register_cb(handle_recv_data);
+       if(!meshlink_start(myhandle)) {
+               fprintf(stderr, "Could not start MeshLink: %s", meshlink_strerror(meshlink_errno));
+               return 1;
+       }
 
-sleep(2); //there is a race condition here, tinc_start detaches to a thread the needs time to setup stuff
-while(1) {
+       while(1) {
+               sleep(10);
 
-//sample data to send out
-char mydata[200];
-memset(mydata,0,200);
-strcpy(mydata,"Hello World!");
+               meshlink_node_t *remotenode = meshlink_get_node(myhandle, remotename);
 
-//send out data
-tinc_send_packet(remotenode,mydata,sizeof(mydata));
-sleep(10); //give time to this thread to finish before we exit
-}
-free(remotenode);
-return 0;
+               if(!remotenode) {
+                       fprintf(stderr, "Node %s not known yet.\n", remotename);
+                       continue;
+               }
+
+               //sample data to send out
+               char mydata[200];
+               memset(mydata, 0, 200);
+               strcpy(mydata, "Hello World!");
+
+               //send out data
+               if(!meshlink_send(myhandle, remotenode, mydata, sizeof(mydata))) {
+                       fprintf(stderr, "Error sending data: %s", meshlink_strerror(meshlink_errno));
+                       return 1;
+               }
+       }
+
+       meshlink_stop(myhandle);
+       meshlink_close(myhandle);
+
+       return 0;
 }