-
Notifications
You must be signed in to change notification settings - Fork 0
/
remotesync.cpp
212 lines (170 loc) · 5.72 KB
/
remotesync.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "remotesync.h"
#include "protocolparser.h"
#include "remoteauthentication.h"
#include "remotestorage.h"
RemoteSync::RemoteSync(DatabaseInterface *database, RemoteDataStorage* remoteStorage, QObject *parent) :
QObject(parent),
fDatabase(database),
fRemoteStorage(remoteStorage),
fAuthentication(remoteStorage->getRemoteAuthentication()),
fRemoteConnection(remoteStorage->getRemoteAuthentication()->getConnection()),
fServerReply(NULL)
{
}
RemoteSync::~RemoteSync()
{
}
WP::err RemoteSync::sync()
{
if (fAuthentication->isVerified())
syncConnected(WP::kOk);
else {
connect(fAuthentication, SIGNAL(authenticationAttemptFinished(WP::err)),
this, SLOT(syncConnected(WP::err)));
fAuthentication->login();
}
return WP::kOk;
}
void RemoteSync::syncConnected(WP::err code)
{
if (code != WP::kOk)
return;
QString branch = fDatabase->branch();
QString lastSyncCommit = fDatabase->getLastSyncCommit(fRemoteStorage->getUid(), branch);
QByteArray outData;
ProtocolOutStream outStream(&outData);
IqOutStanza *iqStanza = new IqOutStanza(kGet);
outStream.pushStanza(iqStanza);
OutStanza *syncStanza = new OutStanza("sync_pull");
syncStanza->addAttribute("branch", branch);
syncStanza->addAttribute("base", lastSyncCommit);
outStream.pushChildStanza(syncStanza);
outStream.flush();
if (fServerReply != NULL)
fServerReply->disconnect();
fServerReply = fRemoteConnection->send(outData);
connect(fServerReply, SIGNAL(finished(WP::err)), this, SLOT(syncReply(WP::err)));
}
class SyncPullData {
public:
QString branch;
QString tip;
QByteArray pack;
};
class SyncPullPackHandler : public InStanzaHandler {
public:
SyncPullPackHandler(SyncPullData *d) :
InStanzaHandler("pack", true),
data(d)
{
}
bool handleStanza(const QXmlStreamAttributes &attributes)
{
return true;
}
bool handleText(const QStringRef &text)
{
data->pack = QByteArray::fromBase64(text.toLatin1());
return true;
}
public:
SyncPullData *data;
};
class SyncPullHandler : public InStanzaHandler {
public:
SyncPullHandler(SyncPullData *d) :
InStanzaHandler("sync_pull"),
data(d)
{
syncPullPackHandler = new SyncPullPackHandler(data);
addChildHandler(syncPullPackHandler);
}
bool handleStanza(const QXmlStreamAttributes &attributes)
{
if (!attributes.hasAttribute("branch"))
return false;
if (!attributes.hasAttribute("tip"))
return false;
data->branch = attributes.value("branch").toString();
data->tip = attributes.value("tip").toString();
return true;
}
public:
SyncPullPackHandler *syncPullPackHandler;
SyncPullData *data;
};
void RemoteSync::syncReply(WP::err code)
{
if (code != WP::kOk)
return;
QByteArray data = fServerReply->readAll();
fServerReply = NULL;
IqInStanzaHandler iqHandler(kResult);
SyncPullData syncPullData;
SyncPullHandler *syncPullHandler = new SyncPullHandler(&syncPullData);
iqHandler.addChildHandler(syncPullHandler);
ProtocolInStream inStream(data);
inStream.addHandler(&iqHandler);
inStream.parse();
QString localBranch = fDatabase->branch();
QString localTipCommit = fDatabase->getTip();
QString lastSyncCommit = fDatabase->getLastSyncCommit(fRemoteStorage->getUid(), localBranch);
if (!syncPullHandler->hasBeenHandled() || syncPullData.branch != localBranch) {
// error occured, the server should at least send back the branch name
// TODO better error message
emit syncFinished(WP::kBadValue);
return;
}
if (syncPullData.tip == localTipCommit) {
// done
emit syncFinished(WP::kOk);
return;
}
// see if the server is ahead by checking if we got packages
if (syncPullData.pack.size() != 0) {
fSyncUid = syncPullData.tip;
WP::err error = fDatabase->importPack(syncPullData.pack, lastSyncCommit,
syncPullData.tip);
if (error != WP::kOk) {
emit syncFinished(error);
return;
}
localTipCommit = fDatabase->getTip();
// done? otherwise it was a merge and we have to push our merge
if (localTipCommit == lastSyncCommit) {
emit syncFinished(WP::kOk);
return;
}
}
// we are ahead of the server: push changes to the server
QByteArray pack;
WP::err error = fDatabase->exportPack(pack, lastSyncCommit, localTipCommit, fSyncUid);
if (error != WP::kOk) {
emit syncFinished(error);
return;
}
fSyncUid = localTipCommit;
QByteArray outData;
ProtocolOutStream outStream(&outData);
IqOutStanza *iqStanza = new IqOutStanza(kSet);
outStream.pushStanza(iqStanza);
OutStanza *pushStanza = new OutStanza("sync_push");
pushStanza->addAttribute("branch", localBranch);
pushStanza->addAttribute("start_commit", syncPullData.tip);
pushStanza->addAttribute("last_commit", localTipCommit);
outStream.pushChildStanza(pushStanza);
OutStanza *pushPackStanza = new OutStanza("pack");
pushPackStanza->setText(pack.toBase64());
outStream.pushChildStanza(pushPackStanza);
outStream.flush();
fServerReply = fRemoteConnection->send(outData);
connect(fServerReply, SIGNAL(finished(WP::err)), this, SLOT(syncPushReply(WP::err)));
}
void RemoteSync::syncPushReply(WP::err code)
{
if (code != WP::kOk)
return;
QByteArray data = fServerReply->readAll();
fDatabase->updateLastSyncCommit(fRemoteStorage->getUid(), fDatabase->branch(), fSyncUid);
emit syncFinished(WP::kOk);
}