diff options
author | Mateusz Piekos <mateuszpiekos@gmail.com> | 2012-06-12 10:52:03 (GMT) |
---|---|---|
committer | Mateusz Piekos <mateuszpiekos@gmail.com> | 2012-06-12 10:52:03 (GMT) |
commit | edd726abb1d0f7532218efbb8067a75a313e9e98 (patch) | |
tree | 7eec4f7912a59bb9a383a61c2c96616d5b5847b6 /Swiften | |
parent | 6e9fb4e4a3aeee8c40617a4dda6e5e0892ceebad (diff) | |
download | swift-contrib-edd726abb1d0f7532218efbb8067a75a313e9e98.zip swift-contrib-edd726abb1d0f7532218efbb8067a75a313e9e98.tar.bz2 |
Improved whiteboard session handling
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/Elements/WhiteboardPayload.h | 2 | ||||
-rw-r--r-- | Swiften/Jingle/#JingleSessionImpl.cpp# | 185 | ||||
-rw-r--r-- | Swiften/Parser/PayloadParsers/WhiteboardParser.cpp | 23 | ||||
-rw-r--r-- | Swiften/Parser/PayloadParsers/WhiteboardParser.h | 4 | ||||
-rw-r--r-- | Swiften/SConscript | 3 | ||||
-rw-r--r-- | Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp | 30 | ||||
-rw-r--r-- | Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.h | 12 | ||||
-rw-r--r-- | Swiften/Whiteboard/IncomingWhiteboardSession.cpp | 22 | ||||
-rw-r--r-- | Swiften/Whiteboard/IncomingWhiteboardSession.h | 24 | ||||
-rw-r--r-- | Swiften/Whiteboard/OutgoingWhiteboardSession.cpp | 24 | ||||
-rw-r--r-- | Swiften/Whiteboard/OutgoingWhiteboardSession.h | 24 | ||||
-rw-r--r-- | Swiften/Whiteboard/WhiteboardResponder.cpp | 24 | ||||
-rw-r--r-- | Swiften/Whiteboard/WhiteboardResponder.h | 2 | ||||
-rw-r--r-- | Swiften/Whiteboard/WhiteboardSession.cpp | 35 | ||||
-rw-r--r-- | Swiften/Whiteboard/WhiteboardSession.h | 15 | ||||
-rw-r--r-- | Swiften/Whiteboard/WhiteboardSessionManager.cpp | 48 | ||||
-rw-r--r-- | Swiften/Whiteboard/WhiteboardSessionManager.h | 21 |
17 files changed, 416 insertions, 82 deletions
diff --git a/Swiften/Elements/WhiteboardPayload.h b/Swiften/Elements/WhiteboardPayload.h index db261ca..3ad1706 100644 --- a/Swiften/Elements/WhiteboardPayload.h +++ b/Swiften/Elements/WhiteboardPayload.h @@ -15,7 +15,7 @@ namespace Swift { public: typedef boost::shared_ptr<WhiteboardPayload> ref; - enum Type {Data, SessionRequest}; + enum Type {Data, SessionRequest, SessionAccept, SessionTerminate}; WhiteboardPayload(Type type = WhiteboardPayload::Data) : type_(type) { } diff --git a/Swiften/Jingle/#JingleSessionImpl.cpp# b/Swiften/Jingle/#JingleSessionImpl.cpp# new file mode 100644 index 0000000..98c5196 --- /dev/null +++ b/Swiften/Jingle/#JingleSessionImpl.cpp# @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2010 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <Swiften/Jingle/JingleSessionImpl.h> + +#include <boost/smart_ptr/make_shared.hpp> + +#include <Swiften/Parser/PayloadParsers/JingleParser.h> +#include <Swiften/Jingle/JingleContentID.h> +#include <Swiften/Elements/JingleContentPayload.h> +#include <Swiften/Queries/Request.h> +#include <Swiften/Queries/GenericRequest.h> + +#include <Swiften/Base/Log.h> + +#include "Swiften/Serializer/PayloadSerializers/JinglePayloadSerializer.h" +#include "Swiften/FileTransfer/JingleTransport.h" + +namespace Swift { + +JingleSessionImpl::JingleSessionImpl(const JID& initiator, const JID& peerJID, const std::string& id, IQRouter* router) : JingleSession(initiator, id), iqRouter(router), peerJID(peerJID) { + SWIFT_LOG(debug) << "initiator: " << initiator << ", peerJID: " << peerJID << std::endl; +} + +void JingleSessionImpl::handleIncomingAction(JinglePayload::ref action) { + if (action->getAction() == JinglePayload::SessionTerminate) { + onSessionTerminateReceived(action->getReason()); + return; + } + if (action->getAction() == JinglePayload::SessionInfo) { + onSessionInfoReceived(action); + return; + } + + JingleContentPayload::ref content = action->getPayload<JingleContentPayload>(); + if (!content) { + SWIFT_LOG(debug) << "no content payload!" << std::endl; + return; + } + JingleContentID contentID(content->getName(), content->getCreator()); + JingleDescription::ref description = content->getDescriptions().empty() ? JingleDescription::ref() : content->getDescriptions()[0]; + JingleTransportPayload::ref transport = content->getTransports().empty() ? JingleTransportPayload::ref() : content->getTransports()[0]; + switch(action->getAction()) { + case JinglePayload::SessionAccept: + onSessionAcceptReceived(contentID, description, transport); + return; + case JinglePayload::TransportAccept: + onTransportAcceptReceived(contentID, transport); + return; + case JinglePayload::TransportInfo: + onTransportInfoReceived(contentID, transport); + return; + case JinglePayload::TransportReject: + onTransportRejectReceived(contentID, transport); + return; + case JinglePayload::TransportReplace: + onTransportReplaceReceived(contentID, transport); + return; + // following unused Jingle actions + case JinglePayload::ContentAccept: + case JinglePayload::ContentAdd: + case JinglePayload::ContentModify: + case JinglePayload::ContentReject: + case JinglePayload::ContentRemove: + case JinglePayload::DescriptionInfo: + case JinglePayload::SecurityInfo: + + // handled elsewhere + case JinglePayload::SessionInitiate: + case JinglePayload::SessionInfo: + case JinglePayload::SessionTerminate: + + case JinglePayload::UnknownAction: + return; + } + std::cerr << "Unhandled Jingle action!!! ACTION: " << action->getAction() << std::endl; +} + +void JingleSessionImpl::sendInitiate(const JingleContentID& id, JingleDescription::ref description, JingleTransportPayload::ref transport) { + JinglePayload::ref payload = boost::make_shared<JinglePayload>(JinglePayload::SessionInitiate, getID()); + payload->setInitiator(getInitiator()); + JingleContentPayload::ref content = boost::make_shared<JingleContentPayload>(); + content->setCreator(id.getCreator()); + content->setName(id.getName()); + content->addDescription(description); + content->addTransport(transport); + payload->addPayload(content); + + sendSetRequest(payload); +} + +void JingleSessionImpl::sendTerminate(JinglePayload::Reason::Type reason) { + JinglePayload::ref payload = boost::make_shared<JinglePayload>(JinglePayload::SessionTerminate, getID()); + payload->setReason(JinglePayload::Reason(reason)); + payload->setInitiator(getInitiator()); + sendSetRequest(payload); +} + +void JingleSessionImpl::sendInfo(boost::shared_ptr<Payload> info) { + JinglePayload::ref payload = boost::make_shared<JinglePayload>(JinglePayload::SessionInfo, getID()); + payload->addPayload(info); + + sendSetRequest(payload); +} + +void JingleSessionImpl::sendAccept(const JingleContentID& id, JingleDescription::ref description, JingleTransportPayload::ref transPayload) { + JinglePayload::ref payload = createPayload(); + + JingleContentPayload::ref content = boost::make_shared<JingleContentPayload>(); + content->setCreator(id.getCreator()); + content->setName(id.getName()); + content->addTransport(transPayload); + content->addDescription(description); + payload->setAction(JinglePayload::SessionAccept); + payload->addPayload(content); + + // put into IQ:set and send it away + sendSetRequest(payload); +} + + +void JingleSessionImpl::sendTransportAccept(const JingleContentID& id, JingleTransportPayload::ref transPayload) { + JinglePayload::ref payload = createPayload(); + + JingleContentPayload::ref content = boost::make_shared<JingleContentPayload>(); + content->setCreator(id.getCreator()); + content->setName(id.getName()); + content->addTransport(transPayload); + payload->setAction(JinglePayload::TransportAccept); + payload->addPayload(content); + + // put into IQ:set and send it away + sendSetRequest(payload); +} + +void JingleSessionImpl::sendTransportInfo(const JingleContentID& id, JingleTransportPayload::ref transPayload) { + JinglePayload::ref payload = createPayload(); + + JingleContentPayload::ref content = boost::make_shared<JingleContentPayload>(); + content->setCreator(id.getCreator()); + content->setName(id.getName()); + content->addTransport(transPayload); + payload->setAction(JinglePayload::TransportInfo); + payload->addPayload(content); + + sendSetRequest(payload); +} + +void JingleSessionImpl::sendTransportReject(const JingleContentID& /* id */, JingleTransportPayload::ref /* transPayload */) { + SWIFT_LOG(debug) << "NOT IMPLEMENTED YET!!!!" << std::endl; +} + +void JingleSessionImpl::sendTransportReplace(const JingleContentID& id, JingleTransportPayload::ref transPayload) { + JinglePayload::ref payload = createPayload(); + + JingleContentPayload::ref content = boost::make_shared<JingleContentPayload>(); + content->setCreator(id.getCreator()); + content->setName(id.getName()); + content->addTransport(transPayload); + payload->setAction(JinglePayload::TransportReplace); + payload->addContent(content); + + sendSetRequest(payload); +} + + +void JingleSessionImpl::sendSetRequest(JinglePayload::ref payload) { + boost::shared_ptr<GenericRequest<JinglePayload> > request = boost::make_shared<GenericRequest<JinglePayload> >(IQ::Set, peerJID, payload, iqRouter); + request->send(); +} + + +JinglePayload::ref JingleSessionImpl::createPayload() const { + JinglePayload::ref payload = boost::make_shared<JinglePayload>(); + payload->setSessionID(getID()); + payload->setInitiator(getInitiator()); + return payload; +} + + + +} diff --git a/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp b/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp index 94b67e5..4306497 100644 --- a/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp +++ b/Swiften/Parser/PayloadParsers/WhiteboardParser.cpp @@ -5,19 +5,16 @@ */ #include <Swiften/Parser/PayloadParsers/WhiteboardParser.h> +#include <boost/optional.hpp> namespace Swift { WhiteboardParser::WhiteboardParser() : level_(0) { } + void WhiteboardParser::handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes) { if (level_ == 0) { - std::string type = attributes.getAttribute("type"); - if (type.empty()) { - getPayloadInternal()->setType(WhiteboardPayload::Data); - } - else if (type == "request") { - getPayloadInternal()->setType(WhiteboardPayload::SessionRequest); - } +// std::string type = attributes.getAttribute("type"); + getPayloadInternal()->setType(stringToType(attributes.getAttributeValue("type").get_value_or(""))); } ++level_; } @@ -32,4 +29,16 @@ namespace Swift { void WhiteboardParser::handleCharacterData(const std::string& data) { data_ += data; } + + WhiteboardPayload::Type WhiteboardParser::stringToType(const std::string& type) const { + if (type.empty()) { + return WhiteboardPayload::Data; + } else if (type == "session-request") { + return WhiteboardPayload::SessionRequest; + } else if (type == "session-accept") { + return WhiteboardPayload::SessionAccept; + } else if (type == "session-terminate") { + return WhiteboardPayload::SessionTerminate; + } + } } diff --git a/Swiften/Parser/PayloadParsers/WhiteboardParser.h b/Swiften/Parser/PayloadParsers/WhiteboardParser.h index 265e12d..faa698b 100644 --- a/Swiften/Parser/PayloadParsers/WhiteboardParser.h +++ b/Swiften/Parser/PayloadParsers/WhiteboardParser.h @@ -17,6 +17,10 @@ namespace Swift { virtual void handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes); virtual void handleEndElement(const std::string& element, const std::string&); virtual void handleCharacterData(const std::string& data); + + private: + WhiteboardPayload::Type stringToType(const std::string& type) const; + private: int level_; std::string data_; diff --git a/Swiften/SConscript b/Swiften/SConscript index 9b31304..1fa47ca 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript @@ -184,6 +184,7 @@ if env["SCONS_STAGE"] == "build" : "Serializer/PayloadSerializers/StreamInitiationFileInfoSerializer.cpp", "Serializer/PayloadSerializers/DeliveryReceiptSerializer.cpp", "Serializer/PayloadSerializers/DeliveryReceiptRequestSerializer.cpp", + "Serializer/PayloadSerializers/WhiteboardSerializer.cpp", "Serializer/PresenceSerializer.cpp", "Serializer/StanzaSerializer.cpp", "Serializer/StreamErrorSerializer.cpp", @@ -202,6 +203,8 @@ if env["SCONS_STAGE"] == "build" : "StringCodecs/Hexify.cpp", "Whiteboard/WhiteboardResponder.cpp", "Whiteboard/WhiteboardSession.cpp", + "Whiteboard/IncomingWhiteboardSession.cpp", + "Whiteboard/OutgoingWhiteboardSession.cpp", "Whiteboard/WhiteboardSessionManager.cpp", ] diff --git a/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp b/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp new file mode 100644 index 0000000..578b0ab --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.cpp @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.h> +#include <Swiften/Serializer/XML/XMLElement.h> +#include <Swiften/Serializer/XML/XMLTextNode.h> + +namespace Swift { + std::string WhiteboardSerializer::serializePayload(boost::shared_ptr<WhiteboardPayload> payload) const { + XMLElement element("wb"); + element.addNode(XMLTextNode::ref(new XMLTextNode(payload->getData()))); + element.setAttribute("type", typeToString(payload->getType())); + return element.serialize(); + } + + std::string WhiteboardSerializer::typeToString(WhiteboardPayload::Type type) const { + if(type == WhiteboardPayload::Data) { + return ""; + } else if (type == WhiteboardPayload::SessionRequest) { + return "session-request"; + } else if (type == WhiteboardPayload::SessionAccept) { + return "session-accept"; + } else if (type == WhiteboardPayload::SessionTerminate) { + return "session-terminate"; + } + } +} diff --git a/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.h b/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.h index 9b7f905..975d537 100644 --- a/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.h +++ b/Swiften/Serializer/PayloadSerializers/WhiteboardSerializer.h @@ -12,13 +12,9 @@ namespace Swift { class WhiteboardSerializer : public GenericPayloadSerializer<WhiteboardPayload> { public: - std::string serializePayload(boost::shared_ptr<WhiteboardPayload> payload) const { - XMLElement element("wb"); - element.addNode(XMLTextNode::ref(new XMLTextNode(payload->getData()))); - if (payload->getType() == WhiteboardPayload::SessionRequest) { - element.setAttribute("type", "request"); - } - return element.serialize(); - } + std::string serializePayload(boost::shared_ptr<WhiteboardPayload> payload) const; + + private: + std::string typeToString(WhiteboardPayload::Type type) const; }; } diff --git a/Swiften/Whiteboard/IncomingWhiteboardSession.cpp b/Swiften/Whiteboard/IncomingWhiteboardSession.cpp new file mode 100644 index 0000000..92a86a1 --- /dev/null +++ b/Swiften/Whiteboard/IncomingWhiteboardSession.cpp @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <Swiften/Whiteboard/IncomingWhiteboardSession.h> +#include <Swiften/Elements/WhiteboardPayload.h> + +namespace Swift { + IncomingWhiteboardSession::IncomingWhiteboardSession(const JID& jid, IQRouter* router) : WhiteboardSession(jid, router) { + } + + IncomingWhiteboardSession::~IncomingWhiteboardSession() { + } + + void IncomingWhiteboardSession::accept() { + boost::shared_ptr<WhiteboardPayload> payload = boost::make_shared<WhiteboardPayload>(WhiteboardPayload::SessionAccept); + boost::shared_ptr<GenericRequest<WhiteboardPayload> > request = boost::make_shared<GenericRequest<WhiteboardPayload> >(IQ::Set, toJID_, payload, router_); + request->send(); + } +} diff --git a/Swiften/Whiteboard/IncomingWhiteboardSession.h b/Swiften/Whiteboard/IncomingWhiteboardSession.h new file mode 100644 index 0000000..3b15a49 --- /dev/null +++ b/Swiften/Whiteboard/IncomingWhiteboardSession.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <Swiften/Whiteboard/WhiteboardSession.h> + +#include <boost/shared_ptr.hpp> + +#pragma once + +namespace Swift { + class IncomingWhiteboardSession : public WhiteboardSession { + public: + typedef boost::shared_ptr<IncomingWhiteboardSession> ref; + + public: + IncomingWhiteboardSession(const JID& jid, IQRouter* router); + ~IncomingWhiteboardSession(); + + void accept(); + }; +} diff --git a/Swiften/Whiteboard/OutgoingWhiteboardSession.cpp b/Swiften/Whiteboard/OutgoingWhiteboardSession.cpp new file mode 100644 index 0000000..2693fcd --- /dev/null +++ b/Swiften/Whiteboard/OutgoingWhiteboardSession.cpp @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <Swiften/Whiteboard/OutgoingWhiteboardSession.h> + +#include <boost/bind.hpp> +#include <Swiften/Elements/WhiteboardPayload.h> + +namespace Swift { + OutgoingWhiteboardSession::OutgoingWhiteboardSession(const JID& jid, IQRouter* router) : WhiteboardSession(jid, router) { + } + + OutgoingWhiteboardSession::~OutgoingWhiteboardSession() { + } + + void OutgoingWhiteboardSession::startSession() { + boost::shared_ptr<WhiteboardPayload> payload = boost::make_shared<WhiteboardPayload>(WhiteboardPayload::SessionRequest); + boost::shared_ptr<GenericRequest<WhiteboardPayload> > request = boost::make_shared<GenericRequest<WhiteboardPayload> >(IQ::Set, toJID_, payload, router_); + request->send(); + } +} diff --git a/Swiften/Whiteboard/OutgoingWhiteboardSession.h b/Swiften/Whiteboard/OutgoingWhiteboardSession.h new file mode 100644 index 0000000..dfbd200 --- /dev/null +++ b/Swiften/Whiteboard/OutgoingWhiteboardSession.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include <Swiften/Whiteboard/WhiteboardSession.h> + +#include <boost/shared_ptr.hpp> +#include <Swiften/Queries/GenericRequest.h> + +namespace Swift { + class OutgoingWhiteboardSession : public WhiteboardSession { + public: + typedef boost::shared_ptr<OutgoingWhiteboardSession> ref; + + public: + OutgoingWhiteboardSession(const JID& jid, IQRouter* router); + virtual ~OutgoingWhiteboardSession(); + void startSession(); + }; +} diff --git a/Swiften/Whiteboard/WhiteboardResponder.cpp b/Swiften/Whiteboard/WhiteboardResponder.cpp index 8a1b290..c947aec 100644 --- a/Swiften/Whiteboard/WhiteboardResponder.cpp +++ b/Swiften/Whiteboard/WhiteboardResponder.cpp @@ -7,6 +7,7 @@ #include <Swiften/Whiteboard/WhiteboardResponder.h> #include <Swiften/Whiteboard/WhiteboardSessionManager.h> +#include <Swiften/Whiteboard/IncomingWhiteboardSession.h> #include <Swiften/Whiteboard/WhiteboardSession.h> #include <Swiften/Queries/IQRouter.h> @@ -16,27 +17,20 @@ namespace Swift { bool WhiteboardResponder::handleSetRequest(const JID& from, const JID& to, const std::string& id, boost::shared_ptr<WhiteboardPayload> payload) { if (payload->getType() == WhiteboardPayload::SessionRequest) { - sessionManager_->onRequestReceived(from); - sessionRequests_[from] = id; + if (sessionManager_->getSession(from.toBare())) { + sendError(from, id, ErrorPayload::Conflict, ErrorPayload::Cancel); + } else { + sendResponse(from, id, boost::shared_ptr<WhiteboardPayload>()); + IncomingWhiteboardSession::ref session = boost::make_shared<IncomingWhiteboardSession>(from, router_); + sessionManager_->handleIncomingSession(session); + } } else { sendResponse(from, id, boost::shared_ptr<WhiteboardPayload>()); - WhiteboardSession* session = sessionManager_->getSession(from.toBare()); + WhiteboardSession::ref session = sessionManager_->getSession(from.toBare()); if (session != NULL) { session->handleIncomingAction(payload); } } return true; } - - void WhiteboardResponder::sendRequestResponse(const JID& contact, bool accepted) { - if (sessionRequests_.find(contact) == sessionRequests_.end()) { - return; - } - if (accepted ) { - sendResponse(contact, sessionRequests_[contact], boost::shared_ptr<WhiteboardPayload>()); - } else { - sendError(contact, sessionRequests_[contact], ErrorPayload::Conflict, ErrorPayload::Cancel); - } - sessionRequests_.erase(contact); - } } diff --git a/Swiften/Whiteboard/WhiteboardResponder.h b/Swiften/Whiteboard/WhiteboardResponder.h index d1b712d..b171ef3 100644 --- a/Swiften/Whiteboard/WhiteboardResponder.h +++ b/Swiften/Whiteboard/WhiteboardResponder.h @@ -17,10 +17,8 @@ namespace Swift { public: WhiteboardResponder(WhiteboardSessionManager* sessionManager, IQRouter* router); bool handleSetRequest(const JID& from, const JID& to, const std::string& id, boost::shared_ptr<WhiteboardPayload> payload); - void sendRequestResponse(const JID& contact, bool accepted); private: - std::map<JID, std::string> sessionRequests_; WhiteboardSessionManager* sessionManager_; IQRouter* router_; }; diff --git a/Swiften/Whiteboard/WhiteboardSession.cpp b/Swiften/Whiteboard/WhiteboardSession.cpp index 859f2cd..bfef7c3 100644 --- a/Swiften/Whiteboard/WhiteboardSession.cpp +++ b/Swiften/Whiteboard/WhiteboardSession.cpp @@ -6,7 +6,6 @@ #include <Swiften/Whiteboard/WhiteboardSession.h> -#include <boost/bind.hpp> #include <boost/smart_ptr/make_shared.hpp> #include <Swiften/Elements/WhiteboardPayload.h> #include <Swiften/Elements/ErrorPayload.h> @@ -17,8 +16,23 @@ namespace Swift { WhiteboardSession::WhiteboardSession(const JID& jid, IQRouter* router) : toJID_(jid), router_(router) { } + WhiteboardSession::~WhiteboardSession() { + } + void WhiteboardSession::handleIncomingAction(boost::shared_ptr<WhiteboardPayload> payload) { - onDataReceived(payload->getData()); + if (payload->getType() == WhiteboardPayload::SessionTerminate) { + onSessionTerminateReceived(toJID_); + return; + } + + switch (payload->getType()) { + case WhiteboardPayload::Data: + onDataReceived(payload->getData()); + break; + case WhiteboardPayload::SessionAccept: + onRequestAccepted(toJID_); + break; + } } void WhiteboardSession::sendData(const std::string& data) { @@ -28,17 +42,14 @@ namespace Swift { request->send(); } - void WhiteboardSession::sendSessionRequest() { - boost::shared_ptr<WhiteboardPayload> payload = boost::make_shared<WhiteboardPayload>(WhiteboardPayload::SessionRequest); - sessionRequest = new GenericRequest<WhiteboardPayload>(IQ::Set, toJID_, payload, router_); - sessionRequest->onResponse.connect(boost::bind(&WhiteboardSession::handleSessionRequestResponse, this, _1, _2)); - sessionRequest->send(); + void WhiteboardSession::cancel() { + boost::shared_ptr<WhiteboardPayload> payload = boost::make_shared<WhiteboardPayload>(WhiteboardPayload::SessionTerminate); + boost::shared_ptr<GenericRequest<WhiteboardPayload> > request = boost::make_shared<GenericRequest<WhiteboardPayload> >(IQ::Set, toJID_, payload, router_); + request->send(); + onSessionCancelled(toJID_); } - void WhiteboardSession::handleSessionRequestResponse(boost::shared_ptr<WhiteboardPayload> whiteboardPayload, boost::shared_ptr<ErrorPayload> errorPayload) { - if (errorPayload.get() == 0) { - onRequestAccepted(toJID_, this); - } - delete sessionRequest; + const JID& WhiteboardSession::getTo() const { + return toJID_; } } diff --git a/Swiften/Whiteboard/WhiteboardSession.h b/Swiften/Whiteboard/WhiteboardSession.h index 14d6778..7aa67ca 100644 --- a/Swiften/Whiteboard/WhiteboardSession.h +++ b/Swiften/Whiteboard/WhiteboardSession.h @@ -19,19 +19,24 @@ namespace Swift { class WhiteboardSession { public: + typedef boost::shared_ptr<WhiteboardSession> ref; + + public: WhiteboardSession(const JID& jid, IQRouter* router); + virtual ~WhiteboardSession(); void handleIncomingAction(boost::shared_ptr<WhiteboardPayload> payload); void sendData(const std::string& data); - void sendSessionRequest(); - void handleSessionRequestResponse(boost::shared_ptr<WhiteboardPayload> whiteboardPayload, boost::shared_ptr<ErrorPayload> errorPayload); + void cancel(); + const JID& getTo() const; public: boost::signal< void(const std::string& data)> onDataReceived; - boost::signal< void(const JID& contact, WhiteboardSession* session)> onRequestAccepted; + boost::signal< void(const JID& contact)> onSessionTerminateReceived; + boost::signal< void(const JID& contact)> onRequestAccepted; + boost::signal< void(const JID& contact)> onSessionCancelled; - private: + protected: JID toJID_; IQRouter* router_; - GenericRequest<WhiteboardPayload>* sessionRequest; }; } diff --git a/Swiften/Whiteboard/WhiteboardSessionManager.cpp b/Swiften/Whiteboard/WhiteboardSessionManager.cpp index ccbc4d7..eae9e5b 100644 --- a/Swiften/Whiteboard/WhiteboardSessionManager.cpp +++ b/Swiften/Whiteboard/WhiteboardSessionManager.cpp @@ -9,7 +9,6 @@ #include <boost/bind.hpp> #include <Swiften/Queries/IQRouter.h> -#include <Swiften/Whiteboard/WhiteboardSession.h> #include <Swiften/Whiteboard/WhiteboardResponder.h> #include <Swiften/Presence/PresenceOracle.h> @@ -24,48 +23,53 @@ namespace Swift { delete responder; } - WhiteboardSession* WhiteboardSessionManager::getSession(const JID& to) { + WhiteboardSession::ref WhiteboardSessionManager::getSession(const JID& to) { if (sessions_.find(to.toBare()) == sessions_.end()) { - return NULL; + return boost::shared_ptr<WhiteboardSession>(); } return sessions_[to.toBare()]; } - WhiteboardSession* WhiteboardSessionManager::createSession(const JID& to) { + OutgoingWhiteboardSession::ref WhiteboardSessionManager::createOutgoingSession(const JID& to) { JID fullJID = to; if (fullJID.isBare()) { fullJID = getFullJID(fullJID); } - WhiteboardSession* session = new WhiteboardSession(fullJID, router_); + OutgoingWhiteboardSession::ref session = boost::make_shared<OutgoingWhiteboardSession>(fullJID, router_); sessions_[to.toBare()] = session; + session->onSessionTerminateReceived.connect(boost::bind(&WhiteboardSessionManager::handleSessionTerminate, this, _1)); + session->onSessionCancelled.connect(boost::bind(&WhiteboardSessionManager::handleSessionCancelled, this, _1)); return session; } - WhiteboardSession* WhiteboardSessionManager::acceptSession(const JID& to) { - responder->sendRequestResponse(to, true); - WhiteboardSession* session = getSession(to); - if (session == NULL) { - return createSession(to); + WhiteboardSession::ref WhiteboardSessionManager::requestSession(const JID& to) { + WhiteboardSession::ref session = getSession(to); + if (!session) { + OutgoingWhiteboardSession::ref outgoingSession = createOutgoingSession(to); + outgoingSession->startSession(); + return outgoingSession; + } else { + return session; } - return session; } - void WhiteboardSessionManager::requestSession(const JID& to) { - WhiteboardSession* session = createSession(to); - session->onRequestAccepted.connect(boost::bind(&WhiteboardSessionManager::handleRequestAccepted, this, _1, _2)); - session->sendSessionRequest(); + void WhiteboardSessionManager::handleIncomingSession(IncomingWhiteboardSession::ref session) { + sessions_[session->getTo().toBare()] = session; + session->onSessionTerminateReceived.connect(boost::bind(&WhiteboardSessionManager::handleSessionTerminate, this, _1)); + session->onSessionCancelled.connect(boost::bind(&WhiteboardSessionManager::handleSessionCancelled, this, _1)); + onRequestReceived(session->getTo()); } - void WhiteboardSessionManager::cancelSession(const JID& to) { - responder->sendRequestResponse(to, false); + JID WhiteboardSessionManager::getFullJID(const JID& bareJID) { + std::vector<Presence::ref> presences = presenceOracle_->getAllPresence(bareJID); + return presences[0]->getFrom(); } - void WhiteboardSessionManager::handleRequestAccepted(const JID& contact, WhiteboardSession* session) { - onRequestAccepted(contact, session); + void WhiteboardSessionManager::handleSessionTerminate(const JID& contact) { + sessions_.erase(contact.toBare()); } - JID WhiteboardSessionManager::getFullJID(const JID& bareJID) { - std::vector<Presence::ref> presences = presenceOracle_->getAllPresence(bareJID); - return presences[0]->getFrom(); + void WhiteboardSessionManager::handleSessionCancelled(const JID& contact) { + sessions_.erase(contact.toBare()); } } diff --git a/Swiften/Whiteboard/WhiteboardSessionManager.h b/Swiften/Whiteboard/WhiteboardSessionManager.h index 3cd3aab..d2a59e0 100644 --- a/Swiften/Whiteboard/WhiteboardSessionManager.h +++ b/Swiften/Whiteboard/WhiteboardSessionManager.h @@ -11,34 +11,35 @@ #include <Swiften/Queries/IQRouter.h> #include <Swiften/JID/JID.h> #include <Swiften/Base/boost_bsignals.h> +#include <Swiften/Whiteboard/WhiteboardSession.h> +#include <Swiften/Whiteboard/IncomingWhiteboardSession.h> +#include <Swiften/Whiteboard/OutgoingWhiteboardSession.h> namespace Swift { class IQRouter; - class WhiteboardSession; class WhiteboardResponder; class PresenceOracle; class WhiteboardSessionManager { + friend class WhiteboardResponder; public: WhiteboardSessionManager(IQRouter* router, PresenceOracle* presenceOracle); ~WhiteboardSessionManager(); - - WhiteboardSession* getSession(const JID& to); - WhiteboardSession* acceptSession(const JID& to); - void requestSession(const JID& to); - void cancelSession(const JID& to); - void handleRequestAccepted(const JID& contact, WhiteboardSession* session); + WhiteboardSession::ref getSession(const JID& to); + WhiteboardSession::ref requestSession(const JID& to); public: boost::signal< void (const JID&)> onRequestReceived; - boost::signal< void (const JID&, WhiteboardSession*)> onRequestAccepted; private: JID getFullJID(const JID& bareJID); - WhiteboardSession* createSession(const JID& to); + OutgoingWhiteboardSession::ref createOutgoingSession(const JID& to); + void handleIncomingSession(IncomingWhiteboardSession::ref session); + void handleSessionTerminate(const JID& contact); + void handleSessionCancelled(const JID& contact); private: - std::map<JID, WhiteboardSession*> sessions_; + std::map<JID, boost::shared_ptr<WhiteboardSession> > sessions_; IQRouter* router_; PresenceOracle* presenceOracle_; WhiteboardResponder* responder; |