More work toward http/https client merge.

This commit is contained in:
Arthur Britto
2013-02-01 18:10:53 -08:00
parent e53e27cfa6
commit c834ce4e06
5 changed files with 39 additions and 49 deletions

View File

@@ -152,7 +152,6 @@ public:
boost::asio::async_write(PlainSocket(), buffers, handler); boost::asio::async_write(PlainSocket(), buffers, handler);
} }
template <typename Buf, typename Condition, typename Handler> template <typename Buf, typename Condition, typename Handler>
void async_read(const Buf& buffers, Condition cond, Handler handler) void async_read(const Buf& buffers, Condition cond, Handler handler)
{ {

View File

@@ -625,22 +625,16 @@ int commandLineRPC(const std::vector<std::string>& vCmd)
boost::asio::io_service isService; boost::asio::io_service isService;
// void 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& jvParams, const bool bSSL, callRPC(
boost::function<void(const Json::Value& jvInput)> callbackFuncP)
callRPCAsync(
isService, isService,
#if 0 theConfig.RPC_IP, theConfig.RPC_PORT,
theConfig.RPC_IP, theConfig.RPC_USER, theConfig.RPC_PASSWORD,
theConfig.RPC_PORT,
theConfig.RPC_USER,
theConfig.RPC_PASSWORD,
"", "",
jvRequest.isMember("method") // Allow parser to rewrite method. jvRequest.isMember("method") // Allow parser to rewrite method.
? jvRequest["method"].asString() ? jvRequest["method"].asString()
: vCmd[0], : vCmd[0],
jvParams, // Parsed, execute. jvParams, // Parsed, execute.
boost::bind(callRPCHandler, jvOutput, _1)); false,
#endif
boost::bind(callRPCHandler, &jvOutput, _1)); boost::bind(callRPCHandler, &jvOutput, _1));
isService.run(); // This blocks until there is no more outstanding async calls. isService.run(); // This blocks until there is no more outstanding async calls.
@@ -749,7 +743,12 @@ void requestRPC(const std::string& strMethod, const Json::Value& jvParams, const
mHeaders); mHeaders);
} }
void 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& jvParams, const bool bSSL, void callRPC(
boost::asio::io_service& io_service,
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& jvParams, const bool bSSL,
boost::function<void(const Json::Value& jvInput)> callbackFuncP) boost::function<void(const Json::Value& jvInput)> callbackFuncP)
{ {
// Connect to localhost // Connect to localhost
@@ -774,7 +773,7 @@ void callRPC(const std::string& strIp, const int iPort, const std::string& strUs
HttpsClient::httpsRequest( HttpsClient::httpsRequest(
bSSL, bSSL,
theApp->getIOService(), io_service,
strIp, strIp,
iPort, iPort,
boost::bind( boost::bind(
@@ -788,5 +787,4 @@ void callRPC(const std::string& strIp, const int iPort, const std::string& strUs
boost::bind(&responseRPC, callbackFuncP, _1, _2, _3)); boost::bind(&responseRPC, callbackFuncP, _1, _2, _3));
} }
// vim:ts=4 // vim:ts=4

View File

@@ -48,9 +48,14 @@ public:
}; };
extern int commandLineRPC(const std::vector<std::string>& vCmd); extern int commandLineRPC(const std::vector<std::string>& vCmd);
extern void void 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& jvParams, const bool bSSL,
boost::function<void(const Json::Value& jvInput)> callbackFuncP);
extern void callRPC(
boost::asio::io_service& io_service,
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& jvParams, const bool bSSL,
boost::function<void(const Json::Value& jvInput)> callbackFuncP);
#endif #endif
// vim:ts=4 // vim:ts=4

View File

@@ -77,31 +77,31 @@ void HttpsClient::httpsGet(
void HttpsClient::httpsNext() void HttpsClient::httpsNext()
{ {
// std::cerr << "Fetch: " << mDeqSites[0] << std::endl; std::cerr << "Fetch: " << mDeqSites[0] << std::endl;
boost::shared_ptr<boost::asio::ip::tcp::resolver::query> query(new boost::asio::ip::tcp::resolver::query(mDeqSites[0], boost::lexical_cast<std::string>(mPort), boost::shared_ptr<boost::asio::ip::tcp::resolver::query> query(new boost::asio::ip::tcp::resolver::query(mDeqSites[0], boost::lexical_cast<std::string>(mPort),
ip::resolver_query_base::numeric_service)); ip::resolver_query_base::numeric_service));
mQuery = query; mQuery = query;
mDeadline.expires_from_now(mTimeout, mShutdown); mDeadline.expires_from_now(mTimeout, mShutdown);
// std::cerr << "expires_from_now: " << mShutdown.message() << std::endl; std::cerr << "expires_from_now: " << mShutdown.message() << std::endl;
if (!mShutdown) if (!mShutdown)
{ {
mDeadline.async_wait( mDeadline.async_wait(
boost::bind( boost::bind(
&HttpsClient::ShandleDeadline, &HttpsClient::handleDeadline,
shared_from_this(), shared_from_this(),
boost::asio::placeholders::error)); boost::asio::placeholders::error));
} }
if (!mShutdown) if (!mShutdown)
{ {
// std::cerr << "Resolving: " << mDeqSites[0] << std::endl; std::cerr << "Resolving: " << mDeqSites[0] << std::endl;
mResolver.async_resolve(*mQuery, mResolver.async_resolve(*mQuery,
boost::bind( boost::bind(
&HttpsClient::ShandleResolve, &HttpsClient::handleResolve,
shared_from_this(), shared_from_this(),
boost::asio::placeholders::error, boost::asio::placeholders::error,
boost::asio::placeholders::iterator)); boost::asio::placeholders::iterator));
@@ -116,7 +116,7 @@ void HttpsClient::handleDeadline(const boost::system::error_code& ecResult)
if (ecResult == boost::asio::error::operation_aborted) if (ecResult == boost::asio::error::operation_aborted)
{ {
// Timer canceled because deadline no longer needed. // Timer canceled because deadline no longer needed.
// std::cerr << "Deadline cancelled." << std::endl; std::cerr << "Deadline cancelled." << std::endl;
nothing(); // Aborter is done. nothing(); // Aborter is done.
} }
@@ -167,13 +167,13 @@ void HttpsClient::handleResolve(
if (mShutdown) if (mShutdown)
{ {
// std::cerr << "Resolve error: " << mDeqSites[0] << ": " << mShutdown.message() << std::endl; std::cerr << "Resolve error: " << mDeqSites[0] << ": " << mShutdown.message() << std::endl;
invokeComplete(mShutdown); invokeComplete(mShutdown);
} }
else else
{ {
// std::cerr << "Resolve complete." << std::endl; std::cerr << "Resolve complete." << std::endl;
boost::asio::async_connect( boost::asio::async_connect(
mSocket.lowest_layer(), mSocket.lowest_layer(),
@@ -197,7 +197,7 @@ void HttpsClient::handleConnect(const boost::system::error_code& ecResult)
if (!mShutdown) if (!mShutdown)
{ {
// std::cerr << "Connected." << std::endl; std::cerr << "Connected." << std::endl;
mShutdown = mSocket.verify(mDeqSites[0]); mShutdown = mSocket.verify(mDeqSites[0]);
if (mShutdown) if (mShutdown)
@@ -206,7 +206,7 @@ void HttpsClient::handleConnect(const boost::system::error_code& ecResult)
} }
} }
if (!mShutdown) if (mShutdown)
{ {
invokeComplete(mShutdown); invokeComplete(mShutdown);
} }
@@ -215,7 +215,7 @@ void HttpsClient::handleConnect(const boost::system::error_code& ecResult)
mSocket.async_handshake( mSocket.async_handshake(
AutoSocket::ssl_socket::client, AutoSocket::ssl_socket::client,
boost::bind( boost::bind(
&HttpsClient::ShandleRequest, &HttpsClient::handleRequest,
shared_from_this(), shared_from_this(),
boost::asio::placeholders::error)); boost::asio::placeholders::error));
} }
@@ -238,19 +238,19 @@ void HttpsClient::handleRequest(const boost::system::error_code& ecResult)
} }
else else
{ {
// std::cerr << "SSL session started." << std::endl; std::cerr << "Session started." << std::endl;
mBuild(mRequest, mDeqSites[0]); mBuild(mRequest, mDeqSites[0]);
boost::asio::async_write( mSocket.async_write(
mSocket,
mRequest, mRequest,
boost::bind(&HttpsClient::ShandleWrite, boost::bind(&HttpsClient::handleWrite,
shared_from_this(), shared_from_this(),
boost::asio::placeholders::error)); boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
} }
} }
void HttpsClient::handleWrite(const boost::system::error_code& ecResult) void HttpsClient::handleWrite(const boost::system::error_code& ecResult, std::size_t bytes_transferred)
{ {
if (!mShutdown) if (!mShutdown)
mShutdown = ecResult; mShutdown = ecResult;
@@ -263,13 +263,13 @@ void HttpsClient::handleWrite(const boost::system::error_code& ecResult)
} }
else else
{ {
// std::cerr << "Wrote." << std::endl; std::cerr << "Wrote." << std::endl;
boost::asio::async_read( boost::asio::async_read(
mSocket, mSocket,
mResponse, mResponse,
boost::asio::transfer_all(), boost::asio::transfer_all(),
boost::bind(&HttpsClient::ShandleData, boost::bind(&HttpsClient::handleData,
shared_from_this(), shared_from_this(),
boost::asio::placeholders::error)); boost::asio::placeholders::error));
} }
@@ -290,7 +290,7 @@ void HttpsClient::handleData(const boost::system::error_code& ecResult)
{ {
if (mShutdown) if (mShutdown)
{ {
// std::cerr << "Complete." << std::endl; std::cerr << "Complete." << std::endl;
nothing(); nothing();
} }
else else

View File

@@ -41,28 +41,16 @@ private:
boost::posix_time::time_duration mTimeout; boost::posix_time::time_duration mTimeout;
void handleDeadline(const boost::system::error_code& ecResult); void handleDeadline(const boost::system::error_code& ecResult);
static void ShandleDeadline(pointer This, const boost::system::error_code& ecResult)
{ This->handleDeadline(ecResult); }
void handleResolve(const boost::system::error_code& ecResult, boost::asio::ip::tcp::resolver::iterator endpoint_iterator); void handleResolve(const boost::system::error_code& ecResult, boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
static void ShandleResolve(pointer This, const boost::system::error_code& ecResult, boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
{ This->handleResolve(ecResult, endpoint_iterator); }
void handleConnect(const boost::system::error_code& ecResult); void handleConnect(const boost::system::error_code& ecResult);
static void ShandleConnect(pointer This, const boost::system::error_code& ecResult)
{ This->handleConnect(ecResult); }
void handleRequest(const boost::system::error_code& ecResult); void handleRequest(const boost::system::error_code& ecResult);
static void ShandleRequest(pointer This, const boost::system::error_code& ecResult)
{ This->handleRequest(ecResult); }
void handleWrite(const boost::system::error_code& ecResult); void handleWrite(const boost::system::error_code& ecResult, std::size_t bytes_transferred);
static void ShandleWrite(pointer This, const boost::system::error_code& ecResult)
{ This->handleWrite(ecResult); }
void handleData(const boost::system::error_code& ecResult); void handleData(const boost::system::error_code& ecResult);
static void ShandleData(pointer This, const boost::system::error_code& ecResult)
{ This->handleData(ecResult); }
void handleShutdown(const boost::system::error_code& ecResult); void handleShutdown(const boost::system::error_code& ecResult);