Added easy way to disable acknowledges and fixed possible issue with builder-constructor

This commit is contained in:
Christian Basler 2017-08-11 17:35:46 +02:00
parent c81c89197b
commit cf6b3e2603

View File

@ -229,7 +229,8 @@ class Plaintext private constructor(
) )
constructor(builder: Builder) : this( 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"), from = builder.from ?: throw IllegalStateException("sender identity not set"),
to = builder.to, to = builder.to,
encodingCode = builder.encoding, encodingCode = builder.encoding,
@ -532,6 +533,7 @@ class Plaintext private constructor(
private var nonceTrialsPerByte: Long = 0 private var nonceTrialsPerByte: Long = 0
private var extraBytes: Long = 0 private var extraBytes: Long = 0
private var destinationRipe: ByteArray? = null private var destinationRipe: ByteArray? = null
private var preventAck: Boolean = false
internal var encoding: Long = 0 internal var encoding: Long = 0
internal var message = ByteArray(0) internal var message = ByteArray(0)
internal var ackData: ByteArray? = null internal var ackData: ByteArray? = null
@ -611,6 +613,12 @@ class Plaintext private constructor(
return this return this
} }
@JvmOverloads
fun preventAck(preventAck: Boolean = true): Builder {
this.preventAck = preventAck
return this
}
fun encoding(encoding: Encoding): Builder { fun encoding(encoding: Encoding): Builder {
this.encoding = encoding.code this.encoding = encoding.code
return this return this
@ -700,7 +708,7 @@ class Plaintext private constructor(
return this return this
} }
fun build(): Plaintext { internal fun prepare(): Builder {
if (from == null) { if (from == null) {
from = BitmessageAddress(Factory.createPubkey( from = BitmessageAddress(Factory.createPubkey(
addressVersion, addressVersion,
@ -715,12 +723,19 @@ class Plaintext private constructor(
if (to == null && type != Type.BROADCAST && destinationRipe != null) { if (to == null && type != Type.BROADCAST && destinationRipe != null) {
to = BitmessageAddress(0, 0, destinationRipe!!) 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) ackData = cryptography().randomBytes(Msg.ACK_LENGTH)
} }
if (ttl <= 0) { if (ttl <= 0) {
ttl = TTL.msg ttl = TTL.msg
} }
return this
}
fun build(): Plaintext {
return Plaintext(this) return Plaintext(this)
} }
} }