diff options
author | Mateusz Piekos <mateuszpiekos@gmail.com> | 2012-07-05 10:12:45 (GMT) |
---|---|---|
committer | Mateusz Piekos <mateuszpiekos@gmail.com> | 2012-07-05 10:12:45 (GMT) |
commit | 349cda4b8e178dc4a6694c47a3eb45e5a1368fb8 (patch) | |
tree | b0576f3c60bf13a8f62396cac332edc2a421e4bd /Swiften | |
parent | fadd51fbed5111aba266a3b8b22be2c57ea11262 (diff) | |
download | swift-contrib-349cda4b8e178dc4a6694c47a3eb45e5a1368fb8.zip swift-contrib-349cda4b8e178dc4a6694c47a3eb45e5a1368fb8.tar.bz2 |
Created simple WhiteboardServer with unit tests
Purpose of WhiteboardServer is to handle server side Operational
Transformation operations
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/SConscript | 3 | ||||
-rw-r--r-- | Swiften/Whiteboard/Operations/WhiteboardInsertOperation.h | 32 | ||||
-rw-r--r-- | Swiften/Whiteboard/Operations/WhiteboardOperation.h | 60 | ||||
-rw-r--r-- | Swiften/Whiteboard/UnitTest/WhiteboardServerTest.cpp | 47 | ||||
-rw-r--r-- | Swiften/Whiteboard/WhiteboardServer.cpp | 32 | ||||
-rw-r--r-- | Swiften/Whiteboard/WhiteboardServer.h | 22 | ||||
-rw-r--r-- | Swiften/Whiteboard/WhiteboardTransformer.cpp | 20 | ||||
-rw-r--r-- | Swiften/Whiteboard/WhiteboardTransformer.h | 17 |
8 files changed, 233 insertions, 0 deletions
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 <Swiften/Whiteboard/Operations/WhiteboardOperation.h> + +#include <Swiften/Whiteboard/Elements/WhiteboardElement.h> + +namespace Swift { + class WhiteboardInsertOperation : public WhiteboardOperation { + public: + typedef boost::shared_ptr<WhiteboardInsertOperation> 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 <boost/smart_ptr/shared_ptr.hpp> +#include <string> + +namespace Swift { + class WhiteboardOperation { + public: + typedef boost::shared_ptr<WhiteboardOperation> 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 <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + +#include <boost/smart_ptr/make_shared.hpp> +#include <Swiften/Whiteboard/WhiteboardServer.h> +#include <Swiften/Whiteboard/Operations/WhiteboardInsertOperation.h> +#include <Swiften/Whiteboard/Elements/WhiteboardEllipseElement.h> + +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<WhiteboardInsertOperation>(); + firstOp->setID("0"); + server.handleLocalOperationReceived(firstOp); + WhiteboardInsertOperation::ref serverOp = boost::make_shared<WhiteboardInsertOperation>(); + serverOp->setID("b"); + serverOp->setParentID("0"); + serverOp->setPos(1); + server.handleLocalOperationReceived(serverOp); + WhiteboardInsertOperation::ref clientOp = boost::make_shared<WhiteboardInsertOperation>(); + WhiteboardEllipseElement::ref clientElement = boost::make_shared<WhiteboardEllipseElement>(0,0,0,0); + clientOp->setID("a"); + clientOp->setParentID("0"); + clientOp->setPos(1); + clientOp->setElement(clientElement); + WhiteboardInsertOperation::ref op = boost::dynamic_pointer_cast<WhiteboardInsertOperation>(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<WhiteboardEllipseElement>(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 <Swiften/Whiteboard/WhiteboardServer.h> +#include <Swiften/Whiteboard/WhiteboardTransformer.h> + +namespace Swift { + void WhiteboardServer::handleLocalOperationReceived(WhiteboardOperation::ref operation) { + operations_.push_back(operation); + } + + WhiteboardOperation::ref WhiteboardServer::handleClientOperationReceived(WhiteboardInsertOperation::ref operation) { + std::list<WhiteboardOperation::ref>::iterator it; + for (it = operations_.begin(); it != operations_.end(); ++it) { + if (operation->getParentID() == (*it)->getParentID()) { + WhiteboardInsertOperation::ref insertOperation = boost::dynamic_pointer_cast<WhiteboardInsertOperation>(*it); + if (insertOperation) { + std::pair<WhiteboardInsertOperation::ref, WhiteboardInsertOperation::ref> 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 <Swiften/Whiteboard/Operations/WhiteboardInsertOperation.h> + +#include <list> + +namespace Swift { + class WhiteboardServer { + public: + void handleLocalOperationReceived(WhiteboardOperation::ref operation); + WhiteboardOperation::ref handleClientOperationReceived(WhiteboardInsertOperation::ref operation); + + private: + std::list<WhiteboardOperation::ref> 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 <Swiften/Whiteboard/WhiteboardTransformer.h> +#include <boost/smart_ptr/make_shared.hpp> + +namespace Swift { + std::pair<WhiteboardInsertOperation::ref, WhiteboardInsertOperation::ref> WhiteboardTransformer::transform(WhiteboardInsertOperation::ref clientOp, WhiteboardInsertOperation::ref serverOp) { + std::pair<WhiteboardInsertOperation::ref, WhiteboardInsertOperation::ref> result; + result.first = boost::make_shared<WhiteboardInsertOperation>(*serverOp.get()); + result.first->setParentID(clientOp->getID()); + result.first->setPos(result.first->getPos()+1); + result.second = boost::make_shared<WhiteboardInsertOperation>(*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 <Swiften/Whiteboard/Operations/WhiteboardInsertOperation.h> +#include <utility> + +namespace Swift { + class WhiteboardTransformer { + public: + static std::pair<WhiteboardInsertOperation::ref, WhiteboardInsertOperation::ref> transform(WhiteboardInsertOperation::ref clientOp, WhiteboardInsertOperation::ref serverOp); + }; +} |