/* * Helper method in case parsing of an array fails and the already parsed data needs to be freed again */ void jsonObject_destroyArray(jsonObjectArray_t* jsonObjectArray) { for(uint32 i=0; i<jsonObjectArray->list_values->objectCount; i++) { jsonObject_t* objValue = (jsonObject_t*)simpleList_get(jsonObjectArray->list_values, i); jsonObject_freeObject(objValue); } simpleList_free(jsonObjectArray->list_values); free(jsonObjectArray); }
/* * Helper method in case parsing of an raw object fails and the already parsed data needs to be freed again */ void jsonObject_destroyRawObject(jsonObjectRawObject_t* jsonObjectRawObject) { for(uint32 i=0; i<jsonObjectRawObject->list_paramPairs->objectCount; i++) { jsonObjectRawObjectParameter_t* objParam = (jsonObjectRawObjectParameter_t*)customBuffer_get(jsonObjectRawObject->list_paramPairs, i); jsonObject_freeStringData(objParam->stringNameData); jsonObject_freeObject(objParam->jsonObjectValue); } customBuffer_free(jsonObjectRawObject->list_paramPairs); free(jsonObjectRawObject); }
/* * A full request was received from a client a can now be processed */ void jsonRpc_processRequest(jsonRpcServer_t* jrs, jsonRpcClient_t* client) { char* requestData = (char*)(client->recvBuffer+client->recvDataHeaderEnd); sint32 requestLength = (sint32)(client->recvDataSizeFull - client->recvDataHeaderEnd); // parse data //jsonParser_parse(requestData, requestLength); jsonObject_t* jsonObject = jsonParser_parse((uint8*)requestData, requestLength); // get method jsonObject_t* jsonMethodName = jsonObject_getParameter(jsonObject, "method"); // jsonObject_t* jsonParameter = jsonObject_getParameter(jsonObject, "params"); unused? uint32 methodNameLength = 0; uint8* methodNameString = jsonObject_getStringData(jsonMethodName, &methodNameLength); if( methodNameString ) { if( methodNameLength == 7 && memcmp(methodNameString, "getwork", 7) == 0 ) { // ... } else { printf("JSON-RPC: Unknown method to call - "); for(uint32 i=0; i<methodNameLength; i++) { printf("%c", methodNameString[i]); } printf("\n"); } } else { // invalid json data // kick the client #ifdef _WIN32 closesocket(client->clientSocket); #else close(client->clientSocket); #endif client->clientSocket = 0; client->disconnected = true; } jsonObject_freeObject(jsonObject); //// close connection (no keep alive) //closesocket(client->clientSocket); //client->clientSocket = 0; //client->disconnected = true; // reset pointers and indices in case the client sends another request (keep alive) client->recvIndex = 0; client->recvDataHeaderEnd = 0; client->recvDataSizeFull = 0; }
void NEWnotifyStats(){ // sprintf(sURL, "%s/report.php?key=%s&workerid=%s&start=%llu&blockstart=%llu&now=%llu&val=%g&primes=%u&sieves=%g&candidates=%g&shares=%u%s", // commandlineInput.centralServer, commandlineInput.csApiKey, curl_easy_escape(curl, jsonRequestTarget.authUser, 0), // primeStats.startTime, primeStats.primeLastUpdate, (uint64)getTimeMilliseconds(), primeStats.fShareValue, primeStats.primeChainsFound, // primeStats.nSieveRounds, primeStats.nCandidateCount, valid_shares, chains ); fStr_buffer4kb_t fStrBuffer_parameter; fStr_t* fStr_parameter = fStr_alloc(&fStrBuffer_parameter, FSTR_FORMAT_UTF8); fStr_appendFormatted(fStr_parameter, "[\"key\":\"%s\"", commandlineInput.csApiKey); fStr_appendFormatted(fStr_parameter, ",\"workerid\":\"%s\"", jsonRequestTarget.authUser); fStr_appendFormatted(fStr_parameter, ",\"start\":\"%u\"", primeStats.startTime); fStr_appendFormatted(fStr_parameter, ",\"blockstart\":\"%u\"", primeStats.primeLastUpdate); fStr_appendFormatted(fStr_parameter, ",\"now\":\"%u\"", (uint64)getTimeMilliseconds()); fStr_appendFormatted(fStr_parameter, ",\"val\":\"%hf\"", primeStats.fShareValue); fStr_appendFormatted(fStr_parameter, ",\"primes\":\"%u\"", primeStats.primeChainsFound); fStr_appendFormatted(fStr_parameter, ",\"sieves\":\"%u\"", primeStats.nSieveRounds); fStr_appendFormatted(fStr_parameter, ",\"candidates\":\"%u\"", primeStats.nCandidateCount); fStr_appendFormatted(fStr_parameter, ",\"shares\":\"%d\"", valid_shares); for(int i=2; i<=std::max(6,(int)primeStats.bestPrimeChainDifficultySinceLaunch); i++){ fStr_appendFormatted(fStr_parameter,",\"ch[%d]\":\"%d\"", i, primeStats.chainCounter[0][i]); } fStr_append(fStr_parameter, "]"); // finish constructing the request printf("JSON String: %s\n", fStr_get(fStr_parameter)); printf("Stats server: %s\n", statsRequestTarget.ip); printf("Stats port: %u\n", statsRequestTarget.port); // send request sint32 rpcErrorCode = 0; char* command = "shareFound"; jsonObject_t* jsonReturnValue = jsonClient_request(&statsRequestTarget, command, fStr_parameter, &rpcErrorCode); if( jsonReturnValue == NULL ) { printf("Notify Stats failed :(\n"); } else { // rpc call worked, sooooo.. is the server happy with the result? jsonObject_t* jsonReturnValueBool = jsonObject_getParameter(jsonReturnValue, "result"); if( jsonObject_isTrue(jsonReturnValueBool) ) { //jsonObject_t* jsonResult = jsonObject_getParameter(jsonReturnValue, "result"); //jsonObject_t* jsonResult_data = jsonObject_getParameter(jsonResult, "data"); //jsonObject_t* jsonResult_hash1 = jsonObject_getParameter(jsonResult, "hash1"); // jsonObject_t* jsonResult_target = jsonObject_getParameter(jsonResult, "target"); unused? //jsonObject_t* jsonResult_serverData = jsonObject_getParameter(jsonResult, "serverData"); //jsonObject_t* jsonResult_algorithm = jsonObject_getParameter(jsonResult, "algorithm"); jsonObject_freeObject(jsonReturnValue); } else { // the server says no to this share :( // printf("Server rejected share (BlockHeight: %u/%u nBits: 0x%08uX)\n", primecoinBlock->serverData.blockHeight, jhMiner_getCurrentWorkBlockHeight(primecoinBlock->threadIndex), primecoinBlock->serverData.client_shareBits); jsonObject_freeObject(jsonReturnValue); } } // jsonObject_freeObject(jsonReturnValue); }
void NEWnotifyCentralServerofShare(uint32 shareErrorCode, float shareValue, char* rejectReason) { printf("Running new notify to %s:%d \n",statsRequestTarget.ip,statsRequestTarget.port); //build the stats into a nice json bundle double statsPassedTime = getTimeMilliseconds() - primeStats.primeLastUpdate; if (statsPassedTime < 0) statsPassedTime *= -1; if( statsPassedTime < 1.0 ) statsPassedTime = 1.0; // avoid division by zero double primesPerSecond = (double)primeStats.primeChainsFound / (statsPassedTime / 1000.0); // prepare buffer to send fStr_buffer4kb_t fStrBuffer_parameter; fStr_t* fStr_parameter = fStr_alloc(&fStrBuffer_parameter, FSTR_FORMAT_UTF8); fStr_appendFormatted(fStr_parameter, "[\"key\":\"%utf8\"", commandlineInput.csApiKey); fStr_appendFormatted(fStr_parameter, ",\"workerid\":\"%utf8\"", jsonRequestTarget.authUser); bool bValidShare; if( shareErrorCode == 0 ){ bValidShare=true; }else{ bValidShare=false; } fStr_appendFormatted(fStr_parameter, ",\"validshare\":\"%B\"", bValidShare); //shareValue fStr_appendFormatted(fStr_parameter, ",\"shareValue\":\"%f\"", shareValue); //reject reason fStr_appendFormatted(fStr_parameter, ",\"rejectReason\":\""); if( rejectReason[0] != '\0' ){ fStr_appendFormatted(fStr_parameter, "%utf8", rejectReason); } fStr_appendFormatted(fStr_parameter, "\""); // fStr_appendFormatted(fStr_parameter, ",\"%s\":\"%s\"","key","value"); fStr_append(fStr_parameter, "]"); // finish constructing the request printf("JSON String: %s\n", fStr_parameter); // send request sint32 rpcErrorCode = 0; jsonObject_t* jsonReturnValue = jsonClient_request(&statsRequestTarget, "shareFound", fStr_parameter, &rpcErrorCode); if( jsonReturnValue == NULL ) { printf("Notify Share failed :(\n"); } else { // rpc call worked, sooooo.. is the server happy with the result? jsonObject_t* jsonReturnValueBool = jsonObject_getParameter(jsonReturnValue, "result"); if( jsonObject_isTrue(jsonReturnValueBool) ) { //jsonObject_t* jsonResult = jsonObject_getParameter(jsonReturnValue, "result"); //jsonObject_t* jsonResult_data = jsonObject_getParameter(jsonResult, "data"); //jsonObject_t* jsonResult_hash1 = jsonObject_getParameter(jsonResult, "hash1"); // jsonObject_t* jsonResult_target = jsonObject_getParameter(jsonResult, "target"); unused? //jsonObject_t* jsonResult_serverData = jsonObject_getParameter(jsonResult, "serverData"); //jsonObject_t* jsonResult_algorithm = jsonObject_getParameter(jsonResult, "algorithm"); jsonObject_freeObject(jsonReturnValue); } else { // the server says no to this share :( // printf("Server rejected share (BlockHeight: %u/%u nBits: 0x%08uX)\n", primecoinBlock->serverData.blockHeight, jhMiner_getCurrentWorkBlockHeight(primecoinBlock->threadIndex), primecoinBlock->serverData.client_shareBits); jsonObject_freeObject(jsonReturnValue); } } jsonObject_freeObject(jsonReturnValue); /* sint32 rpcErrorCode = 0; //uint32 time1 = GetTickCount(); // jsonObject_t* jsonReturnValue = jsonClient_request(&jsonRequestTarget, "notifyStats", NULL, &rpcErrorCode); //uint32 time2 = GetTickCount() - time1; // printf("request time: %dms\n", time2); if( jsonReturnValue == NULL ) { printf("centralServer notifyStats() failed with %serror code %d\n", (rpcErrorCode>1000)?"http ":"", rpcErrorCode>1000?rpcErrorCode-1000:rpcErrorCode); return; } else { jsonObject_t* jsonResult = jsonObject_getParameter(jsonReturnValue, "result"); jsonObject_t* jsonResult_data = jsonObject_getParameter(jsonResult, "data"); //jsonObject_t* jsonResult_hash1 = jsonObject_getParameter(jsonResult, "hash1"); // jsonObject_t* jsonResult_target = jsonObject_getParameter(jsonResult, "target"); unused? jsonObject_t* jsonResult_serverData = jsonObject_getParameter(jsonResult, "serverData"); //jsonObject_t* jsonResult_algorithm = jsonObject_getParameter(jsonResult, "algorithm"); if( jsonResult_data == NULL ) { printf("Error :(\n"); workData.workEntry[0].dataIsValid = false; jsonObject_freeObject(jsonReturnValue); return; } // data uint32 stringData_length = 0; uint8* stringData_data = jsonObject_getStringData(jsonResult_data, &stringData_length); //printf("data: %.*s...\n", (sint32)min(48, stringData_length), stringData_data); jsonObject_freeObject(jsonReturnValue); } */ }