diff options
Diffstat (limited to 'Swiften/Queries/Request.cpp')
-rw-r--r-- | Swiften/Queries/Request.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/Swiften/Queries/Request.cpp b/Swiften/Queries/Request.cpp new file mode 100644 index 0000000..ce9f763 --- /dev/null +++ b/Swiften/Queries/Request.cpp @@ -0,0 +1,55 @@ +#include "Swiften/Queries/Request.h" +#include "Swiften/Queries/IQRouter.h" +#include "Swiften/EventLoop/MainEventLoop.h" + +namespace Swift { + +Request::Request(IQ::Type type, const JID& receiver, boost::shared_ptr<Payload> payload, IQRouter* router) : router_(router), type_(type), receiver_(receiver), payload_(payload), sent_(false) { +} + +Request::Request(IQ::Type type, const JID& receiver, IQRouter* router) : router_(router), type_(type), receiver_(receiver), sent_(false) { +} + +void Request::send() { + assert(payload_); + assert(!sent_); + sent_ = true; + + boost::shared_ptr<IQ> iq(new IQ(type_)); + iq->setTo(receiver_); + iq->addPayload(payload_); + id_ = router_->getNewIQID(); + iq->setID(id_); + + try { + router_->addHandler(shared_from_this()); + } + catch (const std::exception&) { + router_->addHandler(this); + } + + router_->sendIQ(iq); +} + +bool Request::handleIQ(boost::shared_ptr<IQ> iq) { + bool handled = false; + if (sent_ && iq->getID() == id_) { + if (iq->getType() == IQ::Result) { + handleResponse(iq->getPayloadOfSameType(payload_), boost::optional<ErrorPayload>()); + } + else { + boost::shared_ptr<ErrorPayload> errorPayload = iq->getPayload<ErrorPayload>(); + if (errorPayload) { + handleResponse(boost::shared_ptr<Payload>(), boost::optional<ErrorPayload>(*errorPayload)); + } + else { + handleResponse(boost::shared_ptr<Payload>(), boost::optional<ErrorPayload>(ErrorPayload::UndefinedCondition)); + } + } + router_->removeHandler(this); + handled = true; + } + return handled; +} + +} |