From 62d911a478384426d53d785aecb2342200d6717d Mon Sep 17 00:00:00 2001 From: Chris Rankin Date: Mon, 6 Feb 2017 14:54:48 +0000 Subject: [PATCH] First batch of code review changes. --- .../net/corda/demobench/model/DBViewer.kt | 44 ++++++++--------- .../net/corda/demobench/model/Explorer.kt | 12 ++--- .../demobench/model/ExplorerController.kt | 8 +-- .../net/corda/demobench/model/JVMConfig.kt | 11 ++--- .../corda/demobench/model/NetworkMapConfig.kt | 7 +-- .../net/corda/demobench/model/NodeConfig.kt | 29 ++++------- .../corda/demobench/model/NodeController.kt | 24 +++++---- .../demobench/model/ServiceController.kt | 25 +++++----- .../kotlin/net/corda/demobench/rpc/NodeRPC.kt | 16 +++--- .../net/corda/demobench/views/NodeTabView.kt | 6 ++- .../corda/demobench/views/NodeTerminalView.kt | 49 ++++++++++--------- .../org/h2/server/web/LocalWebServer.kt | 3 -- 12 files changed, 115 insertions(+), 119 deletions(-) diff --git a/tools/demobench/src/main/kotlin/net/corda/demobench/model/DBViewer.kt b/tools/demobench/src/main/kotlin/net/corda/demobench/model/DBViewer.kt index ec0d201bdc..cc742eb68f 100644 --- a/tools/demobench/src/main/kotlin/net/corda/demobench/model/DBViewer.kt +++ b/tools/demobench/src/main/kotlin/net/corda/demobench/model/DBViewer.kt @@ -15,28 +15,6 @@ class DBViewer : AutoCloseable { private val pool = Executors.newCachedThreadPool() private val t = Thread("DBViewer") - override fun close() { - webServer.shutdown() - pool.shutdown() - t.join() - } - - fun openBrowser(h2Port: Int) { - val conn = JdbcUtils.getConnection( - org.h2.Driver::class.jvmName, - "jdbc:h2:tcp://localhost:%d/node".format(h2Port), - "sa", - "" - ) - - val url = (webServer.service as LocalWebServer).addSession(conn) - log.info("Session: {}", url) - - pool.execute { - Server.openBrowser(url) - } - } - init { val ws = LocalWebServer() webServer = Server(ws, "-webPort", "0") @@ -51,4 +29,26 @@ class DBViewer : AutoCloseable { } } + override fun close() { + webServer.shutdown() + pool.shutdown() + t.join() + } + + fun openBrowser(h2Port: Int) { + val conn = JdbcUtils.getConnection( + org.h2.Driver::class.jvmName, + "jdbc:h2:tcp://localhost:$h2Port/node", + "sa", + "" + ) + + val url = (webServer.service as LocalWebServer).addSession(conn) + log.info("Session: {}", url) + + pool.execute { + Server.openBrowser(url) + } + } + } diff --git a/tools/demobench/src/main/kotlin/net/corda/demobench/model/Explorer.kt b/tools/demobench/src/main/kotlin/net/corda/demobench/model/Explorer.kt index 82f794fe2c..507fc33877 100644 --- a/tools/demobench/src/main/kotlin/net/corda/demobench/model/Explorer.kt +++ b/tools/demobench/src/main/kotlin/net/corda/demobench/model/Explorer.kt @@ -13,12 +13,12 @@ class Explorer(val explorerController: ExplorerController) : AutoCloseable { fun open(config: NodeConfig, onExit: (c: NodeConfig) -> Unit) { val p = explorerController.execute( "--host=localhost", - "--port=%d".format(config.artemisPort), - "--username=%s".format(config.user["user"]), - "--password=%s".format(config.user["password"]), - "--certificatesDir=%s".format(config.ssl.certificatesDirectory), - "--keyStorePassword=%s".format(config.ssl.keyStorePassword), - "--trustStorePassword=%s".format(config.ssl.trustStorePassword) + "--port=${config.artemisPort}", + "--username=${config.user["user"]}", + "--password=${config.user["password"]}", + "--certificatesDir=${config.ssl.certificatesDirectory}", + "--keyStorePassword=${config.ssl.keyStorePassword}", + "--trustStorePassword=${config.ssl.trustStorePassword}" ) process = p diff --git a/tools/demobench/src/main/kotlin/net/corda/demobench/model/ExplorerController.kt b/tools/demobench/src/main/kotlin/net/corda/demobench/model/ExplorerController.kt index f3cc6efcce..d1c1a38b41 100644 --- a/tools/demobench/src/main/kotlin/net/corda/demobench/model/ExplorerController.kt +++ b/tools/demobench/src/main/kotlin/net/corda/demobench/model/ExplorerController.kt @@ -6,9 +6,12 @@ import java.nio.file.Paths class ExplorerController : Controller() { private val jvm by inject() - private val explorerPath = Paths.get("explorer", "node-explorer.jar").toAbsolutePath() + init { + log.info("Explorer JAR: " + explorerPath) + } + internal fun execute(vararg args: String): Process { return jvm.execute(explorerPath, *args) } @@ -17,7 +20,4 @@ class ExplorerController : Controller() { return Explorer(this) } - init { - log.info("Explorer JAR: " + explorerPath) - } } \ No newline at end of file diff --git a/tools/demobench/src/main/kotlin/net/corda/demobench/model/JVMConfig.kt b/tools/demobench/src/main/kotlin/net/corda/demobench/model/JVMConfig.kt index 7b3ab8086a..36242ea7df 100644 --- a/tools/demobench/src/main/kotlin/net/corda/demobench/model/JVMConfig.kt +++ b/tools/demobench/src/main/kotlin/net/corda/demobench/model/JVMConfig.kt @@ -8,20 +8,19 @@ import tornadofx.Controller class JVMConfig : Controller() { private val javaExe = if (UIUtil.isWindows) "java.exe" else "java" - private val runtime: Runtime = Runtime.getRuntime() val javaPath: Path = Paths.get(System.getProperty("java.home"), "bin", javaExe) + init { + log.info("Java executable: " + javaPath) + } + fun commandFor(jarPath: Path, vararg args: String): Array { return arrayOf(javaPath.toString(), "-jar", jarPath.toString(), *args) } fun execute(jarPath: Path, vararg args: String): Process { - return runtime.exec(commandFor(jarPath, *args)) - } - - init { - log.info("Java executable: " + javaPath) + return Runtime.getRuntime().exec(commandFor(jarPath, *args)) } } \ No newline at end of file diff --git a/tools/demobench/src/main/kotlin/net/corda/demobench/model/NetworkMapConfig.kt b/tools/demobench/src/main/kotlin/net/corda/demobench/model/NetworkMapConfig.kt index f91e35177e..326b9fd60c 100644 --- a/tools/demobench/src/main/kotlin/net/corda/demobench/model/NetworkMapConfig.kt +++ b/tools/demobench/src/main/kotlin/net/corda/demobench/model/NetworkMapConfig.kt @@ -3,13 +3,10 @@ package net.corda.demobench.model open class NetworkMapConfig(val legalName: String, val artemisPort: Int) { private var keyValue = toKey(legalName) - val key : String - get() { return keyValue } + val key: String get() = keyValue } private val WHITESPACE = "\\s++".toRegex() -fun toKey(value: String): String { - return value.replace(WHITESPACE, "").toLowerCase() -} +fun toKey(value: String) = value.replace(WHITESPACE, "").toLowerCase() diff --git a/tools/demobench/src/main/kotlin/net/corda/demobench/model/NodeConfig.kt b/tools/demobench/src/main/kotlin/net/corda/demobench/model/NodeConfig.kt index fbf8e4e660..67780d64cd 100644 --- a/tools/demobench/src/main/kotlin/net/corda/demobench/model/NodeConfig.kt +++ b/tools/demobench/src/main/kotlin/net/corda/demobench/model/NodeConfig.kt @@ -35,7 +35,7 @@ class NodeConfig( override val keyStorePassword: String = "cordacadevpass" } - val toFileConfig : Config + val toFileConfig: Config get() = ConfigFactory.empty() .withValue("myLegalName", valueFor(legalName)) .withValue("artemisAddress", addressValueFor(artemisPort)) @@ -50,36 +50,27 @@ class NodeConfig( .withValue("h2port", valueFor(h2Port)) .withValue("useTestClock", valueFor(true)) - val isCashIssuer : Boolean - get() { - extraServices.forEach { - if (it.startsWith("corda.issuer.")) { - return true + val isCashIssuer: Boolean + get() = extraServices.any { + it.startsWith("corda.issuer.") } - } - return false - } init { userMap = mapOf( - Pair("password", "letmein"), - Pair("user", "guest"), - Pair("permissions", listOf( + "password" to "letmein", + "user" to "guest", + "permissions" to listOf( "StartFlow.net.corda.flows.CashFlow", "StartFlow.net.corda.flows.IssuerFlow\$IssuanceRequester" - )) + ) ) } } -private fun valueFor(any: T): ConfigValue? { - return ConfigValueFactory.fromAnyRef(any) -} +private fun valueFor(any: T): ConfigValue? = ConfigValueFactory.fromAnyRef(any) -private fun addressValueFor(port: Int): ConfigValue? { - return valueFor("localhost:%d".format(port)) -} +private fun addressValueFor(port: Int) = valueFor("localhost:$port") private fun optional(path: String, obj: T?, body: (c: Config, o: T) -> Config): Config { val config = ConfigFactory.empty() diff --git a/tools/demobench/src/main/kotlin/net/corda/demobench/model/NodeController.kt b/tools/demobench/src/main/kotlin/net/corda/demobench/model/NodeController.kt index ae63e9a318..6db639e86d 100644 --- a/tools/demobench/src/main/kotlin/net/corda/demobench/model/NodeController.kt +++ b/tools/demobench/src/main/kotlin/net/corda/demobench/model/NodeController.kt @@ -11,7 +11,9 @@ import net.corda.demobench.pty.R3Pty import tornadofx.Controller class NodeController : Controller() { - private val FIRST_PORT = 10000 + companion object Data { + private const val FIRST_PORT = 10000 + } private val baseDir = Paths.get("work", localDir).toAbsolutePath() private val pluginDir = Paths.get("plugins").toAbsolutePath() @@ -29,6 +31,11 @@ class NodeController : Controller() { private var networkMapConfig: NetworkMapConfig? = null + init { + log.info("Base directory: " + baseDir) + log.info("Corda JAR: " + cordaPath) + } + fun validate(nodeData: NodeData): NodeConfig? { val config = NodeConfig( baseDir, @@ -51,16 +58,11 @@ class NodeController : Controller() { return config } - val nextPort: Int - get() { return port.andIncrement } + val nextPort: Int get() = port.andIncrement - fun keyExists(key: String): Boolean { - return nodes.keys.contains(key) - } + fun keyExists(key: String) = nodes.keys.contains(key) - fun nameExists(name: String): Boolean { - return keyExists(toKey(name)) - } + fun nameExists(name: String) = keyExists(toKey(name)) fun chooseNetworkMap(config: NodeConfig) { if (networkMapConfig != null) { @@ -104,8 +106,4 @@ class NodeController : Controller() { get() = SimpleDateFormat("yyyyMMddHHmmss") .format(Date(ManagementFactory.getRuntimeMXBean().startTime)) - init { - log.info("Base directory: " + baseDir) - log.info("Corda JAR: " + cordaPath) - } } diff --git a/tools/demobench/src/main/kotlin/net/corda/demobench/model/ServiceController.kt b/tools/demobench/src/main/kotlin/net/corda/demobench/model/ServiceController.kt index 5cf0c61fc7..917ca7fbdc 100644 --- a/tools/demobench/src/main/kotlin/net/corda/demobench/model/ServiceController.kt +++ b/tools/demobench/src/main/kotlin/net/corda/demobench/model/ServiceController.kt @@ -12,17 +12,6 @@ class ServiceController : Controller() { val services : List get() = serviceSet - private fun loadConf(url: URL): List { - val set = TreeSet() - InputStreamReader(url.openStream()).readLines().forEach { - val service = it.trim() - set.add(service) - - log.info("Supports: " + service) - } - return set.toList() - } - init { /* * Load our list of known extra Corda services. @@ -34,4 +23,18 @@ class ServiceController : Controller() { loadConf(serviceConf) } } + + private fun loadConf(url: URL): List { + val set = TreeSet() + InputStreamReader(url.openStream()).useLines { + sq -> sq.forEach { + val service = it.trim() + set.add(service) + + log.info("Supports: " + service) + } + } + return set.toList() + } + } \ No newline at end of file diff --git a/tools/demobench/src/main/kotlin/net/corda/demobench/rpc/NodeRPC.kt b/tools/demobench/src/main/kotlin/net/corda/demobench/rpc/NodeRPC.kt index c4e99f9463..483ed001bb 100644 --- a/tools/demobench/src/main/kotlin/net/corda/demobench/rpc/NodeRPC.kt +++ b/tools/demobench/src/main/kotlin/net/corda/demobench/rpc/NodeRPC.kt @@ -10,16 +10,14 @@ import org.slf4j.LoggerFactory class NodeRPC(config: NodeConfig, invoke: (ops: CordaRPCOps) -> Unit): AutoCloseable { private val log = LoggerFactory.getLogger(NodeRPC::class.java) - private val ONE_SECOND = SECONDS.toMillis(1) + + companion object Data { + private val ONE_SECOND = SECONDS.toMillis(1) + } private val rpcClient = CordaRPCClient(HostAndPort.fromParts("localhost", config.artemisPort), config.ssl) private val timer = Timer() - override fun close() { - timer.cancel() - rpcClient.close() - } - init { val setupTask = object : TimerTask() { override fun run() { @@ -46,4 +44,10 @@ class NodeRPC(config: NodeConfig, invoke: (ops: CordaRPCOps) -> Unit): AutoClose // Wait 5 seconds for the node to start, and then poll once per second. timer.schedule(setupTask, 5 * ONE_SECOND, ONE_SECOND) } + + override fun close() { + timer.cancel() + rpcClient.close() + } + } diff --git a/tools/demobench/src/main/kotlin/net/corda/demobench/views/NodeTabView.kt b/tools/demobench/src/main/kotlin/net/corda/demobench/views/NodeTabView.kt index f18faeecbd..6f6b50a966 100644 --- a/tools/demobench/src/main/kotlin/net/corda/demobench/views/NodeTabView.kt +++ b/tools/demobench/src/main/kotlin/net/corda/demobench/views/NodeTabView.kt @@ -14,8 +14,10 @@ class NodeTabView : Fragment() { private val main by inject() - private val INTEGER_FORMAT = DecimalFormat() - private val NOT_NUMBER = "[^\\d]".toRegex() + private companion object Data { + val INTEGER_FORMAT = DecimalFormat() + val NOT_NUMBER = "[^\\d]".toRegex() + } private val model = NodeDataModel() private val nodeController by inject() diff --git a/tools/demobench/src/main/kotlin/net/corda/demobench/views/NodeTerminalView.kt b/tools/demobench/src/main/kotlin/net/corda/demobench/views/NodeTerminalView.kt index e8f2d51da9..1abddd1e53 100644 --- a/tools/demobench/src/main/kotlin/net/corda/demobench/views/NodeTerminalView.kt +++ b/tools/demobench/src/main/kotlin/net/corda/demobench/views/NodeTerminalView.kt @@ -38,6 +38,10 @@ class NodeTerminalView : Fragment() { private var rpc: NodeRPC? = null private var pty: R3Pty? = null + init { + root.vgrow = Priority.ALWAYS + } + fun open(config: NodeConfig) { nodeName.text = config.legalName p2pPort.value = config.artemisPort.toString() @@ -75,27 +79,32 @@ class NodeTerminalView : Fragment() { }) } - rpc = NodeRPC(config, { ops -> - try { - val verifiedTx = ops.verifiedTransactions() - val statesInVault = ops.vaultAndUpdates() - val cashBalances = ops.getCashBalances().entries.joinToString( - separator = ", ", - transform = { e -> e.value.toString() } - ) - - Platform.runLater { - states.value = statesInVault.first.size.toString() - transactions.value = verifiedTx.first.size.toString() - balance.value = cashBalances - } - } catch (e: Exception) { - log.warning("RPC failed: " + e) - } - }) + /* + * Start RPC client that will update node statistics on UI. + */ + rpc = launchRPC(config) }) } + fun launchRPC(config: NodeConfig) = NodeRPC(config, { ops -> + try { + val verifiedTx = ops.verifiedTransactions() + val statesInVault = ops.vaultAndUpdates() + val cashBalances = ops.getCashBalances().entries.joinToString( + separator = ", ", + transform = { e -> e.value.toString() } + ) + + Platform.runLater { + states.value = statesInVault.first.size.toString() + transactions.value = verifiedTx.first.size.toString() + balance.value = cashBalances + } + } catch (e: Exception) { + log.warning("RPC failed: " + e) + } + }) + fun close() { explorer.close() viewer.close() @@ -107,10 +116,6 @@ class NodeTerminalView : Fragment() { // TODO - Force a repaint somehow? My naive attempts have not worked. } - init { - root.vgrow = Priority.ALWAYS - } - class TerminalSettingsProvider : DefaultSettingsProvider() { override fun getDefaultStyle(): TextStyle { return TextStyle(TerminalColor.WHITE, TerminalColor.BLACK) diff --git a/tools/demobench/src/main/kotlin/org/h2/server/web/LocalWebServer.kt b/tools/demobench/src/main/kotlin/org/h2/server/web/LocalWebServer.kt index 1afba47d61..dade325775 100644 --- a/tools/demobench/src/main/kotlin/org/h2/server/web/LocalWebServer.kt +++ b/tools/demobench/src/main/kotlin/org/h2/server/web/LocalWebServer.kt @@ -2,9 +2,6 @@ package org.h2.server.web import java.sql.Connection -/** - * - */ class LocalWebServer : WebServer() { /**