From 644dcc692f1ea8e040793403b376edde3a08e5a9 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Sat, 15 Jul 2017 19:41:20 +0200 Subject: [PATCH] Minor improvements --- .../ch/dissem/bitmessage/BitmessageContext.kt | 23 +++++--------- .../ch/dissem/bitmessage/entity/Plaintext.kt | 22 +++++++------- .../ch/dissem/bitmessage/utils/Property.kt | 30 ++++++++++++------- 3 files changed, 38 insertions(+), 37 deletions(-) diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/BitmessageContext.kt b/core/src/main/kotlin/ch/dissem/bitmessage/BitmessageContext.kt index 2f09636..830c1e5 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/BitmessageContext.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/BitmessageContext.kt @@ -231,12 +231,9 @@ class BitmessageContext( /** * @param host a trusted node that must be reliable (it's used for every synchronization) - * * * @param port of the trusted host, default is 8444 - * * - * @param timeoutInSeconds synchronization should end no later than about 5 seconds after the timeout elapsed, even - * * if not all objects were fetched - * * + * @param timeoutInSeconds synchronization should end no later than about 5 seconds after the timeout elapsed, + * even if not all objects were fetched * @param wait waits for the synchronization thread to finish */ fun synchronize(host: InetAddress, port: Int, timeoutInSeconds: Long, wait: Boolean) { @@ -259,13 +256,10 @@ class BitmessageContext( /** * Send a custom message to a specific node (that should implement handling for this message type) and returns * the response, which in turn is expected to be a [CustomMessage]. - + * * @param server the node's address - * * * @param port the node's port - * * * @param request the request - * * * @return the response */ fun send(server: InetAddress, port: Int, request: CustomMessage): CustomMessage { @@ -284,23 +278,20 @@ class BitmessageContext( * Sends messages again whose time to live expired without being acknowledged. (And whose * recipient is expected to send acknowledgements. * - * * You should call this method regularly, but be aware of the following: * * * As messages might be sent, POW will be done. It is therefore not advised to - * call it on shutdown. + * call it on shutdown. * * It shouldn't be called right after startup, as it's possible the missing - * acknowledgement was sent while the client was offline. + * acknowledgement was sent while the client was offline. * * Other than that, the call isn't expensive as long as there is no message - * to send, so it might be a good idea to just call it every few minutes. - * + * to send, so it might be a good idea to just call it every few minutes. */ fun resendUnacknowledgedMessages() { internals.resendUnacknowledged() } - val isRunning: Boolean - get() = internals.networkHandler.isRunning + fun isRunning() = internals.networkHandler.isRungning fun addContact(contact: BitmessageAddress) { internals.addressRepository.save(contact) 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 9a8c90e..3f0f551 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt @@ -262,7 +262,15 @@ class Plaintext private constructor( fun write(out: OutputStream, includeSignature: Boolean) { Encode.varInt(from.version, out) Encode.varInt(from.stream, out) - if (from.pubkey == null) { + from.pubkey?.apply { + Encode.int32(behaviorBitfield, out) + out.write(signingKey, 1, 64) + out.write(encryptionKey, 1, 64) + if (from.version >= 3) { + Encode.varInt(nonceTrialsPerByte, out) + Encode.varInt(extraBytes, out) + } + } ?: { Encode.int32(0, out) val empty = ByteArray(64) out.write(empty) @@ -271,17 +279,9 @@ class Plaintext private constructor( Encode.varInt(0, out) Encode.varInt(0, out) } - } else { - Encode.int32(from.pubkey!!.behaviorBitfield, out) - out.write(from.pubkey!!.signingKey, 1, 64) - out.write(from.pubkey!!.encryptionKey, 1, 64) - if (from.version >= 3) { - Encode.varInt(from.pubkey!!.nonceTrialsPerByte, out) - Encode.varInt(from.pubkey!!.extraBytes, out) - } - } + }.invoke() if (type == MSG) { - out.write(to!!.ripe) + out.write(to?.ripe ?: throw IllegalStateException("No recipient set for message")) } Encode.varInt(encodingCode, out) Encode.varInt(message.size, out) diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/utils/Property.kt b/core/src/main/kotlin/ch/dissem/bitmessage/utils/Property.kt index 91fbb24..099a610 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/utils/Property.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/utils/Property.kt @@ -52,22 +52,32 @@ class Property private constructor( } override fun toString(): String { - return toString("") + return toJson("") } - private fun toString(indentation: String): String { + @JvmOverloads + fun toJson(indentation: String = ""): String { val result = StringBuilder() - result.append(indentation).append(name).append(": ") + result.append(indentation).append('"').append(name).append('"').append(": ") if (value != null || properties.isEmpty()) { - result.append(value) - } - if (properties.isNotEmpty()) { + result.append(asJson(value, indentation)) + } else if (properties.isNotEmpty()) { result.append("{\n") - for (property in properties) { - result.append(property.toString(indentation + " ")).append('\n') - } - result.append(indentation).append("}") + result.append(properties.map { it.toJson(indentation + " ") }.reduce { l, r -> "$l,\n$r" }) + result.append('\n').append(indentation).append("}") + } else { + result.append("null") } return result.toString() } + + private fun asJson(value: Any?, indentation: String): String = when (value) { + null -> "null" + is Number, is Boolean -> value.toString() + is Property -> value.toJson(indentation) + is Collection<*> -> """[ +${value.map { asJson(it, indentation + " ") }.reduce { l, r -> "$indentation $l,\n$indentation $r" }} +$indentation]""" + else -> "\"$value\"" + } }