diff options
Diffstat (limited to 'src/Client')
-rw-r--r-- | src/Client/CommandManager.cpp | 50 | ||||
-rw-r--r-- | src/Client/CommandManager.h | 14 | ||||
-rw-r--r-- | src/Client/CommandParser.cpp | 7 | ||||
-rw-r--r-- | src/Client/Requests/DaemonStatusRequest.cpp | 16 | ||||
-rw-r--r-- | src/Client/Requests/DaemonStatusRequest.h | 9 |
5 files changed, 51 insertions, 45 deletions
diff --git a/src/Client/CommandManager.cpp b/src/Client/CommandManager.cpp index aacf939..90d15ae 100644 --- a/src/Client/CommandManager.cpp +++ b/src/Client/CommandManager.cpp @@ -20,10 +20,9 @@ #include "CommandManager.h" #include <Common/HostInfo.h> #include <Common/Logger.h> +#include <Common/XmlRequest.h> #include <Net/Packets/FSInfoPacket.h> #include <Net/Packets/HostListPacket.h> -#include <Net/Packets/HostStatusPacket.h> -#include <Net/Packets/UserListPacket.h> #include <cmath> #include <iostream> @@ -78,11 +77,15 @@ void CommandManager::printFSInfo(const Net::Packets::FSInfoPacket &packet) { std::printf("\n"); } -void CommandManager::printHostStatus(const Net::Packets::HostStatusPacket &packet) { - if(packet.getUptime()) { - unsigned long days = packet.getUptime()/86400; - unsigned long hours = (packet.getUptime()%86400)/3600; - unsigned long minutes = (packet.getUptime()%3600)/60; +void CommandManager::printHostStatus(const Common::XmlPacket &packet) { + unsigned long uptime = packet["uptime"]; + + if(uptime) { + unsigned long uptime = packet["uptime"]; + + unsigned long days = uptime/86400; + unsigned long hours = (uptime%86400)/3600; + unsigned long minutes = (uptime%3600)/60; std::printf("\tUptime:\t\t"); @@ -90,18 +93,18 @@ void CommandManager::printHostStatus(const Net::Packets::HostStatusPacket &packe std::printf("%lu:%02lu", hours, minutes); - std::printf(" (load average: %.2f %.2f %.2f, %lu processes)", packet.getLoadAverage1(), packet.getLoadAverage5(), packet.getLoadAverage15(), (unsigned long)packet.getProcessNumber()); + std::printf(" (load average: %.2f %.2f %.2f, %lu processes)", (float)packet["loadAvg1"], (float)packet["loadAvg5"], (float)packet["loadAvg15"], (unsigned long)packet["nProcesses"]); std::printf("\n\n"); } - if(packet.getTotalMem() && packet.getFreeMem()) { + if((unsigned long)packet["totalMem"] && (unsigned long)packet["freeMem"]) { const std::string units[] = { "kB", "MB", "GB", "TB", "" }; unsigned unit = 0; - float totalMem = packet.getTotalMem(), usedMem = packet.getTotalMem()-packet.getFreeMem(); + float totalMem = packet["totalMem"], usedMem = totalMem-(float)packet["freeMem"]; while(totalMem >= 1024 && !units[unit+1].empty()) { ++unit; @@ -112,9 +115,9 @@ void CommandManager::printHostStatus(const Net::Packets::HostStatusPacket &packe std::printf("\tMemory usage:\t%.*f/%.*f %s", (usedMem < 10) ? 2 : 1, usedMem, (totalMem < 10) ? 2 : 1, totalMem, units[unit].c_str()); std::printf(" (%.1f%%)\n", usedMem*100.0f/totalMem); - if(packet.getTotalSwap() && packet.getFreeSwap()) { + if((unsigned long)packet["totalSwap"] && (unsigned long)packet["freeSwap"]) { unit = 0; - totalMem = packet.getTotalSwap(); usedMem = packet.getTotalSwap()-packet.getFreeSwap(); + totalMem = packet["totalSwap"]; usedMem = totalMem-(float)packet["freeSwap"]; while(totalMem >= 1024 && !units[unit+1].empty()) { ++unit; @@ -155,9 +158,9 @@ void CommandManager::daemonFSInfoRequestFinished(const Common::Request<Net::Pack requestFinished(); } -void CommandManager::daemonStatusRequestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request) { +void CommandManager::daemonStatusRequestFinished(const Common::XmlRequest &request) { try { - const Net::Packets::HostStatusPacket &packet = request.getResult(); + const Common::XmlPacket &packet = request.getResult(); std::cout << "Host status:" << std::endl; printHostStatus(packet); } @@ -193,9 +196,9 @@ void CommandManager::fsInfoRequestFinished(const Common::Request<Net::Packets::F requestFinished(); } -void CommandManager::statusRequestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request) { +void CommandManager::statusRequestFinished(const Common::XmlRequest &request) { try { - const Net::Packets::HostStatusPacket &packet = request.getResult(); + const Common::XmlPacket &packet = request.getResult(); std::cout << "Server status:" << std::endl; printHostStatus(packet); } @@ -206,20 +209,21 @@ void CommandManager::statusRequestFinished(const Common::Request<Net::Packets::H requestFinished(); } -void CommandManager::userListRequestFinished(const Common::Request<Net::Packets::UserListPacket> &request) { +void CommandManager::userListRequestFinished(const Common::XmlRequest &request) { try { - const Net::Packets::UserListPacket &packet = request.getResult(); + const Common::XmlPacket &packet = request.getResult(); - const std::vector<Common::UserInfo> &users = packet.getUserInfo(); + const Common::XmlPacket::Element &users = packet["users"]; - if(users.empty()) { + if(users.isEmpty()) { std::cout << "User list is empty." << std::endl; } else { - std::cout << "Found " << packet.getUserInfo().size() << " users:" << std::endl; + std::cout << "Found " << users.getSize() << " users:" << std::endl; + - for(std::vector<Common::UserInfo>::const_iterator user = users.begin(); user != users.end(); ++user) - std::cout << " " << user->getUid() << ", " << user->getGid() << ", " << user->getUsername() << ", " << user->getFullName() << std::endl; + for(size_t i = 0; i < users.getSize(); ++i) + std::cout << " " << (unsigned long)users[i]["uid"] << ", " << (unsigned long)users[i]["gid"] << ", " << (const std::string&)users[i]["username"] << ", " << (const std::string&)users[i]["fullName"] << std::endl; } std::cout << std::endl; diff --git a/src/Client/CommandManager.h b/src/Client/CommandManager.h index 454c77e..fb90070 100644 --- a/src/Client/CommandManager.h +++ b/src/Client/CommandManager.h @@ -24,12 +24,16 @@ namespace Mad { +namespace Common { +class XmlPacket; +class XmlRequest; +} + namespace Net { namespace Packets { class FSInfoPacket; class HostStatusPacket; class HostListPacket; -class UserListPacket; } } @@ -54,15 +58,15 @@ class CommandManager { } void printFSInfo(const Net::Packets::FSInfoPacket &packet); - void printHostStatus(const Net::Packets::HostStatusPacket &packet); + void printHostStatus(const Common::XmlPacket &packet); void daemonCommandRequestFinished(const Common::Request<> &request); void daemonFSInfoRequestFinished(const Common::Request<Net::Packets::FSInfoPacket> &request); - void daemonStatusRequestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request); + void daemonStatusRequestFinished(const Common::XmlRequest &request); void disconnectRequestFinished(const Common::Request<> &request); void fsInfoRequestFinished(const Common::Request<Net::Packets::FSInfoPacket> &request); - void statusRequestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request); - void userListRequestFinished(const Common::Request<Net::Packets::UserListPacket> &request); + void statusRequestFinished(const Common::XmlRequest &request); + void userListRequestFinished(const Common::XmlRequest &request); CommandManager() : activeRequests(0), disconnect(false) {} diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 304a2ff..6665ea2 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -31,7 +31,6 @@ #include <Common/Requests/UserListRequest.h> #include <Common/Tokenizer.h> #include <Net/Packets/HostListPacket.h> -#include <Net/Packets/HostStatusPacket.h> #include <iostream> #include <cstdio> @@ -243,9 +242,9 @@ void CommandParser::shutdownCommand(const std::vector<std::string> &args) { void CommandParser::statusCommand(const std::vector<std::string> &args) { if(args.size() == 1) - Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::StatusRequest(sigc::mem_fun(CommandManager::get(), &CommandManager::statusRequestFinished)))); + Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::XmlRequestBase>(new Common::Requests::StatusRequest(sigc::mem_fun(CommandManager::get(), &CommandManager::statusRequestFinished)))); else if(args.size() == 2) - Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Requests::DaemonStatusRequest(args[1], sigc::mem_fun(CommandManager::get(), &CommandManager::daemonStatusRequestFinished)))); + Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::XmlRequestBase>(new Requests::DaemonStatusRequest(args[1], sigc::mem_fun(CommandManager::get(), &CommandManager::daemonStatusRequestFinished)))); else { Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); printUsage("status"); @@ -258,7 +257,7 @@ void CommandParser::statusCommand(const std::vector<std::string> &args) { void CommandParser::listUsersCommand(const std::vector<std::string>&) { ++CommandManager::get()->activeRequests; - Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::RequestBase>(new Common::Requests::UserListRequest(sigc::mem_fun(CommandManager::get(), &CommandManager::userListRequestFinished)))); + Common::RequestManager::get()->sendRequest(connection, std::auto_ptr<Common::XmlRequestBase>(new Common::Requests::UserListRequest(sigc::mem_fun(CommandManager::get(), &CommandManager::userListRequestFinished)))); } void CommandParser::exitCommand(const std::vector<std::string>&) { diff --git a/src/Client/Requests/DaemonStatusRequest.cpp b/src/Client/Requests/DaemonStatusRequest.cpp index 94407b4..2d83b8b 100644 --- a/src/Client/Requests/DaemonStatusRequest.cpp +++ b/src/Client/Requests/DaemonStatusRequest.cpp @@ -26,20 +26,20 @@ namespace Client { namespace Requests { void DaemonStatusRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { - connection->send(Net::Packet(Net::Packet::DAEMON_STATUS, requestId, daemon.c_str(), daemon.length())); + Common::XmlPacket packet; + packet.setType("GetDaemonStatus"); + packet.add("daemonName", daemon); + + connection->send(packet.encode(requestId)); } -void DaemonStatusRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() == Net::Packet::ERROR) { - finishWithError(Net::Packets::ErrorPacket(packet).getException()); - return; - } - else if(packet.getType() != Net::Packet::OK) { +void DaemonStatusRequest::handlePacket(Net::Connection*, uint16_t, const Common::XmlPacket &packet) { + if(packet.getType() != "OK") { finishWithError(Common::Exception(Common::Exception::UNEXPECTED_PACKET)); return; // TODO Logging } - finish(Net::Packets::HostStatusPacket(packet)); + finish(packet); } } diff --git a/src/Client/Requests/DaemonStatusRequest.h b/src/Client/Requests/DaemonStatusRequest.h index 3190ca1..b0982e6 100644 --- a/src/Client/Requests/DaemonStatusRequest.h +++ b/src/Client/Requests/DaemonStatusRequest.h @@ -20,8 +20,7 @@ #ifndef MAD_CLIENT_REQUESTS_DAEMONSTATUSREQUEST_H_ #define MAD_CLIENT_REQUESTS_DAEMONSTATUSREQUEST_H_ -#include <Common/Request.h> -#include <Net/Packets/HostStatusPacket.h> +#include <Common/XmlRequest.h> #include <string> @@ -29,16 +28,16 @@ namespace Mad { namespace Client { namespace Requests { -class DaemonStatusRequest : public Common::Request<Net::Packets::HostStatusPacket> { +class DaemonStatusRequest : public Common::XmlRequest { private: std::string daemon; protected: virtual void sendRequest(Net::Connection *connection, uint16_t requestId); - virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection*, uint16_t, const Common::XmlPacket &packet); public: - DaemonStatusRequest(const std::string &daemon0, slot_type slot) : Common::Request<Net::Packets::HostStatusPacket>(slot), daemon(daemon0) {} + DaemonStatusRequest(const std::string &daemon0, slot_type slot) : Common::XmlRequest(slot), daemon(daemon0) {} }; } |