diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2009-06-01 23:03:29 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2009-06-01 23:03:29 +0200 |
commit | dcf66f3cc133bf600190b844ab9c0da764e5e82b (patch) | |
tree | e3d30777ae7751841cba3bb2aee524cb5438fb9a /src | |
parent | a77d2d1e08e4e2e8dfb5e4fc326f6c8fe315a898 (diff) | |
download | mad-dcf66f3cc133bf600190b844ab9c0da764e5e82b.tar mad-dcf66f3cc133bf600190b844ab9c0da764e5e82b.zip |
?berarbeitung der Request-Klasse begonnen
Diffstat (limited to 'src')
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<void> 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<void>::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<std::string, Common::HostInfo> CommandParser::parseHostList(const std:: void CommandParser::fsinfoCommand(const std::vector<std::string> &args) { if(args.size() == 1) - Common::RequestManager::get()->sendRequest<Common::Requests::FSInfoRequest>(connection, boost::bind(&CommandManager::fsInfoRequestFinished, CommandManager::get(), _1)); + Common::RequestManager::get()->sendRequest0<Common::Requests::FSInfoRequest>(connection, boost::bind(&CommandManager::fsInfoRequestFinished, CommandManager::get(), _1)); else if(args.size() == 2) - Common::RequestManager::get()->sendRequest<Requests::DaemonFSInfoRequest>(connection, boost::bind(&CommandManager::daemonFSInfoRequestFinished, CommandManager::get(), _1), args[1]); + Common::RequestManager::get()->sendRequest1<Requests::DaemonFSInfoRequest>(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<std::string> &args) { std::map<std::string, Common::HostInfo> hosts = parseHostList(std::vector<std::string>(args.begin()+1, args.end()), true); for(std::map<std::string, Common::HostInfo>::iterator host = hosts.begin(); host != hosts.end(); ++host) { - Common::RequestManager::get()->sendRequest<Requests::DaemonCommandRequest>(connection, - boost::bind(&CommandManager::daemonCommandRequestFinished, CommandManager::get(), _1), host->first, true + Common::RequestManager::get()->sendRequest2<Requests::DaemonCommandRequest>(connection, + host->first, true, boost::bind(&CommandManager::daemonCommandRequestFinished, CommandManager::get(), _1) ); ++CommandManager::get()->activeRequests; @@ -239,8 +239,8 @@ void CommandParser::shutdownCommand(const std::vector<std::string> &args) { std::map<std::string, Common::HostInfo> hosts = parseHostList(std::vector<std::string>(args.begin()+1, args.end()), true); for(std::map<std::string, Common::HostInfo>::iterator host = hosts.begin(); host != hosts.end(); ++host) { - Common::RequestManager::get()->sendRequest<Requests::DaemonCommandRequest>(connection, - boost::bind(&CommandManager::daemonCommandRequestFinished, CommandManager::get(), _1), host->first, false + Common::RequestManager::get()->sendRequest2<Requests::DaemonCommandRequest>(connection, + host->first, false, boost::bind(&CommandManager::daemonCommandRequestFinished, CommandManager::get(), _1) ); ++CommandManager::get()->activeRequests; @@ -249,11 +249,11 @@ 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<Common::Requests::StatusRequest>(connection, + Common::RequestManager::get()->sendRequest0<Common::Requests::StatusRequest>(connection, boost::bind(&CommandManager::statusRequestFinished, CommandManager::get(), _1)); else if(args.size() == 2) - Common::RequestManager::get()->sendRequest<Requests::DaemonStatusRequest>(connection, - boost::bind(&CommandManager::daemonStatusRequestFinished, CommandManager::get(), _1), args[1]); + Common::RequestManager::get()->sendRequest1<Requests::DaemonStatusRequest>(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<std::string> &args) { ++CommandManager::get()->activeRequests; - Common::RequestManager::get()->sendRequest<Common::Requests::UserInfoRequest>(connection, - boost::bind(&CommandManager::userInfoRequestFinished, CommandManager::get(), _1), uid); + Common::RequestManager::get()->sendRequest1<Common::Requests::UserInfoRequest>(connection, + uid, boost::bind(&CommandManager::userInfoRequestFinished, CommandManager::get(), _1)); } void CommandParser::listUsersCommand(const std::vector<std::string>&) { ++CommandManager::get()->activeRequests; - Common::RequestManager::get()->sendRequest<Common::Requests::UserListRequest>(connection, + Common::RequestManager::get()->sendRequest0<Common::Requests::UserListRequest>(connection, boost::bind(&CommandManager::userListRequestFinished, CommandManager::get(), _1)); } @@ -318,14 +318,14 @@ void CommandParser::listUserGroupsCommand(const std::vector<std::string> &args) ++CommandManager::get()->activeRequests; - Common::RequestManager::get()->sendRequest<Common::Requests::UserGroupListRequest>(connection, - boost::bind(&CommandManager::userGroupListRequestFinished, CommandManager::get(), _1), uid); + Common::RequestManager::get()->sendRequest1<Common::Requests::UserGroupListRequest>(connection, + uid, boost::bind(&CommandManager::userGroupListRequestFinished, CommandManager::get(), _1)); } void CommandParser::listGroupsCommand(const std::vector<std::string>&) { ++CommandManager::get()->activeRequests; - Common::RequestManager::get()->sendRequest<Common::Requests::GroupListRequest>(connection, + Common::RequestManager::get()->sendRequest0<Common::Requests::GroupListRequest>(connection, boost::bind(&CommandManager::groupListRequestFinished, CommandManager::get(), _1)); } @@ -351,14 +351,14 @@ void CommandParser::listGroupUsersCommand(const std::vector<std::string> &args) ++CommandManager::get()->activeRequests; - Common::RequestManager::get()->sendRequest<Common::Requests::GroupUserListRequest>(connection, - boost::bind(&CommandManager::groupUserListRequestFinished, CommandManager::get(), _1), gid); + Common::RequestManager::get()->sendRequest1<Common::Requests::GroupUserListRequest>(connection, + gid, boost::bind(&CommandManager::groupUserListRequestFinished, CommandManager::get(), _1)); } void CommandParser::exitCommand(const std::vector<std::string>&) { ++CommandManager::get()->activeRequests; - Common::RequestManager::get()->sendRequest<Common::Requests::DisconnectRequest>(connection, + Common::RequestManager::get()->sendRequest0<Common::Requests::DisconnectRequest>(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<Requests::DaemonListRequest>(con, + Common::RequestManager::get()->sendRequest0<Requests::DaemonListRequest>(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<boost::mutex> 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<void, const XmlPacket&, uint16_t> signalReceive; + Net::Signals::Signal2<const XmlPacket&, uint16_t> 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<void, const XmlPacket&, uint16_t>::slot_type &slot) { + Net::Signals::Connection connectSignalReceive(const Net::Signals::Signal2<const XmlPacket&, uint16_t>::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 <memory> #include <boost/bind.hpp> +#include <boost/thread/condition_variable.hpp> +#include <boost/thread/locks.hpp> +#include <boost/thread/mutex.hpp> namespace Mad { namespace Common { @@ -34,35 +37,88 @@ class Request : public RequestHandler { private: friend class RequestManager; - Net::Signals::Signal1<void, const Request&> finished; + Net::Signals::Signal1<Request&> finished; std::auto_ptr<XmlPacket> res; Net::Exception exp; + boost::mutex mutex; + boost::condition_variable finishCond; + bool isFinished; + + static void doNothing(Request&) {} + public: - typedef Net::Signals::Signal1<void, const Request&>::slot_type slot_type; + typedef Net::Signals::Signal1<Request&>::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<XmlPacket> 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<XmlPacket> result) { + { + boost::lock_guard<boost::mutex> lock(mutex); + + res = result; + isFinished = true; + } + + finishCond.notify_all(); + finished.emit(*this); + } + + void finish(const XmlPacket& result) { + { + boost::lock_guard<boost::mutex> lock(mutex); + + res.reset(new XmlPacket(result)); + isFinished = true; + } + + finishCond.notify_all(); + finished.emit(*this); + } + + void finishWithError(const Net::Exception &e) { + { + boost::lock_guard<boost::mutex> 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<boost::mutex> lock(mutex); + if(res.get()) return *res; throw exp; } + + void wait() { + boost::unique_lock<boost::mutex> 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<void> 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<void>::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<RequestHandler> 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 <Net/ThreadManager.h> #include <map> #include <memory> @@ -43,7 +44,10 @@ class RequestManager : boost::noncopyable { public: bool addRequest(boost::uint16_t id, boost::shared_ptr<RequestHandler> info); boost::shared_ptr<RequestHandler> 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 <class T> - bool sendRequest(Connection *connection, Request::slot_type slot) { + boost::shared_ptr<Request> sendRequest0(Connection *connection, Request::slot_type slot = Request::empty_slot) { boost::unique_lock<boost::shared_mutex> lock(mutex); boost::shared_ptr<RequestMap> requestMap; @@ -132,19 +136,19 @@ class RequestManager : boost::noncopyable { requestMap = _getUnusedRequestId(connection, &requestId); if(!requestMap) - return false; + return boost::shared_ptr<Request>(); boost::shared_ptr<Request> 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 <class T, typename T1> - bool sendRequest(Connection *connection, Request::slot_type slot, T1 t1) { + boost::shared_ptr<Request> sendRequest1(Connection *connection, T1 t1, Request::slot_type slot = Request::empty_slot) { boost::unique_lock<boost::shared_mutex> lock(mutex); boost::shared_ptr<RequestMap> requestMap; @@ -153,19 +157,19 @@ class RequestManager : boost::noncopyable { requestMap = _getUnusedRequestId(connection, &requestId); if(!requestMap) - return false; + return boost::shared_ptr<Request>(); boost::shared_ptr<Request> 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 <class T, typename T1, typename T2> - bool sendRequest(Connection *connection, Request::slot_type slot, T1 t1, T2 t2) { + boost::shared_ptr<Request> sendRequest2(Connection *connection, T1 t1, T2 t2, Request::slot_type slot = Request::empty_slot) { boost::unique_lock<boost::shared_mutex> lock(mutex); boost::shared_ptr<RequestMap> requestMap; @@ -174,19 +178,19 @@ class RequestManager : boost::noncopyable { requestMap = _getUnusedRequestId(connection, &requestId); if(!requestMap) - return false; + return boost::shared_ptr<Request>(); boost::shared_ptr<Request> 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 <class T, typename T1, typename T2, typename T3> - bool sendRequest(Connection *connection, Request::slot_type slot, T1 t1, T2 t2, T3 t3) { + boost::shared_ptr<Request> sendRequest3(Connection *connection, T1 t1, T2 t2, T3 t3, Request::slot_type slot = Request::empty_slot) { boost::unique_lock<boost::shared_mutex> lock(mutex); boost::shared_ptr<RequestMap> requestMap; @@ -195,19 +199,19 @@ class RequestManager : boost::noncopyable { requestMap = _getUnusedRequestId(connection, &requestId); if(!requestMap) - return false; + return boost::shared_ptr<Request>(); boost::shared_ptr<T> 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 <class T, typename T1, typename T2, typename T3, typename T4> - bool sendRequest(Connection *connection, Request::slot_type slot, T1 t1, T2 t2, T3 t3, T4 t4) { + boost::shared_ptr<Request> sendRequest4(Connection *connection, T1 t1, T2 t2, T3 t3, T4 t4, Request::slot_type slot = Request::empty_slot) { boost::unique_lock<boost::shared_mutex> lock(mutex); boost::shared_ptr<RequestMap> requestMap; @@ -216,15 +220,15 @@ class RequestManager : boost::noncopyable { requestMap = _getUnusedRequestId(connection, &requestId); if(!requestMap) - return false; + return boost::shared_ptr<Request>(); boost::shared_ptr<Request> 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<Requests::LogRequest>(connection, boost::bind(&NetworkLogger::requestFinished, this, _1), + Common::RequestManager::get()->sendRequest4<Requests::LogRequest>(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<void>::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<void>::emit, &disconnectedSignal)); + ThreadManager::get()->pushWork(boost::bind(&Signals::Signal0::emit, &disconnectedSignal)); } void Connection::enterReceiveLoop() { @@ -91,7 +91,7 @@ void Connection::handleHeaderReceive(const std::vector<boost::uint8_t> &data) { } if(header.length == 0) { - ThreadManager::get()->pushWork(boost::bind(&Signals::Signal1<void, const Packet&>::emit, &receiveSignal, Packet(ntohs(header.requestId)))); + ThreadManager::get()->pushWork(boost::bind(&Signals::Signal1<const Packet&>::emit, &receiveSignal, Packet(ntohs(header.requestId)))); enterReceiveLoop(); } @@ -105,7 +105,7 @@ void Connection::handleDataReceive(const std::vector<boost::uint8_t> &data) { boost::upgrade_lock<boost::shared_mutex> lock(connectionLock); Packet packet(ntohs(header.requestId), data.data(), ntohs(header.length)); - ThreadManager::get()->pushWork(boost::bind(&Signals::Signal1<void, const Packet&>::emit, &receiveSignal, packet)); + ThreadManager::get()->pushWork(boost::bind(&Signals::Signal1<const Packet&>::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<void, const Packet&> receiveSignal; - Signals::Signal0<void> connectedSignal; - Signals::Signal0<void> disconnectedSignal; + Signals::Signal1<const Packet&> 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<void, const Packet&>::slot_type &slot) { + Signals::Connection connectSignalReceive(const Signals::Signal1<const Packet&>::slot_type &slot) { return receiveSignal.connect(slot); } void disconnectSignalReceive(const Signals::Connection &connection) { receiveSignal.disconnect(connection); } - Signals::Connection connectSignalConnected(const Signals::Signal0<void>::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<void>::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 <Common/Logger.h> +#include <Net/ThreadManager.h> #include <cerrno> #include <cstring> @@ -52,6 +53,10 @@ void Listener::handleAccept(const boost::system::error_code &error, boost::share } void Listener::handleConnect(boost::shared_ptr<Connection> con) { + Net::ThreadManager::get()->pushWork(boost::bind(&Listener::doHandleConnect, this, con)); +} + +void Listener::doHandleConnect(boost::shared_ptr<Connection> con) { { boost::lock_guard<boost::mutex> lock(mutex); std::map<boost::shared_ptr<Connection>, std::pair<Signals::Connection, Signals::Connection> >::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<boost::shared_ptr<Connection>, std::pair<Signals::Connection, Signals::Connection> > connections; - Signals::Signal1<void, boost::shared_ptr<Connection> > signal; + Signals::Signal1<boost::shared_ptr<Connection> > signal; void handleAccept(const boost::system::error_code &error, boost::shared_ptr<Connection> con); void handleConnect(boost::shared_ptr<Connection> con); + void doHandleConnect(boost::shared_ptr<Connection> con); void handleDisconnect(boost::shared_ptr<Connection> 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<void, boost::shared_ptr<Connection> >::slot_type &slot) { + Signals::Connection connectSignalNewConnection(const Signals::Signal1<boost::shared_ptr<Connection> >::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 <typename R> -class Signal0 : public SignalBase<boost::signal0<R>, boost::function0<R> > { +class Signal0 : public SignalBase<boost::signal0<void>, boost::function0<void> > { public: - R emit() { - boost::lock_guard<boost::recursive_mutex> lock(SignalBase<boost::signal0<R>, boost::function0<R> >::mutex); + void emit() { + boost::lock_guard<boost::mutex> lock(mutex); - return SignalBase<boost::signal0<R>, boost::function0<R> >::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 <typename R, typename T1> -class Signal1 : public SignalBase<boost::signal1<R, T1>, boost::function1<R, T1> > { +template <typename T1> +class Signal1 : public SignalBase<boost::signal1<void, T1>, boost::function1<void, T1> > { public: - R emit(T1 arg1) { - boost::lock_guard<boost::recursive_mutex> lock(SignalBase<boost::signal1<R, T1>, boost::function1<R, T1> >::mutex); + void emit(T1 arg1) { + boost::lock_guard<boost::mutex> lock(SignalBase<boost::signal1<void, T1>, boost::function1<void, T1> >::mutex); - return SignalBase<boost::signal1<R, T1>, boost::function1<R, T1> >::signal(arg1); + SignalBase<boost::signal1<void, T1>, boost::function1<void, T1> >::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 <typename R, typename T1, typename T2> -class Signal2 : public SignalBase<boost::signal2<R, T1, T2>, boost::function2<R, T1, T2> > { +template <typename T1, typename T2> +class Signal2 : public SignalBase<boost::signal2<void, T1, T2>, boost::function2<void, T1, T2> > { public: - R emit(T1 arg1, T2 arg2) { - boost::lock_guard<boost::recursive_mutex> lock(SignalBase<boost::signal2<R, T1, T2>, boost::function2<R, T1, T2> >::mutex); + void emit(T1 arg1, T2 arg2) { + boost::lock_guard<boost::mutex> lock(SignalBase<boost::signal2<void, T1, T2>, boost::function2<void, T1, T2> >::mutex); - return SignalBase<boost::signal2<R, T1, T2>, boost::function2<R, T1, T2> >::signal(arg1, arg2); + SignalBase<boost::signal2<void, T1, T2>, boost::function2<void, T1, T2> >::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 <set> #include <boost/signals.hpp> #include <boost/thread/locks.hpp> -#include <boost/thread/recursive_mutex.hpp> +#include <boost/thread/mutex.hpp> namespace Mad { namespace Net { @@ -40,19 +40,19 @@ class SignalBase : private boost::noncopyable { typedef SignalType signal_type; std::set<boost::signals::connection> connections; - boost::recursive_mutex mutex; + boost::mutex mutex; signal_type signal; SignalBase() {} ~SignalBase() { // Wait for other threads - boost::lock_guard<boost::recursive_mutex> lock(mutex); + boost::lock_guard<boost::mutex> lock(mutex); } public: Connection connect(const slot_type &slot) { - boost::lock_guard<boost::recursive_mutex> lock(mutex); + boost::lock_guard<boost::mutex> 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<boost::recursive_mutex> lock(mutex); + boost::lock_guard<boost::mutex> lock(mutex); std::set<boost::signals::connection>::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<boost::shared_ptr<ServerConnection> >::iterator con = connections.begin(); con != connections.end(); ++con) { if((*con)->getConnectionType() == ServerConnection::CLIENT) - Common::RequestManager::get()->sendRequest<Requests::DaemonStateUpdateRequest>(con->get(), boost::bind(&ConnectionManager::updateStateFinished, this, _1), hostInfo->getName(), state); + Common::RequestManager::get()->sendRequest2<Requests::DaemonStateUpdateRequest>(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<Common::Connection> daemonCon = ConnectionManager::get()->getDaemonConnection(packet["daemon"]); - Common::RequestManager::get()->sendRequest<Requests::CommandRequest>(daemonCon.get(), - boost::bind(&DaemonCommandRequestHandler::requestFinished, this, _1), command == "reboot"); + Common::RequestManager::get()->sendRequest1<Requests::CommandRequest>(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<Common::Connection> daemonCon = ConnectionManager::get()->getDaemonConnection(packet["daemon"]); - Common::RequestManager::get()->sendRequest<Common::Requests::FSInfoRequest>(daemonCon.get(), + Common::RequestManager::get()->sendRequest0<Common::Requests::FSInfoRequest>(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<Common::Connection> daemonCon = ConnectionManager::get()->getDaemonConnection(daemonName); - Common::RequestManager::get()->sendRequest<Common::Requests::StatusRequest>(daemonCon.get(), + Common::RequestManager::get()->sendRequest0<Common::Requests::StatusRequest>(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<Daemon::Requests::IdentifyRequest>(connection, sigc::ptr_fun(requestFinished), hostname); - Common::RequestManager::get()->sendRequest<Common::Requests::IdentifyRequest>(connection, &requestFinished, "test"); + Common::RequestManager::get()->sendRequest1<Common::Requests::IdentifyRequest>(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<boost::mutex> lock(commandMutex); commandRunning = true; - Common::RequestManager::get()->sendRequest<Common::Requests::IdentifyRequest>(connection, boost::bind(&commandFinished)); + Common::RequestManager::get()->sendRequest0<Common::Requests::IdentifyRequest>(connection, boost::bind(&commandFinished)); while(commandRunning) { commandNotify.wait(lock); } |