/* * Request.h * * Copyright (C) 2008 Matthias Schiffer * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along * with this program. If not, see . */ #ifndef MAD_COMMON_REQUEST_H_ #define MAD_COMMON_REQUEST_H_ #include "export.h" #include "RequestHandler.h" #include #include #include #include #include #include namespace Mad { namespace Common { class MAD_COMMON_EXPORT Request : public RequestHandler { private: friend class RequestManager; boost::mutex mutex; boost::condition_variable finishCond; bool isFinished; boost::shared_ptr packet; Core::Exception exception; Core::Signals::Signal2, Core::Exception> finished; protected: Request(Application *application) : RequestHandler(application), isFinished(false), finished(application) {} void signalFinished(boost::shared_ptr pkt, Core::Exception exp) { { boost::lock_guard lock(mutex); isFinished = true; packet = pkt; exception = exp; } finished.emit(pkt, exp); finishCond.notify_all(); RequestHandler::signalFinished(); } void signalFinished(boost::shared_ptr packet) { signalFinished(packet, Core::Exception()); } void signalFinished(Core::Exception exp) { signalFinished(boost::shared_ptr(), exp); } void signalFinished() { signalFinished(boost::shared_ptr(), Core::Exception()); } virtual void sendRequest() = 0; virtual void handlePacket(boost::shared_ptr packet); public: Core::Signals::Connection connectSignalFinished(const Core::Signals::Signal2, Core::Exception>::slot_type &slot) { return finished.connect(slot); } void disconnectSignalFinished(const Core::Signals::Connection &con) { finished.disconnect(con); } void wait() { boost::unique_lock lock(mutex); while(!isFinished) finishCond.wait(lock); } std::pair, Core::Exception> getResult() { boost::lock_guard lock(mutex); if(!isFinished) return std::make_pair(boost::shared_ptr(), Core::Exception(Core::Exception::NOT_FINISHED)); else return std::make_pair(packet, exception); } }; } } #endif /* MAD_COMMON_REQUEST_H_ */