static bool alarms_timeout_subscribe_cb(LSHandle *sh, LSMessage *message, void *ctx) { bool retVal; bool fired = false; struct context *alrm_ctx = (struct context *)ctx; const char *payload = LSMessageGetPayload(message); struct json_object *object = json_tokener_parse(payload); if (is_error(object)) { POWERDLOG(LOG_CRIT,"%s: invalid json from sleep daemon",__func__); } else { fired = json_object_get_boolean(json_object_object_get(object, "fired")); } POWERDLOG(LOG_INFO,"%s: response with payload %s, count : %d", __FUNCTION__, payload, alrm_ctx->count); if(alrm_ctx->replyMessage) { if(fired) { retVal = LSMessageReply(GetLunaServiceHandle(), alrm_ctx->replyMessage, payload, NULL); if (!retVal) { POWERDLOG(LOG_WARNING, "%s could not send reply.", __FUNCTION__); } } else if(LSMessageGetConnection(alrm_ctx->replyMessage)) { retVal = LSMessageReply(LSMessageGetConnection(alrm_ctx->replyMessage), alrm_ctx->replyMessage, payload, NULL); if (!retVal) { POWERDLOG(LOG_WARNING, "%s could not send reply.", __FUNCTION__); } } alrm_ctx->count++; } else POWERDLOG(LOG_CRIT,"%s: replyMessage is NULL",__func__); if(alrm_ctx->count == 2) { if(!LSCallCancel(sh, alrm_ctx->call_token, NULL)) { POWERDLOG(LOG_WARNING, "%s could not cancel luna-service alarm call.", __FUNCTION__); } LSMessageUnref(alrm_ctx->replyMessage); free(alrm_ctx); } if (!is_error(object)) json_object_put(object); return true; }
/** * @brief Terminates a call causing any subscription for responses to end. * This is also called by garbage collector's collect() * when no more references to listener exist. * * @param listener */ void service_invoker::cancel(invoke_listener_base* listener) { LOG(__PRETTY_FUNCTION__); hl::ls_error lserror; if (!LSCallCancel(listener->sh_, listener->token_, &lserror)) lserror.warning("Cancel "); // set the token to zero to indicate we have been canceled listener->token_ = 0; }
static bool timingServiceResponse(LSHandle *sh, LSMessage *reply, void *ctx) { LSError lserror; LSErrorInit(&lserror); LSMessageToken token; const char *payload; clock_gettime(CLOCK_MONOTONIC, &stopTime); token = LSMessageGetResponseToken(reply); payload = LSMessageGetPayload(reply); double duration = ((double)stopTime.tv_sec + (((double)stopTime.tv_nsec)/1000000000.0)) - ((double)startTime.tv_sec + (((double)startTime.tv_nsec)/1000000000.0)); roundtripTime += duration; roundtripCount++; rcvdBytes += strlen(payload); sentBytes += url ? strlen(url) : 0; sentBytes += message ? strlen(message) : 0; g_message("%s Got response: duration %.02f ms, token %ld, payload %s", __FUNCTION__, duration * 1000.0, token, payload); if (--count > 0) { // resend the message! LSMessageToken sessionToken; clock_gettime(CLOCK_MONOTONIC, &startTime); /* Basic sending */ bool retVal = LSCallFromApplication(sh, url, message, appId, timingServiceResponse, ctx, &sessionToken, &lserror); if (!retVal) { LSErrorPrint (&lserror, stderr); LSErrorFree (&lserror); } } else { bool retVal = LSCallCancel (sh, token, &lserror); if (!retVal) { LSErrorPrint (&lserror, stderr); LSErrorFree (&lserror); } g_timeout_add (300, goodbye, ctx); return true; } return true; }
void DisplayBlocker::release() { LSError lserror; if (!m_service || !m_token) return; LSErrorInit(&lserror); if (!LSCallCancel(m_service, m_token, &lserror)) { LSErrorPrint(&lserror, stderr); LSErrorFree(&lserror); } }
static void SignalCancel(CBH *helper) { bool retVal; LSError lserror; LSErrorInit(&lserror); retVal = LSCallCancel(gServiceHandle, helper->token, &lserror); if (!retVal) { LSErrorPrint(&lserror, stderr); LSErrorFree(&lserror); } helper->token = 0; }
void EASPolicyManager::save() { if (!m_service) { g_warning ("Service handle not available yet, cannot query device policy"); return; } LSError lserror; LSErrorInit (&lserror); if (m_callToken) { g_debug ("Cancelling call token %lu", m_callToken); if (LSCallCancel (m_service, m_callToken, &lserror)) { g_warning ("Unable to cancel call with token %lu error message %s", m_callToken, lserror.message); } m_callToken = 0; } json_object* policyJson = m_aggregate->toNewJSON(); json_object_object_add(policyJson, "status", getPolicyStatus()); gchar* policyStr = g_strdup_printf ("{ \"objects\":[%s]}", json_object_to_json_string (policyJson)); g_message ("%s: Writing device policy %s to mojodb", __func__, policyStr); if (m_aggregate->m_id.empty()) { if (!LSCallOneReply (m_service, "palm://com.palm.db/put", policyStr, &EASPolicyManager::cbDevicePolicySaved, this, NULL, &lserror)) { g_warning ("%s: Failed at %s with message %s", __func__, lserror.func, lserror.message); LSErrorFree (&lserror); } } else { if (!LSCallOneReply (m_service, "palm://com.palm.db/merge", policyStr, &EASPolicyManager::cbDevicePolicySaved, this, NULL, &lserror)) { g_warning ("%s: Failed at %s with message %s", __func__, lserror.func, lserror.message); LSErrorFree (&lserror); } } json_object_put (policyJson); g_free (policyStr); }
MojErr MojLunaService::cancelImpl(MojServiceRequest* req) { LOG_TRACE("Entering function %s", __FUNCTION__); MojAssert(req); MojAssert(m_service || m_handle); MojAssertMutexUnlocked(m_mutex); MojLunaRequest* lunaReq = static_cast<MojLunaRequest*>(req); if (req->numRepliesExpected() > 1 && !lunaReq->cancelled()) { LSMessageToken lsToken = (LSMessageToken) req->token(); MojAssert(lsToken != LSMESSAGE_TOKEN_INVALID); MojLunaErr lserr; LSHandle* handle = getHandle(lunaReq->onPublic()); bool cancelled = LSCallCancel(handle, lsToken, lserr); MojLsErrCheck(cancelled, lserr); lunaReq->cancelled(true); } return MojErrNone; }
Activity::~Activity() { LSError lserror; LSErrorInit(&lserror); if (mToken != LSMESSAGE_TOKEN_INVALID) { if (!LSCallCancel(mHandle, mToken, &lserror)) { LSErrorPrint(&lserror, stderr); LSErrorFree(&lserror); } } if (mHandle) { if (LSUnregister(mHandle, &lserror)) { LSErrorPrint(&lserror, stderr); LSErrorFree(&lserror); } } }
void WebAppBase::destroyActivity() { if (!m_page) return; if (m_page->parent()) return; if (m_activityManagerToken == LSMESSAGE_TOKEN_INVALID) return; LSError lsError; LSErrorInit(&lsError); if (!LSCallCancel(WebAppManager::instance()->m_servicePrivate, m_activityManagerToken, &lsError)) { g_critical("%s: %d. Failed in canceling activity: %s", __PRETTY_FUNCTION__, __LINE__, lsError.message); LSErrorFree(&lsError); } m_activityManagerToken = LSMESSAGE_TOKEN_INVALID; }
void EASPolicyManager::watchSecurityPolicies() { LSError lserror; LSErrorInit (&lserror); if (m_callToken) { g_debug ("Cancelling call token %lu", m_callToken); if (LSCallCancel (m_service, m_callToken, &lserror)) { g_warning ("Unable to cancel call with token %lu error message %s", m_callToken, lserror.message); } m_callToken = 0; } gchar* query = g_strdup_printf ("{\"query\":{\"from\":\"com.palm.securitypolicy:1\",\"where\":[{\"prop\":\"_rev\",\"op\":\">\",\"val\":%d}], \"incDel\": true }}", m_lastRev); g_message ("Setting watch with paramters %s", query); bool result = LSCall (m_service, "palm://com.palm.db/watch", query, &EASPolicyManager::cbWatchResponse, this,&m_callToken, &lserror); if (!result) { g_warning ("%s: Failed at %s with message %s", __func__, lserror.func, lserror.message); LSErrorFree (&lserror); } g_free (query); }
void LunaServiceCall::cancel() { if (mToken == LSMESSAGE_TOKEN_INVALID) return; if (mServiceHandle == 0) return; if (mResponseLimit != 1 || mResponseCount != 1) { LSError error; LSErrorInit(&error); if (!LSCallCancel(mServiceHandle, mToken, &error)) { qWarning("Failed to cancel call"); LSErrorPrint(&error, stderr); LSErrorFree(&error); } } mToken = LSMESSAGE_TOKEN_INVALID; LunaServiceAdapter *adapter = static_cast<LunaServiceAdapter*>(parent()); adapter->removePendingCall(this); }
int main(int argc, char *argv[]) { bool haveNFlag = false; bool haveTFlag = false; std::string sizeStr; std::string typeStr; for (int i = 1; i < argc; i++) { std::string thisArg = argv[i]; if (thisArg == "-s") { if (i == (argc - 1)) { Usage(); } sizeStr = argv[i + 1]; fileSize = atoi(sizeStr.c_str()); if (fileSize < 0) { Usage(); } i++; } else if (thisArg == "-n") { haveNFlag = true; if (i == (argc -1)) { Usage(); } std::string nFileStr = argv[i + 1]; nFiles = atoi(nFilesStr.c_str()); if (nFiles < 0) { Usage(); } i++; } else if (thisArg == "-t") { haveTFlag = true; if (i == (argc - 1)) { Usage(); } typeStr = argv[i + 1]; i++; } else { Usage(); } } if (!haveTFlag) { Usage(); } if (haveNFlag) { printf("Run for %d files\n", nFiles); } else { printf("Run forever\n"); } printf("File size %d bytes\n", fileSize); printf("Use filecache type %s\n", typeStr.c_str()); LSError lserror; LSErrorInit(&lserror); GMainLoop *mainLoop = NULL; mainLoop = g_main_loop_new(NULL, FALSE); bool ret = LSRegister(NULL, &filecacheService, &lserror); if (!ret) { fprintf(stderr, "LSRegister failed\n"); exit(1); } ret = LSGmainAttach(filecacheService, mainLoop, &lserror); if (!ret) { fprintf(stderr, "LSGmainAttach failed\n"); exit(1); } // // Create types in filecache. // std::string typeName = typeStr; std::string loWatermark = "10000"; std::string hiWatermark = "100000000"; std::string size = sizeStr; std::string cost = "1"; std::string lifetime = "100000"; std::string payload = "{\"typeName\":\"" + typeName + "\"" + ", \"loWatermark\": " + loWatermark + ", \"hiWatermark\": " + hiWatermark + ", \"size\": " + size + ", \"cost\": " + cost + ", \"lifetime\": " + lifetime + ", \"dirType\": false }"; std::string DefineTypeURI = std::string(FILECACHE_SERVICE_URI) + "/DefineType"; printf("calling DefineType %s\n", payload.c_str()); ret = LSCall(filecacheService, DefineTypeURI.c_str(), payload.c_str(), FilecacheServiceCb, GINT_TO_POINTER(send_count - 1), NULL, &lserror); if (!ret) { fprintf(stderr, "DefineType failed\n"); exit(1); } // // Wait for response from this LSCall() // handledResponse = false; while (!handledResponse) { g_main_context_iteration(NULL, true); } printf("created filecache type %s\n", typeName.c_str()); // // OK now create file in the cache. // int filesCreated = 0; while (true) { std::string fileName = "a.txt"; size = sizeStr; std::string cost = "1"; std::string lifetime = "100000"; std::string subscribe = "true"; payload = "{\"typeName\":\"" + typeName + "\"" + ", \"fileName\":\"" + fileName + "\"" + ", \"size\": " + size + ", \"cost\": " + cost + ", \"lifetime\": " + lifetime + ", \"subscribe\": true }"; std::string InsertCacheObjectURI = std::string(FILECACHE_SERVICE_URI) + "/InsertCacheObject"; LSMessageToken token; handledResponse = false; printf("calling InsertCacheObject %s\n", payload.c_str()); ret = LSCall(filecacheService, InsertCacheObjectURI.c_str(), payload.c_str(), FilecacheServiceCb, GINT_TO_POINTER(send_count -1), &token, &lserror); if (!ret) { fprintf(stderr, "InsertCacheObject failed\n"); goto error; } while (!handledResponse) { g_main_context_iteration(NULL, true); } printf("created cache object\n"); // // Parse the returned json. // pbnjson::JSchemaFragment inputSchema("{}"); pbnjson::JDomParser parser(NULL); if (!parser.parse(jsonResponseStr, inputSchema, NULL)) { // handle error fprintf(stderr, "Error parsing json response\n"); exit(1); } pbnjson::JValue parsed = parser.getDom(); std::string cacheFileName = parsed["pathName"].asString(); printf("cacheFileName = %s\n", cacheFileName.c_str()); // // Write data to the file. // char buf[fileSize]; int fd; if ((fd = open(cacheFileName.c_str(), O_WRONLY)) == -1) { fprintf(stderr, "Error, unable to open file %s for writing\n", cacheFileName.c_str()); exit(1); } if (write(fd, buf, fileSize) != fileSize) { fprintf(stderr, "Error, write to %s failed\n", cacheFileName.c_str()); exit(1); } close(fd); // // Cancel the subscription. // ret = LSCallCancel(filecacheService, token, &lserror); if (!ret) { fprintf(stderr, "Cancel of subscription failed\n"); goto error; } printf("subscription cancelled\n"); filesCreated++; if (filesCreated == nFiles) { printf("Done with %d files created\n", nFiles); exit(0); } } exit(0); error: fprintf(stderr, "stresstest exits\n"); exit(1); }
static bool serviceResponse(LSHandle *sh, LSMessage *reply, void *ctx) { LSError lserror; LSErrorInit(&lserror); LSMessageToken token; const char *payload; bool free_payload = false; token = LSMessageGetResponseToken(reply); payload = LSMessageGetPayload(reply); //g_message("%s Handling: %ld, %s", __FUNCTION__, token, payload); if (line_number) { printf("%2d: ", current_line_number++); } if (query_list != NULL) { // Use set of queries to transform original object into reduced form that // only contains queried selections -- then pass that through normal formatting. struct json_object *original = json_tokener_parse(payload); struct json_object *new_object = json_object_new_object(); GList * query = query_list; if ( original && !is_error(original) ) { while (query) { char * query_text = (char*)query->data; struct json_object * result = apply_query(original, query_text); json_object_object_add(new_object, query_text, result); query = query->next; } payload = strdup(json_object_get_string(new_object)); free_payload = true; json_object_put(new_object); } } if (format_response) { struct json_object *object = json_tokener_parse(payload); if ( !object || is_error(object) ) { // fall back to plain print printf("%s\n", payload); } else { pretty_print(object, 0, line_number ? 4 /* expected characters in line numbers */ : 0); printf("\n"); json_object_put(object); } } else { printf("%s\n", payload); } if (free_payload) free((void*)payload); fflush(stdout); if (--count == 0) { bool retVal = LSCallCancel (sh, token, &lserror); if (!retVal) { LSErrorPrint (&lserror, stderr); LSErrorFree (&lserror); } g_timeout_add (300, goodbye, ctx); return true; } return true; }