int st_rpcAsync(elcdRpcCommand_t cmd, cJSON* params, rpcCallback_t callback, void *pArg) { int res = -1; int i; for( i = 0; i<RPC_POOL_SIZE; i++ ) if( pool.waiting[i].id == 0 ) break; if( i >= RPC_POOL_SIZE ) { eprintf("%s: RPC pool is full\n", __FUNCTION__); return -1; } unsigned int id = get_id(); char *msg = rpc_request( rpc_getCmdName(cmd), id, params ); if( !msg ) { eprintf("%s: failed to create RPC message\n", __FUNCTION__); return -1; } pool.waiting[i].id = id; pool.waiting[i].callback = callback; pool.waiting[i].pArg = pArg; #ifdef DEBUG pool.waiting[i].cmd = cmd; #endif #ifdef RPC_POOL_TRACE pool.waiting[i].msg = msg; dprintf("%s[%2d]: -> %s\n", __FUNCTION__, i, msg); #endif RPC_TRACE("st: -> %s\n", msg); #ifdef RPC_DUMP write(st_rpc_fd, msg, strlen(msg)); write(st_rpc_fd, "\n", 1); #endif if( client_write( &pool.socket, msg, strlen(msg)+1 ) > 0 ) { res = i; st_poolPrint(); } else { eprintf("%s: failed to write %s\n", __FUNCTION__, rpc_getCmdName(cmd)); st_poolFreeAt(i); } #ifndef RPC_POOL_TRACE free(msg); #endif return res; }
/** * main processing of the request * */ void TrexRpcServerReqRes::process_request_raw(const std::string &request, std::string &response) { std::vector<TrexJsonRpcV2ParsedObject *> commands; Json::FastWriter writer; Json::Value response_json; /* first parse the request using JSON RPC V2 parser */ TrexJsonRpcV2Parser rpc_request(request); rpc_request.parse(commands); int index = 0; /* for every command parsed - launch it */ for (auto command : commands) { Json::Value single_response; /* the command itself should be protected */ std::unique_lock<std::mutex> lock(*m_lock); command->execute(single_response); lock.unlock(); delete command; response_json[index++] = single_response; /* batch is like getting all the messages one by one - it should not be considered as stuck thread */ /* need to think if this is a good thing */ //m_monitor.tickle(); } /* write the JSON to string and sever on ZMQ */ if (response.size() == 1) { response = writer.write(response_json[0]); } else { response = writer.write(response_json); } verbose_json("Server Replied: ", response); }