/* * Copyright (c) 2012 Mateusz Piękos * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ #include #include #include namespace Swift { WhiteboardOperation::ref WhiteboardClient::handleLocalOperationReceived(WhiteboardOperation::ref operation) { localOperations_.push_back(operation); // if (bridge_.size() > 0) { WhiteboardOperation::ref op; WhiteboardInsertOperation::ref insertOp = boost::dynamic_pointer_cast(operation); if (insertOp) { op = boost::make_shared(*insertOp.get()); } WhiteboardUpdateOperation::ref updateOp = boost::dynamic_pointer_cast(operation); if (updateOp) { op = boost::make_shared(*updateOp.get()); } WhiteboardDeleteOperation::ref deleteOp = boost::dynamic_pointer_cast(operation); if (deleteOp) { op = boost::make_shared(*deleteOp.get()); } if (bridge_.size() > 0) { op->setParentID(bridge_.back()->getID()); } bridge_.push_back(op); // } if (lastSentOperationID_.empty()) { WhiteboardInsertOperation::ref insertOp = boost::dynamic_pointer_cast(operation); if (insertOp) { op = boost::make_shared(*insertOp.get()); } WhiteboardUpdateOperation::ref updateOp = boost::dynamic_pointer_cast(operation); if (updateOp) { op = boost::make_shared(*updateOp.get()); } WhiteboardDeleteOperation::ref deleteOp = boost::dynamic_pointer_cast(operation); if (deleteOp) { op = boost::make_shared(*deleteOp.get()); } if (serverOperations_.size() > 0) { op->setParentID(serverOperations_.back()->getID()); } lastSentOperationID_ = operation->getID(); return op; } else { return WhiteboardOperation::ref(); } } WhiteboardClient::Result WhiteboardClient::handleServerOperationReceived(WhiteboardOperation::ref operation) { serverOperations_.push_back(operation); // WhiteboardOperation::ref clientOp; // WhiteboardOperation::ref serverOp; Result result; // if (localOperations_.empty()) {// || localOperations_.back()->getID() == operation->getParentID()) { //Situation where client and server are in sync if (localOperations_.size() == serverOperations_.size()-1) { localOperations_.push_back(operation); // clientOp = operation; result.client = operation; } else if (lastSentOperationID_ == operation->getID()) { //Client received confirmation about own operation and it sends next operation to server if (bridge_.size() > 0 && lastSentOperationID_ == bridge_.front()->getID()) { bridge_.erase(bridge_.begin()); } /*std::list::iterator it; for (it = bridge_.begin(); it != bridge_.end(); ++it) { if ((*it)->getParentID() == lastSentOperationID_) { lastSentOperationID_ = (*it)->getID(); serverOp = *it; serverOp->setOrigin(WhiteboardOperation::Other); break; } }*/ if (bridge_.size() > 0 && (bridge_.front())->getParentID() == lastSentOperationID_) { lastSentOperationID_ = (bridge_.front())->getID(); result.server = bridge_.front(); } if (!result.server) { lastSentOperationID_.clear(); } } else { if (bridge_.size() > 0 && bridge_.front()->getParentID() == operation->getParentID()) { std::list::iterator it = bridge_.begin(); std::pair opPair; WhiteboardOperation::ref temp; opPair = WhiteboardTransformer::transform(*it, operation); temp = opPair.first; *it = opPair.second; std::string previousID = (*it)->getID(); ++it; for (; it != bridge_.end(); ++it) { opPair = WhiteboardTransformer::transform(*it, temp); temp = opPair.first; *it = opPair.second; (*it)->setParentID(previousID); previousID = (*it)->getID(); } temp->setParentID(localOperations_.back()->getID()); localOperations_.push_back(temp); result.client = temp; } else { //doesn't get executed /* std::list::reverse_iterator it; std::pair opPair; WhiteboardInsertOperation::ref temp = boost::dynamic_pointer_cast(operation); bool end = false; for (it = localOperations_.rbegin(); it != localOperations_.rend(); ++it) { while ((*it)->getParentID() == temp->getParentID()) { opPair = WhiteboardTransformer::transform(boost::dynamic_pointer_cast(*it), temp); if (bridge_.size() > 0) { opPair.second->setParentID(bridge_.back()->getID()); } temp = opPair.first; bridge_.push_back(opPair.second); if (it == localOperations_.rbegin()) { localOperations_.push_back(temp); result.client = temp; end = true; break; } else { --it; } } if (end) { break; }*/ } } return result; } void WhiteboardClient::print() { std::list::iterator it; std::cout << "Client" << std::endl; for(it = localOperations_.begin(); it != localOperations_.end(); ++it) { std::cout << (*it)->getID() << " " << (*it)->getPos() << std::endl; } std::cout << "Server" << std::endl; for(it = serverOperations_.begin(); it != serverOperations_.end(); ++it) { std::cout << (*it)->getID() << " " << (*it)->getPos() << std::endl; } } }