From d294a577faa11910985920f1e70a39398e53d82d Mon Sep 17 00:00:00 2001 From: xiphon Date: Fri, 18 Jan 2019 00:55:55 +0000 Subject: [PATCH] daemon: extend 'print_pl' command, optional filter by type and limit --- src/daemon/command_parser_executor.cpp | 29 ++++++++++++++++++++++++-- src/daemon/command_server.cpp | 1 + src/daemon/rpc_command_executor.cpp | 20 +++++++++++++----- src/daemon/rpc_command_executor.h | 2 +- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/daemon/command_parser_executor.cpp b/src/daemon/command_parser_executor.cpp index 853cde9c3..d31a6507e 100644 --- a/src/daemon/command_parser_executor.cpp +++ b/src/daemon/command_parser_executor.cpp @@ -48,9 +48,34 @@ t_command_parser_executor::t_command_parser_executor( bool t_command_parser_executor::print_peer_list(const std::vector& args) { - if (!args.empty()) return false; + if (args.size() > 3) + { + std::cout << "use: print_pl [white] [gray] []" << std::endl; + return true; + } + + bool white = false; + bool gray = false; + size_t limit = 0; + for (size_t i = 0; i < args.size(); ++i) + { + if (args[i] == "white") + { + white = true; + } + else if (args[i] == "gray") + { + gray = true; + } + else if (!epee::string_tools::get_xtype_from_string(limit, args[i])) + { + std::cout << "unexpected argument: " << args[i] << std::endl; + return true; + } + } - return m_executor.print_peer_list(); + const bool print_both = !white && !gray; + return m_executor.print_peer_list(white | print_both, gray | print_both, limit); } bool t_command_parser_executor::print_peer_list_stats(const std::vector& args) diff --git a/src/daemon/command_server.cpp b/src/daemon/command_server.cpp index 527ed2cf1..0438cb9f7 100644 --- a/src/daemon/command_server.cpp +++ b/src/daemon/command_server.cpp @@ -64,6 +64,7 @@ t_command_server::t_command_server( m_command_lookup.set_handler( "print_pl" , std::bind(&t_command_parser_executor::print_peer_list, &m_parser, p::_1) + , "print_pl [white] [gray] []" , "Print the current peer list." ); m_command_lookup.set_handler( diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp index 015e1e1f9..c1b623aef 100644 --- a/src/daemon/rpc_command_executor.cpp +++ b/src/daemon/rpc_command_executor.cpp @@ -154,7 +154,7 @@ t_rpc_command_executor::~t_rpc_command_executor() } } -bool t_rpc_command_executor::print_peer_list() { +bool t_rpc_command_executor::print_peer_list(bool white, bool gray, size_t limit) { cryptonote::COMMAND_RPC_GET_PEER_LIST::request req; cryptonote::COMMAND_RPC_GET_PEER_LIST::response res; @@ -175,14 +175,24 @@ bool t_rpc_command_executor::print_peer_list() { } } - for (auto & peer : res.white_list) + if (white) { - print_peer("white", peer); + auto peer = res.white_list.cbegin(); + const auto end = limit ? peer + std::min(limit, res.white_list.size()) : res.white_list.cend(); + for (; peer != end; ++peer) + { + print_peer("white", *peer); + } } - for (auto & peer : res.gray_list) + if (gray) { - print_peer("gray", peer); + auto peer = res.gray_list.cbegin(); + const auto end = limit ? peer + std::min(limit, res.gray_list.size()) : res.gray_list.cend(); + for (; peer != end; ++peer) + { + print_peer("gray", *peer); + } } return true; diff --git a/src/daemon/rpc_command_executor.h b/src/daemon/rpc_command_executor.h index 592584a5f..b8d41d9e4 100644 --- a/src/daemon/rpc_command_executor.h +++ b/src/daemon/rpc_command_executor.h @@ -67,7 +67,7 @@ public: ~t_rpc_command_executor(); - bool print_peer_list(); + bool print_peer_list(bool white = true, bool gray = true, size_t limit = 0); bool print_peer_list_stats();