summaryrefslogtreecommitdiffstats
blob: d7ed3ad62a8231f19660626ba3f54afdd0d88ece (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
/*
 * Copyright (c) 2012 Mateusz Piękos
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

/*
 * Copyright (c) 2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Whiteboard/WhiteboardClient.h>

#include <iostream>
#include <memory>

#include <Swiften/Whiteboard/WhiteboardTransformer.h>

namespace Swift {
    WhiteboardOperation::ref WhiteboardClient::handleLocalOperationReceived(WhiteboardOperation::ref operation) {
        localOperations_.push_back(operation);

        WhiteboardOperation::ref op;
        WhiteboardInsertOperation::ref insertOp = std::dynamic_pointer_cast<WhiteboardInsertOperation>(operation);
        if (insertOp) {
            op = std::make_shared<WhiteboardInsertOperation>(*insertOp);
        }
        WhiteboardUpdateOperation::ref updateOp = std::dynamic_pointer_cast<WhiteboardUpdateOperation>(operation);
        if (updateOp) {
            op = std::make_shared<WhiteboardUpdateOperation>(*updateOp);
        }
        WhiteboardDeleteOperation::ref deleteOp = std::dynamic_pointer_cast<WhiteboardDeleteOperation>(operation);
        if (deleteOp) {
            op = std::make_shared<WhiteboardDeleteOperation>(*deleteOp);
        }

        if (!bridge_.empty()) {
            op->setParentID(bridge_.back()->getID());
        }
        bridge_.push_back(op);

        if (lastSentOperationID_.empty())
        {
            WhiteboardInsertOperation::ref insertOp = std::dynamic_pointer_cast<WhiteboardInsertOperation>(operation);
            if (insertOp) {
                op = std::make_shared<WhiteboardInsertOperation>(*insertOp);
            }
            WhiteboardUpdateOperation::ref updateOp = std::dynamic_pointer_cast<WhiteboardUpdateOperation>(operation);
            if (updateOp) {
                op = std::make_shared<WhiteboardUpdateOperation>(*updateOp);
            }
            WhiteboardDeleteOperation::ref deleteOp = std::dynamic_pointer_cast<WhiteboardDeleteOperation>(operation);
            if (deleteOp) {
                op = std::make_shared<WhiteboardDeleteOperation>(*deleteOp);
            }


            if (!serverOperations_.empty()) {
                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);
        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_.empty() && lastSentOperationID_ == bridge_.front()->getID()) {
                bridge_.erase(bridge_.begin());
            }

            if (!bridge_.empty() && (bridge_.front())->getParentID() == lastSentOperationID_) {
                lastSentOperationID_ = (bridge_.front())->getID();
                result.server = bridge_.front();
            }
            if (!result.server) {
                lastSentOperationID_.clear();
            }
        } else {
            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;
        }

        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;
        }
    }
}