Browse Source

send I2PControl response without write_json

pull/165/head
orignal 10 years ago
parent
commit
1cc3dd5ee4
  1. 123
      I2PControl.cpp
  2. 53
      I2PControl.h

123
I2PControl.cpp

@ -168,9 +168,12 @@ namespace client
auto it = m_MethodHandlers.find (method); auto it = m_MethodHandlers.find (method);
if (it != m_MethodHandlers.end ()) if (it != m_MethodHandlers.end ())
{ {
boost::property_tree::ptree results; std::ostringstream response;
(this->*(it->second))(pt.get_child (I2P_CONTROL_PROPERTY_PARAMS), results); response << "{\"id\":" << pt.get<std::string>(I2P_CONTROL_PROPERTY_ID) << ",\"result\":{";
SendResponse (socket, buf, pt.get<std::string>(I2P_CONTROL_PROPERTY_ID), results, isHtml);
(this->*(it->second))(pt.get_child (I2P_CONTROL_PROPERTY_PARAMS), response);
response << "},\"jsonrpc\":\"2.0\"}";
SendResponse (socket, buf, response, isHtml);
} }
else else
LogPrint (eLogWarning, "Unknown I2PControl method ", method); LogPrint (eLogWarning, "Unknown I2PControl method ", method);
@ -187,28 +190,29 @@ namespace client
} }
} }
template<typename T> void I2PControlService::InsertParam (std::ostringstream& ss, const std::string& name, int value) const
void I2PControlService::InsertParam (boost::property_tree::ptree& pt, const std::string& name, T value) const
{ {
pt.put (boost::property_tree::ptree::path_type (name, '/'), value); ss << "\"" << name << "\":" << value;
} }
void I2PControlService::SendResponse (std::shared_ptr<boost::asio::ip::tcp::socket> socket, void I2PControlService::InsertParam (std::ostringstream& ss, const std::string& name, const std::string& value) const
std::shared_ptr<I2PControlBuffer> buf, const std::string& id,
boost::property_tree::ptree& results, bool isHtml)
{ {
boost::property_tree::ptree pt; ss << "\"" << name << "\":";
pt.put (I2P_CONTROL_PROPERTY_ID, id); if (value.length () > 0)
pt.put_child (I2P_CONTROL_PROPERTY_RESULT, results); ss << "\"" << value << "\"";
pt.put ("jsonrpc", "2.0"); else
ss << "null";
}
std::ostringstream ss; void I2PControlService::InsertParam (std::ostringstream& ss, const std::string& name, double value) const
#if GCC47_BOOST149 {
LogPrint (eLogError, "json_write is not supported due bug in boost 1.49 with gcc 4.7"); ss << "\"" << name << "\":" << std::fixed << std::setprecision(2) << value;
#else }
boost::property_tree::write_json (ss, pt, false);
#endif void I2PControlService::SendResponse (std::shared_ptr<boost::asio::ip::tcp::socket> socket,
size_t len = ss.str ().length (), offset = 0; std::shared_ptr<I2PControlBuffer> buf, std::ostringstream& response, bool isHtml)
{
size_t len = response.str ().length (), offset = 0;
if (isHtml) if (isHtml)
{ {
std::ostringstream header; std::ostringstream header;
@ -224,7 +228,7 @@ namespace client
offset = header.str ().size (); offset = header.str ().size ();
memcpy (buf->data (), header.str ().c_str (), offset); memcpy (buf->data (), header.str ().c_str (), offset);
} }
memcpy (buf->data () + offset, ss.str ().c_str (), len); memcpy (buf->data () + offset, response.str ().c_str (), len);
boost::asio::async_write (*socket, boost::asio::buffer (buf->data (), offset + len), boost::asio::async_write (*socket, boost::asio::buffer (buf->data (), offset + len),
boost::asio::transfer_all (), boost::asio::transfer_all (),
std::bind(&I2PControlService::HandleResponseSent, this, std::bind(&I2PControlService::HandleResponseSent, this,
@ -241,7 +245,7 @@ namespace client
// handlers // handlers
void I2PControlService::AuthenticateHandler (const boost::property_tree::ptree& params, boost::property_tree::ptree& results) void I2PControlService::AuthenticateHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
{ {
int api = params.get<int> (I2P_CONTROL_PARAM_API); int api = params.get<int> (I2P_CONTROL_PARAM_API);
auto password = params.get<std::string> (I2P_CONTROL_PARAM_PASSWORD); auto password = params.get<std::string> (I2P_CONTROL_PARAM_PASSWORD);
@ -249,12 +253,13 @@ namespace client
if (password != m_Password) if (password != m_Password)
LogPrint (eLogError, "I2PControl Authenticate Invalid password ", password, " expected ", m_Password); LogPrint (eLogError, "I2PControl Authenticate Invalid password ", password, " expected ", m_Password);
InsertParam (results, I2P_CONTROL_PARAM_API, api); InsertParam (results, I2P_CONTROL_PARAM_API, api);
results << ",";
std::string token = boost::lexical_cast<std::string>(i2p::util::GetSecondsSinceEpoch ()); std::string token = boost::lexical_cast<std::string>(i2p::util::GetSecondsSinceEpoch ());
m_Tokens.insert (token); m_Tokens.insert (token);
InsertParam (results, I2P_CONTROL_PARAM_TOKEN, token); InsertParam (results, I2P_CONTROL_PARAM_TOKEN, token);
} }
void I2PControlService::EchoHandler (const boost::property_tree::ptree& params, boost::property_tree::ptree& results) void I2PControlService::EchoHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
{ {
auto echo = params.get<std::string> (I2P_CONTROL_PARAM_ECHO); auto echo = params.get<std::string> (I2P_CONTROL_PARAM_ECHO);
LogPrint (eLogDebug, "I2PControl Echo Echo=", echo); LogPrint (eLogDebug, "I2PControl Echo Echo=", echo);
@ -264,7 +269,7 @@ namespace client
// I2PControl // I2PControl
void I2PControlService::I2PControlHandler (const boost::property_tree::ptree& params, boost::property_tree::ptree& results) void I2PControlService::I2PControlHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
{ {
LogPrint (eLogDebug, "I2PControl I2PControl"); LogPrint (eLogDebug, "I2PControl I2PControl");
for (auto& it: params) for (auto& it: params)
@ -280,84 +285,85 @@ namespace client
// RouterInfo // RouterInfo
void I2PControlService::RouterInfoHandler (const boost::property_tree::ptree& params, boost::property_tree::ptree& results) void I2PControlService::RouterInfoHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
{ {
LogPrint (eLogDebug, "I2PControl RouterInfo"); LogPrint (eLogDebug, "I2PControl RouterInfo");
for (auto& it: params) for (auto it = params.begin (); it != params.end (); it++)
{ {
LogPrint (eLogDebug, it.first); if (it != params.begin ()) results << ",";
auto it1 = m_RouterInfoHandlers.find (it.first); LogPrint (eLogDebug, it->first);
auto it1 = m_RouterInfoHandlers.find (it->first);
if (it1 != m_RouterInfoHandlers.end ()) if (it1 != m_RouterInfoHandlers.end ())
(this->*(it1->second))(results); (this->*(it1->second))(results);
else else
LogPrint (eLogError, "I2PControl RouterInfo unknown request ", it.first); LogPrint (eLogError, "I2PControl RouterInfo unknown request ", it->first);
} }
} }
void I2PControlService::UptimeHandler (boost::property_tree::ptree& results) void I2PControlService::UptimeHandler (std::ostringstream& results)
{ {
InsertParam (results, I2P_CONTROL_ROUTER_INFO_UPTIME, i2p::context.GetUptime ()*1000); InsertParam (results, I2P_CONTROL_ROUTER_INFO_UPTIME, (int)i2p::context.GetUptime ()*1000);
} }
void I2PControlService::VersionHandler (boost::property_tree::ptree& results) void I2PControlService::VersionHandler (std::ostringstream& results)
{ {
InsertParam (results, I2P_CONTROL_ROUTER_INFO_VERSION, VERSION); InsertParam (results, I2P_CONTROL_ROUTER_INFO_VERSION, VERSION);
} }
void I2PControlService::StatusHandler (boost::property_tree::ptree& results) void I2PControlService::StatusHandler (std::ostringstream& results)
{ {
InsertParam (results, I2P_CONTROL_ROUTER_INFO_STATUS, "???"); // TODO: InsertParam (results, I2P_CONTROL_ROUTER_INFO_STATUS, "???"); // TODO:
} }
void I2PControlService::NetDbKnownPeersHandler (boost::property_tree::ptree& results) void I2PControlService::NetDbKnownPeersHandler (std::ostringstream& results)
{ {
InsertParam (results, I2P_CONTROL_ROUTER_INFO_NETDB_KNOWNPEERS, i2p::data::netdb.GetNumRouters ()); InsertParam (results, I2P_CONTROL_ROUTER_INFO_NETDB_KNOWNPEERS, i2p::data::netdb.GetNumRouters ());
} }
void I2PControlService::NetDbActivePeersHandler (boost::property_tree::ptree& results) void I2PControlService::NetDbActivePeersHandler (std::ostringstream& results)
{ {
InsertParam (results, I2P_CONTROL_ROUTER_INFO_NETDB_ACTIVEPEERS, i2p::transport::transports.GetPeers ().size ()); InsertParam (results, I2P_CONTROL_ROUTER_INFO_NETDB_ACTIVEPEERS, (int)i2p::transport::transports.GetPeers ().size ());
} }
void I2PControlService::NetStatusHandler (boost::property_tree::ptree& results) void I2PControlService::NetStatusHandler (std::ostringstream& results)
{ {
InsertParam (results, I2P_CONTROL_ROUTER_INFO_NET_STATUS, (int)i2p::context.GetStatus ()); InsertParam (results, I2P_CONTROL_ROUTER_INFO_NET_STATUS, (int)i2p::context.GetStatus ());
} }
void I2PControlService::TunnelsParticipatingHandler (boost::property_tree::ptree& results) void I2PControlService::TunnelsParticipatingHandler (std::ostringstream& results)
{ {
InsertParam (results, I2P_CONTROL_ROUTER_INFO_TUNNELS_PARTICIPATING, i2p::tunnel::tunnels.GetTransitTunnels ().size ()); InsertParam (results, I2P_CONTROL_ROUTER_INFO_TUNNELS_PARTICIPATING, (int)i2p::tunnel::tunnels.GetTransitTunnels ().size ());
} }
void I2PControlService::InboundBandwidth1S (boost::property_tree::ptree& results) void I2PControlService::InboundBandwidth1S (std::ostringstream& results)
{ {
InsertParam (results, I2P_CONTROL_ROUTER_INFO_BW_IB_1S, i2p::transport::transports.GetInBandwidth ()); InsertParam (results, I2P_CONTROL_ROUTER_INFO_BW_IB_1S, (double)i2p::transport::transports.GetInBandwidth ());
} }
void I2PControlService::OutboundBandwidth1S (boost::property_tree::ptree& results) void I2PControlService::OutboundBandwidth1S (std::ostringstream& results)
{ {
InsertParam (results, I2P_CONTROL_ROUTER_INFO_BW_OB_1S, i2p::transport::transports.GetOutBandwidth ()); InsertParam (results, I2P_CONTROL_ROUTER_INFO_BW_OB_1S, (double)i2p::transport::transports.GetOutBandwidth ());
} }
// RouterManager // RouterManager
void I2PControlService::RouterManagerHandler (const boost::property_tree::ptree& params, boost::property_tree::ptree& results) void I2PControlService::RouterManagerHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
{ {
LogPrint (eLogDebug, "I2PControl RouterManager"); LogPrint (eLogDebug, "I2PControl RouterManager");
for (auto& it: params) for (auto it = params.begin (); it != params.end (); it++)
{ {
LogPrint (eLogDebug, it.first); if (it != params.begin ()) results << ",";
auto it1 = m_RouterManagerHandlers.find (it.first); LogPrint (eLogDebug, it->first);
auto it1 = m_RouterManagerHandlers.find (it->first);
if (it1 != m_RouterManagerHandlers.end ()) if (it1 != m_RouterManagerHandlers.end ())
(this->*(it1->second))(results); (this->*(it1->second))(results);
else else
LogPrint (eLogError, "I2PControl RouterManager unknown request ", it.first); LogPrint (eLogError, "I2PControl RouterManager unknown request ", it->first);
} }
} }
void I2PControlService::ShutdownHandler (boost::property_tree::ptree& results) void I2PControlService::ShutdownHandler (std::ostringstream& results)
{ {
LogPrint (eLogInfo, "Shutdown requested"); LogPrint (eLogInfo, "Shutdown requested");
InsertParam (results, I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN, ""); InsertParam (results, I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN, "");
@ -369,7 +375,7 @@ namespace client
}); });
} }
void I2PControlService::ShutdownGracefulHandler (boost::property_tree::ptree& results) void I2PControlService::ShutdownGracefulHandler (std::ostringstream& results)
{ {
i2p::context.SetAcceptsTunnels (false); i2p::context.SetAcceptsTunnels (false);
int timeout = i2p::tunnel::tunnels.GetTransitTunnelsExpirationTimeout (); int timeout = i2p::tunnel::tunnels.GetTransitTunnelsExpirationTimeout ();
@ -383,7 +389,7 @@ namespace client
}); });
} }
void I2PControlService::ReseedHandler (boost::property_tree::ptree& results) void I2PControlService::ReseedHandler (std::ostringstream& results)
{ {
LogPrint (eLogInfo, "Reseed requested"); LogPrint (eLogInfo, "Reseed requested");
InsertParam (results, I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN, ""); InsertParam (results, I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN, "");
@ -391,17 +397,18 @@ namespace client
} }
// network setting // network setting
void I2PControlService::NetworkSettingHandler (const boost::property_tree::ptree& params, boost::property_tree::ptree& results) void I2PControlService::NetworkSettingHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
{ {
LogPrint (eLogDebug, "I2PControl NetworkSetting"); LogPrint (eLogDebug, "I2PControl NetworkSetting");
for (auto& it: params) for (auto it = params.begin (); it != params.end (); it++)
{ {
LogPrint (eLogDebug, it.first); if (it != params.begin ()) results << ",";
auto it1 = m_NetworkSettingHandlers.find (it.first); LogPrint (eLogDebug, it->first);
auto it1 = m_NetworkSettingHandlers.find (it->first);
if (it1 != m_NetworkSettingHandlers.end ()) if (it1 != m_NetworkSettingHandlers.end ())
(this->*(it1->second))(it.second.data (), results); (this->*(it1->second))(it->second.data (), results);
else else
LogPrint (eLogError, "I2PControl NetworkSetting unknown request ", it.first); LogPrint (eLogError, "I2PControl NetworkSetting unknown request ", it->first);
} }
} }

53
I2PControl.h

@ -6,6 +6,7 @@
#include <memory> #include <memory>
#include <array> #include <array>
#include <string> #include <string>
#include <sstream>
#include <map> #include <map>
#include <set> #include <set>
#include <boost/asio.hpp> #include <boost/asio.hpp>
@ -80,49 +81,49 @@ namespace client
void HandleRequestReceived (const boost::system::error_code& ecode, size_t bytes_transferred, void HandleRequestReceived (const boost::system::error_code& ecode, size_t bytes_transferred,
std::shared_ptr<boost::asio::ip::tcp::socket> socket, std::shared_ptr<I2PControlBuffer> buf); std::shared_ptr<boost::asio::ip::tcp::socket> socket, std::shared_ptr<I2PControlBuffer> buf);
void SendResponse (std::shared_ptr<boost::asio::ip::tcp::socket> socket, void SendResponse (std::shared_ptr<boost::asio::ip::tcp::socket> socket,
std::shared_ptr<I2PControlBuffer> buf, const std::string& id, std::shared_ptr<I2PControlBuffer> buf, std::ostringstream& response, bool isHtml);
boost::property_tree::ptree& results, bool isHtml);
void HandleResponseSent (const boost::system::error_code& ecode, std::size_t bytes_transferred, void HandleResponseSent (const boost::system::error_code& ecode, std::size_t bytes_transferred,
std::shared_ptr<boost::asio::ip::tcp::socket> socket, std::shared_ptr<I2PControlBuffer> buf); std::shared_ptr<boost::asio::ip::tcp::socket> socket, std::shared_ptr<I2PControlBuffer> buf);
private: private:
template<typename T> void InsertParam (std::ostringstream& ss, const std::string& name, int value) const;
void InsertParam (boost::property_tree::ptree& pt, const std::string& name, T value) const; void InsertParam (std::ostringstream& ss, const std::string& name, double value) const;
void InsertParam (std::ostringstream& ss, const std::string& name, const std::string& value) const;
// methods // methods
typedef void (I2PControlService::*MethodHandler)(const boost::property_tree::ptree& params, boost::property_tree::ptree& results); typedef void (I2PControlService::*MethodHandler)(const boost::property_tree::ptree& params, std::ostringstream& results);
void AuthenticateHandler (const boost::property_tree::ptree& params, boost::property_tree::ptree& results); void AuthenticateHandler (const boost::property_tree::ptree& params, std::ostringstream& results);
void EchoHandler (const boost::property_tree::ptree& params, boost::property_tree::ptree& results); void EchoHandler (const boost::property_tree::ptree& params, std::ostringstream& results);
void I2PControlHandler (const boost::property_tree::ptree& params, boost::property_tree::ptree& results); void I2PControlHandler (const boost::property_tree::ptree& params, std::ostringstream& results);
void RouterInfoHandler (const boost::property_tree::ptree& params, boost::property_tree::ptree& results); void RouterInfoHandler (const boost::property_tree::ptree& params, std::ostringstream& results);
void RouterManagerHandler (const boost::property_tree::ptree& params, boost::property_tree::ptree& results); void RouterManagerHandler (const boost::property_tree::ptree& params, std::ostringstream& results);
void NetworkSettingHandler (const boost::property_tree::ptree& params, boost::property_tree::ptree& results); void NetworkSettingHandler (const boost::property_tree::ptree& params, std::ostringstream& results);
// I2PControl // I2PControl
typedef void (I2PControlService::*I2PControlRequestHandler)(const std::string& value); typedef void (I2PControlService::*I2PControlRequestHandler)(const std::string& value);
// RouterInfo // RouterInfo
typedef void (I2PControlService::*RouterInfoRequestHandler)(boost::property_tree::ptree& results); typedef void (I2PControlService::*RouterInfoRequestHandler)(std::ostringstream& results);
void UptimeHandler (boost::property_tree::ptree& results); void UptimeHandler (std::ostringstream& results);
void VersionHandler (boost::property_tree::ptree& results); void VersionHandler (std::ostringstream& results);
void StatusHandler (boost::property_tree::ptree& results); void StatusHandler (std::ostringstream& results);
void NetDbKnownPeersHandler (boost::property_tree::ptree& results); void NetDbKnownPeersHandler (std::ostringstream& results);
void NetDbActivePeersHandler (boost::property_tree::ptree& results); void NetDbActivePeersHandler (std::ostringstream& results);
void NetStatusHandler (boost::property_tree::ptree& results); void NetStatusHandler (std::ostringstream& results);
void TunnelsParticipatingHandler (boost::property_tree::ptree& results); void TunnelsParticipatingHandler (std::ostringstream& results);
void InboundBandwidth1S (boost::property_tree::ptree& results); void InboundBandwidth1S (std::ostringstream& results);
void OutboundBandwidth1S (boost::property_tree::ptree& results); void OutboundBandwidth1S (std::ostringstream& results);
// RouterManager // RouterManager
typedef void (I2PControlService::*RouterManagerRequestHandler)(boost::property_tree::ptree& results); typedef void (I2PControlService::*RouterManagerRequestHandler)(std::ostringstream& results);
void ShutdownHandler (boost::property_tree::ptree& results); void ShutdownHandler (std::ostringstream& results);
void ShutdownGracefulHandler (boost::property_tree::ptree& results); void ShutdownGracefulHandler (std::ostringstream& results);
void ReseedHandler (boost::property_tree::ptree& results); void ReseedHandler (std::ostringstream& results);
// NetworkSetting // NetworkSetting
typedef void (I2PControlService::*NetworkSettingRequestHandler)(const std::string& value, boost::property_tree::ptree& results); typedef void (I2PControlService::*NetworkSettingRequestHandler)(const std::string& value, std::ostringstream& results);
private: private:

Loading…
Cancel
Save