void processCurrentAuthCandidate() { if (currentAuthCandidate!=authCandidates.end() && hueComm.findInProgress) { // try to authorize FOCUSLOG("Auth candidate: uuid=%s, baseURL=%s -> try creating user", currentAuthCandidate->first.c_str(), currentAuthCandidate->second.c_str()); JsonObjectPtr request = JsonObject::newObj(); request->add("username", JsonObject::newString(userName)); request->add("devicetype", JsonObject::newString(deviceType)); hueComm.apiAction(httpMethodPOST, currentAuthCandidate->second.c_str(), request, boost::bind(&BridgeFinder::handleCreateUserAnswer, this, _1, _2), true); } else { // done with all candidates (or find aborted in hueComm) if (authCandidates.size()>0 && MainLoop::now()<startedAuth+authTimeWindow && hueComm.findInProgress) { // we have still candidates and time to do a retry in a second, and find is not aborted retryLoginTicket = MainLoop::currentMainLoop().executeOnce(boost::bind(&BridgeFinder::attemptPairingWithCandidates, this), 1*Second); return; } else { // all candidates tried, nothing found in given time LOG(LOG_NOTICE, "Could not register with a hue bridge"); hueComm.findInProgress = false; callback(ErrorPtr(new HueCommError(HueCommErrorNoRegistration, "No hue bridge found ready to register"))); // done! keepAlive.reset(); // will delete object if nobody else keeps it return; } } }
ErrorPtr JsonRpcComm::sendError(const char *aJsonRpcId, uint32_t aErrorCode, const char *aErrorMessage, JsonObjectPtr aErrorData) { JsonObjectPtr response = jsonRPCObj(); // create the error object JsonObjectPtr errorObj = JsonObject::newObj(); errorObj->add("code", JsonObject::newInt32(aErrorCode)); string errMsg; if (aErrorMessage) { errMsg = aErrorMessage; } else { errMsg = string_format("Error code %d (0x%X)", aErrorCode, aErrorCode); } errorObj->add("message", JsonObject::newString(errMsg)); // add the data object if any if (aErrorData) { errorObj->add("data", aErrorData); } // add the error object response->add("error", errorObj); // add the ID so the caller can associate with a previous request response->add("id", aJsonRpcId ? JsonObject::newString(aJsonRpcId) : JsonObjectPtr()); // now send FOCUSLOG("Sending JSON-RPC 2.0 error message:\n %s\n", response->c_strValue()); return sendMessage(response); }
ErrorPtr JsonRpcComm::sendResult(const char *aJsonRpcId, JsonObjectPtr aResult) { JsonObjectPtr response = jsonRPCObj(); // add the result, can be NULL response->add("result", aResult); // add the ID so the caller can associate with a previous request response->add("id", JsonObject::newString(aJsonRpcId)); // now send FOCUSLOG("Sending JSON-RPC 2.0 result message:\n %s\n", response->c_strValue()); return sendMessage(response); }
static JsonObjectPtr jsonRPCObj() { JsonObjectPtr obj = JsonObject::newObj(); // the mandatory version string all objects need to have obj->add("jsonrpc", JsonObject::newString("2.0")); return obj; }
ErrorPtr JsonRpcComm::sendRequest(const char *aMethod, JsonObjectPtr aParams, JsonRpcResponseCB aResponseHandler) { JsonObjectPtr request = jsonRPCObj(); // the method or notification name request->add("method", JsonObject::newString(aMethod)); // the optional parameters if (aParams) { request->add("params", aParams); } // in any case, count this call (even if it is a notification) requestIdCounter++; // in case this is a method call (i.e. a answer handler is specified), add the call ID if (aResponseHandler) { // add the ID so the callee can include it in the response request->add("id", JsonObject::newInt32(requestIdCounter)); // remember it in our map pendingAnswers[requestIdCounter] = aResponseHandler; } // now send FOCUSLOG("Sending JSON-RPC 2.0 request message:\n %s\n", request->c_strValue()); return sendMessage(request); }
void P44VdcHost::sendCfgApiResponse(JsonCommPtr aJsonComm, JsonObjectPtr aResult, ErrorPtr aError) { // create response JsonObjectPtr response = JsonObject::newObj(); if (!Error::isOK(aError)) { // error, return error response response->add("error", JsonObject::newInt32((int32_t)aError->getErrorCode())); response->add("errormessage", JsonObject::newString(aError->getErrorMessage())); response->add("errordomain", JsonObject::newString(aError->getErrorDomain())); VdcApiErrorPtr ve = boost::dynamic_pointer_cast<VdcApiError>(aError); if (ve) { response->add("errortype", JsonObject::newInt32(ve->getErrorType())); response->add("userfacingmessage", JsonObject::newString(ve->getUserFacingMessage())); } } else { // no error, return result (if any) response->add("result", aResult); } LOG(LOG_DEBUG, "Config API response: %s", response->c_strValue()); aJsonComm->sendMessage(response); }