diff options
29 files changed, 240 insertions, 530 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) {} }; } diff --git a/src/Common/RequestHandlers/StatusRequestHandler.cpp b/src/Common/RequestHandlers/StatusRequestHandler.cpp index 8494fee..86ed970 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.cpp +++ b/src/Common/RequestHandlers/StatusRequestHandler.cpp @@ -18,20 +18,25 @@ */ #include "StatusRequestHandler.h" +#include "../Exception.h" #include "../SystemBackend.h" #include "../Logger.h" +#include "../XmlPacket.h" #include <Net/Connection.h> -#include <Net/Packets/ErrorPacket.h> -#include <Net/Packets/HostStatusPacket.h> namespace Mad { namespace Common { namespace RequestHandlers { -void StatusRequestHandler::handlePacket(Net::Connection *con, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::STATUS) { +void StatusRequestHandler::handlePacket(Net::Connection *con, uint16_t requestId, const XmlPacket &packet) { + if(packet.getType() != "GetStatus") { Logger::log(Logger::ERROR, "Received an unexpected packet."); - con->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Exception(Exception::UNEXPECTED_PACKET))); + + XmlPacket ret; + ret.setType("Error"); + ret.add("ErrorCode", Exception::UNEXPECTED_PACKET); + + con->send(ret.encode(requestId)); signalFinished().emit(); return; @@ -40,7 +45,7 @@ void StatusRequestHandler::handlePacket(Net::Connection *con, const Net::Packet // TODO Require authentication connection = con; - requestId = packet.getRequestId(); + rid = requestId; if(!SystemBackend::getUptimeInfo(sigc::mem_fun(this, &StatusRequestHandler::uptimeHandler))) needUptime = false; @@ -56,8 +61,23 @@ void StatusRequestHandler::send() { if(needUptime || needMemory || needLoad || sent) return; - connection->send(Net::Packets::HostStatusPacket(Net::Packet::OK, requestId, uptime, idleTime, - totalMem, freeMem, totalSwap, freeSwap, currentLoad, nProcesses, loadAvg1, loadAvg5, loadAvg15)); + XmlPacket packet; + + packet.setType("OK"); + + packet.add("uptime", uptime); + packet.add("idleTime", idleTime); + packet.add("totalMem", totalMem); + packet.add("freeMem", freeMem); + packet.add("totalSwap", totalSwap); + packet.add("freeSwap", freeSwap); + packet.add("currentLoad", currentLoad); + packet.add("nProcesses", nProcesses); + packet.add("loadAvg1", loadAvg1); + packet.add("loadAvg5", loadAvg5); + packet.add("loadAvg15", loadAvg15); + + connection->send(packet.encode(rid)); sent = true; diff --git a/src/Common/RequestHandlers/StatusRequestHandler.h b/src/Common/RequestHandlers/StatusRequestHandler.h index fd907de..0b53db0 100644 --- a/src/Common/RequestHandlers/StatusRequestHandler.h +++ b/src/Common/RequestHandlers/StatusRequestHandler.h @@ -20,17 +20,17 @@ #ifndef MAD_COMMON_REQUESTHANDLERS_STATUSREQUESTHANDLER_H_ #define MAD_COMMON_REQUESTHANDLERS_STATUSREQUESTHANDLER_H_ -#include "../RequestHandler.h" +#include "../XmlRequestHandler.h" #include <stdint.h> namespace Mad { namespace Common { namespace RequestHandlers { -class StatusRequestHandler : public RequestHandler { +class StatusRequestHandler : public XmlRequestHandler { private: Net::Connection *connection; - uint16_t requestId; + uint16_t rid; bool needUptime, needMemory, needLoad, sent; @@ -60,7 +60,7 @@ class StatusRequestHandler : public RequestHandler { void send(); protected: - virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *con, uint16_t requestId, const XmlPacket &packet); public: StatusRequestHandler() : needUptime(true), needMemory(true), needLoad(true), sent(false), diff --git a/src/Common/RequestManager.cpp b/src/Common/RequestManager.cpp index 94b3da9..03a2603 100644 --- a/src/Common/RequestManager.cpp +++ b/src/Common/RequestManager.cpp @@ -135,7 +135,7 @@ void RequestManager::receiveHandler(Net::Connection *connection, const Net::Pack if(xmlRequest) { XmlPacket xmlPacket(packet); - xmlRequest->handlePacket(connection, xmlPacket); + xmlRequest->handlePacket(connection, packet.getRequestId(), xmlPacket); return; } @@ -147,7 +147,7 @@ void RequestManager::receiveHandler(Net::Connection *connection, const Net::Pack if(factoryIt != xmlRequestHandlerFactories.end()) { xmlRequest = factoryIt->second->createRequestHandler(); xmlRequestMap->addRequest(packet.getRequestId(), xmlRequest); - xmlRequest->handlePacket(connection, xmlPacket); + xmlRequest->handlePacket(connection, packet.getRequestId(), xmlPacket); return; } diff --git a/src/Common/Requests/StatusRequest.cpp b/src/Common/Requests/StatusRequest.cpp index dbaba23..3526e23 100644 --- a/src/Common/Requests/StatusRequest.cpp +++ b/src/Common/Requests/StatusRequest.cpp @@ -25,16 +25,19 @@ namespace Common { namespace Requests { void StatusRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { - connection->send(Net::Packet(Net::Packet::STATUS, requestId)); + XmlPacket packet; + packet.setType("GetStatus"); + + connection->send(packet.encode(requestId)); } -void StatusRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::OK) { +void StatusRequest::handlePacket(Net::Connection*, uint16_t, const XmlPacket &packet) { + if(packet.getType() != "OK") { finishWithError(Exception(Exception::UNEXPECTED_PACKET)); return; // TODO Logging } - finish(Net::Packets::HostStatusPacket(packet)); + finish(packet); } } diff --git a/src/Common/Requests/StatusRequest.h b/src/Common/Requests/StatusRequest.h index 5712404..e9c8a4c 100644 --- a/src/Common/Requests/StatusRequest.h +++ b/src/Common/Requests/StatusRequest.h @@ -20,20 +20,19 @@ #ifndef MAD_COMMON_REQUESTS_STATUSREQUEST_H_ #define MAD_COMMON_REQUESTS_STATUSREQUEST_H_ -#include "../Request.h" -#include <Net/Packets/HostStatusPacket.h> +#include "../XmlRequest.h" namespace Mad { namespace Common { namespace Requests { -class StatusRequest : public Request<Net::Packets::HostStatusPacket> { +class StatusRequest : public XmlRequest { 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 *connection, uint16_t, const XmlPacket &packet); public: - StatusRequest(slot_type slot) : Request<Net::Packets::HostStatusPacket>(slot) {} + StatusRequest(slot_type slot) : XmlRequest(slot) {} }; } diff --git a/src/Common/Requests/UserListRequest.cpp b/src/Common/Requests/UserListRequest.cpp index cd9c760..2545677 100644 --- a/src/Common/Requests/UserListRequest.cpp +++ b/src/Common/Requests/UserListRequest.cpp @@ -25,16 +25,19 @@ namespace Common { namespace Requests { void UserListRequest::sendRequest(Net::Connection *connection, uint16_t requestId) { - connection->send(Net::Packet(Net::Packet::USERS_LIST, requestId)); + XmlPacket packet; + packet.setType("ListUsers"); + + connection->send(packet.encode(requestId)); } -void UserListRequest::handlePacket(Net::Connection*, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::OK) { +void UserListRequest::handlePacket(Net::Connection*, uint16_t, const XmlPacket &packet) { + if(packet.getType() != "OK") { finishWithError(Exception(Exception::UNEXPECTED_PACKET)); return; // TODO Logging } - finish(Net::Packets::UserListPacket(packet)); + finish(packet); } } diff --git a/src/Common/Requests/UserListRequest.h b/src/Common/Requests/UserListRequest.h index c62e284..9702a42 100644 --- a/src/Common/Requests/UserListRequest.h +++ b/src/Common/Requests/UserListRequest.h @@ -20,20 +20,19 @@ #ifndef MAD_COMMON_REQUESTS_USERLISTREQUEST_H_ #define MAD_COMMON_REQUESTS_USERLISTREQUEST_H_ -#include "../Request.h" -#include <Net/Packets/UserListPacket.h> +#include "../XmlRequest.h" namespace Mad { namespace Common { namespace Requests { -class UserListRequest : public Request<Net::Packets::UserListPacket> { +class UserListRequest : public XmlRequest { 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 *connection, uint16_t, const XmlPacket &packet); public: - UserListRequest(slot_type slot) : Request<Net::Packets::UserListPacket>(slot) {} + UserListRequest(slot_type slot) : XmlRequest(slot) {} }; } diff --git a/src/Common/XmlPacket.cpp b/src/Common/XmlPacket.cpp index 8da861c..672d092 100644 --- a/src/Common/XmlPacket.cpp +++ b/src/Common/XmlPacket.cpp @@ -222,7 +222,7 @@ void XmlPacket::setType(const std::string &type) { xmlSetProp(packetNode, (xmlChar*)"type", (xmlChar*)type.c_str()); } -Net::Packet XmlPacket::encode(uint16_t requestId) { +Net::Packet XmlPacket::encode(uint16_t requestId) const { xmlChar *buf; int length; diff --git a/src/Common/XmlPacket.h b/src/Common/XmlPacket.h index 007404a..e80a266 100644 --- a/src/Common/XmlPacket.h +++ b/src/Common/XmlPacket.h @@ -122,6 +122,10 @@ class XmlPacket { return 0; } + bool isEmpty() const { + return (getSize() == 0); + } + virtual Entry& operator[](size_t) { return nilEntry; } @@ -130,12 +134,26 @@ class XmlPacket { return nilEntry; } + Entry& back() { + if(isEmpty()) + return nilEntry; + + return operator[](getSize()-1); + } + + const Entry& back() const { + if(isEmpty()) + return nilEntry; + + return operator[](getSize()-1); + } + virtual bool insertEntry(size_t) {return false;} virtual bool addEntry() {return false;} virtual bool removeEntry(size_t) {return false;} template<typename T> - operator T() { + operator T() const { switch(type) { case INT: return static_cast<T>(value.var_int); @@ -156,7 +174,7 @@ class XmlPacket { } } - operator std::string() { + operator std::string() const { return str; } @@ -258,6 +276,20 @@ class XmlPacket { return add(name, buf.str(), "uint"); } + bool add(const std::string &name, long val) { + std::ostringstream buf; + buf << val; + + return add(name, buf.str(), "int"); + } + + bool add(const std::string &name, unsigned long val) { + std::ostringstream buf; + buf << val; + + return add(name, buf.str(), "uint"); + } + bool add(const std::string &name, long long val) { std::ostringstream buf; buf << val; @@ -428,7 +460,7 @@ class XmlPacket { return entry->remove(name); } - Net::Packet encode(uint16_t requestId); + Net::Packet encode(uint16_t requestId) const; }; } diff --git a/src/Common/XmlRequest.h b/src/Common/XmlRequest.h index 0de0291..667b9a7 100644 --- a/src/Common/XmlRequest.h +++ b/src/Common/XmlRequest.h @@ -20,6 +20,7 @@ #ifndef MAD_COMMON_XMLREQUEST_H_ #define MAD_COMMON_XMLREQUEST_H_ +#include "XmlPacket.h" #include "XmlRequestBase.h" #include "Exception.h" @@ -29,15 +30,15 @@ namespace Mad { namespace Common { -template<typename T = void> class XmlRequest : public XmlRequestBase { +class XmlRequest : public XmlRequestBase { private: - sigc::signal<void,const XmlRequest<T>&> finished; + sigc::signal<void,const XmlRequest&> finished; - std::auto_ptr<T> res; + std::auto_ptr<XmlPacket> res; Exception exp; public: - typedef sigc::slot<void,const XmlRequest<T>&> slot_type; + typedef sigc::slot<void,const XmlRequest&> slot_type; protected: XmlRequest(slot_type slot) : exp(Exception::NOT_FINISHED) { @@ -45,12 +46,12 @@ template<typename T = void> class XmlRequest : public XmlRequestBase { finished.connect(sigc::hide(signalFinished().make_slot())); } - void finish(std::auto_ptr<T> result) {res = result; finished(*this);} - void finish(const T& result) {res.reset(new T(result)); finished(*this);} + void finish(std::auto_ptr<XmlPacket> result) {res = result; finished(*this);} + void finish(const XmlPacket& result) {res.reset(new XmlPacket(result)); finished(*this);} void finishWithError(const Exception &e) {exp = e; finished(*this);} public: - const T& getResult() const throw(Exception) { + const XmlPacket& getResult() const throw(Exception) { if(res.get()) return *res; @@ -58,34 +59,6 @@ template<typename T = void> class XmlRequest : public XmlRequestBase { } }; -template<> class XmlRequest<void> : public XmlRequestBase { - private: - sigc::signal<void,const XmlRequest<void>&> finished; - - bool isFinished; - Exception exp; - - public: - typedef sigc::slot<void,const XmlRequest<void>&> slot_type; - - protected: - XmlRequest(slot_type slot) : isFinished(false), exp(Exception::NOT_FINISHED) { - finished.connect(slot); - finished.connect(sigc::hide(signalFinished().make_slot())); - } - - void finish() {isFinished = true; finished(*this);} - void finishWithError(const Exception &e) {exp = e; finished(*this);} - - public: - void getResult() const throw(Exception) { - if(isFinished) - return; - - throw exp; - } -}; - } } diff --git a/src/Common/XmlRequestHandler.h b/src/Common/XmlRequestHandler.h index e04e5b9..30438b7 100644 --- a/src/Common/XmlRequestHandler.h +++ b/src/Common/XmlRequestHandler.h @@ -21,6 +21,7 @@ #define MAD_COMMON_XMLREQUESTHANDLER_H_ #include <sigc++/signal.h> +#include <stdint.h> namespace Mad { @@ -46,7 +47,7 @@ class XmlRequestHandler { sigc::signal<void> signalFinished() {return finished;} - virtual void handlePacket(Net::Connection *connection, const XmlPacket &packet) = 0; + virtual void handlePacket(Net::Connection *connection, uint16_t requestId, const XmlPacket &packet) = 0; public: virtual ~XmlRequestHandler() {} diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index fde7e62..6362314 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -136,16 +136,17 @@ void ConnectionManager::doInit() { Net::Connection::init(); Common::RequestManager::get()->registerPacketType<Common::RequestHandlers::FSInfoRequestHandler>(Net::Packet::FS_INFO); - Common::RequestManager::get()->registerPacketType<Common::RequestHandlers::StatusRequestHandler>(Net::Packet::STATUS); Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonCommandRequestHandler>(Net::Packet::DAEMON_COMMAND_REBOOT); Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonCommandRequestHandler>(Net::Packet::DAEMON_COMMAND_SHUTDOWN); Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonFSInfoRequestHandler>(Net::Packet::DAEMON_FS_INFO); Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonListRequestHandler>(Net::Packet::LIST_DAEMONS); - Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonStatusRequestHandler>(Net::Packet::DAEMON_STATUS); Common::RequestManager::get()->registerPacketType<RequestHandlers::GSSAPIAuthRequestHandler>(Net::Packet::GSSAPI_AUTH); Common::RequestManager::get()->registerPacketType<RequestHandlers::IdentifyRequestHandler>(Net::Packet::IDENTIFY); Common::RequestManager::get()->registerPacketType<RequestHandlers::LogRequestHandler>(Net::Packet::LOG); - Common::RequestManager::get()->registerPacketType<RequestHandlers::UserListRequestHandler>(Net::Packet::USERS_LIST); + + Common::RequestManager::get()->registerPacketType<Common::RequestHandlers::StatusRequestHandler>("GetStatus"); + Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonStatusRequestHandler>("GetDaemonStatus"); + Common::RequestManager::get()->registerPacketType<RequestHandlers::UserListRequestHandler>("ListUsers"); } void ConnectionManager::doDeinit() { @@ -156,16 +157,18 @@ void ConnectionManager::doDeinit() { delete *con; Common::RequestManager::get()->unregisterPacketType(Net::Packet::FS_INFO); - Common::RequestManager::get()->unregisterPacketType(Net::Packet::STATUS); Common::RequestManager::get()->unregisterPacketType(Net::Packet::DAEMON_COMMAND_REBOOT); Common::RequestManager::get()->unregisterPacketType(Net::Packet::DAEMON_COMMAND_SHUTDOWN); Common::RequestManager::get()->unregisterPacketType(Net::Packet::DAEMON_FS_INFO); Common::RequestManager::get()->unregisterPacketType(Net::Packet::LIST_DAEMONS); - Common::RequestManager::get()->unregisterPacketType(Net::Packet::DAEMON_STATUS); Common::RequestManager::get()->unregisterPacketType(Net::Packet::GSSAPI_AUTH); Common::RequestManager::get()->unregisterPacketType(Net::Packet::IDENTIFY); Common::RequestManager::get()->unregisterPacketType(Net::Packet::LOG); + Common::RequestManager::get()->unregisterPacketType("GetStatus"); + Common::RequestManager::get()->unregisterPacketType("GetDaemonStatus"); + Common::RequestManager::get()->unregisterPacketType("ListUsers"); + Net::Connection::deinit(); } diff --git a/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp b/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp index 80b61ba..a1dfdee 100644 --- a/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp +++ b/src/Core/RequestHandlers/DaemonStatusRequestHandler.cpp @@ -21,18 +21,21 @@ #include "../ConnectionManager.h" #include <Common/Logger.h> #include <Common/Requests/StatusRequest.h> -#include <Net/Packets/ErrorPacket.h> -#include <Net/Packets/HostStatusPacket.h> namespace Mad { namespace Core { namespace RequestHandlers { -void DaemonStatusRequestHandler::handlePacket(Net::Connection *connection, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::DAEMON_STATUS) { +void DaemonStatusRequestHandler::handlePacket(Net::Connection *connection, uint16_t requestId, const Common::XmlPacket &packet) { + if(packet.getType() != "GetDaemonStatus") { Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); - connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Common::Exception(Common::Exception::UNEXPECTED_PACKET))); + + Common::XmlPacket ret; + ret.setType("Error"); + ret.add("ErrorCode", Common::Exception::UNEXPECTED_PACKET); + + con->send(ret.encode(requestId)); signalFinished().emit(); return; @@ -41,26 +44,40 @@ void DaemonStatusRequestHandler::handlePacket(Net::Connection *connection, const // TODO Require authentication con = connection; - requestId = packet.getRequestId(); + rid = requestId; - std::string daemonName((char*)packet.getData(), packet.getLength()); + std::string daemonName = packet["daemonName"]; try { Net::Connection *daemonCon = ConnectionManager::get()->getDaemonConnection(daemonName); - Common::RequestManager::get()->sendRequest(daemonCon, std::auto_ptr<Common::RequestBase>(new Common::Requests::StatusRequest(sigc::mem_fun(this, &DaemonStatusRequestHandler::requestFinished)))); + Common::RequestManager::get()->sendRequest(daemonCon, std::auto_ptr<Common::XmlRequestBase>(new Common::Requests::StatusRequest(sigc::mem_fun(this, &DaemonStatusRequestHandler::requestFinished)))); } catch(Common::Exception &e) { - connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), e)); + Common::XmlPacket ret; + ret.setType("Error"); + ret.add("ErrorCode", e.getErrorCode()); + ret.add("SubCode", e.getSubCode()); + ret.add("SubSubCode", e.getSubSubCode()); + ret.add("Where", e.getWhere()); + + con->send(ret.encode(rid)); } } -void DaemonStatusRequestHandler::requestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request) { +void DaemonStatusRequestHandler::requestFinished(const Common::XmlRequest &request) { try { - const Net::Packet &packet = request.getResult(); - con->send(Net::Packet(Net::Packet::OK, requestId, packet.getData(), packet.getLength())); + const Common::XmlPacket &packet = request.getResult(); + con->send(packet.encode(rid)); } catch(Common::Exception &e) { - con->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, requestId, e)); + Common::XmlPacket ret; + ret.setType("Error"); + ret.add("ErrorCode", e.getErrorCode()); + ret.add("SubCode", e.getSubCode()); + ret.add("SubSubCode", e.getSubSubCode()); + ret.add("Where", e.getWhere()); + + con->send(ret.encode(rid)); } signalFinished().emit(); diff --git a/src/Core/RequestHandlers/DaemonStatusRequestHandler.h b/src/Core/RequestHandlers/DaemonStatusRequestHandler.h index 36fabc0..30b2ae1 100644 --- a/src/Core/RequestHandlers/DaemonStatusRequestHandler.h +++ b/src/Core/RequestHandlers/DaemonStatusRequestHandler.h @@ -20,8 +20,8 @@ #ifndef MAD_CORE_REQUESTHANDLERS_DAEMONSTATUSREQUESTHANDLER_H_ #define MAD_CORE_REQUESTHANDLERS_DAEMONSTATUSREQUESTHANDLER_H_ -#include <Common/RequestHandler.h> -#include <Common/Request.h> +#include <Common/XmlRequestHandler.h> +#include <Common/XmlRequest.h> #include <stdint.h> namespace Mad { @@ -35,15 +35,15 @@ class HostStatusPacket; namespace Core { namespace RequestHandlers { -class DaemonStatusRequestHandler : public Common::RequestHandler { +class DaemonStatusRequestHandler : public Common::XmlRequestHandler { private: Net::Connection *con; - uint16_t requestId; + uint16_t rid; - void requestFinished(const Common::Request<Net::Packets::HostStatusPacket> &request); + void requestFinished(const Common::XmlRequest &request); protected: - virtual void handlePacket(Net::Connection *connection, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *connection, uint16_t requestId, const Common::XmlPacket &packet); public: DaemonStatusRequestHandler() {} diff --git a/src/Core/RequestHandlers/UserListRequestHandler.cpp b/src/Core/RequestHandlers/UserListRequestHandler.cpp index 01c2eb3..176fde9 100644 --- a/src/Core/RequestHandlers/UserListRequestHandler.cpp +++ b/src/Core/RequestHandlers/UserListRequestHandler.cpp @@ -19,19 +19,24 @@ #include "UserListRequestHandler.h" #include "../UserBackend.h" +#include <Common/Exception.h> #include <Common/Logger.h> +#include <Common/XmlPacket.h> #include <Net/Connection.h> -#include <Net/Packets/ErrorPacket.h> -#include <Net/Packets/UserListPacket.h> namespace Mad { namespace Core { namespace RequestHandlers { -void UserListRequestHandler::handlePacket(Net::Connection *con, const Net::Packet &packet) { - if(packet.getType() != Net::Packet::USERS_LIST) { +void UserListRequestHandler::handlePacket(Net::Connection *con, uint16_t requestId, const Common::XmlPacket &packet) { + if(packet.getType() != "ListUsers") { Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); - connection->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Common::Exception(Common::Exception::UNEXPECTED_PACKET))); + + Common::XmlPacket ret; + ret.setType("Error"); + ret.add("ErrorCode", Common::Exception::UNEXPECTED_PACKET); + + con->send(ret.encode(requestId)); signalFinished().emit(); return; @@ -40,21 +45,34 @@ void UserListRequestHandler::handlePacket(Net::Connection *con, const Net::Packe // TODO Require authentication connection = con; - requestId = packet.getRequestId(); + rid = requestId; if(!UserBackend::getUserList(sigc::mem_fun(this, &UserListRequestHandler::userListHandler))) { - con->send(Net::Packets::ErrorPacket(Net::Packet::ERROR, packet.getRequestId(), Common::Exception(Common::Exception::NOT_IMPLEMENTED))); + Common::XmlPacket ret; + ret.setType("Error"); + ret.add("ErrorCode", Common::Exception::NOT_IMPLEMENTED); + + con->send(ret.encode(requestId)); signalFinished().emit(); } } void UserListRequestHandler::userListHandler(const std::map<unsigned long, Common::UserInfo> &info) { - std::vector<Common::UserInfo> userList; + Common::XmlPacket ret; + ret.setType("OK"); + ret.addList("users"); - for(std::map<unsigned long, Common::UserInfo>::const_iterator user = info.begin(); user != info.end(); ++user) - userList.push_back(user->second); + for(std::map<unsigned long, Common::UserInfo>::const_iterator user = info.begin(); user != info.end(); ++user) { + ret["users"].addEntry(); + Common::XmlPacket::Entry &entry = ret["users"].back(); + + entry.add("uid", user->second.getUid()); + entry.add("gid", user->second.getGid()); + entry.add("username", user->second.getUsername()); + entry.add("fullName", user->second.getFullName()); + } - connection->send(Net::Packets::UserListPacket(Net::Packet::OK, requestId, userList)); + connection->send(ret.encode(rid)); signalFinished().emit(); } diff --git a/src/Core/RequestHandlers/UserListRequestHandler.h b/src/Core/RequestHandlers/UserListRequestHandler.h index 92eddb7..7739809 100644 --- a/src/Core/RequestHandlers/UserListRequestHandler.h +++ b/src/Core/RequestHandlers/UserListRequestHandler.h @@ -20,7 +20,7 @@ #ifndef MAD_CORE_REQUESTHANDLERS_USERLISTREQUESTHANDLER_H_ #define MAD_CORE_REQUESTHANDLERS_USERLISTREQUESTHANDLER_H_ -#include <Common/RequestHandler.h> +#include <Common/XmlRequestHandler.h> #include <Common/UserInfo.h> #include <map> @@ -30,15 +30,15 @@ namespace Mad { namespace Core { namespace RequestHandlers { -class UserListRequestHandler : public Common::RequestHandler { +class UserListRequestHandler : public Common::XmlRequestHandler { private: Net::Connection *connection; - uint16_t requestId; + uint16_t rid; void userListHandler(const std::map<unsigned long, Common::UserInfo> &info); protected: - virtual void handlePacket(Net::Connection *con, const Net::Packet &packet); + virtual void handlePacket(Net::Connection *con, uint16_t requestId, const Common::XmlPacket &packet); public: UserListRequestHandler() {} diff --git a/src/Net/Packet.h b/src/Net/Packet.h index 7baa454..37df13a 100644 --- a/src/Net/Packet.h +++ b/src/Net/Packet.h @@ -34,11 +34,10 @@ class Packet { OK = 0x0000, ERROR = 0x0001, DISCONNECT = 0x0002, LOG = 0x0003, GSSAPI_AUTH = 0x0010, IDENTIFY = 0x0011, LIST_DAEMONS = 0x0020, - STATUS = 0x0030, DAEMON_STATUS = 0x0031, FS_INFO = 0x0032, DAEMON_FS_INFO = 0x0033, + FS_INFO = 0x0032, DAEMON_FS_INFO = 0x0033, COMMAND_SHUTDOWN = 0x0040, COMMAND_REBOOT = 0x0041, DAEMON_COMMAND_SHUTDOWN = 0x0050, DAEMON_COMMAND_REBOOT = 0x0051, DAEMON_STATE_UPDATE = 0x0060, - USERS_LIST = 0x0070, XML = 0xFFFF }; diff --git a/src/Net/Packets/HostStatusPacket.cpp b/src/Net/Packets/HostStatusPacket.cpp deleted file mode 100644 index f998c2c..0000000 --- a/src/Net/Packets/HostStatusPacket.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* - * HostStatusPacket.cpp - * - * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include "HostStatusPacket.h" - -#include <cstdio> -#include <cstring> -#include <string> - - -namespace Mad { -namespace Net { -namespace Packets { - -HostStatusPacket::HostStatusPacket(Type type, uint16_t requestId, uint32_t uptime, uint32_t idleTime, - uint32_t totalMem, uint32_t freeMem, uint32_t totalSwap, uint32_t freeSwap, - uint32_t currentLoad, uint32_t nProcesses, float loadAvg1val, float loadAvg5val, float loadAvg15val) -: Packet(type, requestId), loadAvg1(loadAvg1val), loadAvg5(loadAvg5val), loadAvg15(loadAvg15val) -{ - char buf[20]; - std::snprintf(buf, sizeof(buf), "%.2f %.2f %.2f", loadAvg1, loadAvg5, loadAvg15); - - setLength(sizeof(CoreStatusData) + strlen(buf)); - coreStatusData = (CoreStatusData*)&rawData->data; - - coreStatusData->uptime = htonl(uptime); - coreStatusData->idleTime = htonl(idleTime); - - coreStatusData->totalMem = htonl(totalMem); - coreStatusData->freeMem = htonl(freeMem); - coreStatusData->totalSwap = htonl(totalSwap); - coreStatusData->freeSwap = htonl(freeSwap); - - coreStatusData->currentLoad = htonl(currentLoad); - coreStatusData->nProcesses = htonl(nProcesses); - - std::memcpy(coreStatusData->charData, buf, strlen(buf)); -} - -HostStatusPacket& HostStatusPacket::operator=(const Packet &p) { - Packet::operator=(p); - if(getLength() < sizeof(CoreStatusData)) - setLength(sizeof(CoreStatusData)); - coreStatusData = (CoreStatusData*)&rawData->data; - - parsePacket(); - - return *this; -} - -void HostStatusPacket::parsePacket() { - coreStatusData = (CoreStatusData*)&rawData->data; - - std::sscanf(std::string((char*)coreStatusData->charData, getLength()-sizeof(coreStatusData)).c_str(), "%f %f %f", &loadAvg1, &loadAvg5, &loadAvg15); -} - -} -} -} diff --git a/src/Net/Packets/HostStatusPacket.h b/src/Net/Packets/HostStatusPacket.h deleted file mode 100644 index fa0910c..0000000 --- a/src/Net/Packets/HostStatusPacket.h +++ /dev/null @@ -1,123 +0,0 @@ -/* - * HostStatusPacket.h - * - * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#ifndef MAD_NET_PACKETS_HOSTSTATUSPACKET_H_ -#define MAD_NET_PACKETS_HOSTSTATUSPACKET_H_ - -#include "../Packet.h" - -namespace Mad { -namespace Net { -namespace Packets { - -class HostStatusPacket : public Packet { - protected: - struct CoreStatusData { - uint32_t uptime; - uint32_t idleTime; - - uint32_t totalMem; - uint32_t freeMem; - uint32_t totalSwap; - uint32_t freeSwap; - - uint32_t currentLoad; - uint32_t nProcesses; - - uint8_t charData[0]; - }; - - CoreStatusData *coreStatusData; - - float loadAvg1, loadAvg5, loadAvg15; - - void parsePacket(); - - public: - HostStatusPacket(Type type, uint16_t requestId, uint32_t uptime = 0, uint32_t idleTime = 0, - uint32_t totalMem = 0, uint32_t freeMem = 0, uint32_t totalSwap = 0, uint32_t freeSwap = 0, - uint32_t currentLoad = 0, uint32_t nProcesses = 0, float loadAvg1val = 0, float loadAvg5val = 0, float loadAvg15val = 0); - - HostStatusPacket(const Packet &p) : Packet(p) { - if(getLength() < sizeof(CoreStatusData)) - setLength(sizeof(CoreStatusData)); - - parsePacket(); - } - - HostStatusPacket(const HostStatusPacket &p) : Packet(p) { - parsePacket(); - } - - HostStatusPacket& operator=(const Packet &p); - - HostStatusPacket& operator=(const HostStatusPacket &p) { - return (*this = (Packet)p); - } - - uint32_t getUptime() const { - return ntohl(coreStatusData->uptime); - } - - uint32_t getIdleTime() const { - return ntohl(coreStatusData->idleTime); - } - - uint32_t getTotalMem() const { - return ntohl(coreStatusData->totalMem); - } - - uint32_t getFreeMem() const { - return ntohl(coreStatusData->freeMem); - } - - uint32_t getTotalSwap() const { - return ntohl(coreStatusData->totalSwap); - } - - uint32_t getFreeSwap() const { - return ntohl(coreStatusData->freeSwap); - } - - uint32_t getCurrentLoad() const { - return ntohl(coreStatusData->currentLoad); - } - - uint32_t getProcessNumber() const { - return ntohl(coreStatusData->nProcesses); - } - - float getLoadAverage1() const { - return loadAvg1; - } - - float getLoadAverage5() const { - return loadAvg5; - } - - float getLoadAverage15() const { - return loadAvg15; - } -}; - -} -} -} - -#endif /* MAD_NET_PACKETS_HOSTSTATUSPACKET_H_ */ diff --git a/src/Net/Packets/Makefile.am b/src/Net/Packets/Makefile.am index b8ebfc0..6f950c9 100644 --- a/src/Net/Packets/Makefile.am +++ b/src/Net/Packets/Makefile.am @@ -1,4 +1,4 @@ noinst_LTLIBRARIES = libpackets.la -libpackets_la_SOURCES = ErrorPacket.cpp FSInfoPacket.cpp HostListPacket.cpp HostStatePacket.cpp HostStatusPacket.cpp LogPacket.cpp UserListPacket.cpp +libpackets_la_SOURCES = ErrorPacket.cpp FSInfoPacket.cpp HostListPacket.cpp HostStatePacket.cpp LogPacket.cpp -noinst_HEADERS = ErrorPacket.h FSInfoPacket.h HostListPacket.h HostStatePacket.h HostStatusPacket.h LogPacket.h UserListPacket.h +noinst_HEADERS = ErrorPacket.h FSInfoPacket.h HostListPacket.h HostStatePacket.h LogPacket.h diff --git a/src/Net/Packets/Makefile.in b/src/Net/Packets/Makefile.in index 47641fb..2fb4fca 100644 --- a/src/Net/Packets/Makefile.in +++ b/src/Net/Packets/Makefile.in @@ -51,8 +51,7 @@ CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libpackets_la_LIBADD = am_libpackets_la_OBJECTS = ErrorPacket.lo FSInfoPacket.lo \ - HostListPacket.lo HostStatePacket.lo HostStatusPacket.lo \ - LogPacket.lo UserListPacket.lo + HostListPacket.lo HostStatePacket.lo LogPacket.lo libpackets_la_OBJECTS = $(am_libpackets_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) depcomp = $(SHELL) $(top_srcdir)/config/depcomp @@ -218,8 +217,8 @@ top_build_prefix = @top_build_prefix@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = libpackets.la -libpackets_la_SOURCES = ErrorPacket.cpp FSInfoPacket.cpp HostListPacket.cpp HostStatePacket.cpp HostStatusPacket.cpp LogPacket.cpp UserListPacket.cpp -noinst_HEADERS = ErrorPacket.h FSInfoPacket.h HostListPacket.h HostStatePacket.h HostStatusPacket.h LogPacket.h UserListPacket.h +libpackets_la_SOURCES = ErrorPacket.cpp FSInfoPacket.cpp HostListPacket.cpp HostStatePacket.cpp LogPacket.cpp +noinst_HEADERS = ErrorPacket.h FSInfoPacket.h HostListPacket.h HostStatePacket.h LogPacket.h all: all-am .SUFFIXES: @@ -275,9 +274,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FSInfoPacket.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HostListPacket.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HostStatePacket.Plo@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HostStatusPacket.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/LogPacket.Plo@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UserListPacket.Plo@am__quote@ .cpp.o: @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< diff --git a/src/Net/Packets/UserListPacket.cpp b/src/Net/Packets/UserListPacket.cpp deleted file mode 100644 index d7415c3..0000000 --- a/src/Net/Packets/UserListPacket.cpp +++ /dev/null @@ -1,85 +0,0 @@ -/* - * UserListPacket.cpp - * - * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de> - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include "UserListPacket.h" -#include <cstring> -#include <sstream> - -namespace Mad { -namespace Net { -namespace Packets { - -void UserListPacket::assemblePacket() { - std::string str; - - for(std::vector<Common::UserInfo>::iterator user = userList.begin(); user != userList.end(); ++user) - str += user->getUsername() + "\n" + user->getFullName() + "\n"; - - setLength(sizeof(uint32_t) + sizeof(UserData)*userList.size() + str.length()); - - nUsers = (uint32_t*)rawData->data; - userData = (UserData*)(rawData->data + sizeof(uint32_t)); - charData = (char*)(rawData->data + sizeof(uint32_t) + sizeof(UserData)*userList.size()); - - std::memcpy(charData, str.c_str(), str.length()); - - *nUsers = htonl(userList.size()); - - for(size_t i = 0; i < userList.size(); ++i) { - userData[i].uid = htonl((uint32_t)userList[i].getUid()); - userData[i].gid = htonl((uint32_t)userList[i].getGid()); - } -} - -void UserListPacket::parsePacket() { - userList.clear(); - - if(getLength() < sizeof(uint16_t)) - return; - - nUsers = (uint32_t*)rawData->data; - userList.resize(ntohl(*nUsers)); - - if(getLength() < sizeof(uint32_t) + sizeof(UserData)*userList.size()) - setLength(sizeof(uint32_t) + sizeof(UserData)*userList.size()); - - nUsers = (uint32_t*)rawData->data; - userData = (UserData*)(rawData->data + sizeof(uint32_t)); - charData = (char*)(rawData->data + sizeof(uint32_t) + sizeof(UserData)*userList.size()); - - std::istringstream stream(std::string(charData, getLength() - (sizeof(uint32_t)+sizeof(UserData)*userList.size()))); - - for(size_t i = 0; i < userList.size(); ++i) { - userList[i].setUid(ntohl(userData[i].uid)); - userList[i].setGid(ntohl(userData[i].gid)); - - if(!stream.eof()) { - std::string str; - - std::getline(stream, str); - userList[i].setUsername(str); - std::getline(stream, str); - userList[i].setFullName(str); - } - } -} - -} -} -} diff --git a/src/Net/Packets/UserListPacket.h b/src/Net/Packets/UserListPacket.h deleted file mode 100644 index 364a36d..0000000 --- a/src/Net/Packets/UserListPacket.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * UserListPacket.h - * - * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de> - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#ifndef MAD_NET_PACKETS_USERLISTPACKET_H_ -#define MAD_NET_PACKETS_USERLISTPACKET_H_ - -#include "../Packet.h" -#include <Common/UserInfo.h> -#include <vector> - -namespace Mad { -namespace Net { -namespace Packets { - -class UserListPacket : public Packet { - protected: - struct UserData { - uint32_t uid; - uint32_t gid; - }; - - uint32_t *nUsers; - UserData *userData; - char *charData; - - std::vector<Common::UserInfo> userList; - - void assemblePacket(); - void parsePacket(); - - public: - UserListPacket(Type type, uint16_t requestId, const std::vector<Common::UserInfo> &users) : Packet(type, requestId), userList(users) { - assemblePacket(); - } - - UserListPacket(const Packet &p) : Packet(p) { - parsePacket(); - } - - UserListPacket& operator=(const Packet &p) { - Packet::operator=(p); - parsePacket(); - - return *this; - } - - UserListPacket& operator=(const UserListPacket &p) { - Packet::operator=(p); - parsePacket(); - - return *this; - } - - const std::vector<Common::UserInfo>& getUserInfo() const { - return userList; - } -}; - -} -} -} - -#endif /* MAD_NET_PACKETS_USERLISTPACKET_H_ */ diff --git a/src/mad.cpp b/src/mad.cpp index 74ddd7a..40d9ead 100644 --- a/src/mad.cpp +++ b/src/mad.cpp @@ -51,10 +51,11 @@ int main() { Common::ConfigManager::get()->finish(); Common::RequestManager::get()->registerPacketType<Common::RequestHandlers::FSInfoRequestHandler>(Net::Packet::FS_INFO); - Common::RequestManager::get()->registerPacketType<Common::RequestHandlers::StatusRequestHandler>(Net::Packet::STATUS); Common::RequestManager::get()->registerPacketType<Daemon::RequestHandlers::CommandRequestHandler>(Net::Packet::COMMAND_REBOOT); Common::RequestManager::get()->registerPacketType<Daemon::RequestHandlers::CommandRequestHandler>(Net::Packet::COMMAND_SHUTDOWN); + Common::RequestManager::get()->registerPacketType<Common::RequestHandlers::StatusRequestHandler>("GetStatus"); + Net::ClientConnection *connection = new Net::ClientConnection; try { @@ -88,10 +89,11 @@ int main() { delete connection; Common::RequestManager::get()->unregisterPacketType(Net::Packet::FS_INFO); - Common::RequestManager::get()->unregisterPacketType(Net::Packet::STATUS); Common::RequestManager::get()->unregisterPacketType(Net::Packet::COMMAND_REBOOT); Common::RequestManager::get()->unregisterPacketType(Net::Packet::COMMAND_SHUTDOWN); + Common::RequestManager::get()->unregisterPacketType("GetStatus"); + Common::Initializable::deinit(); Net::Connection::deinit(); |