AdminNet.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <jsonrpccpp/common/exception.h>
  2. #include <libwebthree/WebThree.h>
  3. #include <libdevcore/CommonJS.h>
  4. #include <libethcore/Common.h>
  5. #include "AdminNet.h"
  6. #include "SessionManager.h"
  7. #include "JsonHelper.h"
  8. using namespace std;
  9. using namespace dev;
  10. using namespace dev::rpc;
  11. AdminNet::AdminNet(NetworkFace& _network, SessionManager& _sm): m_network(_network), m_sm(_sm) {}
  12. bool AdminNet::admin_net_start(std::string const& _session)
  13. {
  14. RPC_ADMIN;
  15. m_network.startNetwork();
  16. return true;
  17. }
  18. bool AdminNet::admin_net_stop(std::string const& _session)
  19. {
  20. RPC_ADMIN;
  21. m_network.stopNetwork();
  22. return true;
  23. }
  24. bool AdminNet::admin_net_connect(std::string const& _node, std::string const& _session)
  25. {
  26. RPC_ADMIN;
  27. return admin_addPeer(_node);
  28. }
  29. Json::Value AdminNet::admin_net_peers(std::string const& _session)
  30. {
  31. RPC_ADMIN;
  32. return admin_peers();
  33. }
  34. Json::Value AdminNet::admin_net_nodeInfo(std::string const& _session)
  35. {
  36. RPC_ADMIN;
  37. Json::Value ret;
  38. p2p::NodeInfo i = m_network.nodeInfo();
  39. ret["name"] = i.version;
  40. ret["port"] = i.port;
  41. ret["address"] = i.address;
  42. ret["listenAddr"] = i.address + ":" + toString(i.port);
  43. ret["id"] = i.id.hex();
  44. ret["enode"] = i.enode();
  45. return ret;
  46. }
  47. Json::Value AdminNet::admin_nodeInfo()
  48. {
  49. Json::Value ret;
  50. p2p::NodeInfo i = m_network.nodeInfo();
  51. ret["name"] = i.version;
  52. ret["ports"] = Json::objectValue;
  53. // Both ports are equal as of 2016-02-04, migt change later
  54. ret["ports"]["discovery"] = i.port;
  55. ret["ports"]["listener"] = i.port;
  56. ret["ip"] = i.address;
  57. ret["listenAddr"] = i.address + ":" + toString(i.port);
  58. ret["id"] = i.id.hex();
  59. ret["enode"] = i.enode();
  60. ret["protocols"] = Json::objectValue;
  61. ret["protocols"]["eth"] = Json::objectValue; //@todo fill with information
  62. return ret;
  63. }
  64. Json::Value AdminNet::admin_peers()
  65. {
  66. Json::Value ret;
  67. for (p2p::PeerSessionInfo const& peer: m_network.peers())
  68. ret.append(toJson(peer));
  69. return ret;
  70. }
  71. bool AdminNet::admin_addPeer(string const& _node)
  72. {
  73. m_network.addPeer(p2p::NodeSpec(_node), p2p::PeerType::Required);
  74. return true;
  75. }