| 242 | } |
| 243 | |
| 244 | int CommandLineRPC(int argc, char *argv[]) |
| 245 | { |
| 246 | string strPrint; |
| 247 | int nRet = 0; |
| 248 | try { |
| 249 | // Skip switches |
| 250 | while (argc > 1 && IsSwitchChar(argv[1][0])) { |
| 251 | argc--; |
| 252 | argv++; |
| 253 | } |
| 254 | |
| 255 | // Method |
| 256 | if (argc < 2) |
| 257 | throw runtime_error("too few parameters"); |
| 258 | string strMethod = argv[1]; |
| 259 | |
| 260 | // Parameters default to strings |
| 261 | std::vector<std::string> strParams(&argv[2], &argv[argc]); |
| 262 | UniValue params = RPCConvertValues(strMethod, strParams); |
| 263 | |
| 264 | // Execute and handle connection failures with -rpcwait |
| 265 | const bool fWait = GetBoolArg("-rpcwait", false); |
| 266 | do { |
| 267 | try { |
| 268 | const UniValue reply = CallRPC(strMethod, params); |
| 269 | |
| 270 | // Parse reply |
| 271 | const UniValue& result = find_value(reply, "result"); |
| 272 | const UniValue& error = find_value(reply, "error"); |
| 273 | |
| 274 | if (!error.isNull()) { |
| 275 | // Error |
| 276 | int code = error["code"].get_int(); |
| 277 | if (fWait && code == RPC_IN_WARMUP) |
| 278 | throw CConnectionFailed("server in warmup"); |
| 279 | strPrint = "error: " + error.write(); |
| 280 | nRet = abs(code); |
| 281 | } else { |
| 282 | // Result |
| 283 | if (result.isNull()) |
| 284 | strPrint = ""; |
| 285 | else if (result.isStr()) |
| 286 | strPrint = result.get_str(); |
| 287 | else |
| 288 | strPrint = result.write(2); |
| 289 | } |
| 290 | // Connection succeeded, no need to retry. |
| 291 | break; |
| 292 | } |
| 293 | catch (const CConnectionFailed&) { |
| 294 | if (fWait) |
| 295 | MilliSleep(1000); |
| 296 | else |
| 297 | throw; |
| 298 | } |
| 299 | } while (fWait); |
| 300 | } |
| 301 | catch (const boost::thread_interrupted&) { |
no test coverage detected