Skip to content

Commit

Permalink
remove old file part packets
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcottle committed Dec 17, 2024
1 parent 0d7eea5 commit 3479be3
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 168 deletions.
86 changes: 0 additions & 86 deletions src/js/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,6 @@ class Connection {
await this.onCancelFileTransferPacket(meshPacket, fileTransferPacket.cancelFileTransfer);
} else if(fileTransferPacket.completedFileTransfer){
await this.onCompletedFileTransferPacket(meshPacket, fileTransferPacket.completedFileTransfer);
} else if(fileTransferPacket.filePart){
await this.onFilePartPacket(meshPacket, fileTransferPacket.filePart);
} else if(fileTransferPacket.requestFileParts){
await this.onRequestFilePartsPacket(meshPacket, fileTransferPacket.requestFileParts);
} else if(fileTransferPacket.fileChunk){
await this.onFileChunkPacket(meshPacket, fileTransferPacket.fileChunk);
} else if(fileTransferPacket.requestFileChunk){
Expand Down Expand Up @@ -618,88 +614,6 @@ class Connection {

}

static async onFilePartPacket(meshPacket, filePart) {

// find existing file transfer
let fileTransfer = GlobalState.fileTransfers.find((fileTransfer) => {
return fileTransfer.id === filePart.fileTransferId;
});

// do nothing if file transfer not found
if(!fileTransfer){
return;
}

// do nothing if file transfer not in offering, accepted or receiving state
if(fileTransfer.status !== FileTransferrer.STATUS_OFFERING && fileTransfer.status !== FileTransferrer.STATUS_ACCEPTED && fileTransfer.status !== FileTransferrer.STATUS_RECEIVING){
console.log(`[FileTransfer] ${fileTransfer.id} received part ${filePart.partIndex + 1}/${filePart.totalParts}, but not in offering, accepted or receiving state.`);
return;
}

console.log(`[FileTransfer] ${fileTransfer.id} received part ${filePart.partIndex + 1}/${filePart.totalParts}`);

// cache received data
fileTransfer.chunks[filePart.partIndex] = filePart.data;

// update file transfer status
fileTransfer.status = FileTransferrer.STATUS_RECEIVING;
fileTransfer.progress = Math.ceil((filePart.partIndex + 1) / filePart.totalParts * 100);

// check if complete
// todo, check if all chunks received, and request others if not?
if(filePart.partIndex === filePart.totalParts - 1){
fileTransfer.status = FileTransferrer.STATUS_COMPLETED;
fileTransfer.blob = new Blob(Object.values(fileTransfer.chunks), {
type: "application/octet-stream",
});
await FileTransferrer.completeFileTransfer(fileTransfer);
return;
}

// request next part
const nextFilePartIndex = filePart.partIndex + 1;
await FileTransferrer.requestFileParts(fileTransfer, [
nextFilePartIndex,
]);

}

static async onRequestFilePartsPacket(meshPacket, requestFileParts) {

// find existing file transfer
let fileTransfer = GlobalState.fileTransfers.find((fileTransfer) => {
return fileTransfer.id === requestFileParts.fileTransferId;
});

// do nothing if file transfer not found
if(!fileTransfer){
return;
}

// do nothing if file transfer not in accepted or sending state
if(fileTransfer.status !== FileTransferrer.STATUS_ACCEPTED && fileTransfer.status !== FileTransferrer.STATUS_SENDING){
console.log(`[FileTransfer] ${fileTransfer.id} requested file parts ${requestFileParts.partIndexes}, but not in accepted or sending state.`);
return;
}

console.log(`[FileTransfer] ${fileTransfer.id} requested file parts ${requestFileParts.partIndexes}.`);

// send parts
for(const partIndex of requestFileParts.partIndexes){

console.log(`[FileTransfer] ${fileTransfer.id} sending part ${partIndex}`);

// send file part
await FileTransferrer.sendFilePart(fileTransfer, partIndex);

// update file transfer progress
fileTransfer.status = FileTransferrer.STATUS_SENDING;
fileTransfer.progress = Math.ceil((partIndex + 1) / fileTransfer.total_parts * 100);

}

}

static async onFileChunkPacket(meshPacket, fileChunk) {
for(const fileChunkListener of this.fileChunkListeners){
try {
Expand Down
20 changes: 0 additions & 20 deletions src/js/FileTransferAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,6 @@ class FileTransferAPI {
});
}

static async requestFileParts(nodeId, fileTransferId, partIndexes) {
await this.sendFileTransferPacket(nodeId, {
requestFileParts: {
fileTransferId: fileTransferId,
partIndexes: partIndexes,
},
});
}

static async sendFilePart(nodeId, fileTransferId, partIndex, totalParts, data) {
await this.sendFileTransferPacket(nodeId, {
filePart: {
fileTransferId: fileTransferId,
partIndex: partIndex,
totalParts: totalParts,
data: data,
},
});
}

static async requestFileChunk(nodeId, fileTransferId, offset, length) {
await this.sendFileTransferPacket(nodeId, {
requestFileChunk: {
Expand Down
48 changes: 0 additions & 48 deletions src/js/FileTransferrer.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,54 +168,6 @@ class FileTransferrer {
});
}

static async sendFilePart(fileTransfer, partIndex) {
try {

// get data for this part
const partSize = fileTransfer.max_acceptable_part_size;
const start = partIndex * partSize;
const end = start + partSize;
const partData = fileTransfer.data.slice(start, end);

// update status
fileTransfer.status = FileTransferrer.STATUS_SENDING;

// send part to remote node
for(var attempt = 1; attempt <= this.MAX_PACKET_ATTEMPTS; attempt++){
try {
this.log(`sendFilePart attempt ${attempt}`);
await FileTransferAPI.sendFilePart(fileTransfer.to, fileTransfer.id, partIndex, fileTransfer.total_parts, partData);
return;
} catch(e) {
console.log(e);
if(attempt === this.MAX_PACKET_ATTEMPTS){
this.log("sendFilePart failed", e);
throw e;
}
}
}

} catch(e) {
console.log(e);
}
}

static async requestFileParts(fileTransfer, partIndexes) {
for(var attempt = 1; attempt <= this.MAX_PACKET_ATTEMPTS; attempt++){
try {
this.log(`requestFileParts attempt ${attempt}`);
await FileTransferAPI.requestFileParts(fileTransfer.from, fileTransfer.id, partIndexes);
return;
} catch(e) {
console.log(e);
if(attempt === this.MAX_PACKET_ATTEMPTS){
this.log("requestFileParts failed", e);
throw e;
}
}
}
}

static async requestFileChunk(fileTransfer, offset, length) {
await FileTransferAPI.requestFileChunk(fileTransfer.from, fileTransfer.id, offset, length);
}
Expand Down
14 changes: 0 additions & 14 deletions src/public/protos/file_transfer.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ message FileTransferPacket {
optional RejectFileTransfer rejectFileTransfer = 3;
optional CancelFileTransfer cancelFileTransfer = 4;
optional CompletedFileTransfer completedFileTransfer = 5;
optional FilePart filePart = 6;
optional RequestFileParts requestFileParts = 7;
optional RequestFileChunk requestFileChunk = 8;
optional FileChunk fileChunk = 9;
}
Expand All @@ -39,18 +37,6 @@ message CompletedFileTransfer {
uint32 fileTransferId = 1;
}

message RequestFileParts {
uint32 fileTransferId = 1;
repeated uint32 partIndexes = 2;
}

message FilePart {
uint32 fileTransferId = 1;
uint32 partIndex = 2;
uint32 totalParts = 3;
bytes data = 4;
}

message RequestFileChunk {
uint32 fileTransferId = 1;
uint32 offset = 2;
Expand Down

0 comments on commit 3479be3

Please sign in to comment.