Fix everything bu function server
This commit is contained in:
parent
76e7f47528
commit
ad2f5681b6
@ -22,13 +22,13 @@ class EnvelopeBuilder {
|
||||
var name by metaBuilder.string(key = Envelope.ENVELOPE_NAME_KEY)
|
||||
|
||||
/**
|
||||
* Construct a binary and transform it into byte-array based buffer
|
||||
* Construct a data binary from given builder
|
||||
*/
|
||||
@ExperimentalIoApi
|
||||
fun data(block: Output.() -> Unit) {
|
||||
val bytes = buildBytes {
|
||||
block()
|
||||
}
|
||||
data = ArrayBinary(bytes.toByteArray())
|
||||
val bytes = buildBytes(builder = block)
|
||||
data = bytes.toByteArray().asBinary() //save data to byte array so
|
||||
bytes.close()
|
||||
}
|
||||
|
||||
internal fun build() = SimpleEnvelope(metaBuilder.seal(), data)
|
||||
|
@ -4,12 +4,15 @@ import hep.dataforge.context.Global
|
||||
import hep.dataforge.io.EnvelopeParts.FORMAT_META_KEY
|
||||
import hep.dataforge.io.EnvelopeParts.FORMAT_NAME_KEY
|
||||
import hep.dataforge.io.EnvelopeParts.INDEX_KEY
|
||||
import hep.dataforge.io.EnvelopeParts.MULTIPART_DATA_SEPARATOR
|
||||
import hep.dataforge.io.EnvelopeParts.MULTIPART_DATA_TYPE
|
||||
import hep.dataforge.io.EnvelopeParts.SIZE_KEY
|
||||
import hep.dataforge.meta.*
|
||||
import hep.dataforge.names.asName
|
||||
import hep.dataforge.names.plus
|
||||
import hep.dataforge.names.toName
|
||||
import kotlinx.io.text.readRawString
|
||||
import kotlinx.io.text.writeRawString
|
||||
|
||||
object EnvelopeParts {
|
||||
val MULTIPART_KEY = "multipart".asName()
|
||||
@ -18,6 +21,8 @@ object EnvelopeParts {
|
||||
val FORMAT_NAME_KEY = Envelope.ENVELOPE_NODE_KEY + MULTIPART_KEY + "format"
|
||||
val FORMAT_META_KEY = Envelope.ENVELOPE_NODE_KEY + MULTIPART_KEY + "meta"
|
||||
|
||||
const val MULTIPART_DATA_SEPARATOR = "#~PART~#\r\n"
|
||||
|
||||
const val MULTIPART_DATA_TYPE = "envelope.multipart"
|
||||
}
|
||||
|
||||
@ -41,6 +46,7 @@ fun EnvelopeBuilder.multipart(
|
||||
data {
|
||||
format(formatMeta).run {
|
||||
envelopes.forEach {
|
||||
writeRawString(MULTIPART_DATA_SEPARATOR)
|
||||
writeEnvelope(it)
|
||||
}
|
||||
}
|
||||
@ -68,7 +74,8 @@ fun EnvelopeBuilder.multipart(
|
||||
format.run {
|
||||
var counter = 0
|
||||
envelopes.forEach { (key, envelope) ->
|
||||
writeObject(envelope)
|
||||
writeRawString(MULTIPART_DATA_SEPARATOR)
|
||||
writeEnvelope(envelope)
|
||||
meta {
|
||||
append(INDEX_KEY, buildMeta {
|
||||
"key" put key
|
||||
@ -105,6 +112,8 @@ fun Envelope.parts(io: IOPlugin = Global.plugins.fetch(IOPlugin)): Sequence<Enve
|
||||
data?.read {
|
||||
sequence {
|
||||
repeat(size) {
|
||||
val separator = readRawString(MULTIPART_DATA_SEPARATOR.length)
|
||||
if(separator!= MULTIPART_DATA_SEPARATOR) error("Separator is expected")
|
||||
yield(readObject())
|
||||
}
|
||||
}
|
||||
|
@ -73,9 +73,7 @@ class TaggedEnvelopeFormat(
|
||||
}
|
||||
}
|
||||
|
||||
val data = buildBytes {
|
||||
writeInput(this@readObject, tag.dataSize.toInt())
|
||||
}
|
||||
val data = ByteArray(tag.dataSize.toInt()).also { readArray(it) }.asBinary()
|
||||
|
||||
return SimpleEnvelope(meta, data)
|
||||
}
|
||||
|
@ -108,7 +108,7 @@ class TaglessEnvelopeFormat(
|
||||
} else {
|
||||
buildBytes {
|
||||
writeInput(this@readObject)
|
||||
}
|
||||
}.toByteArray().asBinary()
|
||||
}
|
||||
|
||||
return SimpleEnvelope(meta, data)
|
||||
|
@ -18,24 +18,29 @@ class MultipartTest {
|
||||
}
|
||||
data {
|
||||
writeUtf8String("Hello World $it")
|
||||
repeat(2000) {
|
||||
writeInt(it)
|
||||
}
|
||||
// repeat(2000) {
|
||||
// writeInt(it)
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val partsEnvelope = Envelope {
|
||||
multipart(envelopes, TaggedEnvelopeFormat)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testParts() {
|
||||
val bytes = TaggedEnvelopeFormat.writeByteArray(partsEnvelope)
|
||||
assertTrue { bytes.size > envelopes.sumBy { it.data!!.size.toInt() } }
|
||||
val reconstructed = TaggedEnvelopeFormat.readByteArray(bytes)
|
||||
val parts = reconstructed.parts()?.toList() ?: emptyList()
|
||||
assertEquals(2, parts[2].meta["value"].int)
|
||||
println(reconstructed.data!!.size)
|
||||
TaggedEnvelopeFormat.run {
|
||||
val singleEnvelopeData = writeBytes(envelopes[0])
|
||||
val singleEnvelopeSize = singleEnvelopeData.size
|
||||
val bytes = writeBytes(partsEnvelope)
|
||||
assertTrue(5*singleEnvelopeSize < bytes.size)
|
||||
val reconstructed = bytes.readWith(this)
|
||||
val parts = reconstructed.parts()?.toList() ?: emptyList()
|
||||
assertEquals(2, parts[2].meta["value"].int)
|
||||
println(reconstructed.data!!.size)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user