diff options
Diffstat (limited to 'src/Common/Request.h')
-rw-r--r-- | src/Common/Request.h | 87 |
1 files changed, 35 insertions, 52 deletions
diff --git a/src/Common/Request.h b/src/Common/Request.h index d351c2a..b8477f7 100644 --- a/src/Common/Request.h +++ b/src/Common/Request.h @@ -17,8 +17,8 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef MAD_COMMON_XMLREQUEST_H_ -#define MAD_COMMON_XMLREQUEST_H_ +#ifndef MAD_COMMON_REQUEST_H_ +#define MAD_COMMON_REQUEST_H_ #include "RequestHandler.h" #include <Net/Exception.h> @@ -37,80 +37,54 @@ class Request : public RequestHandler { private: friend class RequestManager; - 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&) {} + boost::shared_ptr<const XmlPacket> packet; + Net::Exception exception; - public: - typedef Net::Signals::Signal1<Request&>::slot_type slot_type; + Net::Signals::Signal22<boost::shared_ptr<const XmlPacket>, Net::Exception> finished; protected: - static slot_type empty_slot; - - Request(Connection *connection, uint16_t requestId, slot_type slot) - : RequestHandler(connection, requestId), exp(Net::Exception::NOT_FINISHED), isFinished(false) { - finished.connect(slot); - finished.connect(boost::bind(&Request::signalFinished, this)); - } - - virtual ~Request() { - wait(); - } + Request() : isFinished(false) {} - void finish(std::auto_ptr<XmlPacket> result) { + void signalFinished(boost::shared_ptr<const XmlPacket> pkt, Net::Exception exp) { { boost::lock_guard<boost::mutex> lock(mutex); - res = result; isFinished = true; + packet = pkt; + exception = exp; } + finished.emit(pkt, exp); 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); + RequestHandler::signalFinished(); } - void finishWithError(const Net::Exception &e) { - { - boost::lock_guard<boost::mutex> lock(mutex); + void signalFinished(boost::shared_ptr<const XmlPacket> packet) { + signalFinished(packet, Net::Exception()); + } - exp = e; - isFinished = true; - } + void signalFinished(Net::Exception exp) { + signalFinished(boost::shared_ptr<const XmlPacket>(), exp); + } - finishCond.notify_all(); - finished.emit(*this); + void signalFinished() { + signalFinished(boost::shared_ptr<const XmlPacket>(), Net::Exception()); } virtual void sendRequest() = 0; - virtual void handlePacket(const XmlPacket &packet); + virtual void handlePacket(boost::shared_ptr<const XmlPacket> packet); public: - const XmlPacket& getResult() throw(Net::Exception) { - boost::lock_guard<boost::mutex> lock(mutex); - - if(res.get()) - return *res; - - throw exp; + Net::Signals::Connection2 connectSignalFinished(const Net::Signals::Signal22<boost::shared_ptr<const XmlPacket>, Net::Exception>::slot_type &slot) { + return finished.connect(slot); + } + void disconnectSignalFinished(const Net::Signals::Connection2 &con) { + finished.disconnect(con); } void wait() { @@ -119,9 +93,18 @@ class Request : public RequestHandler { while(!isFinished) finishCond.wait(lock); } + + std::pair<boost::shared_ptr<const XmlPacket>, Net::Exception> getResult() { + boost::lock_guard<boost::mutex> lock(mutex); + + if(!isFinished) + return std::make_pair(boost::shared_ptr<const XmlPacket>(), Net::Exception(Net::Exception::NOT_FINISHED)); + else + return std::make_pair(packet, exception); + } }; } } -#endif /* MAD_COMMON_XMLREQUEST_H_ */ +#endif /* MAD_COMMON_REQUEST_H_ */ |