Have RPCSub use new callRPC.

This commit is contained in:
Arthur Britto
2013-02-01 19:25:01 -08:00
parent c25f480cb8
commit 1b9bf68877
5 changed files with 42 additions and 25 deletions

View File

@@ -701,31 +701,36 @@ bool responseRPC(
boost::function<void(const Json::Value& jvInput)> callbackFuncP, boost::function<void(const Json::Value& jvInput)> callbackFuncP,
const boost::system::error_code& ecResult, int iStatus, const std::string& strData) const boost::system::error_code& ecResult, int iStatus, const std::string& strData)
{ {
// Receive reply if (callbackFuncP)
if (iStatus == 401) {
throw std::runtime_error("incorrect rpcuser or rpcpassword (authorization failed)"); // Only care about the result, if we care to deliver it callbackFuncP.
else if ((iStatus >= 400) && (iStatus != 400) && (iStatus != 404) && (iStatus != 500)) // ?
throw std::runtime_error(strprintf("server returned HTTP error %d", iStatus));
else if (strData.empty())
throw std::runtime_error("no response from server");
// Parse reply // Receive reply
cLog(lsDEBUG) << "RPC reply: " << strData << std::endl; if (iStatus == 401)
throw std::runtime_error("incorrect rpcuser or rpcpassword (authorization failed)");
else if ((iStatus >= 400) && (iStatus != 400) && (iStatus != 404) && (iStatus != 500)) // ?
throw std::runtime_error(strprintf("server returned HTTP error %d", iStatus));
else if (strData.empty())
throw std::runtime_error("no response from server");
Json::Reader reader; // Parse reply
Json::Value jvReply; cLog(lsDEBUG) << "RPC reply: " << strData << std::endl;
if (!reader.parse(strData, jvReply)) Json::Reader reader;
throw std::runtime_error("couldn't parse reply from server"); Json::Value jvReply;
if (jvReply.isNull()) if (!reader.parse(strData, jvReply))
throw std::runtime_error("expected reply to have result, error and id properties"); throw std::runtime_error("couldn't parse reply from server");
Json::Value jvResult(Json::objectValue); if (jvReply.isNull())
throw std::runtime_error("expected reply to have result, error and id properties");
jvResult["result"] = jvReply; Json::Value jvResult(Json::objectValue);
(callbackFuncP)(jvResult); jvResult["result"] = jvReply;
(callbackFuncP)(jvResult);
}
return false; return false;
} }

View File

@@ -55,7 +55,7 @@ extern void callRPC(
const std::string& strUsername, const std::string& strPassword, const std::string& strUsername, const std::string& strPassword,
const std::string& strPath, const std::string& strMethod, const std::string& strPath, const std::string& strMethod,
const Json::Value& jvParams, const bool bSSL, const Json::Value& jvParams, const bool bSSL,
boost::function<void(const Json::Value& jvInput)> callbackFuncP); boost::function<void(const Json::Value& jvInput)> callbackFuncP = 0);
#endif #endif
// vim:ts=4 // vim:ts=4

View File

@@ -331,7 +331,7 @@ void HttpsClient::invokeComplete(const boost::system::error_code& ecResult, int
// ecResult: !0 = had an error, last entry // ecResult: !0 = had an error, last entry
// iStatus: result, if no error // iStatus: result, if no error
// strData: data, if no error // strData: data, if no error
bAgain = mComplete(ecResult ? ecResult : ecCancel, iStatus, strData); bAgain = mComplete && mComplete(ecResult ? ecResult : ecCancel, iStatus, strData);
} }
if (!mDeqSites.empty() && bAgain) if (!mDeqSites.empty() && bAgain)

View File

@@ -8,7 +8,7 @@
SETUP_LOG(); SETUP_LOG();
RPCSub::RPCSub(const std::string& strUrl, const std::string& strUsername, const std::string& strPassword) RPCSub::RPCSub(const std::string& strUrl, const std::string& strUsername, const std::string& strPassword)
: mUrl(strUrl), mUsername(strUsername), mPassword(strPassword) : mUrl(strUrl), mSSL(false), mUsername(strUsername), mPassword(strPassword)
{ {
std::string strScheme; std::string strScheme;
@@ -16,17 +16,22 @@ RPCSub::RPCSub(const std::string& strUrl, const std::string& strUsername, const
{ {
throw std::runtime_error("Failed to parse url."); throw std::runtime_error("Failed to parse url.");
} }
else if (strScheme == "https")
{
mSSL = true;
}
else if (strScheme != "http") else if (strScheme != "http")
{ {
throw std::runtime_error("Only http is supported."); throw std::runtime_error("Only http and https is supported.");
} }
mSeq = 1; mSeq = 1;
if (mPort < 0) if (mPort < 0)
mPort = 80; mPort = mSSL ? 443 : 80;
} }
// XXX Could probably create a bunch of send jobs in a single get of the lock.
void RPCSub::sendThread() void RPCSub::sendThread()
{ {
Json::Value jvEvent; Json::Value jvEvent;
@@ -59,12 +64,18 @@ void RPCSub::sendThread()
// Send outside of the lock. // Send outside of the lock.
if (bSend) if (bSend)
{ {
// XXX Might not need this in a try.
try try
{ {
cLog(lsDEBUG) << boost::str(boost::format("callRPC calling: %s") % mIp); cLog(lsDEBUG) << boost::str(boost::format("callRPC calling: %s") % mIp);
// Drop result. callRPC(
// (void) callRPC(theApp->getIOService(), mIp, mPort, mUsername, mPassword, mPath, "event", jvEvent); theApp->getIOService(),
mIp, mPort,
mUsername, mPassword,
mPath, "event",
jvEvent,
mSSL);
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {

View File

@@ -15,6 +15,7 @@ class RPCSub : public InfoSub
std::string mUrl; std::string mUrl;
std::string mIp; std::string mIp;
int mPort; int mPort;
bool mSSL;
std::string mUsername; std::string mUsername;
std::string mPassword; std::string mPassword;
std::string mPath; std::string mPath;