Added version to user agent info, and other minor fixes

This commit is contained in:
Christian Basler 2017-07-05 00:37:45 +02:00
parent 35d7486869
commit d3a06e7639
12 changed files with 69 additions and 14 deletions

View File

@ -54,6 +54,14 @@ subprojects {
archives javadocJar, sourcesJar archives javadocJar, sourcesJar
} }
jar {
manifest {
attributes 'Implementation-Title': "Jabit ${project.name.capitalize()}",
'Implementation-Version': version
}
baseName "jabit-${project.name}"
}
signing { signing {
required { isRelease && project.getProperties().get("signing.keyId")?.length() > 0 } required { isRelease && project.getProperties().get("signing.keyId")?.length() > 0 }
sign configurations.archives sign configurations.archives

View File

@ -25,9 +25,27 @@ artifacts {
dependencies { dependencies {
compile 'org.slf4j:slf4j-api' compile 'org.slf4j:slf4j-api'
compile 'ch.dissem.msgpack:msgpack' compile 'ch.dissem.msgpack:msgpack:1.0.0'
testCompile 'junit:junit' testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-library' testCompile 'org.hamcrest:hamcrest-library:1.3'
testCompile 'com.nhaarman:mockito-kotlin' testCompile 'com.nhaarman:mockito-kotlin:1.5.0'
testCompile project(':cryptography-bc') testCompile project(':cryptography-bc')
} }
def generatedResources = "${project.buildDir}/generated-resources/main"
sourceSets {
main {
output.dir(generatedResources, builtBy: 'generateVersionInfo')
}
}
task('generateVersionInfo') {
doLast {
def dir = new File(generatedResources)
if (!dir.exists()) {
dir.mkdirs()
}
def file = new File(generatedResources, "version")
file.write(project.version.toString())
}
}

View File

@ -75,6 +75,7 @@ class BitmessageContext(
}, },
listener: Listener, listener: Listener,
labeler: Labeler = DefaultLabeler(), labeler: Labeler = DefaultLabeler(),
userAgent: String? = null,
port: Int = 8444, port: Int = 8444,
connectionTTL: Long = 30 * MINUTE, connectionTTL: Long = 30 * MINUTE,
connectionLimit: Int = 150, connectionLimit: Int = 150,
@ -99,6 +100,7 @@ class BitmessageContext(
}, },
builder.listener, builder.listener,
builder.labeler ?: DefaultLabeler(), builder.labeler ?: DefaultLabeler(),
builder.userAgent,
builder.port, builder.port,
builder.connectionTTL, builder.connectionTTL,
builder.connectionLimit, builder.connectionLimit,
@ -333,6 +335,7 @@ class BitmessageContext(
fun status(): Property { fun status(): Property {
return Property("status", return Property("status",
Property("user agent", internals.userAgent),
internals.networkHandler.getNetworkStatus(), internals.networkHandler.getNetworkStatus(),
Property("unacknowledged", internals.messageRepository.findMessagesToResend().size) Property("unacknowledged", internals.messageRepository.findMessagesToResend().size)
) )
@ -361,6 +364,7 @@ class BitmessageContext(
internal var cryptography by Delegates.notNull<Cryptography>() internal var cryptography by Delegates.notNull<Cryptography>()
internal var customCommandHandler: CustomCommandHandler? = null internal var customCommandHandler: CustomCommandHandler? = null
internal var labeler: Labeler? = null internal var labeler: Labeler? = null
internal var userAgent: String? = null
internal var listener by Delegates.notNull<Listener>() internal var listener by Delegates.notNull<Listener>()
internal var connectionLimit = 150 internal var connectionLimit = 150
internal var connectionTTL = 30 * MINUTE internal var connectionTTL = 30 * MINUTE
@ -480,6 +484,7 @@ class BitmessageContext(
customCommandHandler, customCommandHandler,
listener, listener,
labeler, labeler,
userAgent?.let { "/$it/Jabit:$version/" } ?: "/Jabit:$version/",
port, port,
connectionTTL, connectionTTL,
connectionLimit connectionLimit
@ -494,5 +499,11 @@ class BitmessageContext(
companion object { companion object {
@JvmField val CURRENT_VERSION = 3 @JvmField val CURRENT_VERSION = 3
private val LOG = LoggerFactory.getLogger(BitmessageContext::class.java) private val LOG = LoggerFactory.getLogger(BitmessageContext::class.java)
val version: String by lazy {
BitmessageContext::class.java.getResource("/version")?.readText() ?: "local build"
}
@JvmStatic get
} }
} }

View File

@ -29,7 +29,7 @@ import ch.dissem.bitmessage.utils.Strings.hex
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.util.* import java.util.*
internal open class DefaultMessageListener( open class DefaultMessageListener(
private val labeler: Labeler, private val labeler: Labeler,
private val listener: BitmessageContext.Listener private val listener: BitmessageContext.Listener
) : NetworkHandler.MessageListener, InternalContext.ContextHolder { ) : NetworkHandler.MessageListener, InternalContext.ContextHolder {

View File

@ -51,6 +51,8 @@ class InternalContext(
listener: BitmessageContext.Listener, listener: BitmessageContext.Listener,
val labeler: Labeler, val labeler: Labeler,
val userAgent: String,
val port: Int, val port: Int,
val connectionTTL: Long, val connectionTTL: Long,
val connectionLimit: Int val connectionLimit: Int

View File

@ -62,7 +62,7 @@ class Version constructor(
/** /**
* User Agent (0x00 if string is 0 bytes long). Sending nodes must not include a user_agent longer than 5000 bytes. * User Agent (0x00 if string is 0 bytes long). Sending nodes must not include a user_agent longer than 5000 bytes.
*/ */
val userAgent: String = "/Jabit:0.0.1/", val userAgent: String,
/** /**
* The stream numbers that the emitting node is interested in. Sending nodes must not include more than 160000 * The stream numbers that the emitting node is interested in. Sending nodes must not include more than 160000

View File

@ -25,7 +25,11 @@ package ch.dissem.bitmessage.utils
* If you need a real JSON representation, please add a method `toJson()`. * If you need a real JSON representation, please add a method `toJson()`.
* *
*/ */
class Property private constructor(val name: String, val value: Any? = null, val properties: Array<Property> = emptyArray()) { class Property private constructor(
val name: String,
val value: Any? = null,
val properties: Array<Property>
) {
constructor(name: String, value: Any) : this(name = name, value = value, properties = emptyArray()) constructor(name: String, value: Any) : this(name = name, value = value, properties = emptyArray())
constructor(name: String, vararg properties: Property) : this(name, null, arrayOf(*properties)) constructor(name: String, vararg properties: Property) : this(name, null, arrayOf(*properties))

View File

@ -28,6 +28,7 @@ import ch.dissem.bitmessage.ports.DefaultLabeler
import ch.dissem.bitmessage.ports.ProofOfWorkEngine import ch.dissem.bitmessage.ports.ProofOfWorkEngine
import ch.dissem.bitmessage.ports.ProofOfWorkRepository import ch.dissem.bitmessage.ports.ProofOfWorkRepository
import ch.dissem.bitmessage.testutils.TestInventory import ch.dissem.bitmessage.testutils.TestInventory
import ch.dissem.bitmessage.utils.Property
import ch.dissem.bitmessage.utils.Singleton.cryptography import ch.dissem.bitmessage.utils.Singleton.cryptography
import ch.dissem.bitmessage.utils.Strings.hex import ch.dissem.bitmessage.utils.Strings.hex
import ch.dissem.bitmessage.utils.TTL import ch.dissem.bitmessage.utils.TTL
@ -98,7 +99,9 @@ class BitmessageContextTest {
.inventory(inventory) .inventory(inventory)
.listener(listener) .listener(listener)
.messageRepo(mock()) .messageRepo(mock())
.networkHandler(mock()) .networkHandler(mock {
on { getNetworkStatus() } doReturn Property("test", "mocked")
})
.nodeRegistry(mock()) .nodeRegistry(mock())
.labeler(spy(DefaultLabeler())) .labeler(spy(DefaultLabeler()))
.powRepo(testPowRepo) .powRepo(testPowRepo)
@ -318,4 +321,10 @@ class BitmessageContextTest {
ctx.resendUnacknowledgedMessages() ctx.resendUnacknowledgedMessages()
verify(ctx.labeler, timeout(1000).times(1)).markAsSent(eq(plaintext)) verify(ctx.labeler, timeout(1000).times(1)).markAsSent(eq(plaintext))
} }
@Test
fun `ensure status contains user agent`() {
val userAgent = ctx.status().getProperty("user agent")?.value.toString()
assertThat(userAgent, `is`("/Jabit:${BitmessageContext.version}/"))
}
} }

View File

@ -125,6 +125,7 @@ object TestUtils {
customCommandHandler, customCommandHandler,
listener, listener,
labeler, labeler,
"/Jabit:TEST/",
port, port,
connectionTTL, connectionTTL,
connectionLimit connectionLimit

View File

@ -46,6 +46,8 @@ public class Main {
if (System.getProperty("org.slf4j.simpleLogger.logFile") == null) if (System.getProperty("org.slf4j.simpleLogger.logFile") == null)
System.setProperty("org.slf4j.simpleLogger.logFile", "./jabit.log"); System.setProperty("org.slf4j.simpleLogger.logFile", "./jabit.log");
System.out.println("Version: " + BitmessageContext.getVersion());
CmdLineOptions options = new CmdLineOptions(); CmdLineOptions options = new CmdLineOptions();
CmdLineParser parser = new CmdLineParser(options); CmdLineParser parser = new CmdLineParser(options);
try { try {

View File

@ -41,7 +41,7 @@ class NetworkConnectionInitializer(
fun start() { fun start() {
if (mode == Connection.Mode.CLIENT || mode == Connection.Mode.SYNC) { if (mode == Connection.Mode.CLIENT || mode == Connection.Mode.SYNC) {
send(Version(nonce = ctx.clientNonce, addrFrom = NetworkAddress.ANY, addrRecv = node)) send(Version(nonce = ctx.clientNonce, addrFrom = NetworkAddress.ANY, addrRecv = node, userAgent = ctx.userAgent))
} }
} }

View File

@ -408,18 +408,18 @@ class NioNetworkHandler : NetworkHandler, InternalContext.ContextHolder {
val incomingConnections = TreeMap<Long, Int>() val incomingConnections = TreeMap<Long, Int>()
val outgoingConnections = TreeMap<Long, Int>() val outgoingConnections = TreeMap<Long, Int>()
for (connection in connections.keys) { connections.keys
if (connection.state == Connection.State.ACTIVE) { .filter { it.state == Connection.State.ACTIVE }
for (stream in connection.streams) { .forEach {
for (stream in it.streams) {
streams.add(stream) streams.add(stream)
if (connection.mode == SERVER) { if (it.mode == SERVER) {
DebugUtils.inc(incomingConnections, stream) DebugUtils.inc(incomingConnections, stream)
} else { } else {
DebugUtils.inc(outgoingConnections, stream) DebugUtils.inc(outgoingConnections, stream)
} }
} }
} }
}
val streamProperties = mutableListOf<Property>() val streamProperties = mutableListOf<Property>()
for (stream in streams) { for (stream in streams) {
val incoming = incomingConnections[stream] ?: 0 val incoming = incomingConnections[stream] ?: 0