|
@@ -10,35 +10,26 @@ import kotlin.concurrent.fixedRateTimer
|
|
|
class ChunkCollector(private val id: String) {
|
|
|
private val mapSocketChunk: ConcurrentMap<Int, SocketChunkData> by lazy { ConcurrentHashMap() }
|
|
|
|
|
|
- init {
|
|
|
- val tStart = LocalDateTime.now()
|
|
|
- fixedRateTimer("timer-$id", false, 20, 50) {
|
|
|
- val listSocketChunk = mapSocketChunk.entries.sortedBy { it.key }.map { it.value }
|
|
|
- if (listSocketChunk.size > 1 && listSocketChunk[0].totalPart == listSocketChunk.size) {
|
|
|
- val sortedChunk = listSocketChunk.sortedBy { it.part }
|
|
|
- val bodyList = sortedChunk.mapNotNull { it.body }
|
|
|
- var bd = ByteArray(0)
|
|
|
- bodyList.forEach { bd += it }
|
|
|
- Singletons.responseQue.getOrPut(id) {
|
|
|
- Resp(
|
|
|
- bd,
|
|
|
- sortedChunk.last().status,
|
|
|
- listSocketChunk.firstOrNull { it.header != null }?.header?.entries?.associate {
|
|
|
- Pair(it.key, it.value.toTypedArray())
|
|
|
- }
|
|
|
- )
|
|
|
- }
|
|
|
-
|
|
|
- this.cancel()
|
|
|
- }
|
|
|
- if (Duration.between(tStart, LocalDateTime.now()).toMillis() > 10 * 60 * 1000) {
|
|
|
- this.cancel()
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+ @Synchronized
|
|
|
fun add(dt: SocketChunkData) {
|
|
|
mapSocketChunk.getOrPut(dt.part) { dt }
|
|
|
+ val listSocketChunk = mapSocketChunk.entries.sortedBy { it.key }.map { it.value }
|
|
|
+ if (listSocketChunk.isNotEmpty() && listSocketChunk[0].totalPart == listSocketChunk.size) {
|
|
|
+ println("----> $id")
|
|
|
+ val sortedChunk = listSocketChunk.sortedBy { it.part }
|
|
|
+ val bodyList = sortedChunk.mapNotNull { it.body }
|
|
|
+ var bd = ByteArray(0)
|
|
|
+ bodyList.forEach { bd += it }
|
|
|
+ Singletons.responseQue.getOrPut(id) {
|
|
|
+ Resp(
|
|
|
+ bd,
|
|
|
+ sortedChunk.last().status,
|
|
|
+ listSocketChunk.firstOrNull { it.header != null }?.header?.entries?.associate {
|
|
|
+ Pair(it.key, it.value.toTypedArray())
|
|
|
+ }
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|