blob: 70271e61f80f770b870fef6d94eec4fbec1c53b9 (
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/*
* 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;
WhiteboardInsertOperation::ref insertOp = boost::dynamic_pointer_cast<WhiteboardInsertOperation>(operation);
if (insertOp) {
op = boost::make_shared<WhiteboardInsertOperation>(*insertOp.get());
}
WhiteboardUpdateOperation::ref updateOp = boost::dynamic_pointer_cast<WhiteboardUpdateOperation>(operation);
if (updateOp) {
op = boost::make_shared<WhiteboardUpdateOperation>(*updateOp.get());
}
WhiteboardDeleteOperation::ref deleteOp = boost::dynamic_pointer_cast<WhiteboardDeleteOperation>(operation);
if (deleteOp) {
op = boost::make_shared<WhiteboardDeleteOperation>(*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<WhiteboardInsertOperation>(operation);
if (insertOp) {
op = boost::make_shared<WhiteboardInsertOperation>(*insertOp.get());
}
WhiteboardUpdateOperation::ref updateOp = boost::dynamic_pointer_cast<WhiteboardUpdateOperation>(operation);
if (updateOp) {
op = boost::make_shared<WhiteboardUpdateOperation>(*updateOp.get());
}
WhiteboardDeleteOperation::ref deleteOp = boost::dynamic_pointer_cast<WhiteboardDeleteOperation>(operation);
if (deleteOp) {
op = boost::make_shared<WhiteboardDeleteOperation>(*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<WhiteboardOperation::ref>::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();
result.server->setOrigin(WhiteboardOperation::Other);
}
if (!result.server) {
lastSentOperationID_.clear();
}
} else {
if (bridge_.size() > 0 && bridge_.front()->getParentID() == operation->getParentID()) {
std::list<WhiteboardOperation::ref>::iterator it = bridge_.begin();
std::pair<WhiteboardOperation::ref, WhiteboardOperation::ref> 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<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);
result.client = temp;
end = true;
break;
} else {
--it;
}
}
if (end) {
break;
}*/
}
}
return result;
}
void WhiteboardClient::print() {
std::list<WhiteboardOperation::ref>::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;
}
}
}
|