From 6dc519aa2e1a58f9d666ad670162b94476c4b148 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Thu, 24 Apr 2014 09:19:08 +0200 Subject: [PATCH] Update meshlinkapp.c. --- examples/meshlinkapp.c | 64 +++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/examples/meshlinkapp.c b/examples/meshlinkapp.c index 115f85dc..d9d5850f 100644 --- a/examples/meshlinkapp.c +++ b/examples/meshlinkapp.c @@ -1,46 +1,46 @@ -#include -#include - - -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; } -- 2.39.5