static void duv_walk_cb(uv_handle_t *handle, duk_context *ctx) { duv_handle_t* data = handle->data; duk_dup(ctx, 0); duv_push_ref(ctx, data->context); duv_push_ref(ctx, data->ref); duk_call_method(ctx, 1); }
// Loop the client thread here void *wallyClientThread(void *argPtr){ slog(DEBUG,DEBUG,"WallyClient Thread started. Waiting for plugins to get ready."); pthread_mutex_init(&commandMutex,0); while(ph->pluginLoaderDone == false){ usleep(100); } slog(DEBUG,FULLDEBUG,"WallyClient Thread started. Plugins ready."); commandMap = malloc(sizeof(hash_table)); ht_init(commandMap, HT_KEY_CONST | HT_VALUE_CONST, 0.05); tempMap = malloc(sizeof(hash_table)); ht_init(tempMap, HT_KEY_CONST | HT_VALUE_CONST, 0.05); int startDelay = 1; int ret=0; char *startDelayString = NULL; clientThreadRunning = true; while(clientThreadRunning == true) { switch(clientState) { case DISCOVERY: //ssdpDiscovery(1); // Both plugins (ssdp and cloudConnector) // set ph->location and return once they registered // Cloud connector needs additional handling after asprintf(&startDelayString,"%d",startDelay); if(ph->ssdp) { call("ssdp::discovery",&ret,startDelayString); } else if(ph->cloud) { call("cloud::connect",&ret,startDelayString); } free(startDelayString); if(ph->location) { if(run_callback){ slog(DEBUG,DEBUG,"Executing JS Callback for client discovery"); #ifndef WITH_SEADUK duv_push_ref(ctx, finishCallback); duk_push_string(ctx, ph->location); duk_call(ctx, 1); duk_to_int(ctx, -1); #endif } saveLocation(FLAGFILE); } startDelay = 1; slog(DEBUG,DEBUG,"Change state from discovery to register"); clientState = REGISTER; break; case REGISTER: if(ph->location){ if(registerClient(ph->location)){ slog(DEBUG,DEBUG,"Starting longpoll."); url_longpoll(commandURL,60,LONGPOLL_INIT,NULL); clientState = COMMAND; } else { slog(DEBUG,DEBUG,"Registration failed."); } } else { slog(LVL_INFO,WARN,"Register was called without a valid location"); } break; case COMMAND: slog(DEBUG,DEBUG,"Requesting next core command from %s",commandURL); int ret = getCommand(commandURL,60); if(ret == true && ht_get_simple(commandMap,"command")){ int commandValid = false; slog(DEBUG,DEBUG,"Command : %s",ht_get_simple(commandMap,"command")); // Handle the commands which are supported by the wallyd here if(ht_compare(commandMap,"command","config")){ commandValid = true; slog(DEBUG,DEBUG,"Preparing to persist config"); if(persistConfig(registerMap)){ sendSuccess(ht_get_simple(commandMap,"id")); } } if(ht_compare(commandMap,"command","reboot")){ commandValid = true; sendSuccess(ht_get_simple(commandMap,"id")); slog(DEBUG,DEBUG,"Preparing to reboot"); system(BIN_REBOOT); } if(ht_compare(commandMap,"command","firmwareUpdate")){ commandValid = true; sendSuccess(ht_get_simple(commandMap,"id")); slog(DEBUG,DEBUG,"Preparing to update firmware"); system(BIN_UPDATEFW); } if(ht_compare(commandMap,"command","getlog")){ commandValid = true; slog(DEBUG,DEBUG,"Preparing to send log to server"); //sendSuccess(ht_get_simple(commandMap,"id")); //persistConfig(registerMap); } if(commandValid == false){ slog(DEBUG,DEBUG,"Command %s not valid. Ingoring.",ht_get_simple(commandMap,"command")); sendFailed(ht_get_simple(commandMap,"id"),"unknown.command"); } } else { slog(LVL_INFO,WARN,"getCommand failed."); } pthread_mutex_unlock(&commandMutex); break; case QUIT: slog(DEBUG,DEBUG,"Thread is quiting."); break; } sleep(threadDelay); } return 0; }