Loading src/client.cpp +78 −6 Original line number Diff line number Diff line Loading @@ -19,6 +19,7 @@ #include <iostream> #include <random> #include <ctime> #include <future> #include <boost/algorithm/string.hpp> Loading @@ -44,6 +45,7 @@ std::map<std::string, std::string> p3::client::command_shorthands { {"rr", "reregister"}, {"rl", "reload"}, {"st", "status"}, {"e", "events"}, {"q", "quit"}, {"x", "exit"} }; Loading Loading @@ -91,7 +93,7 @@ p3::client::~client() { delete connection; } std::string p3::client::get_input(std::string q, p3::client::inputType type) { std::string p3::client::get_input(const std::string & q, p3::client::inputType type) { std::string in; if (!argv.empty()) { in = argv.front(); Loading @@ -118,8 +120,19 @@ std::string p3::client::get_input(std::string q, p3::client::inputType type) { void p3::client::run() { bool r = true; connection->send(p3::protocol::COMMAND, get_input("", COMMAND)); while (r) { std::string in = get_input("", COMMAND); if (in == "exit" || in == "quit") { break; } else if ("events" == in) { if(push_event_manager()) { continue; } else { break; } } else { connection->send(p3::protocol::COMMAND, in); } p3::mQueueMessage m = connection->receive(-1); bool print_list = true; switch (m.get_command()) { Loading Loading @@ -160,12 +173,71 @@ void p3::client::run() { default: std::cout << "Unknown server command. " << m.get_value() << std::endl; } std::string in = get_input("", COMMAND); if (in == "exit" || in == "quit") { break; } } bool p3::client::push_event_manager() { bool ointer = interactive; interactive = true; auto res = std::async(std::launch::async, &p3::client::push_event_reader, this); connection->send(p3::protocol::COMMAND, "startevents"); while (res.wait_for(std::chrono::milliseconds(0)) != std::future_status::ready) { std::string in(""); std::cin >> in; boost::trim(in); boost::to_lower(in); if (in == "exit" || in == "quit" || in == "q" || in == "x" || !std::cin) { connection->send(p3::protocol::COMMAND, "stopevents"); res.wait(); return false; } else if ("events" == in || "e" == in) { connection->send(p3::protocol::COMMAND, "stopevents"); res.wait(); } else { connection->send(p3::protocol::COMMAND, in); std::cout << "No commands supported in push event mode. Use 'event' to stop." << std::endl; } } interactive = ointer; return res.get(); } bool p3::client::push_event_reader() { bool okexit = false; while (true) { p3::mQueueMessage m = connection->receive(-1); bool print_list = true; switch (m.get_command()) { case p3::protocol::BEGINTEXT: m = connection->receive(30); while (m.get_command() != p3::protocol::ENDTEXT) { if (print_list) { std::cout << m.get_value() << std::endl; } m = connection->receive(30); } std::cout << "---" << std::endl; break; case p3::protocol::OK: if (verbose >= 1) std::cout << m.get_value() << std::endl; if (okexit) { return true; } else { okexit = true; } break; case p3::protocol::ERROR: std::cout << "ERROR: " << m.get_value() << std::endl; return true; case p3::protocol::FATAL_ERROR: std::cout << "FATAL ERROR: " << m.get_value() << std::endl; return true; case p3::protocol::BYE: std::cout << "done." << std::endl; return false; default: std::cout << "Unknown server command. " << m.get_value() << std::endl; } } } src/client.h +4 −1 Original line number Diff line number Diff line Loading @@ -56,7 +56,10 @@ namespace p3 { bool interactive; int verbose; std::string get_input(std::string question, inputType is_command); bool push_event_reader(); // Return false if client should exit. bool push_event_manager(); // Return false if client should exit. std::string get_input(const std::string & question, inputType is_command); public: client(int argc, char *argv[]); Loading src/config.cpp +17 −0 Original line number Diff line number Diff line Loading @@ -91,3 +91,20 @@ bool p3::config::get_bool(const std::string& k) { throw; } } std::list<std::string> p3::config::get_string_list(const std::string& k) { try { std::list<std::string> res; boost::split(res, data.at(k), boost::is_any_of(",;")); if (res.size() == 1 && (res.front() == "none" || res.front() == "")) { return std::list<std::string>(); } for (auto &n : res) { boost::trim(n); } return res; } catch (std::out_of_range& e) { std::cout << "!! config::get_string_list(): Invalide configuration key: " << k << std::endl; throw; } } src/config.h +2 −0 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ #define __P3_CONFIG_H__ #include <string> #include <list> #include <map> namespace p3 { Loading @@ -40,6 +41,7 @@ namespace p3 { std::string get_string(const std::string& k); int get_int(const std::string& k); bool get_bool(const std::string& k); std::list <std::string> get_string_list(const std::string& k); }; } Loading src/error.h +1 −1 Original line number Diff line number Diff line Loading @@ -28,7 +28,7 @@ namespace p3 { std::string message; public: perror(std::string c, std::string e) : context(c), error(e), message(c + ": " + e) {}; perror(const std::string & c, const std::string & e) : context(c), error(e), message(c + ": " + e) {}; ~perror() throw () {}; const char * getContext() { return context.c_str(); } Loading Loading
src/client.cpp +78 −6 Original line number Diff line number Diff line Loading @@ -19,6 +19,7 @@ #include <iostream> #include <random> #include <ctime> #include <future> #include <boost/algorithm/string.hpp> Loading @@ -44,6 +45,7 @@ std::map<std::string, std::string> p3::client::command_shorthands { {"rr", "reregister"}, {"rl", "reload"}, {"st", "status"}, {"e", "events"}, {"q", "quit"}, {"x", "exit"} }; Loading Loading @@ -91,7 +93,7 @@ p3::client::~client() { delete connection; } std::string p3::client::get_input(std::string q, p3::client::inputType type) { std::string p3::client::get_input(const std::string & q, p3::client::inputType type) { std::string in; if (!argv.empty()) { in = argv.front(); Loading @@ -118,8 +120,19 @@ std::string p3::client::get_input(std::string q, p3::client::inputType type) { void p3::client::run() { bool r = true; connection->send(p3::protocol::COMMAND, get_input("", COMMAND)); while (r) { std::string in = get_input("", COMMAND); if (in == "exit" || in == "quit") { break; } else if ("events" == in) { if(push_event_manager()) { continue; } else { break; } } else { connection->send(p3::protocol::COMMAND, in); } p3::mQueueMessage m = connection->receive(-1); bool print_list = true; switch (m.get_command()) { Loading Loading @@ -160,12 +173,71 @@ void p3::client::run() { default: std::cout << "Unknown server command. " << m.get_value() << std::endl; } std::string in = get_input("", COMMAND); if (in == "exit" || in == "quit") { break; } } bool p3::client::push_event_manager() { bool ointer = interactive; interactive = true; auto res = std::async(std::launch::async, &p3::client::push_event_reader, this); connection->send(p3::protocol::COMMAND, "startevents"); while (res.wait_for(std::chrono::milliseconds(0)) != std::future_status::ready) { std::string in(""); std::cin >> in; boost::trim(in); boost::to_lower(in); if (in == "exit" || in == "quit" || in == "q" || in == "x" || !std::cin) { connection->send(p3::protocol::COMMAND, "stopevents"); res.wait(); return false; } else if ("events" == in || "e" == in) { connection->send(p3::protocol::COMMAND, "stopevents"); res.wait(); } else { connection->send(p3::protocol::COMMAND, in); std::cout << "No commands supported in push event mode. Use 'event' to stop." << std::endl; } } interactive = ointer; return res.get(); } bool p3::client::push_event_reader() { bool okexit = false; while (true) { p3::mQueueMessage m = connection->receive(-1); bool print_list = true; switch (m.get_command()) { case p3::protocol::BEGINTEXT: m = connection->receive(30); while (m.get_command() != p3::protocol::ENDTEXT) { if (print_list) { std::cout << m.get_value() << std::endl; } m = connection->receive(30); } std::cout << "---" << std::endl; break; case p3::protocol::OK: if (verbose >= 1) std::cout << m.get_value() << std::endl; if (okexit) { return true; } else { okexit = true; } break; case p3::protocol::ERROR: std::cout << "ERROR: " << m.get_value() << std::endl; return true; case p3::protocol::FATAL_ERROR: std::cout << "FATAL ERROR: " << m.get_value() << std::endl; return true; case p3::protocol::BYE: std::cout << "done." << std::endl; return false; default: std::cout << "Unknown server command. " << m.get_value() << std::endl; } } }
src/client.h +4 −1 Original line number Diff line number Diff line Loading @@ -56,7 +56,10 @@ namespace p3 { bool interactive; int verbose; std::string get_input(std::string question, inputType is_command); bool push_event_reader(); // Return false if client should exit. bool push_event_manager(); // Return false if client should exit. std::string get_input(const std::string & question, inputType is_command); public: client(int argc, char *argv[]); Loading
src/config.cpp +17 −0 Original line number Diff line number Diff line Loading @@ -91,3 +91,20 @@ bool p3::config::get_bool(const std::string& k) { throw; } } std::list<std::string> p3::config::get_string_list(const std::string& k) { try { std::list<std::string> res; boost::split(res, data.at(k), boost::is_any_of(",;")); if (res.size() == 1 && (res.front() == "none" || res.front() == "")) { return std::list<std::string>(); } for (auto &n : res) { boost::trim(n); } return res; } catch (std::out_of_range& e) { std::cout << "!! config::get_string_list(): Invalide configuration key: " << k << std::endl; throw; } }
src/config.h +2 −0 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ #define __P3_CONFIG_H__ #include <string> #include <list> #include <map> namespace p3 { Loading @@ -40,6 +41,7 @@ namespace p3 { std::string get_string(const std::string& k); int get_int(const std::string& k); bool get_bool(const std::string& k); std::list <std::string> get_string_list(const std::string& k); }; } Loading
src/error.h +1 −1 Original line number Diff line number Diff line Loading @@ -28,7 +28,7 @@ namespace p3 { std::string message; public: perror(std::string c, std::string e) : context(c), error(e), message(c + ": " + e) {}; perror(const std::string & c, const std::string & e) : context(c), error(e), message(c + ": " + e) {}; ~perror() throw () {}; const char * getContext() { return context.c_str(); } Loading