int main(int argc, char **argv) { char url[1024]; char value[1024]; char keys[256]; char host[512]; char authData[512]; BOOL authFlag = FALSE; memset(url,0,sizeof(url)); memset(value,0,sizeof(value)); memset(keys,0,sizeof(keys)); memset(host,0,sizeof(host)); memset(authData,0,sizeof(authData)); int type = 0; int opt,i,res = 0,cnt=0,cntmax=0; int option_index = 0; BOOL isFinish = FALSE,authFinish; Wilddog_T wilddog = 0; Wilddog_Node_T * p_node = NULL,*p_head = NULL; static struct option long_options[] = { {"auth", required_argument, 0, 0 }, {"value", required_argument, 0, 0 }, {"key", required_argument, 0, 0 }, {0, 0, 0, 0 } }; while((opt=getopt_long(argc,argv,"hl:",long_options,&option_index)) != -1) { switch (opt) { case 0: //printf("option %s", long_options[option_index].name); if (optarg) { if(strcmp(long_options[option_index].name,"key")==0) memcpy(keys, optarg,strlen(optarg)); if(strcmp(long_options[option_index].name,"value")==0) memcpy(value, optarg,strlen(optarg)); if(strcmp(long_options[option_index].name,"auth")==0) { authFlag = TRUE; memcpy(authData, optarg,strlen(optarg)); } } break; case 'h': fprintf(stderr, \ "Usage: %s setAuth|getValue|setValue|push|removeValue|addObser\n" "\t|disSetValue|disPush|disRemove|cacelDis|offLine|onLine\n" "\tver -l coap://<your appid>.wilddogio.com/ [ --auth=<auth data> " "--key=<key> --value=<value>]\n", argv[0]); return 0; case 'l': strcpy(url, (const char*)optarg); getHostFromAppid(host,url); sprintf(&host[strlen(host)],".wilddogio.com"); break; default: /* '?' */ fprintf(stderr, \ "Usage: %s setAuth|getValue|setValue|push|removeValue|addObser\n" "\t|disSetValue|disPush|disRemove|cacelDis|offLine|onLine\n" "\tver -l coap://<your appid>.wilddogio.com/ [ --auth=<auth data> " "--key=<key> --value=<value>]\n", argv[0]); return 0; } } for (i = 0; optind < argc; i++, optind++) { if(i == 0) { if(strcmp(argv[optind],"getValue")==0) { type= TEST_CMD_GET; cntmax = 0; } else if(strcmp(argv[optind],"setValue")==0) { type= TEST_CMD_SET; cntmax = 0; } else if(strcmp(argv[optind],"push")==0) { type=TEST_CMD_PUSH; cntmax = 0; } else if(strcmp(argv[optind],"removeValue")==0) { type=TEST_CMD_DELE; cnt =0; } else if(strcmp(argv[optind],"addObserver")==0) { type= TEST_CMD_ON; cntmax = 10; } else if(strcmp(argv[optind],"setAuth")==0) { type= TEST_CMD_SETAUTH; cntmax = 0; } else if(strcmp(argv[optind],"disSetValue")==0) { type= TEST_CMD_ONDISSET; cntmax = 0; } else if(strcmp(argv[optind],"disPush")==0) { type= TEST_CMD_ONDISPUSH; cntmax = 0; } else if(strcmp(argv[optind],"disRemove")==0) { type= TEST_CMD_ONDISREMOVE; cntmax = 0; } else if(strcmp(argv[optind],"cacelDis")==0) { type= TEST_CMD_CANCELDIS; cntmax = 0; } else if(strcmp(argv[optind],"offLine")==0) { type= TEST_CMD_OFFLINE; cntmax = 0; } else if(strcmp(argv[optind],"onLine")==0) { type= TEST_CMD_ONLINE; cntmax = 0; } } } if( !type) { printf("Usage: %s setAuth|getValue|setValue|push|removeValue|addObser\n" "\t|disSetValue|disPush|disRemove|cacelDis|offLine|onLine\n" "\tver -l coap://<your appid>.wilddogio.com/ [ --auth=<auth data> " "--key=<key> --value=<value>]\n", argv[0]); return 0; } /*Create an node which type is an object*/ p_head = wilddog_node_createObject(NULL); /*Create an node which type is UTF-8 Sring*/ p_node = wilddog_node_createUString((Wilddog_Str_T *)keys, \ (Wilddog_Str_T *)value); /*Add p_node to p_head, then p_node is the p_head's child node*/ wilddog_node_addChild(p_head, p_node); /*Init a wilddog client*/ wilddog = wilddog_initWithUrl((Wilddog_Str_T *)url); if(authFlag == TRUE) res = wilddog_auth((u8*)host,(u8*)authData, \ strlen((const char *)authData), auth_callback,(void*)&authFinish); switch(type) { case TEST_CMD_GET: /*Send the query method*/ res = wilddog_getValue(wilddog, (onQueryFunc)getValue_callback, \ (void*)&isFinish); break; case TEST_CMD_SET: /*Send the set method*/ res = wilddog_setValue(wilddog,p_head,setValue_callback, \ (void*)&isFinish); break; case TEST_CMD_PUSH: /*Send the push method*/ res = wilddog_push(wilddog, p_head, push_callback, \ (void *)&isFinish); break; case TEST_CMD_DELE: /*Send the remove method*/ res = wilddog_removeValue(wilddog, removeValue_callback, \ (void*)&isFinish); break; case TEST_CMD_ON: /*Observe on*/ res = wilddog_addObserver(wilddog, WD_ET_VALUECHANGE,\ addObserver_callback, \ (void*)&isFinish); break; case TEST_CMD_SETAUTH: wilddog_debug("TEST_CMD_SETAUTH token = %s",value); res = wilddog_auth((u8*)host,(u8*)value, \ strlen((const char *)value), auth_callback,(void*)&isFinish); case TEST_CMD_ONDISSET: /*Send the remove method*/ res = wilddog_onDisconnectSetValue(wilddog,p_head,onDis_callback, \ (void*)&isFinish); break; case TEST_CMD_ONDISPUSH: /*Send the remove method*/ res = wilddog_onDisconnectPush(wilddog,p_head,onDis_callback, \ (void*)&isFinish); break; case TEST_CMD_ONDISREMOVE: /*Send the remove method*/ res = wilddog_onDisconnectRemoveValue(wilddog, onDis_callback, \ (void*)&isFinish); break; case TEST_CMD_CANCELDIS: /*Send the remove method*/ res = wilddog_cancelDisconnectOperations(wilddog, onDis_callback, \ (void*)&isFinish); break; case TEST_CMD_OFFLINE: /*Send the remove method*/ res = wilddog_goOffline(); break; case TEST_CMD_ONLINE: /*Send the remove method*/ res = wilddog_goOnline(); break; } /*Delete the node*/ wilddog_node_delete(p_head); while(1) { if(TRUE == isFinish) { if(type == TEST_CMD_ON) wilddog_debug("get new data %d times! after %d times, will " \ "remove Observer.", cnt, cntmax); cnt++; isFinish = FALSE; if(cnt > cntmax)/*force break when received new data.*/ { if(type == TEST_CMD_ON) { wilddog_debug("off the data!"); /*Observe off*/ wilddog_removeObserver(wilddog, WD_ET_VALUECHANGE); } break; } } /* * Handle the event and callback function, it must be called in a * special frequency */ wilddog_trySync(); } /*Destroy the wilddog clent and release the memory*/ wilddog_destroy(&wilddog); return res; }
int main(int argc, char **argv) { char url[1024]; char cmd[256]; char data[32]; char verbose[32]; char quote[32]; char authvalue[256]; BOOL isFinished = FALSE; Wilddog_T wilddog = 0; int opt; int option_index = 0; WATCH_CTX watch_ctx; char *host = NULL; memset(url,0,sizeof(url)); memset(cmd,0,sizeof(cmd)); memset(data,0,sizeof(data)); memcpy(data, "yes",strlen("yes")); memset(verbose,0,sizeof(verbose)); memcpy(verbose, "no",strlen("no")); memset(quote,0,sizeof(quote)); memcpy(quote, "yes",strlen("yes")); memset(authvalue,0,sizeof(authvalue)); static struct option long_options[] = { {"ignore-origin-data", required_argument, 0, 0 }, {"verbose", required_argument, 0, 0}, {"ignore-leaf-quote", required_argument, 0, 0}, {"version", no_argument, 0, 0}, {"authvalue", required_argument, 0, 0}, {0, 0, 0, 0 } }; while ((opt = getopt_long(argc, argv, "ovsh",long_options, &option_index)) != -1) { switch (opt) { case 0: //printf("option!!! %s\n", long_options[option_index].name); if (optarg) { if(strcmp(long_options[option_index].name,"ignore-origin-data")==0) memcpy(data, optarg,strlen(optarg)); if(strcmp(long_options[option_index].name,"verbose")==0) memcpy(verbose, optarg,strlen(optarg)); if(strcmp(long_options[option_index].name,"ignore-leaf-quote")==0) memcpy(quote, optarg,strlen(optarg)); if(strcmp(long_options[option_index].name,"authvalue")==0) memcpy(authvalue, optarg,strlen(optarg)); } if(strcmp(long_options[option_index].name,"version")==0) { printf("liveshell Version: %s\n", LIVESHELL_VERSION); return 0; } break; case 'o': memcpy(data, "no",strlen("no")); break; case 'v': memcpy(verbose, "yes",strlen("yes")); break; case 's': memcpy(quote, "no",strlen("no")); break; case 'h': fprintf(stderr, "Usage: %s [option] [--authvalue= your auth data] <your wilddog url> <your callback command> \n", argv[0]); return 0; default: /* '?' */ fprintf(stderr, "Usage: %s [option] [--authvalue= your auth data] <your wilddog url> <your callback command> \n", argv[0]); return 0; } } if( argc < 3 ) { printf("Usage: %s [option] [--authvalue= your auth data] <your wilddog url> <your callback command> \n", argv[0]); return 0; } memcpy(cmd, argv[argc-1], strlen(argv[argc-1])); memcpy(url, argv[argc-2], strlen(argv[argc-2])); wilddog = wilddog_initWithUrl((Wilddog_Str_T *)url); if(0 == wilddog) { wilddog_debug("new wilddog failed!"); return 0; } if(strlen(authvalue) != 0) { host = getHostFromUrl(url); //printf("host:%s\n", host); //printf("authvalue:%s\n", authvalue); wilddog_auth((u8*)host,(u8*)authvalue,strlen((const char *)authvalue),auth_callback,(void*)&isFinished); } isFinished = FALSE; watch_ctx.cmd = cmd; watch_ctx.data = data; watch_ctx.verbose = verbose; watch_ctx.quote = quote; watch_ctx.isFinished = &isFinished; wilddog_addObserver(wilddog, WD_ET_VALUECHANGE, observer_callback, (void*)&watch_ctx); while(1) { if(TRUE == *(watch_ctx.isFinished)) { //wilddog_debug("get new data %d times!", count++); *(watch_ctx.isFinished) = FALSE; /*do not break*/ //break; } wilddog_trySync(); } wfree(host); wilddog_destroy(&wilddog); return 0; }