From cf6b3e2603e00dadea2c405f8930f2a0c3321cae Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Fri, 11 Aug 2017 17:35:46 +0200 Subject: [PATCH] Added easy way to disable acknowledges and fixed possible issue with builder-constructor --- .../ch/dissem/bitmessage/entity/Plaintext.kt | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt b/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt index 72506b6..aa28f6e 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt @@ -229,7 +229,8 @@ class Plaintext private constructor( ) constructor(builder: Builder) : this( - type = builder.type, + // Calling prepare() here is somewhat ugly, but also a foolproof way to make sure the builder is properly initialized + type = builder.prepare().type, from = builder.from ?: throw IllegalStateException("sender identity not set"), to = builder.to, encodingCode = builder.encoding, @@ -532,6 +533,7 @@ class Plaintext private constructor( private var nonceTrialsPerByte: Long = 0 private var extraBytes: Long = 0 private var destinationRipe: ByteArray? = null + private var preventAck: Boolean = false internal var encoding: Long = 0 internal var message = ByteArray(0) internal var ackData: ByteArray? = null @@ -611,6 +613,12 @@ class Plaintext private constructor( return this } + @JvmOverloads + fun preventAck(preventAck: Boolean = true): Builder { + this.preventAck = preventAck + return this + } + fun encoding(encoding: Encoding): Builder { this.encoding = encoding.code return this @@ -700,7 +708,7 @@ class Plaintext private constructor( return this } - fun build(): Plaintext { + internal fun prepare(): Builder { if (from == null) { from = BitmessageAddress(Factory.createPubkey( addressVersion, @@ -715,12 +723,19 @@ class Plaintext private constructor( if (to == null && type != Type.BROADCAST && destinationRipe != null) { to = BitmessageAddress(0, 0, destinationRipe!!) } - if (type == MSG && ackMessage == null && ackData == null) { + if (preventAck) { + ackData = null + ackMessage = null + } else if (type == MSG && ackMessage == null && ackData == null) { ackData = cryptography().randomBytes(Msg.ACK_LENGTH) } if (ttl <= 0) { ttl = TTL.msg } + return this + } + + fun build(): Plaintext { return Plaintext(this) } }