blob: cc41e8563c7c1a35e049cececc86bb284a9ee383 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
/*
* Copyright (c) 2012 Mateusz Piękos
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <Swiften/Whiteboard/WhiteboardClient.h>
#include <Swiften/Whiteboard/WhiteboardTransformer.h>
#include <boost/smart_ptr/make_shared.hpp>
namespace Swift {
WhiteboardOperation::ref WhiteboardClient::handleLocalOperationReceived(WhiteboardOperation::ref operation) {
localOperations_.push_back(operation);
if (bridge_.size() > 0) {
WhiteboardOperation::ref op = boost::make_shared<WhiteboardInsertOperation>(*boost::dynamic_pointer_cast<WhiteboardInsertOperation>(operation).get());
op->setParentID(bridge_.back()->getID());
bridge_.push_back(op);
}
if (lastSentOperationID_.empty())
{
lastSentOperationID_ = operation->getID();
return operation;
}
return WhiteboardOperation::ref();
}
std::pair<WhiteboardOperation::ref, WhiteboardOperation::ref> WhiteboardClient::handleServerOperationReceived(WhiteboardOperation::ref operation) {
serverOperations_.push_back(operation);
WhiteboardOperation::ref clientOp;
WhiteboardOperation::ref serverOp;
if (localOperations_.empty()) {// || localOperations_.back()->getID() == operation->getParentID()) {
localOperations_.push_back(operation);
clientOp = operation;
} else if (lastSentOperationID_ == operation->getID()) {
std::list<WhiteboardOperation::ref>::iterator it;
for (it = bridge_.begin(); it != bridge_.end(); ++it) {
if ((*it)->getParentID() == lastSentOperationID_) {
lastSentOperationID_ = (*it)->getID();
serverOperations_.push_back(*it);
serverOp = *it;
serverOp->setOrigin(WhiteboardOperation::Other);
break;
}
}
} else {
if (bridge_.size() > 0 && bridge_.front()->getParentID() == operation->getParentID()) {
std::list<WhiteboardOperation::ref>::iterator it = bridge_.begin();
std::pair<WhiteboardInsertOperation::ref, WhiteboardInsertOperation::ref> opPair;
WhiteboardInsertOperation::ref temp;
opPair = WhiteboardTransformer::transform(boost::dynamic_pointer_cast<WhiteboardInsertOperation>(*it), boost::dynamic_pointer_cast<WhiteboardInsertOperation>(operation));
temp = opPair.first;
*it = opPair.second;
std::string previousID = (*it)->getID();
++it;
for (; it != bridge_.end(); ++it) {
opPair = WhiteboardTransformer::transform(boost::dynamic_pointer_cast<WhiteboardInsertOperation>(*it), temp);
temp = opPair.first;
*it = opPair.second;
(*it)->setParentID(previousID);
previousID = (*it)->getID();
}
temp->setParentID(localOperations_.back()->getID());
localOperations_.push_back(temp);
clientOp = temp;
} else {
std::list<WhiteboardOperation::ref>::reverse_iterator it;
std::pair<WhiteboardInsertOperation::ref, WhiteboardInsertOperation::ref> opPair;
WhiteboardInsertOperation::ref temp = boost::dynamic_pointer_cast<WhiteboardInsertOperation>(operation);
bool end = false;
for (it = localOperations_.rbegin(); it != localOperations_.rend(); ++it) {
while ((*it)->getParentID() == temp->getParentID()) {
opPair = WhiteboardTransformer::transform(boost::dynamic_pointer_cast<WhiteboardInsertOperation>(*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);
clientOp = temp;
end = true;
break;
} else {
--it;
}
}
if (end) {
break;
}
}
}
}
return std::pair<WhiteboardOperation::ref, WhiteboardOperation::ref>(clientOp, serverOp);
}
}
|