From 58c5d4eefdf1cdee0651f7c74ffd1501adbdc9c3 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 8 Oct 2008 23:08:21 +0200 Subject: fsinfo-Befehl implementiert --- src/Client/CommandManager.cpp | 83 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 77 insertions(+), 6 deletions(-) (limited to 'src/Client/CommandManager.cpp') diff --git a/src/Client/CommandManager.cpp b/src/Client/CommandManager.cpp index 2a20b29..08a2780 100644 --- a/src/Client/CommandManager.cpp +++ b/src/Client/CommandManager.cpp @@ -20,9 +20,11 @@ #include "CommandManager.h" #include #include +#include #include #include +#include #include #include @@ -30,6 +32,48 @@ namespace Mad { namespace Client { +void CommandManager::printFSInfo(const Net::Packets::FSInfoPacket &packet) { + const std::string units[] = { + "kB", "MB", "GB", "TB", "" + }; + + const std::vector& fsList = packet.getFSInfo(); + + for(std::vector::const_iterator fs = fsList.begin(); fs != fsList.end(); ++fs) { + unsigned usedUnit = 0, totalUnit = 0; + + float used = fs->used; + float total = fs->total; + float available = fs->available; + + while(used >= 1024 && !units[usedUnit+1].empty()) { + ++usedUnit; + used /= 1024; + available /= 1024; + } + + while(total >= 1024 && !units[totalUnit+1].empty()) { + ++totalUnit; + total /= 1024; + } + + std::string nameString = fs->mountedOn + " (" + fs->fsName + ")"; + + if(nameString.length() < 32) { + nameString.resize(32, ' '); + } + else { + nameString += '\n'; + nameString.resize(nameString.length() + 32, ' '); + } + + std::printf("\t%s%.*f%s", nameString.c_str(), (used < 10) ? 2 : 1, used, (usedUnit == totalUnit) ? "" : (" " + units[usedUnit]).c_str()); + std::printf("/%.*f %s (%.1f%%)\n", (total < 10) ? 2 : 1, total, units[totalUnit].c_str(), std::min(100*used/(used+available), 100.0f)); + } + + std::printf("\n"); +} + void CommandManager::printHostStatus(const Net::Packets::HostStatusPacket &packet) { if(packet.getUptime()) { unsigned long days = packet.getUptime()/86400; @@ -82,11 +126,10 @@ void CommandManager::printHostStatus(const Net::Packets::HostStatusPacket &packe } } -void CommandManager::coreStatusRequestFinished(const Common::Request &request) { + +void CommandManager::daemonCommandRequestFinished(const Common::Request<> &request) { try { - const Net::Packets::HostStatusPacket &packet = request.getResult(); - std::cout << "Server status:" << std::endl; - printHostStatus(packet); + request.getResult(); } catch(Common::Exception &exception) { Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); @@ -95,9 +138,11 @@ void CommandManager::coreStatusRequestFinished(const Common::Request &request) { +void CommandManager::daemonFSInfoRequestFinished(const Common::Request &request) { try { - request.getResult(); + const Net::Packets::FSInfoPacket &packet = request.getResult(); + std::cout << "Host file system usage:" << std::endl; + printFSInfo(packet); } catch(Common::Exception &exception) { Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); @@ -131,5 +176,31 @@ void CommandManager::disconnectRequestFinished(const Common::Request<> &request) requestFinished(); } +void CommandManager::fsInfoRequestFinished(const Common::Request &request) { + try { + const Net::Packets::FSInfoPacket &packet = request.getResult(); + std::cout << "Server file system usage:" << std::endl; + printFSInfo(packet); + } + catch(Common::Exception &exception) { + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); + } + + requestFinished(); +} + +void CommandManager::statusRequestFinished(const Common::Request &request) { + try { + const Net::Packets::HostStatusPacket &packet = request.getResult(); + std::cout << "Server status:" << std::endl; + printHostStatus(packet); + } + catch(Common::Exception &exception) { + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); + } + + requestFinished(); +} + } } -- cgit v1.2.3