IBR-DTNSuite  0.8
tools/src/c_api_examples/dtnping_async.c
Go to the documentation of this file.
00001 /* DTNPing C API asynchronous ersion */
00002 
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include <unistd.h>
00007 #include <ibrdtn/api/dtn_api.h>
00008 
00009 
00011 DTN_EP dep=-1;
00012 
00014 int16_t waitfor=0;
00015 
00016 
00018 void print_usage(char *me) {
00019     printf("Usage: %s dst-eid message\n",me);
00020     exit(-1);
00021 }
00022 
00023 
00024 void bundle_received(const void *data, uint32_t len) {
00025     int i;
00026     printf("Bundle received, %i bytes\n",len);
00027     printf("Data: \"");
00028     for (i=0; i< len; i++)
00029         putchar( *((char *)data+i) );
00030     puts("\"");
00031     waitfor-=len;
00032     if (waitfor < 0) {
00033         dtn_close_endpoint(dep);
00034         exit(0);
00035     }
00036 }
00037 
00038 
00040 int main(int argc, char *argv[])
00041 {
00042 
00043 
00044     if (argc < 3)
00045         print_usage(argv[0]);
00046 
00047     //Register endpoint and receive callback
00048     dep=dtn_register_endpoint("test",bundle_received, NULL);
00049 
00050     if (dep < 0) {
00051         puts("Problems registering endpoint. Terminating");
00052         return -2;
00053     }
00054 
00055     printf("Sending %s\n", argv[2]);
00056     waitfor=strlen(argv[2]+1);
00057 
00058     dtn_send_bundle(dep,argv[1],argv[2],strlen(argv[2])+1);
00059 
00060 
00061 
00062 
00063     //Wait for answer
00064     while(1)
00065         sleep(5);
00066 
00067     return 0;
00068 }