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 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)

View File

@ -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)

View File

@ -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\""
}
}