-
Notifications
You must be signed in to change notification settings - Fork 1
/
incoming_jslib.js
486 lines (444 loc) · 19.9 KB
/
incoming_jslib.js
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Incoming!! JavaScript library
//
// Copyright (C) 2014 Lars Tiede, UiT The Arctic University of Norway
//
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
_incoming_lib = function() {
var incoming_lib = {};
var server_hostname = window.location.host;
incoming_lib.set_server_hostname = function set_server_hostname(hostname) {
server_hostname = hostname;
};
var msgUploadReq = function msgUploadReq(upload_id, length_bytes, name) {
var msg = {
MsgType: "MsgUploadReq",
MsgData : {
Id: upload_id,
LengthBytes: length_bytes,
Name: name
}
};
return JSON.stringify(msg);
};
var msgAck = function msgAck(ack) {
var msg = {
MsgType: "MsgAck",
MsgData: {
Ack: ack
}
};
return JSON.stringify(msg);
};
var msgCancelAck = function msgCancelAck(ack) {
var msg = {
MsgType: "MsgCancelAck",
MsgData: {
Ack: ack
}
};
return JSON.stringify(msg);
};
var msgPause = function msgPause() {
var msg = {
MsgType: "MsgPause",
MsgData: {
Pause: true
}
};
return JSON.stringify(msg);
};
var msgCancel = function msgCancel(reason) {
var msg = {
MsgType: "MsgCancel",
MsgData: {
Reason: reason
}
};
return JSON.stringify(msg);
};
var msgError = function msgError(reason) {
var msg = {
MsgType: "MsgError",
MsgData: {
ErrorCode: 0,
Msg: reason
}
};
return JSON.stringify(msg);
};
incoming_lib.Uploader = function Uploader(upload_id, file) {
var ul = {};
var ws = null; // WebSocket - opened in start()
var file_reader = new FileReader();
var upload_conf = null; // from incoming - has ChunkSizeKB,
// FilePos (for resume), SendAhead.
// Set in start()
var conn_retry = null;
ul.filename = file.name;
ul.chunks_tx_now = 0; // "now" because upload could have been resumed
ul.chunks_acked_now = 0; // "now" because upload could have been resumed
ul.bytes_tx = 0; // bytes sent over websocket
ul.bytes_acked = 0; // bytes acknowledged by incoming backend
ul.bytes_total = file.size;
ul.frac_complete = 0.0;
ul.finished = false;
ul.cancelled = false;
ul.cancelling = false;
ul.can_cancel = true;
ul.can_pause = false;
ul.connected = false;
ul.paused = false;
ul.chunks_ahead = 0; // how many chunks have been sent without
// having gotten acknowledgement from incoming
// (upload_conf.SendAhead is upper limit for this)
ul.bytes_ahead = 0; // how many bytes have been sent without
// having gotten acknowledgement from incoming
ul.error_code = null; // set only in case of error
ul.error_msg = null;
ul.cancel_msg = null;
ul.state_msg = "not yet started"; // purely informal, human readable state
// information
// the following callbacks should be set by the caller directly. All are
// functions taking one parameter: the uploader object.
ul.onprogress = function(o){};
ul.onfinished = function(o){};
ul.oncancelled = function(o){};
ul.onerror = function(o){};
// try_load_and_send_file_chunk is the function we call when we want to
// send chunks. It makes file_reader load a chunk from the file. When
// that chunk is loaded, file_reader will send it over the websocket
// and call this function again (see onloadend below). In consequence,
// this function will be called even if we can't send anything at the
// moment. We call it
// - whenever we have just sent a chunk in onloadend (because we might
// be able to send another one)
// - when we receive an ack from the incoming backend that a chunk
// has been transferred (in receive_chunk_acks), and
// - once in the beginning, in start().
var try_load_and_send_file_chunk = function try_load_and_send_file_chunk() {
// are we clear to transfer more right now? if yes, make
// file_reader load (and in onloadend send) the next chunk
if (ul.chunks_ahead < upload_conf.SendAhead &&
file_reader.readyState != FileReader.LOADING &&
ul.bytes_tx < ul.bytes_total &&
!ul.cancelling) {
var end = ul.bytes_tx + (upload_conf.ChunkSizeKB*1024);
if (end > ul.bytes_total) {
end = ul.bytes_total;
}
var blob = file.slice(ul.bytes_tx, end);
file_reader.readAsArrayBuffer(blob);
}
};
// file_reader - when a chunk is loaded, send it over the websocket,
// then try to load&send another chunk. If something went wrong
// during loading, cancel the upload.
file_reader.onload = function onload(evt) {
if (evt.target.readyState == FileReader.DONE &&
evt.target.result != null &&
!ul.cancelling && !ul.cancelled && !ul.paused) {
// send chunk if websocket is open
if (ws.readyState == WebSocket.OPEN) {
buf = evt.target.result;
ws.send(buf);
// update state
ul.bytes_tx += buf.byteLength;
ul.bytes_ahead += buf.byteLength;
ul.chunks_tx_now += 1;
ul.chunks_ahead += 1;
// if this was the last chunk, we can no longer
// cancel or pause
if (ul.bytes_tx == ul.bytes_total) {
ul.can_cancel = false;
ul.can_pause = false;
}
// call progress cb
ul.onprogress(ul);
// try to load&send another chunk
try_load_and_send_file_chunk();
}
} else {
if (!ul.cancelling && !ul.cancelled && !ul.paused) {
ul.cancel("unexpected file_reader.onloadend error");
}
}
};
file_reader.onerror = function onerror(evt) {
if (!ul.cancelled) {
ul.cancel("error on file load: " + evt.name + " " + evt.message);
}
};
// when we receive an ack from the incoming backend that a chunk has
// been received, we can try to load&send another chunk. When all
// chunks have been sent and acked, we are finished reading chunk acks.
// (Instead, we will wait for the final "upload is done" message)
var receive_chunk_acks = function receive_chunk_acks(msg) {
// after handshake is done in start(), this is ws.onmessage.
var obj = JSON.parse(msg.data);
if (obj.MsgType == "MsgChunkAck") {
// update state
ul.bytes_acked += obj.MsgData.ChunkSize;
ul.frac_complete = ul.bytes_acked / ul.bytes_total;
ul.bytes_ahead -= obj.MsgData.ChunkSize;
ul.chunks_acked_now += 1;
ul.chunks_ahead -= 1;
// if all file chunks have been acked, set ws.onmessage to wait
// for final "upload complete" message. if not, send more
// chunks
if (ul.bytes_acked == ul.bytes_total) {
ul.state_msg = "handing file to app";
ws.onmessage = receive_final_message;
} else {
try_load_and_send_file_chunk();
}
// call progress cb
ul.onprogress(ul);
} else if (obj.MsgType == "MsgError") {
ul.error_code = obj.MsgData.ErrorCode;
ul.error_msg = obj.MsgData.Msg;
ul.onprogress(ul);
ul.onerror(ul);
ul.cancel("Error from server: " + ul.error_msg);
} else if (obj.MsgType == "MsgCancel") {
ul.cancel(obj.MsgData.Reason);
} else {
alert("Bug! Didn't understand what came out of the socket");
}
};
// when all is uploaded, we wait for the incoming!! backend to tell us
// that the upload is done; that even the web app backend is done
// fetching the file from the incoming!! backend.
var receive_final_message = function receive_final_message(msg) {
var obj = JSON.parse(msg.data);
if (obj.MsgType == "MsgAllDone") {
ul.finished = true;
ws.close();
ul.state_msg = "all done";
ul.onprogress(ul);
ul.onfinished(ul);
} else if (obj.MsgType == "MsgError") {
ul.error_code = obj.MsgData.ErrorCode;
ul.error_msg = obj.MsgData.Msg;
ws.close();
ul.state_msg = "Upload failed on server side: " + ul.error_msg;
ul.onprogress(ul);
ul.onerror(ul);
}
};
ul.start = function start() {
// this is called on start and restarts too (when connection is lost).
// So we (re)initialize some state first
ul.chunks_tx_now = 0;
ul.chunks_acked_now = 0;
ul.bytes_tx = ul.bytes_acked;
ul.frac_complete = ul.bytes_acked / ul.bytes_total;
ul.chunks_ahead = 0;
ul.bytes_ahead = 0;
ul.connected = false;
ul.paused = false;
ul.can_cancel = true;
ul.can_pause = false;
ul.state_msg = "connecting to upload server";
// figure out whether we want an encrypted ("wss:") or unencrypted ("ws:")
// WebSocket, depending on whether the browser got the page with HTTPS.
var ws_scheme = "wss"; // we default to encrypted
if (window.location.protocol == "http:") {
ws_scheme = "ws";
}
// open WebSocket
ws = new WebSocket(ws_scheme + '://' + server_hostname + '/incoming/0.1/frontend/upload_ws');
//ws.binaryType = "arraybuffer"; // relevant only for recv? http://dev.w3.org/html5/websockets/#dom-websocket-binarytype
ws.onopen = function onopen() {
// we handle the handshake here, up until we start
// sending file chunks.
ul.connected = true;
ul.state_msg = "upload protocol handshake"
// send upload request
ws.send(msgUploadReq(upload_id, file.size, file.name));
// receive error or upload config
ws.onmessage = function prot01_recvConfig(msg) {
var obj = JSON.parse(msg.data);
if (obj.MsgType == "MsgError") {
ul.error_code = obj.MsgData.ErrorCode;
ul.error_msg = obj.MsgData.Msg;
ul.onprogress(ul);
ul.onerror(ul);
ul.cancel("can't handle '" + ul.error_msg + "'");
} else if (obj.MsgType == "MsgUploadConf") {
// got upload config. set us up for upload!
upload_conf = obj.MsgData;
ul.bytes_acked = upload_conf.FilePos;
ul.frac_complete = ul.bytes_acked / ul.bytes_total;
ul.bytes_tx = ul.bytes_acked;
ws.send(msgAck(true));
// we might already be finished uploading data...
if (ul.bytes_acked == ul.bytes_total) {
ul.can_cancel = false;
ul.can_pause = false;
ul.state_msg = "processing file on server";
ws.onmessage = receive_final_message;
} else {
ul.state_msg = "transfer file chunks to upload server"
ul.can_pause = true;
ws.onmessage = receive_chunk_acks;
// start uploading chunks
try_load_and_send_file_chunk();
}
ul.onprogress(ul);
} else {
alert("Bug! Didn't understand what came out of the socket");
}
};
};
ws.onclose = function onclose(msg) {
ul.connected = false;
if (ul.paused || ul.finished || ul.cancelled || ul.error_code != null) {
ul.onprogress(ul);
ws = null;
return;
}
// try to start again every 60 seconds
// as of now, this must be greater than WebSocketConnectionTimeoutS
// in the backend.
ul.state_msg = "lost connection, trying to start again every 60 seconds";
ul.connected = false;
conn_retry = setTimeout(ul.start, 60000);
ws = null;
ul.can_pause = true;
ul.onprogress(ul);
// The event object passed to "onclose" has three fields named
// "code", "reason", and "wasClean". The "code" is a numeric
// status provided by the server, and can hold the same values
// as the "code" argument of close(). The "reason" field is a
// string describing the circumstances of the "close" event.
}
ws.onerror = function onerror(evt_error) {
ws.close();
//ul.error_code = 0;
//ul.error_msg = "websocket error: " + evt_error.message;
//ul.onerror(ul);
//ul.cancel("websocket error: " + evt_error.message);
}
return ul;
};
ul.cancel = function cancel(reason) {
if (!ul.cancelled && ul.can_cancel) {
file_reader.abort()
if (ws != null && ws.readyState == WebSocket.OPEN) {
ul.cancelling = true;
ws.send(msgCancel(reason));
// we need the backend to ack this, so we set ws.onmessage to
// receive a MsgCancelAck. However, there might be other
// messages arriving before that (most likely chunk acks),
// so we need to call whichever "usual" message handler we have
// in ws.onmessage in case we don't get a MsgCancelAck.
var usual_onmessage_handler = ws.onmessage;
ws.onmessage = function recv_cancel_ack(msg) {
var obj = JSON.parse(msg.data);
if (obj.MsgType == "MsgCancelAck") {
ul.cancelled = true;
ul.cancelling = false;
ul.can_cancel = false;
ul.state_msg = "cancelled: " + reason;
ul.cancel_msg = reason;
ws.close();
ul.onprogress(ul);
ul.oncancelled(ul);
} else {
// we have received a different message - most
// likely chunk acks. Call message handler that we
// would call normally.
usual_onmessage_handler(msg);
// message handler might have changed ws.onmessage
if (ws.onmessage != recv_cancel_ack) {
usual_onmessage_handler = ws.onmessage;
ws.onmessage = recv_cancel_ack;
}
}
};
} else {
// we couldn't send a cancel message to the backend
ul.cancelled = true;
ul.can_cancel = false;
ul.state_msg = "cancelled: " + reason;
ul.cancel_msg = reason;
ul.error_code = 0;
ul.error_msg = "upload is cancelled, but the backend doesn't know it yet";
ul.onprogress(ul);
ul.onerror(ul);
ul.oncancelled(ul)
}
// the following regardless of whether we could send a cancel
// message to the backend or not
ul.can_cancel = false;
ul.can_pause = false;
ul.state_msg = "cancelling: " + reason;
if (conn_retry != null) {
clearTimeout(conn_retry);
conn_retry = null;
}
ul.onprogress(ul);
}
return ul;
};
// pause pauses, unpauses, or toggles pause, depending on the
// parameter. "pause" pauses, "unpause" unpauses, "toggle" toggles.
ul.pause = function pause(what) {
if (ul.finished || ul.cancelled) {
return;
}
var new_state = false;
if (what == "pause") {new_state = true;}
else if (what == "unpause") {new_state = false;}
else if (what == "toggle") {new_state = !ul.paused;}
else {alert("Bug! Uploader.pause() called with unknown parameter!");}
if (new_state == ul.paused) {
return;
}
if (conn_retry != null) {
clearTimeout(conn_retry);
conn_retry = null;
}
if (new_state) {
// pause upload
file_reader.abort();
if (ws != null && ws.readyState == WebSocket.OPEN) {
ws.send(msgPause());
ws.close();
}
ul.paused = true;
ul.can_cancel = false;
ul.state_msg = "paused";
ul.onprogress(ul);
} else {
// unpause upload: call ul.start(), which should connect and resume.
ul.paused = false;
ul.state_msg = "unpaused";
ul.onprogress(ul);
ul.start();
}
};
return ul;
};
return incoming_lib;
}
incoming = _incoming_lib();