From 02b9e16833acbdaa820bd3b8b64d652a41a8ff58 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 23 Jun 2009 18:54:46 +0200 Subject: XmlPacket-Klasse ?berarbeitet --- src/Client/InformationManager.cpp | 20 +++---- src/Client/Requests/DaemonCommandRequest.cpp | 4 +- src/Client/Requests/DaemonFSInfoRequest.cpp | 2 +- src/Client/Requests/DaemonStatusRequest.cpp | 2 +- src/Client/SystemCommands.cpp | 78 +++++++++++++++------------- src/Client/UserCommands.cpp | 47 ++++++++--------- 6 files changed, 81 insertions(+), 72 deletions(-) (limited to 'src/Client') diff --git a/src/Client/InformationManager.cpp b/src/Client/InformationManager.cpp index a4a63fb..a618eb3 100644 --- a/src/Client/InformationManager.cpp +++ b/src/Client/InformationManager.cpp @@ -35,9 +35,9 @@ void InformationManager::DaemonStateUpdateRequestHandler::handleRequest(boost::s { boost::lock_guard lock(informationManager->mutex); - std::map::iterator host = informationManager->daemons.find((*packet)["name"]); + std::map::iterator host = informationManager->daemons.find(packet->get("name")); if(host != informationManager->daemons.end()) - host->second.setState((*packet)["state"]); + host->second.setState(packet->get("state")); else getApplication()->log(Core::LoggerBase::WARNING, "Received a state update for an unknown host."); } @@ -75,17 +75,19 @@ void InformationManager::daemonListRequestFinished(boost::shared_ptrlogf(Core::LoggerBase::CRITICAL, "Host list request failed: %s", error.strerror().c_str()); } else { - const Common::XmlPacket::Element &hostInfo = (*packet)["hosts"]; + const Common::XmlPacket::List *list = packet->getList("hosts"); daemons.clear(); - for(size_t i = 0; i < hostInfo.getSize(); ++i) { - Common::HostInfo info; - info.setName(hostInfo[i]["name"]); - info.setIP(hostInfo[i]["address"]); - info.setState(hostInfo[i]["state"]); + if(list) { + for(Common::XmlPacket::List::const_iterator entry = list->begin(); entry != list->end(); ++entry) { + Common::HostInfo info; + info.setName(entry->get("name")); + info.setIP(entry->get("address")); + info.setState(entry->get("state")); - daemons.insert(std::make_pair(info.getName(), info)); + daemons.insert(std::make_pair(info.getName(), info)); + } } } diff --git a/src/Client/Requests/DaemonCommandRequest.cpp b/src/Client/Requests/DaemonCommandRequest.cpp index 45a346b..a6b3bb4 100644 --- a/src/Client/Requests/DaemonCommandRequest.cpp +++ b/src/Client/Requests/DaemonCommandRequest.cpp @@ -26,8 +26,8 @@ namespace Requests { void DaemonCommandRequest::sendRequest() { Common::XmlPacket packet; packet.setType("DaemonCommand"); - packet.add("command", reboot ? "reboot" : "shutdown"); - packet.add("daemon", daemon); + packet.set("command", reboot ? "reboot" : "shutdown"); + packet.set("daemon", daemon); sendPacket(packet); } diff --git a/src/Client/Requests/DaemonFSInfoRequest.cpp b/src/Client/Requests/DaemonFSInfoRequest.cpp index 5d77850..6189ec5 100644 --- a/src/Client/Requests/DaemonFSInfoRequest.cpp +++ b/src/Client/Requests/DaemonFSInfoRequest.cpp @@ -26,7 +26,7 @@ namespace Requests { void DaemonFSInfoRequest::sendRequest() { Common::XmlPacket packet; packet.setType("DaemonFSInfo"); - packet.add("daemon", daemon); + packet.set("daemon", daemon); sendPacket(packet); } diff --git a/src/Client/Requests/DaemonStatusRequest.cpp b/src/Client/Requests/DaemonStatusRequest.cpp index ff1cda3..baec42d 100644 --- a/src/Client/Requests/DaemonStatusRequest.cpp +++ b/src/Client/Requests/DaemonStatusRequest.cpp @@ -26,7 +26,7 @@ namespace Requests { void DaemonStatusRequest::sendRequest() { Common::XmlPacket packet; packet.setType("GetDaemonStatus"); - packet.add("daemon", daemon); + packet.set("daemon", daemon); sendPacket(packet); } diff --git a/src/Client/SystemCommands.cpp b/src/Client/SystemCommands.cpp index 5b2d4e5..6c0f91d 100644 --- a/src/Client/SystemCommands.cpp +++ b/src/Client/SystemCommands.cpp @@ -40,53 +40,53 @@ void SystemCommands::printFSInfo(boost::shared_ptr &pac "kB", "MB", "GB", "TB", "" }; - for(size_t i = 0; i < (*packet)["filesystems"].getSize(); ++i) { - const Common::XmlPacket::Entry &fs = (*packet)["filesystems"][i]; + const Common::XmlPacket::List *list = packet->getList("filesystems"); - unsigned usedUnit = 0, totalUnit = 0; + if(list) { + for(Common::XmlPacket::List::const_iterator fs = list->begin(); fs != list->end(); ++fs) { + unsigned usedUnit = 0, totalUnit = 0; - float used = fs["usedSize"]; - float total = fs["totalSize"]; - float available = fs["availableSize"]; + float used = fs->get("usedSize"); + float total = fs->get("totalSize"); + float available = fs->get("availableSize"); - while(used >= 1024 && !units[usedUnit+1].empty()) { - ++usedUnit; - used /= 1024; - available /= 1024; - } + while(used >= 1024 && !units[usedUnit+1].empty()) { + ++usedUnit; + used /= 1024; + available /= 1024; + } - while(total >= 1024 && !units[totalUnit+1].empty()) { - ++totalUnit; - total /= 1024; - } + while(total >= 1024 && !units[totalUnit+1].empty()) { + ++totalUnit; + total /= 1024; + } - std::string name = fs["name"]; - std::string mountedOn = fs["mountedOn"]; + std::string name = fs->get("name"); + std::string mountedOn = fs->get("mountedOn"); - std::string nameString = mountedOn + " (" + name + ")"; + std::string nameString = mountedOn + " (" + name + ")"; - if(nameString.length() < 32) { - nameString.resize(32, ' '); - } - else { - nameString += "\n\t"; - nameString.resize(nameString.length() + 32, ' '); - } + if(nameString.length() < 32) { + nameString.resize(32, ' '); + } + else { + nameString += "\n\t"; + 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("\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 SystemCommands::printHostStatus(boost::shared_ptr &packet) { - unsigned long uptime = (*packet)["uptime"]; + unsigned long uptime = packet->get("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; @@ -97,18 +97,23 @@ void SystemCommands::printHostStatus(boost::shared_ptr std::printf("%lu:%02lu", hours, minutes); - std::printf(" (load average: %.2f %.2f %.2f, %lu processes)", (float)(*packet)["loadAvg1"], (float)(*packet)["loadAvg5"], (float)(*packet)["loadAvg15"], (unsigned long)(*packet)["nProcesses"]); + std::printf(" (load average: %.2f %.2f %.2f, %lu processes)", + packet->get("loadAvg1"), packet->get("loadAvg5"), + packet->get("loadAvg15"), packet->get("nProcesses")); std::printf("\n\n"); } - if((unsigned long)(*packet)["totalMem"] && (unsigned long)(*packet)["freeMem"]) { + float totalMem = packet->get("totalMem"); + float freeMem = packet->get("freeMem"); + + if(totalMem && freeMem) { const std::string units[] = { "kB", "MB", "GB", "TB", "" }; unsigned unit = 0; - float totalMem = (*packet)["totalMem"], usedMem = totalMem-(float)(*packet)["freeMem"]; + float usedMem = totalMem-freeMem; while(totalMem >= 1024 && !units[unit+1].empty()) { ++unit; @@ -119,9 +124,12 @@ void SystemCommands::printHostStatus(boost::shared_ptr 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((unsigned long)(*packet)["totalSwap"] && (unsigned long)(*packet)["freeSwap"]) { + totalMem = packet->get("totalSwap"); + freeMem = packet->get("freeSwap"); + + if(totalMem && freeMem) { unit = 0; - totalMem = (*packet)["totalSwap"]; usedMem = totalMem-(float)(*packet)["freeSwap"]; + usedMem = totalMem-freeMem; while(totalMem >= 1024 && !units[unit+1].empty()) { ++unit; diff --git a/src/Client/UserCommands.cpp b/src/Client/UserCommands.cpp index a1cfd0f..de5b7ea 100644 --- a/src/Client/UserCommands.cpp +++ b/src/Client/UserCommands.cpp @@ -67,8 +67,8 @@ void UserCommands::userInfoCommand(CommandParser *commandParser, const std::vect else { const Common::XmlPacket &packet = *result.first; - std::cout << " " << (unsigned long)packet["uid"] << ", " << (unsigned long)packet["gid"] << ", " << (const std::string&)packet["username"] << ", " - << (const std::string&)packet["fullName"] << std::endl << std::endl; + std::cout << " " << packet.get("uid") << ", " << packet.get("gid") << ", " + << packet.get("username") << ", " << packet.get("fullName") << std::endl << std::endl; } } @@ -84,17 +84,18 @@ void UserCommands::listUsersCommand(CommandParser *commandParser, const std::vec std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } else { - const Common::XmlPacket::Element &users = (*result.first)["users"]; + const Common::XmlPacket::List *users = result.first->getList("users"); - if(users.isEmpty()) { + if(!users || users->isEmpty()) { std::cout << "User list is empty." << std::endl; } else { - std::cout << "Found " << users.getSize() << " user" << (users.getSize()==1 ? "" : "s") << ":" << std::endl; + std::cout << "Found " << users->getSize() << " user" << (users->getSize()==1 ? "" : "s") << ":" << 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; + for(Common::XmlPacket::List::const_iterator user = users->begin(); user != users->end(); ++user) + std::cout << " " << user->get("uid") << ", " << user->get("gid") + << ", " << user->get("username") << ", " << user->get("fullName") << std::endl; } std::cout << std::endl; @@ -131,18 +132,17 @@ void UserCommands::listUserGroupsCommand(CommandParser *commandParser, const std std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } else { - const Common::XmlPacket::Element &groups = (*result.first)["groups"]; + const Common::XmlPacket::List *groups = result.first->getList("groups"); - if(groups.isEmpty()) { + if(!groups || groups->isEmpty()) { std::cout << "The user isn't member of any group." << std::endl; } else { + std::cout << "User is member of " << groups->getSize() << " group" << (groups->getSize()==1 ? "" : "s") << ":" << std::endl; - std::cout << "User is member of " << groups.getSize() << " group" << (groups.getSize()==1 ? "" : "s") << ":" << std::endl; - - for(size_t i = 0; i < groups.getSize(); ++i) - std::cout << " " << (unsigned long)groups[i]["gid"] << std::endl; + for(Common::XmlPacket::List::const_iterator group = groups->begin(); group != groups->end(); ++group) + std::cout << " " << group->get("gid") << std::endl; } std::cout << std::endl; @@ -161,17 +161,17 @@ void UserCommands::listGroupsCommand(CommandParser *commandParser, const std::ve std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } else { - const Common::XmlPacket::Element &groups = (*result.first)["groups"]; + const Common::XmlPacket::List *groups = result.first->getList("groups"); - if(groups.isEmpty()) { + if(!groups || groups->isEmpty()) { std::cout << "Group list is empty." << std::endl; } else { - std::cout << "Found " << groups.getSize() << " group" << (groups.getSize()==1 ? "" : "s") << ":" << std::endl; + std::cout << "Found " << groups->getSize() << " group" << (groups->getSize()==1 ? "" : "s") << ":" << std::endl; - for(size_t i = 0; i < groups.getSize(); ++i) - std::cout << " " << (unsigned long)groups[i]["gid"] << ", " << (const std::string&)groups[i]["name"] << std::endl; + for(Common::XmlPacket::List::const_iterator group = groups->begin(); group != groups->end(); ++group) + std::cout << " " << group->get("gid") << ", " << group->get("name") << std::endl; } std::cout << std::endl; @@ -208,18 +208,17 @@ void UserCommands::listGroupUsersCommand(CommandParser *commandParser, const std std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } else { - const Common::XmlPacket::Element &users = (*result.first)["users"]; + const Common::XmlPacket::List *users = result.first->getList("users"); - if(users.isEmpty()) { + if(!users || users->isEmpty()) { std::cout << "The group doesn't have any members." << std::endl; } else { - - std::cout << "The group has " << users.getSize() << " member" << (users.getSize()==1 ? "" : "s") << ":" << std::endl; + std::cout << "The group has " << users->getSize() << " member" << (users->getSize()==1 ? "" : "s") << ":" << std::endl; - for(size_t i = 0; i < users.getSize(); ++i) - std::cout << " " << (unsigned long)users[i]["uid"] << std::endl; + for(Common::XmlPacket::List::const_iterator user = users->begin(); user != users->end(); ++user) + std::cout << " " << user->get("uid") << std::endl; } std::cout << std::endl; -- cgit v1.2.3