-#include <system.h>
-#include <meshlink.h>
-
-
-void handle_recv_data(void *data);
-void handle_recv_data(void *data) {
-printf("Data received is %s\n",data);
+#include "../src/system.h"
+#include "../src/meshlink.h"
+void handle_recv_data(meshlink_handle_t *mesh, meshlink_node_t *source, void *data, size_t len) {
+ printf("Received %zu bytes from %s: %s\n", len, source->name, data);
}
int main(int argc , char **argv){
-char *confbase = argc > 1 ? argv[1] : "/tmp/meshlink/";
-char *name = argc > 2 ? argv[2] : "foo";
-//debug_level = 5;
+ char *confbase = argc > 1 ? argv[1] : "/tmp/meshlink/";
+ char *name = argc > 2 ? argv[2] : "foo";
-meshlink_node_t* remotenode = new_node();
-char *remotename = argc > 3 ? argv[3] : "bar";
+ char *remotename = argc > 3 ? argv[3] : "bar";
-//TODO: change this, calling a function that returns node_t
-//remotenode->name = malloc(16);
-//remotenode->name = remotename;
+ meshlink_handle_t* myhandle;
-meshlink_handle_t* myhandle;
+ myhandle = meshlink_open(confbase, name);
-meshlink_open(confbase, name);
-meshlink_start(myhandle);
+ //Register callback function for incoming data
+ meshlink_set_receive_cb(myhandle, handle_recv_data);
-//Register callback function for incoming data
-meshlink_receive_cb_t(handle_recv_data);
+ meshlink_start(myhandle);
-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);
+ if(!remotenode) {
+ fprintf(stderr, "Node %s not known yet.\n", remotenode);
+ continue;
+ }
-//send out data
-meshlink_send(myhandle,remotenode,mydata,sizeof(mydata));
-sleep(10); //give time to this thread to finish before we exit
-}
-free(remotenode);
-return 0;
+ //sample data to send out
+ char mydata[200];
+ memset(mydata,0,200);
+ strcpy(mydata,"Hello World!");
+
+ //send out data
+ meshlink_send(myhandle,remotenode,mydata,sizeof(mydata));
+ }
+
+ meshlink_stop(myhandle);
+ meshlink_close(myhandle);
+
+ return 0;
}