From 46b76c02c6556d5dfffb98927b69e59f90e18ec5 Mon Sep 17 00:00:00 2001 From: Alka Prasad Date: Mon, 3 Apr 2023 16:27:49 +0530 Subject: [PATCH 1/5] feat(#43): update 0-based counter in the chunk sequence number to 1-based counter --- .../io/mosip/tuvali/transfer/Assembler.kt | 12 ++++---- .../java/io/mosip/tuvali/transfer/Chunker.kt | 24 ++++++++------- .../io/mosip/tuvali/transfer/RetryChunker.kt | 4 +-- .../verifier/transfer/TransferHandler.kt | 1 - ios/ble/Utility/TransferHandler.swift | 4 +-- ios/ble/Utility/chunker.swift | 30 ++++++++----------- 6 files changed, 36 insertions(+), 39 deletions(-) diff --git a/android/src/main/java/io/mosip/tuvali/transfer/Assembler.kt b/android/src/main/java/io/mosip/tuvali/transfer/Assembler.kt index faad8ab9..12a165a6 100644 --- a/android/src/main/java/io/mosip/tuvali/transfer/Assembler.kt +++ b/android/src/main/java/io/mosip/tuvali/transfer/Assembler.kt @@ -38,8 +38,9 @@ class Assembler(private val totalSize: Int, private val maxDataBytes: Int ): Chu return seqNumberInMeta } lastReadSeqNumber = seqNumberInMeta - System.arraycopy(chunkData, chunkMetaSize, data, seqNumberInMeta * effectivePayloadSize, (chunkData.size-chunkMetaSize)) - chunkReceivedMarker[seqNumberInMeta] = chunkReceivedMarkerByte + val seqIndex = seqNumberInMeta - 1 + System.arraycopy(chunkData, chunkMetaSize, data, seqIndex * effectivePayloadSize, (chunkData.size-chunkMetaSize)) + chunkReceivedMarker[seqIndex] = chunkReceivedMarkerByte //Log.d(logTag, "adding chunk complete at index(0-based): ${seqNumberInMeta}, received chunkSize: ${chunkData.size}") return seqNumberInMeta } @@ -61,14 +62,15 @@ class Assembler(private val totalSize: Int, private val maxDataBytes: Int ): Chu } fun getMissedSequenceNumbers(): IntArray { - var missedSeqNumbers = intArrayOf() + var missedSeqNumberList = intArrayOf() chunkReceivedMarker.forEachIndexed() { i, elem -> if (elem != chunkReceivedMarkerByte) { //Log.d(logTag, "getMissedSequenceNumbers: adding missed sequence number $i") - missedSeqNumbers += i + val missedSeqNumber = i + 1 + missedSeqNumberList += missedSeqNumber } } - return missedSeqNumbers + return missedSeqNumberList } fun data(): ByteArray { diff --git a/android/src/main/java/io/mosip/tuvali/transfer/Chunker.kt b/android/src/main/java/io/mosip/tuvali/transfer/Chunker.kt index f326dd09..e1c99f9f 100644 --- a/android/src/main/java/io/mosip/tuvali/transfer/Chunker.kt +++ b/android/src/main/java/io/mosip/tuvali/transfer/Chunker.kt @@ -22,23 +22,24 @@ class Chunker(private val data: ByteArray, private val maxDataBytes: Int) : } fun next(): ByteArray { - val seqNumber = chunksReadCounter + val seqIndex = chunksReadCounter chunksReadCounter++ - return preSlicedChunks[seqNumber]!! - } + return preSlicedChunks[seqIndex]!! - fun chunkBySequenceNumber(num: Int): ByteArray { - return preSlicedChunks[num]!! } - private fun chunk(seqNumber: Int): ByteArray { - val fromIndex = seqNumber * effectivePayloadSize + fun chunkBySequenceNumber(missedSeqNumber: Int): ByteArray { + val missedSeqIndex = missedSeqNumber - 1 + return preSlicedChunks[missedSeqIndex]!! + } - return if (seqNumber == (totalChunkCount - 1).toInt() && lastChunkByteCount > 0) { - Log.d(logTag, "fetching last chunk") + private fun chunk(seqIndex: Int): ByteArray { + val fromIndex = seqIndex * effectivePayloadSize + val seqNumber = seqIndex + 1 + return if (seqIndex == (totalChunkCount - 1) && lastChunkByteCount > 0) { frameChunk(seqNumber, fromIndex, fromIndex + lastChunkByteCount) } else { - val toIndex = (seqNumber + 1) * effectivePayloadSize + val toIndex = (seqIndex + 1) * effectivePayloadSize frameChunk(seqNumber, fromIndex, toIndex) } } @@ -61,7 +62,8 @@ class Chunker(private val data: ByteArray, private val maxDataBytes: Int) : } fun isComplete(): Boolean { - val isComplete = chunksReadCounter > (totalChunkCount - 1).toInt() + Log.i(logTag,"chunksReadCounter: $chunksReadCounter") + val isComplete = chunksReadCounter > (totalChunkCount - 1) if (isComplete) { Log.d( logTag, diff --git a/android/src/main/java/io/mosip/tuvali/transfer/RetryChunker.kt b/android/src/main/java/io/mosip/tuvali/transfer/RetryChunker.kt index 5d299374..bc3e7d55 100644 --- a/android/src/main/java/io/mosip/tuvali/transfer/RetryChunker.kt +++ b/android/src/main/java/io/mosip/tuvali/transfer/RetryChunker.kt @@ -12,10 +12,10 @@ class RetryChunker(private val chunker: Chunker, private val missedSequences: In } fun next(): ByteArray { - val missedSeqNo = missedSequences[seqCounter] + val missedSeqNumber = missedSequences[seqCounter] seqCounter++ - return chunker.chunkBySequenceNumber(missedSeqNo) + return chunker.chunkBySequenceNumber(missedSeqNumber) } fun isComplete(): Boolean { diff --git a/android/src/main/java/io/mosip/tuvali/verifier/transfer/TransferHandler.kt b/android/src/main/java/io/mosip/tuvali/verifier/transfer/TransferHandler.kt index 11e66305..715862ba 100644 --- a/android/src/main/java/io/mosip/tuvali/verifier/transfer/TransferHandler.kt +++ b/android/src/main/java/io/mosip/tuvali/verifier/transfer/TransferHandler.kt @@ -14,7 +14,6 @@ import io.mosip.tuvali.verifier.exception.CorruptedChunkReceivedException import io.mosip.tuvali.verifier.exception.TooManyFailureChunksException import io.mosip.tuvali.verifier.transfer.message.* import java.util.* -import kotlin.math.ceil import io.mosip.tuvali.transfer.Util.Companion.getLogTag class TransferHandler(looper: Looper, private val peripheral: Peripheral, private val transferListener: ITransferListener, val serviceUUID: UUID) : Handler(looper) { diff --git a/ios/ble/Utility/TransferHandler.swift b/ios/ble/Utility/TransferHandler.swift index 6b5927e8..d1f45190 100644 --- a/ios/ble/Utility/TransferHandler.swift +++ b/ios/ble/Utility/TransferHandler.swift @@ -68,8 +68,8 @@ class TransferHandler { } private func sendRetryRespChunk(missingChunks: [Int]) { - for chunkIndex in missingChunks { - if let chunk = chunker?.getChunkWithIndex(index: chunkIndex) { + for missedSeqNumber in missingChunks { + if let chunk = chunker?.chunkBySequenceNumber(missedSeqNumber: missedSeqNumber) { delegate?.write(serviceUuid: Peripheral.SERVICE_UUID, charUUID: NetworkCharNums.SUBMIT_RESPONSE_CHAR_UUID, data: chunk, withResponse: true) } // checks if no more missing chunks exist on verifier diff --git a/ios/ble/Utility/chunker.swift b/ios/ble/Utility/chunker.swift index 3b414ccb..4a0b0ece 100644 --- a/ios/ble/Utility/chunker.swift +++ b/ios/ble/Utility/chunker.swift @@ -15,14 +15,6 @@ class Chunker { assignPreSlicedChunks() } - func getChunkWithIndex(index: Int) -> Data { - if index < self.preSlicedChunks.count { - return self.preSlicedChunks[index] - } - // TODO: Figure out how to throw errors! - return Data() - } - func getLastChunkByteCount(dataSize: Int) -> Int { return dataSize % effectivePayloadSize } @@ -30,7 +22,7 @@ class Chunker { func assignPreSlicedChunks(){ os_log(.info, "expected total data size: %{public}d and totalChunkCount: %{public}d ", (chunkData?.count)!, totalChunkCount) for i in 0.. Data { - var seqNumber = chunksReadCounter + var seqIndex = chunksReadCounter chunksReadCounter += 1 - if seqNumber <= totalChunkCount - 1 { - return (preSlicedChunks[seqNumber]) + if seqIndex <= totalChunkCount - 1 { + return (preSlicedChunks[seqIndex]) } else { @@ -63,17 +55,19 @@ class Chunker { } } - func chunkBySequenceNumber(num: Int) -> Data { - return (preSlicedChunks[num]) + func chunkBySequenceNumber(missedSeqNumber: Int) -> Data { + let missedSeqIndex = missedSeqNumber - 1 + return (preSlicedChunks[missedSeqIndex]) } - private func chunk(seqNumber: Int) -> Data { - let fromIndex = seqNumber * effectivePayloadSize - if (seqNumber == (totalChunkCount - 1) && lastChunkByteCount > 0) { + private func chunk(seqIndex: Int) -> Data { + let fromIndex = seqIndex * effectivePayloadSize + let seqNumber = seqIndex + 1 + if (seqIndex == (totalChunkCount - 1) && lastChunkByteCount > 0) { let chunkLength = lastChunkByteCount + chunkMetaSize return frameChunk(seqNumber: seqNumber, chunkLength: chunkLength, fromIndex: fromIndex, toIndex: fromIndex + lastChunkByteCount) } else { - let toIndex = (seqNumber + 1) * effectivePayloadSize + let toIndex = (seqIndex + 1) * effectivePayloadSize return frameChunk(seqNumber: seqNumber, chunkLength: mtuSize, fromIndex: fromIndex, toIndex: toIndex) } } From 6754808061e3acc25c19610e49fde2829aa05742 Mon Sep 17 00:00:00 2001 From: Alka Prasad Date: Wed, 5 Apr 2023 12:04:37 +0530 Subject: [PATCH 2/5] feat(#43): add documentation related to 1-based counter for sequence number --- docs/one-based-counter-for-sequence-number.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 docs/one-based-counter-for-sequence-number.md diff --git a/docs/one-based-counter-for-sequence-number.md b/docs/one-based-counter-for-sequence-number.md new file mode 100644 index 00000000..8f08b6c6 --- /dev/null +++ b/docs/one-based-counter-for-sequence-number.md @@ -0,0 +1,5 @@ +# One-based Counter for Sequence Number in VC Chunk + +The VC is divided into multiple chunks and stored in an array initially. While creating a chunk we add the sequence number to it. Initially, we used the array index as the sequence number which was added in the chunks making it a 0-based counter. + +Now, we are adding 1 to the array index when using it as a sequence number on the chunk making it a 1-based counter. After we read the chunk on the verifier side, we are decrementing the sequence number by 1 and then store in back in the array. From 984c45445fcfba1cbeef7b2a571b82b5da6a46ee Mon Sep 17 00:00:00 2001 From: krishnakumar4a4 Date: Tue, 11 Apr 2023 19:49:14 +0530 Subject: [PATCH 3/5] refactor(#43): chunker and assembler to use explicit types for seq index and seq number --- .../io/mosip/tuvali/transfer/Assembler.kt | 13 ++-- .../java/io/mosip/tuvali/transfer/Chunker.kt | 36 +++++------ .../io/mosip/tuvali/transfer/ChunkerBase.kt | 11 ++++ ios/ble/Utility/TransferReport.swift | 15 ++--- ios/ble/Utility/chunker.swift | 59 +++++++++++-------- 5 files changed, 79 insertions(+), 55 deletions(-) diff --git a/android/src/main/java/io/mosip/tuvali/transfer/Assembler.kt b/android/src/main/java/io/mosip/tuvali/transfer/Assembler.kt index 12a165a6..95e7e5dd 100644 --- a/android/src/main/java/io/mosip/tuvali/transfer/Assembler.kt +++ b/android/src/main/java/io/mosip/tuvali/transfer/Assembler.kt @@ -5,7 +5,7 @@ import io.mosip.tuvali.transfer.Util.Companion.twoBytesToIntBigEndian import io.mosip.tuvali.verifier.exception.CorruptedChunkReceivedException import io.mosip.tuvali.transfer.Util.Companion.getLogTag -class Assembler(private val totalSize: Int, private val maxDataBytes: Int ): ChunkerBase(maxDataBytes) { +class Assembler(totalSize: Int, private val maxDataBytes: Int ): ChunkerBase(maxDataBytes) { private val logTag = getLogTag(javaClass.simpleName) private var data: ByteArray = ByteArray(totalSize) private var lastReadSeqNumber: Int? = null @@ -25,7 +25,7 @@ class Assembler(private val totalSize: Int, private val maxDataBytes: Int ): Chu Log.e(logTag, "received invalid chunk chunkSize: ${chunkData.size}, lastReadSeqNumber: $lastReadSeqNumber") return 0 } - val seqNumberInMeta = twoBytesToIntBigEndian(chunkData.copyOfRange(0, 2)) + val seqNumberInMeta: ChunkSeqNumber = twoBytesToIntBigEndian(chunkData.copyOfRange(0, 2)) val crcReceived = twoBytesToIntBigEndian(chunkData.copyOfRange(2,4)).toUShort() //Log.d(logTag, "received add chunk received chunkSize: ${chunkData.size}, seqNumberInMeta: $seqNumberInMeta") @@ -38,7 +38,7 @@ class Assembler(private val totalSize: Int, private val maxDataBytes: Int ): Chu return seqNumberInMeta } lastReadSeqNumber = seqNumberInMeta - val seqIndex = seqNumberInMeta - 1 + val seqIndex = seqNumberInMeta.toSeqIndex() System.arraycopy(chunkData, chunkMetaSize, data, seqIndex * effectivePayloadSize, (chunkData.size-chunkMetaSize)) chunkReceivedMarker[seqIndex] = chunkReceivedMarkerByte //Log.d(logTag, "adding chunk complete at index(0-based): ${seqNumberInMeta}, received chunkSize: ${chunkData.size}") @@ -63,11 +63,10 @@ class Assembler(private val totalSize: Int, private val maxDataBytes: Int ): Chu fun getMissedSequenceNumbers(): IntArray { var missedSeqNumberList = intArrayOf() - chunkReceivedMarker.forEachIndexed() { i, elem -> + chunkReceivedMarker.forEachIndexed() { missedChunkSeqIndex: ChunkSeqIndex, elem -> if (elem != chunkReceivedMarkerByte) { - //Log.d(logTag, "getMissedSequenceNumbers: adding missed sequence number $i") - val missedSeqNumber = i + 1 - missedSeqNumberList += missedSeqNumber + //Log.d(logTag, "getMissedSequenceNumbers: adding missed sequence number $missedChunkSeqIndex") + missedSeqNumberList += missedChunkSeqIndex.toSeqNumber() } } return missedSeqNumberList diff --git a/android/src/main/java/io/mosip/tuvali/transfer/Chunker.kt b/android/src/main/java/io/mosip/tuvali/transfer/Chunker.kt index e1c99f9f..bd66c62f 100644 --- a/android/src/main/java/io/mosip/tuvali/transfer/Chunker.kt +++ b/android/src/main/java/io/mosip/tuvali/transfer/Chunker.kt @@ -7,14 +7,14 @@ import io.mosip.tuvali.transfer.Util.Companion.getLogTag class Chunker(private val data: ByteArray, private val maxDataBytes: Int) : ChunkerBase(maxDataBytes) { private val logTag = getLogTag(javaClass.simpleName) - private var chunksReadCounter: Int = 0 + private var chunksReadIndex: Int = 0 private val lastChunkByteCount = getLastChunkByteCount(data.size) private val totalChunkCount = getTotalChunkCount(data.size).toInt() private val preSlicedChunks: Array = Array(totalChunkCount) { null } init { Log.i(logTag, "Total number of chunks calculated: $totalChunkCount") - val startTime = System.currentTimeMillis() +// val startTime = System.currentTimeMillis() for (idx in 0 until totalChunkCount) { preSlicedChunks[idx] = chunk(idx) } @@ -22,28 +22,28 @@ class Chunker(private val data: ByteArray, private val maxDataBytes: Int) : } fun next(): ByteArray { - val seqIndex = chunksReadCounter - chunksReadCounter++ - return preSlicedChunks[seqIndex]!! - + return preSlicedChunks[chunksReadIndex++]!! } - fun chunkBySequenceNumber(missedSeqNumber: Int): ByteArray { - val missedSeqIndex = missedSeqNumber - 1 - return preSlicedChunks[missedSeqIndex]!! + fun chunkBySequenceNumber(missedSeqNumber: ChunkSeqNumber): ByteArray { + return preSlicedChunks[missedSeqNumber.toSeqIndex()]!! } - private fun chunk(seqIndex: Int): ByteArray { + private fun chunk(seqIndex: ChunkSeqIndex): ByteArray { val fromIndex = seqIndex * effectivePayloadSize - val seqNumber = seqIndex + 1 - return if (seqIndex == (totalChunkCount - 1) && lastChunkByteCount > 0) { - frameChunk(seqNumber, fromIndex, fromIndex + lastChunkByteCount) + return if (isLastChunkSmallerSize(seqIndex)) { + frameChunk(seqIndex.toSeqNumber(), fromIndex, fromIndex + lastChunkByteCount) } else { - val toIndex = (seqIndex + 1) * effectivePayloadSize - frameChunk(seqNumber, fromIndex, toIndex) + val toIndex = fromIndex + effectivePayloadSize + frameChunk(seqIndex.toSeqNumber(), fromIndex, toIndex) } } + private fun isLastChunkSmallerSize(seqIndex: Int) = + isLastChunkIndex(seqIndex) && lastChunkByteCount > 0 + + private fun isLastChunkIndex(seqIndex: Int) = seqIndex == (totalChunkCount - 1) + /* <--------------------------------------------------Max Data Bytes --------------------------------------------------------------> +-----------------------+-----------------------------+-------------------------------------------------------------------------+ @@ -62,12 +62,12 @@ class Chunker(private val data: ByteArray, private val maxDataBytes: Int) : } fun isComplete(): Boolean { - Log.i(logTag,"chunksReadCounter: $chunksReadCounter") - val isComplete = chunksReadCounter > (totalChunkCount - 1) + Log.i(logTag,"chunksReadCounter: $chunksReadIndex") + val isComplete = chunksReadIndex > (totalChunkCount - 1) if (isComplete) { Log.d( logTag, - "isComplete: true, totalChunks: $totalChunkCount , chunkReadCounter(1-indexed): $chunksReadCounter" + "isComplete: true, totalChunks: $totalChunkCount , chunkReadCounter(1-indexed): $chunksReadIndex" ) } return isComplete diff --git a/android/src/main/java/io/mosip/tuvali/transfer/ChunkerBase.kt b/android/src/main/java/io/mosip/tuvali/transfer/ChunkerBase.kt index 8be2cf3f..f29eebd6 100644 --- a/android/src/main/java/io/mosip/tuvali/transfer/ChunkerBase.kt +++ b/android/src/main/java/io/mosip/tuvali/transfer/ChunkerBase.kt @@ -2,6 +2,17 @@ package io.mosip.tuvali.transfer import kotlin.math.ceil +typealias ChunkSeqIndex = Int +typealias ChunkSeqNumber = Int + +fun ChunkSeqIndex.toSeqNumber(): ChunkSeqNumber { + return this + 1 +} + +fun ChunkSeqNumber.toSeqIndex(): ChunkSeqIndex { + return this - 1 +} + open class ChunkerBase(maxDataBytes: Int) { private val seqNumberReservedByteSize = 2 private val crcChecksumValueByteSize = 2 diff --git a/ios/ble/Utility/TransferReport.swift b/ios/ble/Utility/TransferReport.swift index 1d255327..3d0ab3e7 100644 --- a/ios/ble/Utility/TransferReport.swift +++ b/ios/ble/Utility/TransferReport.swift @@ -1,5 +1,12 @@ import Foundation +/* + +---------+------------------+---------------------+-------------------+-------------------+-------------------+ + | | | | | | | + | type | total pages | missed seq no.0 | missed seq no. 1 | missed seq no.2 | . . . . . | + |(1 byte) | (2 bytes) | (2 bytes) | (2 bytes) | (2 bytes) | | + +---------+------------------+---------------------+-------------------+-------------------+-------------------+ + */ class TransferReport { var type: ReportType var totalPages: Int @@ -35,10 +42,4 @@ class TransferReport { } } } -/* - +---------+------------------+---------------------+-------------------+-------------------+-------------------+ - | | | | | | | - | type | total pages | missed seq no.0 | missed seq no. 1 | missed seq no.2 | . . . . . | - |(1 byte) | (2 bytes) | (2 bytes) | (2 bytes) | (2 bytes) | | - +---------+------------------+---------------------+-------------------+-------------------+-------------------+ - */ + diff --git a/ios/ble/Utility/chunker.swift b/ios/ble/Utility/chunker.swift index 4a0b0ece..33f09375 100644 --- a/ios/ble/Utility/chunker.swift +++ b/ios/ble/Utility/chunker.swift @@ -1,9 +1,24 @@ import Foundation +typealias ChunkSeqIndex = Int +typealias ChunkSeqNumber = Int + +extension ChunkSeqIndex { + func toSeqNumber() -> ChunkSeqNumber { + return self + 1 + } +} + +extension ChunkSeqNumber { + func toSeqIndex() -> ChunkSeqIndex { + return self - 1 + } +} + class Chunker { private var logTag = "Chunker" - private var chunksReadCounter: Int = 0 + private var chunksReadIndex: Int = 0 private var preSlicedChunks: [Data] = [] private var chunkData: Data? private var mtuSize: Int = BLEConstants.DEFAULT_CHUNK_SIZE @@ -21,7 +36,7 @@ class Chunker { func assignPreSlicedChunks(){ os_log(.info, "expected total data size: %{public}d and totalChunkCount: %{public}d ", (chunkData?.count)!, totalChunkCount) - for i in 0.. Data { - var seqIndex = chunksReadCounter - chunksReadCounter += 1 - if seqIndex <= totalChunkCount - 1 { - return (preSlicedChunks[seqIndex]) - } - else - { - return Data() - } + return preSlicedChunks[chunksReadIndex++] } - func chunkBySequenceNumber(missedSeqNumber: Int) -> Data { - let missedSeqIndex = missedSeqNumber - 1 - return (preSlicedChunks[missedSeqIndex]) + func chunkBySequenceNumber(missedSeqNumber: ChunkSeqNumber) -> Data { + return (preSlicedChunks[missedSeqNumber.toSeqIndex()]) } - private func chunk(seqIndex: Int) -> Data { + private func chunk(seqIndex: ChunkSeqIndex) -> Data { let fromIndex = seqIndex * effectivePayloadSize - let seqNumber = seqIndex + 1 - if (seqIndex == (totalChunkCount - 1) && lastChunkByteCount > 0) { + if isLastChunkSmallerSize(seqIndex) { let chunkLength = lastChunkByteCount + chunkMetaSize - return frameChunk(seqNumber: seqNumber, chunkLength: chunkLength, fromIndex: fromIndex, toIndex: fromIndex + lastChunkByteCount) + return frameChunk(seqNumber: seqIndex.toSeqNumber(), chunkLength: chunkLength, fromIndex: fromIndex, toIndex: fromIndex + lastChunkByteCount) } else { - let toIndex = (seqIndex + 1) * effectivePayloadSize - return frameChunk(seqNumber: seqNumber, chunkLength: mtuSize, fromIndex: fromIndex, toIndex: toIndex) + let toIndex = fromIndex + effectivePayloadSize + return frameChunk(seqNumber: seqIndex.toSeqNumber(), chunkLength: mtuSize, fromIndex: fromIndex, toIndex: toIndex) } } + private func isLastChunkSmallerSize(seqIndex: Int) -> Bool { + return isLastChunkIndex(seqIndex) && lastChunkByteCount > 0 + } + + private func isLastChunkIndex(seqIndex: Int) -> Bool { + return seqIndex == (totalChunkCount - 1) + } + /* <------------------------------------------------------- MTU -------------------------------------------------------------------> +-----------------------+-----------------------------+-------------------------------------------------------------------------+ @@ -82,7 +95,7 @@ class Chunker { +-----------------------+-----------------------------+-------------------------------------------------------------------------+ */ - private func frameChunk(seqNumber: Int, chunkLength: Int, fromIndex: Int, toIndex: Int) -> Data { + private func frameChunk(seqNumber: ChunkSeqNumber, chunkLength: Int, fromIndex: Int, toIndex: Int) -> Data { if let chunkData = chunkData { let payload = chunkData.subdata(in: fromIndex + chunkData.startIndex.. Bool { - let isComplete = chunksReadCounter > (totalChunkCount - 1) + let isComplete = chunksReadIndex > (totalChunkCount - 1) if isComplete { - os_log(.info, "isComplete: true, totalChunks: %{public}d , chunkReadCounter(1-indexed): %{public}d", totalChunkCount, chunksReadCounter) + os_log(.info, "isComplete: true, totalChunks: %{public}d , chunkReadCounter(1-indexed): %{public}d", totalChunkCount, chunksReadIndex) } return isComplete } From 88516af8060f6d6ff35987021e18d75ea11a0740 Mon Sep 17 00:00:00 2001 From: krishnakumar4a4 Date: Wed, 12 Apr 2023 11:00:20 +0530 Subject: [PATCH 4/5] refactor(#43): restored name of chunksReadIndex to chunksReadCounter and simplified a check --- .../src/main/java/io/mosip/tuvali/transfer/Chunker.kt | 10 +++++----- ios/ble/Utility/chunker.swift | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/android/src/main/java/io/mosip/tuvali/transfer/Chunker.kt b/android/src/main/java/io/mosip/tuvali/transfer/Chunker.kt index bd66c62f..bde233c2 100644 --- a/android/src/main/java/io/mosip/tuvali/transfer/Chunker.kt +++ b/android/src/main/java/io/mosip/tuvali/transfer/Chunker.kt @@ -7,7 +7,7 @@ import io.mosip.tuvali.transfer.Util.Companion.getLogTag class Chunker(private val data: ByteArray, private val maxDataBytes: Int) : ChunkerBase(maxDataBytes) { private val logTag = getLogTag(javaClass.simpleName) - private var chunksReadIndex: Int = 0 + private var chunksReadCounter: Int = 0 private val lastChunkByteCount = getLastChunkByteCount(data.size) private val totalChunkCount = getTotalChunkCount(data.size).toInt() private val preSlicedChunks: Array = Array(totalChunkCount) { null } @@ -22,7 +22,7 @@ class Chunker(private val data: ByteArray, private val maxDataBytes: Int) : } fun next(): ByteArray { - return preSlicedChunks[chunksReadIndex++]!! + return preSlicedChunks[chunksReadCounter++]!! } fun chunkBySequenceNumber(missedSeqNumber: ChunkSeqNumber): ByteArray { @@ -62,12 +62,12 @@ class Chunker(private val data: ByteArray, private val maxDataBytes: Int) : } fun isComplete(): Boolean { - Log.i(logTag,"chunksReadCounter: $chunksReadIndex") - val isComplete = chunksReadIndex > (totalChunkCount - 1) + Log.i(logTag,"chunksReadCounter: $chunksReadCounter") + val isComplete = chunksReadCounter >= totalChunkCount if (isComplete) { Log.d( logTag, - "isComplete: true, totalChunks: $totalChunkCount , chunkReadCounter(1-indexed): $chunksReadIndex" + "isComplete: true, totalChunks: $totalChunkCount , chunkReadCounter(1-indexed): $chunksReadCounter" ) } return isComplete diff --git a/ios/ble/Utility/chunker.swift b/ios/ble/Utility/chunker.swift index 33f09375..4e3851d3 100644 --- a/ios/ble/Utility/chunker.swift +++ b/ios/ble/Utility/chunker.swift @@ -18,7 +18,7 @@ extension ChunkSeqNumber { class Chunker { private var logTag = "Chunker" - private var chunksReadIndex: Int = 0 + private var chunksReadCounter: Int = 0 private var preSlicedChunks: [Data] = [] private var chunkData: Data? private var mtuSize: Int = BLEConstants.DEFAULT_CHUNK_SIZE @@ -59,7 +59,7 @@ class Chunker { } func next() -> Data { - return preSlicedChunks[chunksReadIndex++] + return preSlicedChunks[chunksReadCounter++] } func chunkBySequenceNumber(missedSeqNumber: ChunkSeqNumber) -> Data { @@ -105,9 +105,9 @@ class Chunker { } func isComplete() -> Bool { - let isComplete = chunksReadIndex > (totalChunkCount - 1) + let isComplete = chunksReadCounter >= totalChunkCount if isComplete { - os_log(.info, "isComplete: true, totalChunks: %{public}d , chunkReadCounter(1-indexed): %{public}d", totalChunkCount, chunksReadIndex) + os_log(.info, "isComplete: true, totalChunks: %{public}d , chunkReadCounter(1-indexed): %{public}d", totalChunkCount, chunksReadCounter) } return isComplete } From abd7d502e87dfae0a031107cd647044310e6216d Mon Sep 17 00:00:00 2001 From: Alka Prasad Date: Wed, 12 Apr 2023 11:43:02 +0530 Subject: [PATCH 5/5] refactor(#42): fixed some compilation issue in iOS code --- ios/ble/Utility/chunker.swift | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ios/ble/Utility/chunker.swift b/ios/ble/Utility/chunker.swift index 4e3851d3..f7fada0c 100644 --- a/ios/ble/Utility/chunker.swift +++ b/ios/ble/Utility/chunker.swift @@ -59,7 +59,9 @@ class Chunker { } func next() -> Data { - return preSlicedChunks[chunksReadCounter++] + let chunkIndex = chunksReadCounter + chunksReadCounter += 1 + return preSlicedChunks[chunkIndex] } func chunkBySequenceNumber(missedSeqNumber: ChunkSeqNumber) -> Data { @@ -68,7 +70,7 @@ class Chunker { private func chunk(seqIndex: ChunkSeqIndex) -> Data { let fromIndex = seqIndex * effectivePayloadSize - if isLastChunkSmallerSize(seqIndex) { + if isLastChunkSmallerSize(seqIndex: seqIndex) { let chunkLength = lastChunkByteCount + chunkMetaSize return frameChunk(seqNumber: seqIndex.toSeqNumber(), chunkLength: chunkLength, fromIndex: fromIndex, toIndex: fromIndex + lastChunkByteCount) } else { @@ -78,7 +80,7 @@ class Chunker { } private func isLastChunkSmallerSize(seqIndex: Int) -> Bool { - return isLastChunkIndex(seqIndex) && lastChunkByteCount > 0 + return isLastChunkIndex(seqIndex: seqIndex) && lastChunkByteCount > 0 } private func isLastChunkIndex(seqIndex: Int) -> Bool {