Skip to content

Commit

Permalink
show if file transfer is being accepted, and add guard clauses for ou…
Browse files Browse the repository at this point in the history
…t of order packets
  • Loading branch information
liamcottle committed Dec 14, 2024
1 parent f5efab0 commit a1df778
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
41 changes: 38 additions & 3 deletions src/components/pages/NodeFilesPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,22 @@
</div>

<!-- incoming file transfer offer -->
<div v-if="fileTransfer.direction === 'incoming' && fileTransfer.status === 'offering'" class="flex space-x-1">
<TextButton @click="acceptFileTransfer(fileTransfer)" class="bg-green-500 hover:bg-green-400">Accept</TextButton>
<TextButton @click="rejectFileTransfer(fileTransfer)" class="bg-red-500 hover:bg-red-400">Reject</TextButton>
<div v-if="fileTransfer.direction === 'incoming' && fileTransfer.status === 'offering'">
<div v-if="!isAcceptingFileTransfer(fileTransfer.id)" class="flex space-x-1">
<TextButton @click="acceptFileTransfer(fileTransfer)" class="bg-green-500 hover:bg-green-400">Accept</TextButton>
<TextButton @click="rejectFileTransfer(fileTransfer)" class="bg-red-500 hover:bg-red-400">Reject</TextButton>
</div>
<div v-else class="flex text-gray-500 space-x-1">
<div class="my-auto">
<svg class="animate-spin size-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
</div>
<div class="my-auto">
Accepting
</div>
</div>
</div>

<!-- action buttons -->
Expand Down Expand Up @@ -149,6 +162,11 @@ export default {
}
},
data() {
return {
isAcceptingFileTransferIds: [],
};
},
methods: {
hasPublicKey: (nodeId) => NodeUtils.hasPublicKey(nodeId),
getNodeLongName: (nodeId) => NodeUtils.getNodeLongName(nodeId),
Expand Down Expand Up @@ -188,6 +206,20 @@ export default {
setTimeout(() => URL.revokeObjectURL(objectUrl), 10000);
},
isAcceptingFileTransfer(fileTransferId) {
return this.isAcceptingFileTransferIds.includes(fileTransferId);
},
setIsAcceptingFileTransfer(fileTransferId, isAcceptingFileTransfer) {
if(isAcceptingFileTransfer){
// add to list of ids
this.isAcceptingFileTransferIds.push(fileTransferId);
} else {
// remove from list of ids
this.isAcceptingFileTransferIds = this.isAcceptingFileTransferIds.filter((existingFileTransferId) => {
return existingFileTransferId !== fileTransferId;
});
}
},
async offerFileTransfer() {
// do nothing if file is not selected
Expand All @@ -207,9 +239,12 @@ export default {
},
async acceptFileTransfer(fileTransfer) {
try {
this.setIsAcceptingFileTransfer(fileTransfer.id, true);
await FileTransferrer.acceptFileTransfer(fileTransfer);
} catch(e) {
DialogUtils.showErrorAlert(e);
} finally {
this.setIsAcceptingFileTransfer(fileTransfer.id, false);
}
},
async rejectFileTransfer(fileTransfer) {
Expand Down
30 changes: 30 additions & 0 deletions src/js/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,12 @@ class Connection {
return;
}

// do nothing if file transfer not in offering state
if(fileTransfer.status !== FileTransferrer.STATUS_OFFERING){
console.log(`[FileTransfer] ${fileTransfer.id} accepted, but no longer in offering state`);
return;
}

console.log(`[FileTransfer] ${fileTransfer.id} accepted`);

// determine how many parts will be sent
Expand Down Expand Up @@ -568,6 +574,12 @@ class Connection {
return;
}

// do nothing if file transfer not in offering state
if(fileTransfer.status !== FileTransferrer.STATUS_OFFERING){
console.log(`[FileTransfer] ${fileTransfer.id} rejected, but no longer in offering state`);
return;
}

console.log(`[FileTransfer] ${fileTransfer.id} rejected`);

// update file transfer status
Expand All @@ -587,6 +599,12 @@ class Connection {
return;
}

// do nothing if file transfer in completed state
if(fileTransfer.status === FileTransferrer.STATUS_COMPLETED){
console.log(`[FileTransfer] ${fileTransfer.id} cancelled, but already in completed state`);
return;
}

console.log(`[FileTransfer] ${fileTransfer.id} cancelled`);

// remove cancelled file transfer if it was in offering state
Expand Down Expand Up @@ -633,6 +651,12 @@ class Connection {
return;
}

// do nothing if file transfer not in accepted or receiving state
if(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 accepted or receiving state.`);
return;
}

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

// cache received data
Expand Down Expand Up @@ -673,6 +697,12 @@ class Connection {
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
Expand Down
7 changes: 4 additions & 3 deletions src/js/FileTransferrer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class FileTransferrer {
static DIRECTION_OUTGOING = "outgoing";

static STATUS_OFFERING = "offering";
static STATUS_OFFERED = "offered";
static STATUS_ACCEPTED = "accepted";
static STATUS_REJECTED = "rejected";
static STATUS_CANCELLED = "cancelled";
Expand Down Expand Up @@ -54,7 +53,6 @@ class FileTransferrer {
this.log(`offerFileTransfer attempt ${attempt}`);
await FileTransferAPI.sendFileTransferRequest(to, fileTransferId, fileName, fileSize);
this.log(`offerFileTransfer attempt ${attempt} success`);
fileTransfer.status = this.STATUS_OFFERED;
return;
} catch(e) {
console.log(e);
Expand Down Expand Up @@ -105,7 +103,7 @@ class FileTransferrer {
for(var attempt = 1; attempt <= this.MAX_PACKET_ATTEMPTS; attempt++){
try {
this.log(`rejectFileTransfer attempt ${attempt}`);
await FileTransferAPI.acceptFileTransfer(fileTransfer.from, fileTransfer.id);
await FileTransferAPI.rejectFileTransfer(fileTransfer.from, fileTransfer.id);
fileTransfer.status = this.STATUS_ACCEPTED;
return;
} catch(e) {
Expand Down Expand Up @@ -154,6 +152,9 @@ class FileTransferrer {
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 {
Expand Down

0 comments on commit a1df778

Please sign in to comment.