X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=examples%2Fmeshlinkapp.c;h=54e82a39ed4099fcc0d5d2bb752970091f333779;hb=HEAD;hp=115f85dcc44e8245a25c464fa0d34c9817135076;hpb=0e9443d08d75c94c679676d0bfd7a4e0c8bbce5b;p=meshlink diff --git a/examples/meshlinkapp.c b/examples/meshlinkapp.c index 115f85dc..67f996a4 100644 --- a/examples/meshlinkapp.c +++ b/examples/meshlinkapp.c @@ -1,46 +1,60 @@ -#include -#include +#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){ -char *confbase = argc > 1 ? argv[1] : "/tmp/meshlink/"; -char *name = argc > 2 ? argv[2] : "foo"; -//debug_level = 5; +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"; -meshlink_node_t* remotenode = new_node(); -char *remotename = argc > 3 ? argv[3] : "bar"; + meshlink_handle_t *myhandle; -//TODO: change this, calling a function that returns node_t -//remotenode->name = malloc(16); -//remotenode->name = remotename; + myhandle = meshlink_open(confbase, name, "meshlinkapp", DEV_CLASS_STATIONARY); -meshlink_handle_t* myhandle; + if(!myhandle) { + fprintf(stderr, "Could not open MeshLink: %s", meshlink_strerror(meshlink_errno)); + return 1; + } -meshlink_open(confbase, name); -meshlink_start(myhandle); + //Register callback function for incoming data + meshlink_set_receive_cb(myhandle, (meshlink_receive_cb_t)handle_recv_data); -//Register callback function for incoming data -meshlink_receive_cb_t(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 -meshlink_send(myhandle,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; }