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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
#include "uploadmanager.h"
#include "unpacker.h"
#include "packer.h"
#include "stm32crc.h"
static const int CHUNK_SIZE = 2000;
using std::function;
UploadManager::UploadManager(WatchConnector *watch, QObject *parent) :
QObject(parent), watch(watch), _lastUploadId(0), _state(StateNotStarted)
{
watch->setEndpointHandler(WatchConnector::watchPUTBYTES,
[this](const QByteArray &msg) {
if (_pending.empty()) {
logger()->warn() << "putbytes message, but queue is empty!";
return false;
}
handleMessage(msg);
return true;
});
}
uint UploadManager::upload(WatchConnector::UploadType type, int index, QIODevice *device, int size,
function<void()> successCallback, function<void(int)> errorCallback)
{
PendingUpload upload;
upload.id = ++_lastUploadId;
upload.type = type;
upload.index = index;
upload.device = device;
if (size < 0) {
upload.remaining = device->size();
} else {
upload.remaining = size;
}
upload.successCallback = successCallback;
upload.errorCallback = errorCallback;
if (upload.remaining <= 0) {
logger()->warn() << "upload is empty";
if (errorCallback) {
errorCallback(-1);
return -1;
}
}
_pending.enqueue(upload);
if (_pending.size() == 1) {
startNextUpload();
}
return upload.id;
}
void UploadManager::cancel(uint id, int code)
{
if (_pending.empty()) {
logger()->warn() << "cannot cancel, empty queue";
return;
}
if (id == _pending.head().id) {
PendingUpload upload = _pending.dequeue();
logger()->debug() << "aborting current upload" << id << "(code:" << code << ")";
if (_state != StateNotStarted && _state != StateWaitForToken && _state != StateComplete) {
QByteArray msg;
Packer p(&msg);
p.write<quint8>(WatchConnector::putbytesABORT);
p.write<quint32>(_token);
logger()->debug() << "sending abort for upload" << id;
watch->sendMessage(WatchConnector::watchPUTBYTES, msg);
}
_state = StateNotStarted;
_token = 0;
if (upload.errorCallback) {
upload.errorCallback(code);
}
if (!_pending.empty()) {
startNextUpload();
}
} else {
for (int i = 1; i < _pending.size(); ++i) {
if (_pending[i].id == id) {
logger()->debug() << "cancelling upload" << id << "(code:" << code << ")";
if (_pending[i].errorCallback) {
_pending[i].errorCallback(code);
}
_pending.removeAt(i);
return;
}
}
logger()->warn() << "cannot cancel, id" << id << "not found";
}
}
void UploadManager::startNextUpload()
{
Q_ASSERT(!_pending.empty());
Q_ASSERT(_state == StateNotStarted);
PendingUpload &upload = _pending.head();
QByteArray msg;
Packer p(&msg);
p.write<quint8>(WatchConnector::putbytesINIT);
p.write<quint32>(upload.remaining);
p.write<quint8>(upload.type);
p.write<quint8>(upload.index);
_state = StateWaitForToken;
watch->sendMessage(WatchConnector::watchPUTBYTES, msg);
}
void UploadManager::handleMessage(const QByteArray &msg)
{
Q_ASSERT(!_pending.empty());
PendingUpload &upload = _pending.head();
Unpacker u(msg);
int status = u.read<quint8>();
if (u.bad() || status != 1) {
logger()->warn() << "upload" << upload.id << "got error code=" << status;
cancel(upload.id, status);
return;
}
quint32 recv_token = u.read<quint32>();
if (u.bad()) {
logger()->warn() << "upload" << upload.id << ": could not read the token";
cancel(upload.id, -1);
return;
}
if (_state != StateNotStarted && _state != StateWaitForToken && _state != StateComplete) {
if (recv_token != _token) {
logger()->warn() << "upload" << upload.id << ": invalid token";
cancel(upload.id, -1);
return;
}
}
switch (_state) {
case StateNotStarted:
logger()->warn() << "got packet when upload is not started";
break;
case StateWaitForToken:
logger()->debug() << "token received";
_token = recv_token;
_state = StateInProgress;
/* fallthrough */
case StateInProgress:
logger()->debug() << "moving to the next chunk";
if (upload.remaining > 0) {
if (!uploadNextChunk(upload)) {
cancel(upload.id, -1);
return;
}
} else {
logger()->debug() << "no additional chunks, commit";
_state = StateCommit;
if (!commit(upload)) {
cancel(upload.id, -1);
return;
}
}
break;
case StateCommit:
logger()->debug() << "commited succesfully";
_state = StateComplete;
if (!complete(upload)) {
cancel(upload.id, -1);
return;
}
break;
case StateComplete:
logger()->debug() << "upload" << upload.id << "succesful, invoking callback";
if (upload.successCallback) {
upload.successCallback();
}
_pending.dequeue();
_token = 0;
_state = StateNotStarted;
if (!_pending.empty()) {
startNextUpload();
}
break;
default:
logger()->warn() << "received message in wrong state";
break;
}
}
bool UploadManager::uploadNextChunk(PendingUpload &upload)
{
QByteArray chunk = upload.device->read(qMin<int>(upload.remaining, CHUNK_SIZE));
if (upload.remaining < CHUNK_SIZE && chunk.size() < upload.remaining) {
// Short read!
logger()->warn() << "short read during upload" << upload.id;
return false;
}
Q_ASSERT(!chunk.isEmpty());
Q_ASSERT(_state = StateInProgress);
QByteArray msg;
Packer p(&msg);
p.write<quint8>(WatchConnector::putbytesSEND);
p.write<quint32>(_token);
p.write<quint32>(chunk.size());
msg.append(chunk);
logger()->debug() << "sending a chunk of" << chunk.size() << "bytes";
watch->sendMessage(WatchConnector::watchPUTBYTES, msg);
upload.remaining -= chunk.size();
upload.crc.addData(chunk);
logger()->debug() << "remaining" << upload.remaining << "bytes";
return true;
}
bool UploadManager::commit(PendingUpload &upload)
{
Q_ASSERT(_state == StateCommit);
Q_ASSERT(upload.remaining == 0);
QByteArray msg;
Packer p(&msg);
p.write<quint8>(WatchConnector::putbytesCOMMIT);
p.write<quint32>(_token);
p.write<quint32>(upload.crc.result());
logger()->debug() << "commiting upload" << upload.id
<< "with crc" << qPrintable(QString("0x%1").arg(upload.crc.result(), 0, 16));
watch->sendMessage(WatchConnector::watchPUTBYTES, msg);
return true;
}
bool UploadManager::complete(PendingUpload &upload)
{
Q_ASSERT(_state == StateComplete);
QByteArray msg;
Packer p(&msg);
p.write<quint8>(WatchConnector::putbytesCOMPLETE);
p.write<quint32>(_token);
logger()->debug() << "completing upload" << upload.id;
watch->sendMessage(WatchConnector::watchPUTBYTES, msg);
return true;
}
|