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/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 +++++++++++++++------------- 6 files changed, 105 insertions(+), 40 deletions(-) (limited to 'src/Common') 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; } }; -- cgit v1.2.3