/* * Allows to directly send raw json data instead of generating it on the fly */ void jsonRpc_sendResponseRaw(jsonRpcServer_t* jrs, jsonRpcClient_t* client, fStr_t* fStr_responseRawData, char* additionalHeaderData) { // build json request data // example: {"method": "getwork", "params": [], "id":0} fStr_t* fStr_jsonRequestData = fStr_alloc(1024*64); // 64KB (this is also used as the recv buffer!) fStr_appendFormatted(fStr_jsonRequestData, "{ \"result\": "); // { "result": "Hallo JSON-RPC", "error": null, "id": 1} fStr_append(fStr_jsonRequestData, fStr_responseRawData); fStr_append(fStr_jsonRequestData, ", \"error\": null, \"id\": 1}"); // prepare header fStr_buffer1kb_t fStrBuffer_header; fStr_t* fStr_headerData = fStr_alloc(&fStrBuffer_header, FSTR_FORMAT_UTF8); // header fields fStr_appendFormatted(fStr_headerData, "HTTP/1.1 200 OK\r\n"); fStr_appendFormatted(fStr_headerData, "Server: ypoolbackend 0.1\r\n"); fStr_appendFormatted(fStr_headerData, "Connection: keep-alive\r\n"); // keep-alive fStr_appendFormatted(fStr_headerData, "Content-Type: application/json\r\n"); fStr_appendFormatted(fStr_headerData, "Content-Length: %d\r\n", fStr_len(fStr_jsonRequestData)); if( additionalHeaderData ) fStr_appendFormatted(fStr_headerData, "%s", additionalHeaderData); fStr_appendFormatted(fStr_headerData, "\r\n"); // empty line concludes the header // send header and data send(client->clientSocket, fStr_get(fStr_headerData), fStr_len(fStr_headerData), 0); send(client->clientSocket, fStr_get(fStr_jsonRequestData), fStr_len(fStr_jsonRequestData), 0); fStr_free(fStr_jsonRequestData); }
/* * Allows to directly send raw json data instead of generating it on the fly */ void jsonRpc_sendFailedToAuthorize(jsonRpcServer_t* jrs, jsonRpcClient_t* client) { // build json request data // example: {"method": "getwork", "params": [], "id":0} fStr_t* fStr_jsonRequestData = fStr_alloc(1024*64); // 64KB (this is also used as the recv buffer!) fStr_append(fStr_jsonRequestData, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"" "\"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd\">" "<HTML>" "<HEAD>" "<TITLE>Error</TITLE>" "<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>" "</HEAD>" "<BODY><H1>401 Unauthorized.</H1></BODY>" "</HTML>" ); // prepare header fStr_buffer1kb_t fStrBuffer_header; fStr_t* fStr_headerData = fStr_alloc(&fStrBuffer_header, FSTR_FORMAT_UTF8); // header fields fStr_appendFormatted(fStr_headerData, "HTTP/1.0 401 Authorization Required\r\n"); fStr_appendFormatted(fStr_headerData, "Server: ypoolbackend 0.1\r\n"); fStr_appendFormatted(fStr_headerData, "WWW-Authenticate: Basic realm=\"jsonrpc\"\r\n"); fStr_appendFormatted(fStr_headerData, "Connection: keep-alive\r\n"); // keep-alive fStr_appendFormatted(fStr_headerData, "Content-Type: text/html\r\n"); fStr_appendFormatted(fStr_headerData, "Content-Length: %d\r\n", fStr_len(fStr_jsonRequestData)); fStr_appendFormatted(fStr_headerData, "\r\n"); // empty line concludes the header // send header and data send(client->clientSocket, fStr_get(fStr_headerData), fStr_len(fStr_headerData), 0); send(client->clientSocket, fStr_get(fStr_jsonRequestData), fStr_len(fStr_jsonRequestData), 0); fStr_free(fStr_jsonRequestData); }
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); } */ }