-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from GeorgeTsagk/add-assembly-buffers
Add assembly buffers
- Loading branch information
Showing
6 changed files
with
104 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const { decodeDataStruct } = require('./data-struct') | ||
|
||
let buffers = {} | ||
|
||
let completedBufferCallback = (id) => { | ||
dataReadyCallback(buffers[id].buffer) | ||
delete buffers[id] | ||
} | ||
|
||
let dataReadyCallback = () => {} | ||
|
||
const setDataReadyCallback = (callback) => { | ||
dataReadyCallback = callback | ||
} | ||
|
||
const checkIfBufferComplete = (assemblyBuffer) => { | ||
if(assemblyBuffer.receivedBytes === assemblyBuffer.buffer.length) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
const receiveDataStructBuffer = async (dataStructBuffer) => { | ||
const dataStruct = await decodeDataStruct(dataStructBuffer) | ||
if(dataStruct.fragment == undefined) { | ||
dataReadyCallback(dataStruct.payload) | ||
} | ||
|
||
if(!(dataStruct.fragment.fragmentationId in buffers)){ | ||
console.log('Creating new buffer with id', dataStruct.fragment.fragmentationId) | ||
buffers[dataStruct.fragment.fragmentationId] = { | ||
receivedBytes: 0, | ||
buffer: Buffer.alloc(dataStruct.fragment.totalSize) | ||
} | ||
} | ||
|
||
for(let i = 0; i < dataStruct.payload.length; i++) { | ||
buffers[dataStruct.fragment.fragmentationId].buffer.writeUint8( | ||
dataStruct.payload.readUint8(i), | ||
dataStruct.fragment.offset + i | ||
) | ||
buffers[dataStruct.fragment.fragmentationId].receivedBytes++ | ||
} | ||
console.log( | ||
'Fragment Received:', | ||
`${dataStruct.payload.length} B`, | ||
'|', | ||
buffers[dataStruct.fragment.fragmentationId].receivedBytes, | ||
'/', | ||
buffers[dataStruct.fragment.fragmentationId].buffer.length, | ||
'B' | ||
|
||
) | ||
|
||
if(checkIfBufferComplete(buffers[dataStruct.fragment.fragmentationId])) { | ||
completedBufferCallback(dataStruct.fragment.fragmentationId) | ||
} | ||
} | ||
|
||
module.exports = { | ||
receiveDataStructBuffer, | ||
setDataReadyCallback | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const defaultDataReadyCallback = (data) => { | ||
console.log('-----------DATA-READY------------') | ||
console.log(data.toString('utf8')) | ||
console.log('---------------------------------') | ||
} | ||
|
||
module.exports = { | ||
defaultDataReadyCallback | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters