Fix everything bu function server

This commit is contained in:
Alexander Nozik 2019-12-01 18:13:10 +03:00
parent 76e7f47528
commit ad2f5681b6
5 changed files with 31 additions and 19 deletions

View File

@ -22,13 +22,13 @@ class EnvelopeBuilder {
var name by metaBuilder.string(key = Envelope.ENVELOPE_NAME_KEY) 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) { fun data(block: Output.() -> Unit) {
val bytes = buildBytes { val bytes = buildBytes(builder = block)
block() data = bytes.toByteArray().asBinary() //save data to byte array so
} bytes.close()
data = ArrayBinary(bytes.toByteArray())
} }
internal fun build() = SimpleEnvelope(metaBuilder.seal(), data) internal fun build() = SimpleEnvelope(metaBuilder.seal(), data)

View File

@ -4,12 +4,15 @@ import hep.dataforge.context.Global
import hep.dataforge.io.EnvelopeParts.FORMAT_META_KEY import hep.dataforge.io.EnvelopeParts.FORMAT_META_KEY
import hep.dataforge.io.EnvelopeParts.FORMAT_NAME_KEY import hep.dataforge.io.EnvelopeParts.FORMAT_NAME_KEY
import hep.dataforge.io.EnvelopeParts.INDEX_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.MULTIPART_DATA_TYPE
import hep.dataforge.io.EnvelopeParts.SIZE_KEY import hep.dataforge.io.EnvelopeParts.SIZE_KEY
import hep.dataforge.meta.* import hep.dataforge.meta.*
import hep.dataforge.names.asName import hep.dataforge.names.asName
import hep.dataforge.names.plus import hep.dataforge.names.plus
import hep.dataforge.names.toName import hep.dataforge.names.toName
import kotlinx.io.text.readRawString
import kotlinx.io.text.writeRawString
object EnvelopeParts { object EnvelopeParts {
val MULTIPART_KEY = "multipart".asName() val MULTIPART_KEY = "multipart".asName()
@ -18,6 +21,8 @@ object EnvelopeParts {
val FORMAT_NAME_KEY = Envelope.ENVELOPE_NODE_KEY + MULTIPART_KEY + "format" val FORMAT_NAME_KEY = Envelope.ENVELOPE_NODE_KEY + MULTIPART_KEY + "format"
val FORMAT_META_KEY = Envelope.ENVELOPE_NODE_KEY + MULTIPART_KEY + "meta" 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" const val MULTIPART_DATA_TYPE = "envelope.multipart"
} }
@ -41,6 +46,7 @@ fun EnvelopeBuilder.multipart(
data { data {
format(formatMeta).run { format(formatMeta).run {
envelopes.forEach { envelopes.forEach {
writeRawString(MULTIPART_DATA_SEPARATOR)
writeEnvelope(it) writeEnvelope(it)
} }
} }
@ -68,7 +74,8 @@ fun EnvelopeBuilder.multipart(
format.run { format.run {
var counter = 0 var counter = 0
envelopes.forEach { (key, envelope) -> envelopes.forEach { (key, envelope) ->
writeObject(envelope) writeRawString(MULTIPART_DATA_SEPARATOR)
writeEnvelope(envelope)
meta { meta {
append(INDEX_KEY, buildMeta { append(INDEX_KEY, buildMeta {
"key" put key "key" put key
@ -105,6 +112,8 @@ fun Envelope.parts(io: IOPlugin = Global.plugins.fetch(IOPlugin)): Sequence<Enve
data?.read { data?.read {
sequence { sequence {
repeat(size) { repeat(size) {
val separator = readRawString(MULTIPART_DATA_SEPARATOR.length)
if(separator!= MULTIPART_DATA_SEPARATOR) error("Separator is expected")
yield(readObject()) yield(readObject())
} }
} }

View File

@ -73,9 +73,7 @@ class TaggedEnvelopeFormat(
} }
} }
val data = buildBytes { val data = ByteArray(tag.dataSize.toInt()).also { readArray(it) }.asBinary()
writeInput(this@readObject, tag.dataSize.toInt())
}
return SimpleEnvelope(meta, data) return SimpleEnvelope(meta, data)
} }

View File

@ -108,7 +108,7 @@ class TaglessEnvelopeFormat(
} else { } else {
buildBytes { buildBytes {
writeInput(this@readObject) writeInput(this@readObject)
} }.toByteArray().asBinary()
} }
return SimpleEnvelope(meta, data) return SimpleEnvelope(meta, data)

View File

@ -18,24 +18,29 @@ class MultipartTest {
} }
data { data {
writeUtf8String("Hello World $it") writeUtf8String("Hello World $it")
repeat(2000) { // repeat(2000) {
writeInt(it) // writeInt(it)
} // }
} }
} }
} }
val partsEnvelope = Envelope { val partsEnvelope = Envelope {
multipart(envelopes, TaggedEnvelopeFormat) multipart(envelopes, TaggedEnvelopeFormat)
} }
@Test @Test
fun testParts() { fun testParts() {
val bytes = TaggedEnvelopeFormat.writeByteArray(partsEnvelope) TaggedEnvelopeFormat.run {
assertTrue { bytes.size > envelopes.sumBy { it.data!!.size.toInt() } } val singleEnvelopeData = writeBytes(envelopes[0])
val reconstructed = TaggedEnvelopeFormat.readByteArray(bytes) val singleEnvelopeSize = singleEnvelopeData.size
val parts = reconstructed.parts()?.toList() ?: emptyList() val bytes = writeBytes(partsEnvelope)
assertEquals(2, parts[2].meta["value"].int) assertTrue(5*singleEnvelopeSize < bytes.size)
println(reconstructed.data!!.size) val reconstructed = bytes.readWith(this)
val parts = reconstructed.parts()?.toList() ?: emptyList()
assertEquals(2, parts[2].meta["value"].int)
println(reconstructed.data!!.size)
}
} }
} }