forked from cory/tildefriends
blob wants from JS -> C.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4106 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
94
core/ssb.js
94
core/ssb.js
@ -2,10 +2,7 @@
|
||||
var g_wants_requests = {};
|
||||
var g_database = new Database('core');
|
||||
let g_attendants = {};
|
||||
let g_blob_wants_sent = 0;
|
||||
let g_blob_last_requested = '';
|
||||
const k_use_create_history_stream = false;
|
||||
const k_blobs_concurrent_target = 8;
|
||||
|
||||
function get_latest_sequence_for_author(author) {
|
||||
var sequence = 0;
|
||||
@ -48,72 +45,8 @@ function tunnel_attendants(request) {
|
||||
});
|
||||
}
|
||||
|
||||
function get_more_blobs(connection) {
|
||||
while (Object.keys(connection.active_blob_gets).length < k_blobs_concurrent_target) {
|
||||
let next = Object.keys(connection.blob_get_queue).pop();
|
||||
let expected_bytes = connection.blob_get_queue[next];
|
||||
if (!next) {
|
||||
break;
|
||||
}
|
||||
delete connection.blob_get_queue[next];
|
||||
connection.active_blob_gets[next] = true;
|
||||
|
||||
let received_bytes = 0;
|
||||
let buffer = new Uint8Array(expected_bytes);
|
||||
connection.send_json({'name': ['blobs', 'get'], 'type': 'source', 'args': [next]}, function(message) {
|
||||
if (message.flags & 0x4 /* end */) {
|
||||
delete connection.active_blob_gets[next];
|
||||
} else {
|
||||
buffer.set(new Uint8Array(message.message, 0, message.message.byteLength), received_bytes);
|
||||
received_bytes += message.message.byteLength;
|
||||
if (received_bytes == expected_bytes) {
|
||||
ssb.blobStore(buffer);
|
||||
delete connection.active_blob_gets[next];
|
||||
get_more_blobs(connection);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function send_blobs_create_wants(connection) {
|
||||
connection.send_json({'name': ['blobs', 'createWants'], 'type': 'source', 'args': []}, function on_blob_create_wants(message) {
|
||||
if (message.message?.name === 'Error') {
|
||||
return;
|
||||
}
|
||||
Object.keys(message.message).forEach(function(id) {
|
||||
g_blob_wants_sent--;
|
||||
if (g_blob_wants_sent == 0) {
|
||||
request_more_blobs(g_wants_requests[connection.id]);
|
||||
}
|
||||
if (message.message[id] < 0) {
|
||||
let blob;
|
||||
if (g_wants_requests[connection.id]) {
|
||||
blob = ssb.blobGet(id);
|
||||
if (blob) {
|
||||
let out_message = {};
|
||||
out_message[id] = blob.byteLength;
|
||||
g_wants_requests[connection.id].send_json(out_message);
|
||||
}
|
||||
}
|
||||
if (!blob && message.message[id] == -1) {
|
||||
let out_message = {};
|
||||
out_message[id] = -2;
|
||||
g_wants_requests[connection.id].send_json(out_message);
|
||||
}
|
||||
} else {
|
||||
let expected_bytes = message.message[id];
|
||||
connection.blob_get_queue[id] = expected_bytes;
|
||||
get_more_blobs(connection);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
ssb.addEventListener('connections', function on_connections_changed(change, connection) {
|
||||
if (change == 'add') {
|
||||
connection.active_blob_gets = {};
|
||||
connection.blob_get_queue = {};
|
||||
var sequence = get_latest_sequence_for_author(connection.id);
|
||||
if (k_use_create_history_stream) {
|
||||
connection.send_json({'name': ['createHistoryStream'], 'type': 'source', 'args': [{'id': connection.id, 'seq': sequence, 'live': true, 'keys': false}]}, storeMessage);
|
||||
@ -137,7 +70,6 @@ ssb.addEventListener('connections', function on_connections_changed(change, conn
|
||||
}
|
||||
});
|
||||
}
|
||||
send_blobs_create_wants(connection);
|
||||
}
|
||||
} else if (change == 'remove') {
|
||||
print('REMOVE', connection.id);
|
||||
@ -149,32 +81,6 @@ ssb.addEventListener('connections', function on_connections_changed(change, conn
|
||||
}
|
||||
});
|
||||
|
||||
function blob_want_discovered(request, id) {
|
||||
if (!request || !request.connection) {
|
||||
return;
|
||||
}
|
||||
var message = {};
|
||||
message[id] = -1;
|
||||
g_blob_wants_sent++;
|
||||
request.send_json(message);
|
||||
}
|
||||
|
||||
function request_more_blobs(request) {
|
||||
return ssb.sqlStream(
|
||||
'SELECT id FROM blob_wants WHERE id > ? ORDER BY id LIMIT 32',
|
||||
[g_blob_last_requested],
|
||||
function(row) {
|
||||
blob_want_discovered(request, row.id);
|
||||
g_blob_last_requested = row.id;
|
||||
});
|
||||
}
|
||||
|
||||
ssb.addRpc(['blobs', 'createWants'], function(request) {
|
||||
g_wants_requests[request.connection.id] = request;
|
||||
ssb.addEventListener('blob_want_added', id => blob_want_discovered(request, id));
|
||||
request_more_blobs(request);
|
||||
});
|
||||
|
||||
function notify_attendant_changed(id, type) {
|
||||
if (!id) {
|
||||
print(`notify_attendant_changed called with id=${id}`);
|
||||
|
Reference in New Issue
Block a user