diff options
author | Mateusz Piekos <mateuszpiekos@gmail.com> | 2012-07-05 15:06:57 (GMT) |
---|---|---|
committer | Mateusz Piekos <mateuszpiekos@gmail.com> | 2012-07-05 15:06:57 (GMT) |
commit | 0071e9ed46775ee4e49ed969099ce08c49c9a07b (patch) | |
tree | c29abf4e0f67a995148766086fd435b5455b5096 /Swiften/Whiteboard | |
parent | 349cda4b8e178dc4a6694c47a3eb45e5a1368fb8 (diff) | |
download | swift-contrib-0071e9ed46775ee4e49ed969099ce08c49c9a07b.zip swift-contrib-0071e9ed46775ee4e49ed969099ce08c49c9a07b.tar.bz2 |
Handling less simple server side operations
Diffstat (limited to 'Swiften/Whiteboard')
-rw-r--r-- | Swiften/Whiteboard/UnitTest/WhiteboardServerTest.cpp | 34 | ||||
-rw-r--r-- | Swiften/Whiteboard/WhiteboardServer.cpp | 30 |
2 files changed, 53 insertions, 11 deletions
diff --git a/Swiften/Whiteboard/UnitTest/WhiteboardServerTest.cpp b/Swiften/Whiteboard/UnitTest/WhiteboardServerTest.cpp index 3d17064..c61f9d1 100644 --- a/Swiften/Whiteboard/UnitTest/WhiteboardServerTest.cpp +++ b/Swiften/Whiteboard/UnitTest/WhiteboardServerTest.cpp @@ -18,6 +18,7 @@ using namespace Swift; class WhiteboardServerTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(WhiteboardServerTest); CPPUNIT_TEST(testSimpleOp); + CPPUNIT_TEST(testFewSimpleOps); CPPUNIT_TEST_SUITE_END(); public: void testSimpleOp() { @@ -42,6 +43,39 @@ public: CPPUNIT_ASSERT_EQUAL(1, op->getPos()); CPPUNIT_ASSERT_EQUAL(clientElement, boost::dynamic_pointer_cast<WhiteboardEllipseElement>(op->getElement())); } + + void testFewSimpleOps() { + WhiteboardServer server; + WhiteboardInsertOperation::ref firstOp = boost::make_shared<WhiteboardInsertOperation>(); + firstOp->setID("0"); + server.handleLocalOperationReceived(firstOp); + WhiteboardInsertOperation::ref serverOp = boost::make_shared<WhiteboardInsertOperation>(); + serverOp->setID("a"); + serverOp->setParentID("0"); + serverOp->setPos(1); + server.handleLocalOperationReceived(serverOp); + serverOp = boost::make_shared<WhiteboardInsertOperation>(); + serverOp->setID("b"); + serverOp->setParentID("a"); + serverOp->setPos(2); + server.handleLocalOperationReceived(serverOp); + serverOp = boost::make_shared<WhiteboardInsertOperation>(); + serverOp->setID("c"); + serverOp->setParentID("b"); + serverOp->setPos(3); + server.handleLocalOperationReceived(serverOp); + WhiteboardInsertOperation::ref clientOp = boost::make_shared<WhiteboardInsertOperation>(); + WhiteboardEllipseElement::ref clientElement = boost::make_shared<WhiteboardEllipseElement>(0,0,0,0); + clientOp->setID("d"); + 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("c"), op->getParentID()); + CPPUNIT_ASSERT_EQUAL(std::string("d"), 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 index 89fb00d..a12e4e8 100644 --- a/Swiften/Whiteboard/WhiteboardServer.cpp +++ b/Swiften/Whiteboard/WhiteboardServer.cpp @@ -12,18 +12,26 @@ namespace Swift { 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); + WhiteboardOperation::ref WhiteboardServer::handleClientOperationReceived(WhiteboardInsertOperation::ref newOperation) { + std::list<WhiteboardOperation::ref>::reverse_iterator it; + for (it = operations_.rbegin(); it != operations_.rend(); ++it) { + WhiteboardOperation::ref operation = *it; + + while (newOperation->getParentID() == operation->getParentID()) { + WhiteboardInsertOperation::ref insertOperation = boost::dynamic_pointer_cast<WhiteboardInsertOperation>(operation); 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); + std::pair<WhiteboardInsertOperation::ref, WhiteboardInsertOperation::ref> tResult = WhiteboardTransformer::transform(newOperation, insertOperation); + + if (it == operations_.rbegin()) { + operations_.push_back(tResult.second); + return tResult.second; + } else { + newOperation = tResult.second; + --it; + operation = *it; + } + } else { + operations_.push_back(operation); return *it; } } |