Minor improvements

This commit is contained in:
Christian Basler 2017-07-15 19:41:20 +02:00
parent 009346cd30
commit 644dcc692f
3 changed files with 38 additions and 37 deletions

View File

@ -231,12 +231,9 @@ class BitmessageContext(
/** /**
* @param host a trusted node that must be reliable (it's used for every synchronization) * @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 port of the trusted host, default is 8444
* * * @param timeoutInSeconds synchronization should end no later than about 5 seconds after the timeout elapsed,
* @param timeoutInSeconds synchronization should end no later than about 5 seconds after the timeout elapsed, even * even if not all objects were fetched
* * if not all objects were fetched
* *
* @param wait waits for the synchronization thread to finish * @param wait waits for the synchronization thread to finish
*/ */
fun synchronize(host: InetAddress, port: Int, timeoutInSeconds: Long, wait: Boolean) { 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 * 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]. * the response, which in turn is expected to be a [CustomMessage].
*
* @param server the node's address * @param server the node's address
* *
* @param port the node's port * @param port the node's port
* *
* @param request the request * @param request the request
* *
* @return the response * @return the response
*/ */
fun send(server: InetAddress, port: Int, request: CustomMessage): CustomMessage { fun send(server: InetAddress, port: Int, request: CustomMessage): CustomMessage {
@ -284,7 +278,6 @@ class BitmessageContext(
* Sends messages again whose time to live expired without being acknowledged. (And whose * Sends messages again whose time to live expired without being acknowledged. (And whose
* recipient is expected to send acknowledgements. * recipient is expected to send acknowledgements.
* *
*
* You should call this method regularly, but be aware of the following: * 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 * * As messages might be sent, POW will be done. It is therefore not advised to
@ -293,14 +286,12 @@ class BitmessageContext(
* 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 * * 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() { fun resendUnacknowledgedMessages() {
internals.resendUnacknowledged() internals.resendUnacknowledged()
} }
val isRunning: Boolean fun isRunning() = internals.networkHandler.isRungning
get() = internals.networkHandler.isRunning
fun addContact(contact: BitmessageAddress) { fun addContact(contact: BitmessageAddress) {
internals.addressRepository.save(contact) internals.addressRepository.save(contact)

View File

@ -262,7 +262,15 @@ class Plaintext private constructor(
fun write(out: OutputStream, includeSignature: Boolean) { fun write(out: OutputStream, includeSignature: Boolean) {
Encode.varInt(from.version, out) Encode.varInt(from.version, out)
Encode.varInt(from.stream, 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) Encode.int32(0, out)
val empty = ByteArray(64) val empty = ByteArray(64)
out.write(empty) out.write(empty)
@ -271,17 +279,9 @@ class Plaintext private constructor(
Encode.varInt(0, out) Encode.varInt(0, out)
Encode.varInt(0, out) Encode.varInt(0, out)
} }
} else { }.invoke()
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)
}
}
if (type == MSG) { if (type == MSG) {
out.write(to!!.ripe) out.write(to?.ripe ?: throw IllegalStateException("No recipient set for message"))
} }
Encode.varInt(encodingCode, out) Encode.varInt(encodingCode, out)
Encode.varInt(message.size, out) Encode.varInt(message.size, out)

View File

@ -52,22 +52,32 @@ class Property private constructor(
} }
override fun toString(): String { override fun toString(): String {
return toString("") return toJson("")
} }
private fun toString(indentation: String): String { @JvmOverloads
fun toJson(indentation: String = ""): String {
val result = StringBuilder() val result = StringBuilder()
result.append(indentation).append(name).append(": ") result.append(indentation).append('"').append(name).append('"').append(": ")
if (value != null || properties.isEmpty()) { if (value != null || properties.isEmpty()) {
result.append(value) result.append(asJson(value, indentation))
} } else if (properties.isNotEmpty()) {
if (properties.isNotEmpty()) {
result.append("{\n") result.append("{\n")
for (property in properties) { result.append(properties.map { it.toJson(indentation + " ") }.reduce { l, r -> "$l,\n$r" })
result.append(property.toString(indentation + " ")).append('\n') result.append('\n').append(indentation).append("}")
} } else {
result.append(indentation).append("}") result.append("null")
} }
return result.toString() 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\""
}
} }