From dcf66f3cc133bf600190b844ab9c0da764e5e82b Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 1 Jun 2009 23:03:29 +0200 Subject: ?berarbeitung der Request-Klasse begonnen --- src/Client/CommandManager.cpp | 22 +++---- src/Client/CommandManager.h | 26 ++++---- src/Client/CommandParser.cpp | 36 +++++------ src/Client/InformationManager.cpp | 4 +- src/Client/InformationManager.h | 8 +-- src/Common/Connection.h | 4 +- src/Common/Request.cpp | 2 + src/Common/Request.h | 70 +++++++++++++++++++--- src/Common/RequestHandler.h | 15 +++-- src/Common/RequestManager.cpp | 8 ++- src/Common/RequestManager.h | 46 +++++++------- src/Daemon/Backends/NetworkLogger.h | 4 +- src/Net/Connection.cpp | 8 +-- src/Net/Connection.h | 12 ++-- src/Net/Listener.cpp | 5 ++ src/Net/Listener.h | 5 +- src/Net/Signals/Signal0.h | 9 ++- src/Net/Signals/Signal1.h | 12 ++-- src/Net/Signals/Signal2.h | 12 ++-- src/Net/Signals/SignalBase.h | 10 ++-- src/Server/ConnectionManager.cpp | 2 +- .../DaemonCommandRequestHandler.cpp | 6 +- .../RequestHandlers/DaemonCommandRequestHandler.h | 2 +- .../RequestHandlers/DaemonFSInfoRequestHandler.cpp | 4 +- .../RequestHandlers/DaemonFSInfoRequestHandler.h | 2 +- .../RequestHandlers/DaemonStatusRequestHandler.cpp | 4 +- .../RequestHandlers/DaemonStatusRequestHandler.h | 2 +- src/mad.cpp | 4 +- src/madc.cpp | 2 +- 29 files changed, 207 insertions(+), 139 deletions(-) diff --git a/src/Client/CommandManager.cpp b/src/Client/CommandManager.cpp index 603f679..6c643f2 100644 --- a/src/Client/CommandManager.cpp +++ b/src/Client/CommandManager.cpp @@ -136,7 +136,7 @@ void CommandManager::printHostStatus(const Common::XmlPacket &packet) { } -void CommandManager::daemonCommandRequestFinished(const Common::Request &request) { +void CommandManager::daemonCommandRequestFinished(Common::Request &request) { try { request.getResult(); } @@ -147,7 +147,7 @@ void CommandManager::daemonCommandRequestFinished(const Common::Request &request requestFinished(); } -void CommandManager::daemonFSInfoRequestFinished(const Common::Request &request) { +void CommandManager::daemonFSInfoRequestFinished(Common::Request &request) { try { const Common::XmlPacket &packet = request.getResult(); std::cout << "Host file system usage:" << std::endl; @@ -160,7 +160,7 @@ void CommandManager::daemonFSInfoRequestFinished(const Common::Request &request) requestFinished(); } -void CommandManager::daemonStatusRequestFinished(const Common::Request &request) { +void CommandManager::daemonStatusRequestFinished(Common::Request &request) { try { const Common::XmlPacket &packet = request.getResult(); std::cout << "Host status:" << std::endl; @@ -173,7 +173,7 @@ void CommandManager::daemonStatusRequestFinished(const Common::Request &request) requestFinished(); } -void CommandManager::disconnectRequestFinished(const Common::Request &request) { +void CommandManager::disconnectRequestFinished(Common::Request &request) { try { request.getResult(); disconnect = true; @@ -185,7 +185,7 @@ void CommandManager::disconnectRequestFinished(const Common::Request &request) { requestFinished(); } -void CommandManager::fsInfoRequestFinished(const Common::Request &request) { +void CommandManager::fsInfoRequestFinished(Common::Request &request) { try { const Common::XmlPacket &packet = request.getResult(); std::cout << "Server file system usage:" << std::endl; @@ -198,7 +198,7 @@ void CommandManager::fsInfoRequestFinished(const Common::Request &request) { requestFinished(); } -void CommandManager::statusRequestFinished(const Common::Request &request) { +void CommandManager::statusRequestFinished(Common::Request &request) { try { const Common::XmlPacket &packet = request.getResult(); std::cout << "Server status:" << std::endl; @@ -211,7 +211,7 @@ void CommandManager::statusRequestFinished(const Common::Request &request) { requestFinished(); } -void CommandManager::userInfoRequestFinished(const Common::Request &request) { +void CommandManager::userInfoRequestFinished(Common::Request &request) { try { const Common::XmlPacket &packet = request.getResult(); @@ -225,7 +225,7 @@ void CommandManager::userInfoRequestFinished(const Common::Request &request) { requestFinished(); } -void CommandManager::userListRequestFinished(const Common::Request &request) { +void CommandManager::userListRequestFinished(Common::Request &request) { try { const Common::XmlPacket &packet = request.getResult(); @@ -251,7 +251,7 @@ void CommandManager::userListRequestFinished(const Common::Request &request) { requestFinished(); } -void CommandManager::userGroupListRequestFinished(const Common::Request &request) { +void CommandManager::userGroupListRequestFinished(Common::Request &request) { try { const Common::XmlPacket &packet = request.getResult(); @@ -278,7 +278,7 @@ void CommandManager::userGroupListRequestFinished(const Common::Request &request requestFinished(); } -void CommandManager::groupListRequestFinished(const Common::Request &request) { +void CommandManager::groupListRequestFinished(Common::Request &request) { try { const Common::XmlPacket &packet = request.getResult(); @@ -304,7 +304,7 @@ void CommandManager::groupListRequestFinished(const Common::Request &request) { requestFinished(); } -void CommandManager::groupUserListRequestFinished(const Common::Request &request) { +void CommandManager::groupUserListRequestFinished(Common::Request &request) { try { const Common::XmlPacket &packet = request.getResult(); diff --git a/src/Client/CommandManager.h b/src/Client/CommandManager.h index 3ab5d9c..3c51bd3 100644 --- a/src/Client/CommandManager.h +++ b/src/Client/CommandManager.h @@ -39,7 +39,7 @@ class CommandManager { unsigned int activeRequests; - Net::Signals::Signal0 finished; + Net::Signals::Signal0 finished; bool disconnect; @@ -52,17 +52,17 @@ class CommandManager { void printFSInfo(const Common::XmlPacket &packet); void printHostStatus(const Common::XmlPacket &packet); - void daemonCommandRequestFinished(const Common::Request &request); - void daemonFSInfoRequestFinished(const Common::Request &request); - void daemonStatusRequestFinished(const Common::Request &request); - void disconnectRequestFinished(const Common::Request &request); - void fsInfoRequestFinished(const Common::Request &request); - void statusRequestFinished(const Common::Request &request); - void userInfoRequestFinished(const Common::Request &request); - void userListRequestFinished(const Common::Request &request); - void userGroupListRequestFinished(const Common::Request &request); - void groupListRequestFinished(const Common::Request &request); - void groupUserListRequestFinished(const Common::Request &request); + void daemonCommandRequestFinished(Common::Request &request); + void daemonFSInfoRequestFinished(Common::Request &request); + void daemonStatusRequestFinished(Common::Request &request); + void disconnectRequestFinished(Common::Request &request); + void fsInfoRequestFinished(Common::Request &request); + void statusRequestFinished(Common::Request &request); + void userInfoRequestFinished(Common::Request &request); + void userListRequestFinished(Common::Request &request); + void userGroupListRequestFinished(Common::Request &request); + void groupListRequestFinished(Common::Request &request); + void groupUserListRequestFinished(Common::Request &request); CommandManager() : activeRequests(0), disconnect(false) {} @@ -74,7 +74,7 @@ class CommandManager { bool requestsActive() {return (activeRequests > 0);} bool willDisconnect() {return disconnect;} - Net::Signals::Connection connectSignalFinished(const Net::Signals::Signal0::slot_type &slot) { + Net::Signals::Connection connectSignalFinished(const Net::Signals::Signal0::slot_type &slot) { return finished.connect(slot); } }; diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 1c0403d..4c395b7 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -118,9 +118,9 @@ std::map CommandParser::parseHostList(const std:: void CommandParser::fsinfoCommand(const std::vector &args) { if(args.size() == 1) - Common::RequestManager::get()->sendRequest(connection, boost::bind(&CommandManager::fsInfoRequestFinished, CommandManager::get(), _1)); + Common::RequestManager::get()->sendRequest0(connection, boost::bind(&CommandManager::fsInfoRequestFinished, CommandManager::get(), _1)); else if(args.size() == 2) - Common::RequestManager::get()->sendRequest(connection, boost::bind(&CommandManager::daemonFSInfoRequestFinished, CommandManager::get(), _1), args[1]); + Common::RequestManager::get()->sendRequest1(connection, args[1], boost::bind(&CommandManager::daemonFSInfoRequestFinished, CommandManager::get(), _1)); else { Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); printUsage("fsinfo"); @@ -221,8 +221,8 @@ void CommandParser::rebootCommand(const std::vector &args) { std::map hosts = parseHostList(std::vector(args.begin()+1, args.end()), true); for(std::map::iterator host = hosts.begin(); host != hosts.end(); ++host) { - Common::RequestManager::get()->sendRequest(connection, - boost::bind(&CommandManager::daemonCommandRequestFinished, CommandManager::get(), _1), host->first, true + Common::RequestManager::get()->sendRequest2(connection, + host->first, true, boost::bind(&CommandManager::daemonCommandRequestFinished, CommandManager::get(), _1) ); ++CommandManager::get()->activeRequests; @@ -239,8 +239,8 @@ void CommandParser::shutdownCommand(const std::vector &args) { std::map hosts = parseHostList(std::vector(args.begin()+1, args.end()), true); for(std::map::iterator host = hosts.begin(); host != hosts.end(); ++host) { - Common::RequestManager::get()->sendRequest(connection, - boost::bind(&CommandManager::daemonCommandRequestFinished, CommandManager::get(), _1), host->first, false + Common::RequestManager::get()->sendRequest2(connection, + host->first, false, boost::bind(&CommandManager::daemonCommandRequestFinished, CommandManager::get(), _1) ); ++CommandManager::get()->activeRequests; @@ -249,11 +249,11 @@ void CommandParser::shutdownCommand(const std::vector &args) { void CommandParser::statusCommand(const std::vector &args) { if(args.size() == 1) - Common::RequestManager::get()->sendRequest(connection, + Common::RequestManager::get()->sendRequest0(connection, boost::bind(&CommandManager::statusRequestFinished, CommandManager::get(), _1)); else if(args.size() == 2) - Common::RequestManager::get()->sendRequest(connection, - boost::bind(&CommandManager::daemonStatusRequestFinished, CommandManager::get(), _1), args[1]); + Common::RequestManager::get()->sendRequest1(connection, + args[1], boost::bind(&CommandManager::daemonStatusRequestFinished, CommandManager::get(), _1)); else { Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); printUsage("status"); @@ -285,14 +285,14 @@ void CommandParser::userInfoCommand(const std::vector &args) { ++CommandManager::get()->activeRequests; - Common::RequestManager::get()->sendRequest(connection, - boost::bind(&CommandManager::userInfoRequestFinished, CommandManager::get(), _1), uid); + Common::RequestManager::get()->sendRequest1(connection, + uid, boost::bind(&CommandManager::userInfoRequestFinished, CommandManager::get(), _1)); } void CommandParser::listUsersCommand(const std::vector&) { ++CommandManager::get()->activeRequests; - Common::RequestManager::get()->sendRequest(connection, + Common::RequestManager::get()->sendRequest0(connection, boost::bind(&CommandManager::userListRequestFinished, CommandManager::get(), _1)); } @@ -318,14 +318,14 @@ void CommandParser::listUserGroupsCommand(const std::vector &args) ++CommandManager::get()->activeRequests; - Common::RequestManager::get()->sendRequest(connection, - boost::bind(&CommandManager::userGroupListRequestFinished, CommandManager::get(), _1), uid); + Common::RequestManager::get()->sendRequest1(connection, + uid, boost::bind(&CommandManager::userGroupListRequestFinished, CommandManager::get(), _1)); } void CommandParser::listGroupsCommand(const std::vector&) { ++CommandManager::get()->activeRequests; - Common::RequestManager::get()->sendRequest(connection, + Common::RequestManager::get()->sendRequest0(connection, boost::bind(&CommandManager::groupListRequestFinished, CommandManager::get(), _1)); } @@ -351,14 +351,14 @@ void CommandParser::listGroupUsersCommand(const std::vector &args) ++CommandManager::get()->activeRequests; - Common::RequestManager::get()->sendRequest(connection, - boost::bind(&CommandManager::groupUserListRequestFinished, CommandManager::get(), _1), gid); + Common::RequestManager::get()->sendRequest1(connection, + gid, boost::bind(&CommandManager::groupUserListRequestFinished, CommandManager::get(), _1)); } void CommandParser::exitCommand(const std::vector&) { ++CommandManager::get()->activeRequests; - Common::RequestManager::get()->sendRequest(connection, + Common::RequestManager::get()->sendRequest0(connection, boost::bind(&CommandManager::disconnectRequestFinished, CommandManager::get(), _1)); } diff --git a/src/Client/InformationManager.cpp b/src/Client/InformationManager.cpp index b878b94..78a0cf7 100644 --- a/src/Client/InformationManager.cpp +++ b/src/Client/InformationManager.cpp @@ -77,13 +77,13 @@ void InformationManager::updateDaemonList(Common::Connection *con) { if(updating) return; - Common::RequestManager::get()->sendRequest(con, + Common::RequestManager::get()->sendRequest0(con, boost::bind(&InformationManager::daemonListRequestFinished, this, _1)); updating = true; } -void InformationManager::daemonListRequestFinished(const Common::Request &request) { +void InformationManager::daemonListRequestFinished(Common::Request &request) { boost::lock_guard lock(mutex); try { diff --git a/src/Client/InformationManager.h b/src/Client/InformationManager.h index 2c39601..239b8d8 100644 --- a/src/Client/InformationManager.h +++ b/src/Client/InformationManager.h @@ -34,7 +34,7 @@ namespace Mad { namespace Client { -class InformationManager : public Common::Initializable { +class InformationManager : public Common::Initializable, private boost::noncopyable { private: class DaemonStateUpdateRequestHandler : public Common::RequestHandler { protected: @@ -54,13 +54,9 @@ class InformationManager : public Common::Initializable { bool updating; - // Prevent shallow copy - InformationManager(const InformationManager &o); - InformationManager& operator=(const InformationManager &o); - InformationManager() : updating(false) {} - void daemonListRequestFinished(const Common::Request &request); + void daemonListRequestFinished(Common::Request &request); protected: virtual void doInit(); diff --git a/src/Common/Connection.h b/src/Common/Connection.h index 2d74548..0dd2f2e 100644 --- a/src/Common/Connection.h +++ b/src/Common/Connection.h @@ -39,7 +39,7 @@ class Connection : private boost::noncopyable { private: bool authenticated; - Net::Signals::Signal2 signalReceive; + Net::Signals::Signal2 signalReceive; protected: Connection() : authenticated(0) {} @@ -53,7 +53,7 @@ class Connection : private boost::noncopyable { bool sendPacket(const XmlPacket &packet, uint16_t requestId); - Net::Signals::Connection connectSignalReceive(const Net::Signals::Signal2::slot_type &slot) { + Net::Signals::Connection connectSignalReceive(const Net::Signals::Signal2::slot_type &slot) { return signalReceive.connect(slot); } diff --git a/src/Common/Request.cpp b/src/Common/Request.cpp index 5f4c201..e27c1b9 100644 --- a/src/Common/Request.cpp +++ b/src/Common/Request.cpp @@ -22,6 +22,8 @@ namespace Mad { namespace Common { +Request::slot_type Request::empty_slot(&Request::doNothing); + void Request::handlePacket(const XmlPacket &packet) { if(packet.getType() == "Error") { finishWithError(Net::Exception(packet["Where"], packet["ErrorCode"], packet["SubCode"], packet["SubSubCode"])); diff --git a/src/Common/Request.h b/src/Common/Request.h index 6f72667..d351c2a 100644 --- a/src/Common/Request.h +++ b/src/Common/Request.h @@ -26,6 +26,9 @@ #include #include +#include +#include +#include namespace Mad { namespace Common { @@ -34,35 +37,88 @@ class Request : public RequestHandler { private: friend class RequestManager; - Net::Signals::Signal1 finished; + Net::Signals::Signal1 finished; std::auto_ptr res; Net::Exception exp; + boost::mutex mutex; + boost::condition_variable finishCond; + bool isFinished; + + static void doNothing(Request&) {} + public: - typedef Net::Signals::Signal1::slot_type slot_type; + typedef Net::Signals::Signal1::slot_type slot_type; protected: + static slot_type empty_slot; + Request(Connection *connection, uint16_t requestId, slot_type slot) - : RequestHandler(connection, requestId), exp(Net::Exception::NOT_FINISHED) { + : RequestHandler(connection, requestId), exp(Net::Exception::NOT_FINISHED), isFinished(false) { finished.connect(slot); finished.connect(boost::bind(&Request::signalFinished, this)); } - void finish(std::auto_ptr result) {res = result; finished.emit(*this);} - void finish(const XmlPacket& result) {res.reset(new XmlPacket(result)); finished.emit(*this);} - void finishWithError(const Net::Exception &e) {exp = e; finished.emit(*this);} + virtual ~Request() { + wait(); + } + + void finish(std::auto_ptr result) { + { + boost::lock_guard lock(mutex); + + res = result; + isFinished = true; + } + + finishCond.notify_all(); + finished.emit(*this); + } + + void finish(const XmlPacket& result) { + { + boost::lock_guard lock(mutex); + + res.reset(new XmlPacket(result)); + isFinished = true; + } + + finishCond.notify_all(); + finished.emit(*this); + } + + void finishWithError(const Net::Exception &e) { + { + boost::lock_guard lock(mutex); + + exp = e; + isFinished = true; + } + + finishCond.notify_all(); + finished.emit(*this); + } virtual void sendRequest() = 0; virtual void handlePacket(const XmlPacket &packet); public: - const XmlPacket& getResult() const throw(Net::Exception) { + const XmlPacket& getResult() throw(Net::Exception) { + boost::lock_guard lock(mutex); + if(res.get()) return *res; throw exp; } + + void wait() { + boost::unique_lock lock(mutex); + + while(!isFinished) + finishCond.wait(lock); + } }; } diff --git a/src/Common/RequestHandler.h b/src/Common/RequestHandler.h index 58ffeb7..d35ab1b 100644 --- a/src/Common/RequestHandler.h +++ b/src/Common/RequestHandler.h @@ -33,22 +33,21 @@ namespace Common { class Connection; class RequestManager; -class RequestHandler { +class RequestHandler : private boost::noncopyable { private: - Net::Signals::Signal0 finished; + Net::Signals::Signal0 finished; Connection *connection; boost::uint16_t requestId; - // Prevent shallow copy - RequestHandler(const RequestHandler &o); - RequestHandler& operator=(const RequestHandler &o); - protected: RequestHandler(Connection *connection0, boost::uint16_t requestId0) : connection(connection0), requestId(requestId0) {} - void signalFinished() {finished.emit();} - Net::Signals::Connection connectSignalFinished(const Net::Signals::Signal0::slot_type &slot) { + void signalFinished() { + finished.emit(); + } + + Net::Signals::Connection connectSignalFinished(const Net::Signals::Signal0::slot_type &slot) { return finished.connect(slot); } diff --git a/src/Common/RequestManager.cpp b/src/Common/RequestManager.cpp index f328bf7..da9979a 100644 --- a/src/Common/RequestManager.cpp +++ b/src/Common/RequestManager.cpp @@ -46,8 +46,12 @@ boost::shared_ptr RequestManager::RequestMap::findRequest(boost: return it->second; } -bool RequestManager::RequestMap::deleteRequest(boost::uint16_t id) { - return erase(id); +void RequestManager::RequestMap::deleteRequest(boost::uint16_t id) { + Net::ThreadManager::get()->pushWork(boost::bind(&RequestMap::doDeleteRequest, this, id)); +} + +void RequestManager::RequestMap::doDeleteRequest(boost::uint16_t id) { + erase(id); } diff --git a/src/Common/RequestManager.h b/src/Common/RequestManager.h index d9e7fbb..bd1c12d 100644 --- a/src/Common/RequestManager.h +++ b/src/Common/RequestManager.h @@ -21,6 +21,7 @@ #define MAD_COMMON_REQUESTMANAGER_H_ #include "Request.h" +#include #include #include @@ -43,7 +44,10 @@ class RequestManager : boost::noncopyable { public: bool addRequest(boost::uint16_t id, boost::shared_ptr info); boost::shared_ptr findRequest(boost::uint16_t id); - bool deleteRequest(boost::uint16_t id); + void deleteRequest(boost::uint16_t id); + + private: + void doDeleteRequest(boost::uint16_t id); }; class RequestHandlerFactory { @@ -123,7 +127,7 @@ class RequestManager : boost::noncopyable { } template - bool sendRequest(Connection *connection, Request::slot_type slot) { + boost::shared_ptr sendRequest0(Connection *connection, Request::slot_type slot = Request::empty_slot) { boost::unique_lock lock(mutex); boost::shared_ptr requestMap; @@ -132,19 +136,19 @@ class RequestManager : boost::noncopyable { requestMap = _getUnusedRequestId(connection, &requestId); if(!requestMap) - return false; + return boost::shared_ptr(); boost::shared_ptr request(new T(connection, requestId, slot)); requestMap->addRequest(requestId, request); lock.unlock(); - request->sendRequest(); + Net::ThreadManager::get()->pushWork(boost::bind(&Request::sendRequest, request)); - return true; + return request; } template - bool sendRequest(Connection *connection, Request::slot_type slot, T1 t1) { + boost::shared_ptr sendRequest1(Connection *connection, T1 t1, Request::slot_type slot = Request::empty_slot) { boost::unique_lock lock(mutex); boost::shared_ptr requestMap; @@ -153,19 +157,19 @@ class RequestManager : boost::noncopyable { requestMap = _getUnusedRequestId(connection, &requestId); if(!requestMap) - return false; + return boost::shared_ptr(); boost::shared_ptr request(new T(connection, requestId, slot, t1)); requestMap->addRequest(requestId, request); lock.unlock(); - request->sendRequest(); + Net::ThreadManager::get()->pushWork(boost::bind(&Request::sendRequest, request)); - return true; + return request; } template - bool sendRequest(Connection *connection, Request::slot_type slot, T1 t1, T2 t2) { + boost::shared_ptr sendRequest2(Connection *connection, T1 t1, T2 t2, Request::slot_type slot = Request::empty_slot) { boost::unique_lock lock(mutex); boost::shared_ptr requestMap; @@ -174,19 +178,19 @@ class RequestManager : boost::noncopyable { requestMap = _getUnusedRequestId(connection, &requestId); if(!requestMap) - return false; + return boost::shared_ptr(); boost::shared_ptr request(new T(connection, requestId, slot, t1, t2)); requestMap->addRequest(requestId, request); lock.unlock(); - request->sendRequest(); + Net::ThreadManager::get()->pushWork(boost::bind(&Request::sendRequest, request)); - return true; + return request; } template - bool sendRequest(Connection *connection, Request::slot_type slot, T1 t1, T2 t2, T3 t3) { + boost::shared_ptr sendRequest3(Connection *connection, T1 t1, T2 t2, T3 t3, Request::slot_type slot = Request::empty_slot) { boost::unique_lock lock(mutex); boost::shared_ptr requestMap; @@ -195,19 +199,19 @@ class RequestManager : boost::noncopyable { requestMap = _getUnusedRequestId(connection, &requestId); if(!requestMap) - return false; + return boost::shared_ptr(); boost::shared_ptr request(new T(connection, requestId, slot, t1, t2, t3)); requestMap->addRequest(requestId, request); lock.unlock(); - request->sendRequest(); + Net::ThreadManager::get()->pushWork(boost::bind(&Request::sendRequest, request)); - return true; + return request; } template - bool sendRequest(Connection *connection, Request::slot_type slot, T1 t1, T2 t2, T3 t3, T4 t4) { + boost::shared_ptr sendRequest4(Connection *connection, T1 t1, T2 t2, T3 t3, T4 t4, Request::slot_type slot = Request::empty_slot) { boost::unique_lock lock(mutex); boost::shared_ptr requestMap; @@ -216,15 +220,15 @@ class RequestManager : boost::noncopyable { requestMap = _getUnusedRequestId(connection, &requestId); if(!requestMap) - return false; + return boost::shared_ptr(); boost::shared_ptr request(new T(connection, requestId, slot, t1, t2, t3, t4)); requestMap->addRequest(requestId, request); lock.unlock(); - request->sendRequest(); + Net::ThreadManager::get()->pushWork(boost::bind(&Request::sendRequest, request)); - return true; + return request; } }; diff --git a/src/Daemon/Backends/NetworkLogger.h b/src/Daemon/Backends/NetworkLogger.h index a6b6c21..1c5749d 100644 --- a/src/Daemon/Backends/NetworkLogger.h +++ b/src/Daemon/Backends/NetworkLogger.h @@ -32,11 +32,9 @@ class NetworkLogger : public Common::Logger { private: Common::Connection *connection; - void requestFinished(const Common::Request&) {} - protected: virtual void logMessage(Common::Logger::MessageCategory category, Common::Logger::MessageLevel level, time_t messageTimestamp, const std::string &message) { - Common::RequestManager::get()->sendRequest(connection, boost::bind(&NetworkLogger::requestFinished, this, _1), + Common::RequestManager::get()->sendRequest4(connection, category, level, messageTimestamp, message); } diff --git a/src/Net/Connection.cpp b/src/Net/Connection.cpp index 1ff9926..b7580d5 100644 --- a/src/Net/Connection.cpp +++ b/src/Net/Connection.cpp @@ -55,7 +55,7 @@ void Connection::handleHandshake(const boost::system::error_code& error) { received = 0; } - ThreadManager::get()->pushWork(boost::bind(&Signals::Signal0::emit, &connectedSignal)); + ThreadManager::get()->pushWork(boost::bind(&Signals::Signal0::emit, &connectedSignal)); enterReceiveLoop(); } @@ -69,7 +69,7 @@ void Connection::handleShutdown(const boost::system::error_code& error) { _setState(DISCONNECTED); - ThreadManager::get()->pushWork(boost::bind(&Signals::Signal0::emit, &disconnectedSignal)); + ThreadManager::get()->pushWork(boost::bind(&Signals::Signal0::emit, &disconnectedSignal)); } void Connection::enterReceiveLoop() { @@ -91,7 +91,7 @@ void Connection::handleHeaderReceive(const std::vector &data) { } if(header.length == 0) { - ThreadManager::get()->pushWork(boost::bind(&Signals::Signal1::emit, &receiveSignal, Packet(ntohs(header.requestId)))); + ThreadManager::get()->pushWork(boost::bind(&Signals::Signal1::emit, &receiveSignal, Packet(ntohs(header.requestId)))); enterReceiveLoop(); } @@ -105,7 +105,7 @@ void Connection::handleDataReceive(const std::vector &data) { boost::upgrade_lock lock(connectionLock); Packet packet(ntohs(header.requestId), data.data(), ntohs(header.length)); - ThreadManager::get()->pushWork(boost::bind(&Signals::Signal1::emit, &receiveSignal, packet)); + ThreadManager::get()->pushWork(boost::bind(&Signals::Signal1::emit, &receiveSignal, packet)); } enterReceiveLoop(); diff --git a/src/Net/Connection.h b/src/Net/Connection.h index cda8bfe..3938140 100644 --- a/src/Net/Connection.h +++ b/src/Net/Connection.h @@ -70,9 +70,9 @@ class Connection : boost::noncopyable { Packet::Data header; - Signals::Signal1 receiveSignal; - Signals::Signal0 connectedSignal; - Signals::Signal0 disconnectedSignal; + Signals::Signal1 receiveSignal; + Signals::Signal0 connectedSignal; + Signals::Signal0 disconnectedSignal; bool receiving; unsigned long sending; @@ -171,21 +171,21 @@ class Connection : boost::noncopyable { bool send(const Packet &packet); - Signals::Connection connectSignalReceive(const Signals::Signal1::slot_type &slot) { + Signals::Connection connectSignalReceive(const Signals::Signal1::slot_type &slot) { return receiveSignal.connect(slot); } void disconnectSignalReceive(const Signals::Connection &connection) { receiveSignal.disconnect(connection); } - Signals::Connection connectSignalConnected(const Signals::Signal0::slot_type &slot) { + Signals::Connection connectSignalConnected(const Signals::Signal0::slot_type &slot) { return connectedSignal.connect(slot); } void disconnectSignalConnected(const Signals::Connection &connection) { connectedSignal.disconnect(connection); } - Signals::Connection connectSignalDisconnected(const Signals::Signal0::slot_type &slot) { + Signals::Connection connectSignalDisconnected(const Signals::Signal0::slot_type &slot) { return disconnectedSignal.connect(slot); } void disconnectSignalDisconnected(const Signals::Connection &connection) { diff --git a/src/Net/Listener.cpp b/src/Net/Listener.cpp index 6da2762..1188ba3 100644 --- a/src/Net/Listener.cpp +++ b/src/Net/Listener.cpp @@ -20,6 +20,7 @@ #include "Listener.h" #include +#include #include #include @@ -52,6 +53,10 @@ void Listener::handleAccept(const boost::system::error_code &error, boost::share } void Listener::handleConnect(boost::shared_ptr con) { + Net::ThreadManager::get()->pushWork(boost::bind(&Listener::doHandleConnect, this, con)); +} + +void Listener::doHandleConnect(boost::shared_ptr con) { { boost::lock_guard lock(mutex); std::map, std::pair >::iterator it = connections.find(con); diff --git a/src/Net/Listener.h b/src/Net/Listener.h index ec0cd22..2f45ea6 100644 --- a/src/Net/Listener.h +++ b/src/Net/Listener.h @@ -40,11 +40,12 @@ class Listener : boost::noncopyable { std::map, std::pair > connections; - Signals::Signal1 > signal; + Signals::Signal1 > signal; void handleAccept(const boost::system::error_code &error, boost::shared_ptr con); void handleConnect(boost::shared_ptr con); + void doHandleConnect(boost::shared_ptr con); void handleDisconnect(boost::shared_ptr con); public: @@ -52,7 +53,7 @@ class Listener : boost::noncopyable { const boost::asio::ip::tcp::endpoint &address0 = boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 6666)) throw(Exception); virtual ~Listener(); - Signals::Connection connectSignalNewConnection(const Signals::Signal1 >::slot_type &slot) { + Signals::Connection connectSignalNewConnection(const Signals::Signal1 >::slot_type &slot) { return signal.connect(slot); } void disconnectSignalNewConnection(const Signals::Connection &connection) { diff --git a/src/Net/Signals/Signal0.h b/src/Net/Signals/Signal0.h index 32859ab..c468135 100644 --- a/src/Net/Signals/Signal0.h +++ b/src/Net/Signals/Signal0.h @@ -26,13 +26,12 @@ namespace Mad { namespace Net { namespace Signals { -template -class Signal0 : public SignalBase, boost::function0 > { +class Signal0 : public SignalBase, boost::function0 > { public: - R emit() { - boost::lock_guard lock(SignalBase, boost::function0 >::mutex); + void emit() { + boost::lock_guard lock(mutex); - return SignalBase, boost::function0 >::signal(); + signal(); } }; diff --git a/src/Net/Signals/Signal1.h b/src/Net/Signals/Signal1.h index 78eaa47..b9649be 100644 --- a/src/Net/Signals/Signal1.h +++ b/src/Net/Signals/Signal1.h @@ -20,17 +20,19 @@ #ifndef MAD_NET_SIGNALS_SIGNAL1_H_ #define MAD_NET_SIGNALS_SIGNAL1_H_ +#include "SignalBase.h" + namespace Mad { namespace Net { namespace Signals { -template -class Signal1 : public SignalBase, boost::function1 > { +template +class Signal1 : public SignalBase, boost::function1 > { public: - R emit(T1 arg1) { - boost::lock_guard lock(SignalBase, boost::function1 >::mutex); + void emit(T1 arg1) { + boost::lock_guard lock(SignalBase, boost::function1 >::mutex); - return SignalBase, boost::function1 >::signal(arg1); + SignalBase, boost::function1 >::signal(arg1); } }; diff --git a/src/Net/Signals/Signal2.h b/src/Net/Signals/Signal2.h index afa528e..374a239 100644 --- a/src/Net/Signals/Signal2.h +++ b/src/Net/Signals/Signal2.h @@ -20,17 +20,19 @@ #ifndef MAD_NET_SIGNALS_SIGNAL2_H_ #define MAD_NET_SIGNALS_SIGNAL2_H_ +#include "SignalBase.h" + namespace Mad { namespace Net { namespace Signals { -template -class Signal2 : public SignalBase, boost::function2 > { +template +class Signal2 : public SignalBase, boost::function2 > { public: - R emit(T1 arg1, T2 arg2) { - boost::lock_guard lock(SignalBase, boost::function2 >::mutex); + void emit(T1 arg1, T2 arg2) { + boost::lock_guard lock(SignalBase, boost::function2 >::mutex); - return SignalBase, boost::function2 >::signal(arg1, arg2); + SignalBase, boost::function2 >::signal(arg1, arg2); } }; diff --git a/src/Net/Signals/SignalBase.h b/src/Net/Signals/SignalBase.h index fc5f2c6..9e2de62 100644 --- a/src/Net/Signals/SignalBase.h +++ b/src/Net/Signals/SignalBase.h @@ -25,7 +25,7 @@ #include #include #include -#include +#include namespace Mad { namespace Net { @@ -40,19 +40,19 @@ class SignalBase : private boost::noncopyable { typedef SignalType signal_type; std::set connections; - boost::recursive_mutex mutex; + boost::mutex mutex; signal_type signal; SignalBase() {} ~SignalBase() { // Wait for other threads - boost::lock_guard lock(mutex); + boost::lock_guard lock(mutex); } public: Connection connect(const slot_type &slot) { - boost::lock_guard lock(mutex); + boost::lock_guard lock(mutex); boost::signals::connection con(signal.connect(slot)); @@ -61,7 +61,7 @@ class SignalBase : private boost::noncopyable { } void disconnect(const Connection &connection) { - boost::lock_guard lock(mutex); + boost::lock_guard lock(mutex); std::set::iterator it = connections.find(connection.connection); diff --git a/src/Server/ConnectionManager.cpp b/src/Server/ConnectionManager.cpp index 37e04d2..cd02d21 100644 --- a/src/Server/ConnectionManager.cpp +++ b/src/Server/ConnectionManager.cpp @@ -86,7 +86,7 @@ void ConnectionManager::updateState(Common::HostInfo *hostInfo, Common::HostInfo for(std::set >::iterator con = connections.begin(); con != connections.end(); ++con) { if((*con)->getConnectionType() == ServerConnection::CLIENT) - Common::RequestManager::get()->sendRequest(con->get(), boost::bind(&ConnectionManager::updateStateFinished, this, _1), hostInfo->getName(), state); + Common::RequestManager::get()->sendRequest2(con->get(), hostInfo->getName(), state, boost::bind(&ConnectionManager::updateStateFinished, this, _1)); } } diff --git a/src/Server/RequestHandlers/DaemonCommandRequestHandler.cpp b/src/Server/RequestHandlers/DaemonCommandRequestHandler.cpp index 3a09de7..0a7bd39 100644 --- a/src/Server/RequestHandlers/DaemonCommandRequestHandler.cpp +++ b/src/Server/RequestHandlers/DaemonCommandRequestHandler.cpp @@ -47,8 +47,8 @@ void DaemonCommandRequestHandler::handlePacket(const Common::XmlPacket &packet) try { boost::shared_ptr daemonCon = ConnectionManager::get()->getDaemonConnection(packet["daemon"]); - Common::RequestManager::get()->sendRequest(daemonCon.get(), - boost::bind(&DaemonCommandRequestHandler::requestFinished, this, _1), command == "reboot"); + Common::RequestManager::get()->sendRequest1(daemonCon.get(), + command == "reboot", boost::bind(&DaemonCommandRequestHandler::requestFinished, this, _1)); } catch(Net::Exception &e) { Common::XmlPacket ret; @@ -62,7 +62,7 @@ void DaemonCommandRequestHandler::handlePacket(const Common::XmlPacket &packet) } } -void DaemonCommandRequestHandler::requestFinished(const Common::Request &request) { +void DaemonCommandRequestHandler::requestFinished(Common::Request &request) { try { sendPacket(request.getResult()); } diff --git a/src/Server/RequestHandlers/DaemonCommandRequestHandler.h b/src/Server/RequestHandlers/DaemonCommandRequestHandler.h index 6bd1278..b0b49df 100644 --- a/src/Server/RequestHandlers/DaemonCommandRequestHandler.h +++ b/src/Server/RequestHandlers/DaemonCommandRequestHandler.h @@ -30,7 +30,7 @@ namespace RequestHandlers { class DaemonCommandRequestHandler : public Common::RequestHandler { private: - void requestFinished(const Common::Request &request); + void requestFinished(Common::Request &request); protected: virtual void handlePacket(const Common::XmlPacket &packet); diff --git a/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.cpp b/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.cpp index 2bb316b..a9791b5 100644 --- a/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.cpp +++ b/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.cpp @@ -45,7 +45,7 @@ void DaemonFSInfoRequestHandler::handlePacket(const Common::XmlPacket &packet) { try { boost::shared_ptr daemonCon = ConnectionManager::get()->getDaemonConnection(packet["daemon"]); - Common::RequestManager::get()->sendRequest(daemonCon.get(), + Common::RequestManager::get()->sendRequest0(daemonCon.get(), boost::bind(&DaemonFSInfoRequestHandler::requestFinished, this, _1)); } catch(Net::Exception &e) { @@ -60,7 +60,7 @@ void DaemonFSInfoRequestHandler::handlePacket(const Common::XmlPacket &packet) { } } -void DaemonFSInfoRequestHandler::requestFinished(const Common::Request &request) { +void DaemonFSInfoRequestHandler::requestFinished(Common::Request &request) { try { sendPacket(request.getResult()); } diff --git a/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.h b/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.h index 3352f11..5d2ae1c 100644 --- a/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.h +++ b/src/Server/RequestHandlers/DaemonFSInfoRequestHandler.h @@ -30,7 +30,7 @@ namespace RequestHandlers { class DaemonFSInfoRequestHandler : public Common::RequestHandler { private: - void requestFinished(const Common::Request &request); + void requestFinished(Common::Request &request); protected: virtual void handlePacket(const Common::XmlPacket &packet); diff --git a/src/Server/RequestHandlers/DaemonStatusRequestHandler.cpp b/src/Server/RequestHandlers/DaemonStatusRequestHandler.cpp index 15f63c7..94df846 100644 --- a/src/Server/RequestHandlers/DaemonStatusRequestHandler.cpp +++ b/src/Server/RequestHandlers/DaemonStatusRequestHandler.cpp @@ -47,7 +47,7 @@ void DaemonStatusRequestHandler::handlePacket(const Common::XmlPacket &packet) { try { boost::shared_ptr daemonCon = ConnectionManager::get()->getDaemonConnection(daemonName); - Common::RequestManager::get()->sendRequest(daemonCon.get(), + Common::RequestManager::get()->sendRequest0(daemonCon.get(), boost::bind(&DaemonStatusRequestHandler::requestFinished, this, _1)); } catch(Net::Exception &e) { @@ -62,7 +62,7 @@ void DaemonStatusRequestHandler::handlePacket(const Common::XmlPacket &packet) { } } -void DaemonStatusRequestHandler::requestFinished(const Common::Request &request) { +void DaemonStatusRequestHandler::requestFinished(Common::Request &request) { try { sendPacket(request.getResult()); } diff --git a/src/Server/RequestHandlers/DaemonStatusRequestHandler.h b/src/Server/RequestHandlers/DaemonStatusRequestHandler.h index 8e61ada..de7f83d 100644 --- a/src/Server/RequestHandlers/DaemonStatusRequestHandler.h +++ b/src/Server/RequestHandlers/DaemonStatusRequestHandler.h @@ -30,7 +30,7 @@ namespace RequestHandlers { class DaemonStatusRequestHandler : public Common::RequestHandler { private: - void requestFinished(const Common::Request &request); + void requestFinished(Common::Request &request); protected: virtual void handlePacket(const Common::XmlPacket &packet); diff --git a/src/mad.cpp b/src/mad.cpp index a5162c3..c89487b 100644 --- a/src/mad.cpp +++ b/src/mad.cpp @@ -35,7 +35,7 @@ using namespace Mad; -static void requestFinished(const Common::Request&) { +static void requestFinished(Common::Request&) { Common::Logger::log("Identified."); } @@ -69,7 +69,7 @@ int main() { //char hostname[256]; //gethostname(hostname, sizeof(hostname)); //Common::RequestManager::get()->sendRequest(connection, sigc::ptr_fun(requestFinished), hostname); - Common::RequestManager::get()->sendRequest(connection, &requestFinished, "test"); + Common::RequestManager::get()->sendRequest1(connection, "test", &requestFinished); connection->waitWhileConnected(); diff --git a/src/madc.cpp b/src/madc.cpp index 017b486..40bd51c 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -77,7 +77,7 @@ int main(int argc, char *argv[]) { { boost::unique_lock lock(commandMutex); commandRunning = true; - Common::RequestManager::get()->sendRequest(connection, boost::bind(&commandFinished)); + Common::RequestManager::get()->sendRequest0(connection, boost::bind(&commandFinished)); while(commandRunning) { commandNotify.wait(lock); } -- cgit v1.2.3