Add basic inbound tunnel information to webui.

This commit is contained in:
EinMByte 2015-09-18 21:37:21 +02:00
parent 19557a0908
commit f04f556b75
5 changed files with 212 additions and 1 deletions

View file

@ -24,6 +24,64 @@ namespace i2p {
namespace client {
namespace i2pcontrol {
JsonObject::JsonObject(const std::string& value)
: children(), value("\"" + value + "\"")
{
}
JsonObject::JsonObject(int value)
: children(), value(std::to_string(value))
{
}
JsonObject::JsonObject(double v)
: children(), value()
{
std::ostringstream oss;
oss << std::fixed << std::setprecision(2) << v;
value = oss.str();
}
JsonObject& JsonObject::operator[](const std::string& key)
{
return children[key];
}
std::string JsonObject::toString() const
{
if(children.empty())
return value;
std::ostringstream oss;
oss << '{';
for(auto it = children.begin(); it != children.end(); ++it) {
if(it != children.begin())
oss << ',';
oss << '"' << it->first << "\":" << it->second.toString();
}
oss << '}';
return oss.str();
}
JsonObject tunnelToJsonObject(i2p::tunnel::Tunnel* tunnel)
{
JsonObject obj;
std::stringstream ss;
tunnel->GetTunnelConfig()->Print(ss); // TODO: use a JsonObject
obj["layout"] = JsonObject(ss.str());
const auto state = tunnel->GetState();
if(state == i2p::tunnel::eTunnelStateFailed)
obj["state"] = JsonObject("failed");
else if(state == i2p::tunnel::eTunnelStateExpiring)
obj["state"] = JsonObject("expiring");
return obj;
}
I2PControlSession::Response::Response(const std::string& version)
: id(), version(version), error(ErrorCode::None), parameters()
{
@ -92,6 +150,11 @@ void I2PControlSession::Response::setParam(const std::string& param, double valu
parameters[param] = oss.str();
}
void I2PControlSession::Response::setParam(const std::string& param, const JsonObject& value)
{
parameters[param] = value.toString();
}
void I2PControlSession::Response::setError(ErrorCode code)
{
error = code;
@ -126,6 +189,8 @@ I2PControlSession::I2PControlSession(boost::asio::io_service& ios, const std::st
routerInfoHandlers[ROUTER_INFO_NET_STATUS] = &I2PControlSession::handleNetStatus;
routerInfoHandlers[ROUTER_INFO_TUNNELS_PARTICIPATING] = &I2PControlSession::handleTunnelsParticipating;
routerInfoHandlers[ROUTER_INFO_TUNNELS_CREATION_SUCCESS] = &I2PControlSession::handleTunnelsCreationSuccess;
routerInfoHandlers[ROUTER_INFO_TUNNELS_IN_LIST] = &I2PControlSession::handleTunnelsInList;
routerInfoHandlers[ROUTER_INFO_TUNNELS_OUT_LIST] = &I2PControlSession::handleTunnelsOutList;
routerInfoHandlers[ROUTER_INFO_BW_IB_1S] = &I2PControlSession::handleInBandwidth1S;
routerInfoHandlers[ROUTER_INFO_BW_OB_1S] = &I2PControlSession::handleOutBandwidth1S;
@ -368,6 +433,33 @@ void I2PControlSession::handleTunnelsCreationSuccess(Response& response)
);
}
void I2PControlSession::handleTunnelsInList(Response& response)
{
JsonObject list;
for(auto pair : i2p::tunnel::tunnels.GetInboundTunnels()) {
const std::string id = std::to_string(pair.first);
list[id] = tunnelToJsonObject(pair.second.get());
list[id]["bytes"] = JsonObject(
static_cast<int>(pair.second->GetNumReceivedBytes())
);
}
response.setParam(constants::ROUTER_INFO_TUNNELS_IN_LIST, list);
}
void I2PControlSession::handleTunnelsOutList(Response& response)
{
JsonObject list;
for(auto tunnel : i2p::tunnel::tunnels.GetOutboundTunnels()) {
const std::string id = std::to_string(tunnel->GetTunnelID());
list[id] = tunnelToJsonObject(tunnel.get());
list[id]["bytes"] = JsonObject(
static_cast<int>(tunnel->GetNumSentBytes())
);
}
response.setParam(constants::ROUTER_INFO_TUNNELS_OUT_LIST, list);
}
void I2PControlSession::handleInBandwidth1S(Response& response)
{
response.setParam(