Merge branch 'master' of github.com:jedmccaleb/NewCoin

This commit is contained in:
JoelKatz
2012-12-30 22:37:09 -08:00
11 changed files with 243 additions and 20 deletions

View File

@@ -537,6 +537,7 @@ int commandLineRPC(const std::vector<std::string>& vCmd)
theConfig.RPC_PORT,
theConfig.RPC_USER,
theConfig.RPC_PASSWORD,
"",
jvRequest.isMember("method") // Allow parser to rewrite method.
? jvRequest["method"].asString()
: vCmd[0],
@@ -597,11 +598,11 @@ int commandLineRPC(const std::vector<std::string>& vCmd)
return nRet;
}
Json::Value callRPC(const std::string& strIp, const int iPort, const std::string& strUsername, const std::string& strPassword, const std::string& strMethod, const Json::Value& params)
Json::Value callRPC(const std::string& strIp, const int iPort, const std::string& strUsername, const std::string& strPassword, const std::string& strPath, const std::string& strMethod, const Json::Value& params)
{
// Connect to localhost
if (!theConfig.QUIET)
std::cerr << "Connecting to: " << theConfig.RPC_IP << ":" << theConfig.RPC_PORT << std::endl;
std::cerr << "Connecting to: " << strIp << ":" << iPort << std::endl;
boost::asio::ip::tcp::endpoint
endpoint(boost::asio::ip::address::from_string(strIp), iPort);
@@ -618,7 +619,7 @@ Json::Value callRPC(const std::string& strIp, const int iPort, const std::string
// Send request
std::string strRequest = JSONRPCRequest(strMethod, params, Json::Value(1));
cLog(lsDEBUG) << "send request " << strMethod << " : " << strRequest << std::endl;
std::string strPost = createHTTPPost(strRequest, mapRequestHeaders);
std::string strPost = createHTTPPost(strPath, strRequest, mapRequestHeaders);
stream << strPost << std::flush;
// std::cerr << "post " << strPost << std::endl;

View File

@@ -41,7 +41,7 @@ public:
};
extern int commandLineRPC(const std::vector<std::string>& vCmd);
extern Json::Value callRPC(const std::string& strIp, const int iPort, const std::string& strUsername, const std::string& strPassword, const std::string& strMethod, const Json::Value& params);
extern Json::Value callRPC(const std::string& strIp, const int iPort, const std::string& strUsername, const std::string& strPassword, const std::string& strPath, const std::string& strMethod, const Json::Value& params);
#endif

View File

@@ -29,7 +29,7 @@ enum http_status_type
extern std::string JSONRPCRequest(const std::string& strMethod, const Json::Value& params,
const Json::Value& id);
extern std::string createHTTPPost(const std::string& strMsg,
extern std::string createHTTPPost(const std::string& strPath, const std::string& strMsg,
const std::map<std::string, std::string>& mapRequestHeaders);
extern int ReadHTTP(std::basic_istream<char>& stream,

View File

@@ -2370,12 +2370,12 @@ Json::Value RPCHandler::doUnsubscribe(Json::Value jvRequest)
// command is the method. The request object is supplied as the first element of the params.
Json::Value RPCHandler::doRpcCommand(const std::string& strMethod, Json::Value& jvParams, int iRole)
{
// cLog(lsTRACE) << "doRpcCommand:" << strMethod << ":" << jvParams;
cLog(lsTRACE) << "doRpcCommand:" << strMethod << ":" << jvParams;
if (!jvParams.isArray() || jvParams.size() != 1)
if (!jvParams.isArray() || jvParams.size() > 1)
return rpcError(rpcINVALID_PARAMS);
Json::Value jvRequest = jvParams[0u];
Json::Value jvRequest = jvParams.size() ? jvParams[0u] : Json::Value(Json::objectValue);
if (!jvRequest.isObject())
return rpcError(rpcINVALID_PARAMS);

View File

@@ -4,10 +4,23 @@
#include "CallRPC.h"
SETUP_LOG();
RPCSub::RPCSub(const std::string& strUrl, const std::string& strUsername, const std::string& strPassword)
: mUrl(strUrl), mUsername(strUsername), mPassword(strPassword)
{
mId = 1;
std::string strScheme;
if (!parseUrl(strUrl, strScheme, mIp, mPort, mPath))
{
throw std::runtime_error("Failed to parse url.");
}
else if (strScheme != "http")
{
throw std::runtime_error("Only http is supported.");
}
mSeq = 1;
}
void RPCSub::sendThread()
@@ -33,7 +46,7 @@ void RPCSub::sendThread()
mDeque.pop_front();
jvEvent = pEvent.second;
jvEvent["id"] = pEvent.first;
jvEvent["seq"] = pEvent.first;
bSend = true;
}
@@ -43,9 +56,14 @@ void RPCSub::sendThread()
if (bSend)
{
// Drop result.
(void) callRPC(mIp, mPort, mUsername, mPassword, "event", jvEvent);
sendThread();
try
{
(void) callRPC(mIp, mPort, mUsername, mPassword, mPath, "event", jvEvent);
}
catch (const std::exception& e)
{
cLog(lsDEBUG) << boost::str(boost::format("callRPC exception: %s") % e.what());
}
}
} while (bSend);
}
@@ -61,7 +79,7 @@ void RPCSub::send(const Json::Value& jvObj)
mDeque.pop_back();
}
mDeque.push_back(std::make_pair(mId++, jvObj));
mDeque.push_back(std::make_pair(mSeq++, jvObj));
if (!mSending)
{

View File

@@ -17,8 +17,9 @@ class RPCSub : public InfoSub
int mPort;
std::string mUsername;
std::string mPassword;
std::string mPath;
int mId; // Next id to allocate.
int mSeq; // Next id to allocate.
bool mSending; // Sending threead is active.

View File

@@ -39,11 +39,13 @@ Json::Value JSONRPCError(int code, const std::string& message)
// and to be compatible with other JSON-RPC implementations.
//
std::string createHTTPPost(const std::string& strMsg, const std::map<std::string, std::string>& mapRequestHeaders)
std::string createHTTPPost(const std::string& strPath, const std::string& strMsg, const std::map<std::string, std::string>& mapRequestHeaders)
{
std::ostringstream s;
s << "POST / HTTP/1.1\r\n"
s << "POST "
<< (strPath.empty() ? "/" : strPath)
<< " HTTP/1.1\r\n"
<< "User-Agent: " SYSTEM_NAME "-json-rpc/" << FormatFullVersion() << "\r\n"
<< "Host: 127.0.0.1\r\n"
<< "Content-Type: application/json\r\n"