From 349cda4b8e178dc4a6694c47a3eb45e5a1368fb8 Mon Sep 17 00:00:00 2001 From: Mateusz Piekos Date: Thu, 5 Jul 2012 12:12:45 +0200 Subject: Created simple WhiteboardServer with unit tests Purpose of WhiteboardServer is to handle server side Operational Transformation operations diff --git a/Swift/Controllers/Chat/UnitTest/ChatsManagerTest.cpp b/Swift/Controllers/Chat/UnitTest/ChatsManagerTest.cpp index 3d2db7d..798f8b3 100644 --- a/Swift/Controllers/Chat/UnitTest/ChatsManagerTest.cpp +++ b/Swift/Controllers/Chat/UnitTest/ChatsManagerTest.cpp @@ -49,6 +49,7 @@ #include "Swiften/Elements/DeliveryReceipt.h" #include #include +#include using namespace Swift; @@ -100,9 +101,10 @@ public: chatListWindow_ = new MockChatListWindow(); ftManager_ = new DummyFileTransferManager(); ftOverview_ = new FileTransferOverview(ftManager_); + wbSessionManager_ = new WhiteboardSessionManager(iqRouter_, presenceOracle_); mocks_->ExpectCall(chatListWindowFactory_, ChatListWindowFactory::createChatListWindow).With(uiEventStream_).Return(chatListWindow_); - manager_ = new ChatsManager(jid_, stanzaChannel_, iqRouter_, eventController_, chatWindowFactory_, joinMUCWindowFactory_, nickResolver_, presenceOracle_, directedPresenceSender_, uiEventStream_, chatListWindowFactory_, true, NULL, mucRegistry_, entityCapsManager_, mucManager_, mucSearchWindowFactory_, profileSettings_, ftOverview_, xmppRoster_, false, settings_); + manager_ = new ChatsManager(jid_, stanzaChannel_, iqRouter_, eventController_, chatWindowFactory_, joinMUCWindowFactory_, nickResolver_, presenceOracle_, directedPresenceSender_, uiEventStream_, chatListWindowFactory_, true, NULL, mucRegistry_, entityCapsManager_, mucManager_, mucSearchWindowFactory_, profileSettings_, ftOverview_, xmppRoster_, false, settings_, wbSessionManager_); avatarManager_ = new NullAvatarManager(); manager_->setAvatarManager(avatarManager_); @@ -471,6 +473,7 @@ private: ChatListWindow* chatListWindow_; FileTransferOverview* ftOverview_; FileTransferManager* ftManager_; + WhiteboardSessionManager* wbSessionManager_; }; CPPUNIT_TEST_SUITE_REGISTRATION(ChatsManagerTest); diff --git a/Swift/Controllers/UnitTest/MockChatWindow.h b/Swift/Controllers/UnitTest/MockChatWindow.h index e0e18a0..983619b 100644 --- a/Swift/Controllers/UnitTest/MockChatWindow.h +++ b/Swift/Controllers/UnitTest/MockChatWindow.h @@ -52,6 +52,10 @@ namespace Swift { void setSubject(const std::string& /*subject*/) {} virtual void showRoomConfigurationForm(Form::ref) {} virtual void addMUCInvitation(const std::string& /*senderName*/, const JID& /*jid*/, const std::string& /*reason*/, const std::string& /*password*/, bool = true) {}; + + virtual std::string addWhiteboardRequest(const JID&, bool) {return "";}; + virtual void setWhiteboardSessionStatus(std::string, const ChatWindow::WhiteboardSessionState){}; + virtual void setAffiliations(MUCOccupant::Affiliation, const std::vector&) {} virtual void setAvailableRoomActions(const std::vector &) {}; diff --git a/Swiften/SConscript b/Swiften/SConscript index 140f696..120186f 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript @@ -206,7 +206,9 @@ if env["SCONS_STAGE"] == "build" : "Whiteboard/IncomingWhiteboardSession.cpp", "Whiteboard/OutgoingWhiteboardSession.cpp", "Whiteboard/WhiteboardSessionManager.cpp", + "Whiteboard/WhiteboardServer.cpp", "Whiteboard/Elements/Color.cpp", + "Whiteboard/WhiteboardTransformer.cpp", ] SConscript(dirs = [ @@ -402,6 +404,7 @@ if env["SCONS_STAGE"] == "build" : File("TLS/UnitTest/ServerIdentityVerifierTest.cpp"), File("TLS/UnitTest/CertificateTest.cpp"), File("VCards/UnitTest/VCardManagerTest.cpp"), + File("Whiteboard/UnitTest/WhiteboardServerTest.cpp"), ]) # Generate the Swiften header diff --git a/Swiften/Whiteboard/Operations/WhiteboardInsertOperation.h b/Swiften/Whiteboard/Operations/WhiteboardInsertOperation.h new file mode 100644 index 0000000..6934053 --- /dev/null +++ b/Swiften/Whiteboard/Operations/WhiteboardInsertOperation.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include + +#include + +namespace Swift { + class WhiteboardInsertOperation : public WhiteboardOperation { + public: + typedef boost::shared_ptr ref; + public: + ~WhiteboardInsertOperation() { + } + + WhiteboardElement::ref getElement() const { + return element_; + } + + void setElement(WhiteboardElement::ref element) { + element_ = element; + } + + private: + WhiteboardElement::ref element_; + }; +} diff --git a/Swiften/Whiteboard/Operations/WhiteboardOperation.h b/Swiften/Whiteboard/Operations/WhiteboardOperation.h new file mode 100644 index 0000000..73a8b74 --- /dev/null +++ b/Swiften/Whiteboard/Operations/WhiteboardOperation.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include +#include + +namespace Swift { + class WhiteboardOperation { + public: + typedef boost::shared_ptr ref; + public: + + enum OperationOrigin { Local, Other }; + + virtual ~WhiteboardOperation(){}; + + std::string getID() const { + return id_; + } + + void setID(const std::string& id) { + id_ = id; + } + + std::string getParentID() const { + return parentID_; + } + + void setParentID(const std::string& parentID) { + parentID_ = parentID; + } + + int getPos() const { + return pos_; + } + + void setPos(int pos) { + pos_ = pos; + } + + OperationOrigin getOrigin() const { + return origin_; + } + + void setOrigin(OperationOrigin origin) { + origin_ = origin; + } + + private: + std::string id_; + std::string parentID_; + int pos_; + OperationOrigin origin_; + }; +} diff --git a/Swiften/Whiteboard/UnitTest/WhiteboardServerTest.cpp b/Swiften/Whiteboard/UnitTest/WhiteboardServerTest.cpp new file mode 100644 index 0000000..3d17064 --- /dev/null +++ b/Swiften/Whiteboard/UnitTest/WhiteboardServerTest.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + + +#include +#include + +#include +#include +#include +#include + +using namespace Swift; + +class WhiteboardServerTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(WhiteboardServerTest); + CPPUNIT_TEST(testSimpleOp); + CPPUNIT_TEST_SUITE_END(); +public: + void testSimpleOp() { + WhiteboardServer server; + WhiteboardInsertOperation::ref firstOp = boost::make_shared(); + firstOp->setID("0"); + server.handleLocalOperationReceived(firstOp); + WhiteboardInsertOperation::ref serverOp = boost::make_shared(); + serverOp->setID("b"); + serverOp->setParentID("0"); + serverOp->setPos(1); + server.handleLocalOperationReceived(serverOp); + WhiteboardInsertOperation::ref clientOp = boost::make_shared(); + WhiteboardEllipseElement::ref clientElement = boost::make_shared(0,0,0,0); + clientOp->setID("a"); + clientOp->setParentID("0"); + clientOp->setPos(1); + clientOp->setElement(clientElement); + WhiteboardInsertOperation::ref op = boost::dynamic_pointer_cast(server.handleClientOperationReceived(clientOp)); + CPPUNIT_ASSERT_EQUAL(std::string("b"), op->getParentID()); + CPPUNIT_ASSERT_EQUAL(std::string("a"), op->getID()); + CPPUNIT_ASSERT_EQUAL(1, op->getPos()); + CPPUNIT_ASSERT_EQUAL(clientElement, boost::dynamic_pointer_cast(op->getElement())); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(WhiteboardServerTest); diff --git a/Swiften/Whiteboard/WhiteboardServer.cpp b/Swiften/Whiteboard/WhiteboardServer.cpp new file mode 100644 index 0000000..89fb00d --- /dev/null +++ b/Swiften/Whiteboard/WhiteboardServer.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include +#include + +namespace Swift { + void WhiteboardServer::handleLocalOperationReceived(WhiteboardOperation::ref operation) { + operations_.push_back(operation); + } + + WhiteboardOperation::ref WhiteboardServer::handleClientOperationReceived(WhiteboardInsertOperation::ref operation) { + std::list::iterator it; + for (it = operations_.begin(); it != operations_.end(); ++it) { + if (operation->getParentID() == (*it)->getParentID()) { + WhiteboardInsertOperation::ref insertOperation = boost::dynamic_pointer_cast(*it); + if (insertOperation) { + std::pair tResult = WhiteboardTransformer::transform(operation, insertOperation); + operations_.push_back(tResult.second); + return tResult.second; + } + else { + operations_.push_back(*it); + return *it; + } + } + } + } +} diff --git a/Swiften/Whiteboard/WhiteboardServer.h b/Swiften/Whiteboard/WhiteboardServer.h new file mode 100644 index 0000000..11ef64d --- /dev/null +++ b/Swiften/Whiteboard/WhiteboardServer.h @@ -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. + */ + +#pragma once + +#include + +#include + +namespace Swift { + class WhiteboardServer { + public: + void handleLocalOperationReceived(WhiteboardOperation::ref operation); + WhiteboardOperation::ref handleClientOperationReceived(WhiteboardInsertOperation::ref operation); + + private: + std::list operations_; + }; +} diff --git a/Swiften/Whiteboard/WhiteboardTransformer.cpp b/Swiften/Whiteboard/WhiteboardTransformer.cpp new file mode 100644 index 0000000..32e9815 --- /dev/null +++ b/Swiften/Whiteboard/WhiteboardTransformer.cpp @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include +#include + +namespace Swift { + std::pair WhiteboardTransformer::transform(WhiteboardInsertOperation::ref clientOp, WhiteboardInsertOperation::ref serverOp) { + std::pair result; + result.first = boost::make_shared(*serverOp.get()); + result.first->setParentID(clientOp->getID()); + result.first->setPos(result.first->getPos()+1); + result.second = boost::make_shared(*clientOp.get()); + result.second->setParentID(serverOp->getID()); + return result; + } +} diff --git a/Swiften/Whiteboard/WhiteboardTransformer.h b/Swiften/Whiteboard/WhiteboardTransformer.h new file mode 100644 index 0000000..33086d8 --- /dev/null +++ b/Swiften/Whiteboard/WhiteboardTransformer.h @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2012 Mateusz Piękos + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include +#include + +namespace Swift { + class WhiteboardTransformer { + public: + static std::pair transform(WhiteboardInsertOperation::ref clientOp, WhiteboardInsertOperation::ref serverOp); + }; +} -- cgit v0.10.2-6-g49f6