First batch of code review changes.

This commit is contained in:
Chris Rankin 2017-02-06 14:54:48 +00:00
parent 297a7e6208
commit 62d911a478
12 changed files with 115 additions and 119 deletions

View File

@ -15,28 +15,6 @@ class DBViewer : AutoCloseable {
private val pool = Executors.newCachedThreadPool() private val pool = Executors.newCachedThreadPool()
private val t = Thread("DBViewer") 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 { init {
val ws = LocalWebServer() val ws = LocalWebServer()
webServer = Server(ws, "-webPort", "0") 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)
}
}
} }

View File

@ -13,12 +13,12 @@ class Explorer(val explorerController: ExplorerController) : AutoCloseable {
fun open(config: NodeConfig, onExit: (c: NodeConfig) -> Unit) { fun open(config: NodeConfig, onExit: (c: NodeConfig) -> Unit) {
val p = explorerController.execute( val p = explorerController.execute(
"--host=localhost", "--host=localhost",
"--port=%d".format(config.artemisPort), "--port=${config.artemisPort}",
"--username=%s".format(config.user["user"]), "--username=${config.user["user"]}",
"--password=%s".format(config.user["password"]), "--password=${config.user["password"]}",
"--certificatesDir=%s".format(config.ssl.certificatesDirectory), "--certificatesDir=${config.ssl.certificatesDirectory}",
"--keyStorePassword=%s".format(config.ssl.keyStorePassword), "--keyStorePassword=${config.ssl.keyStorePassword}",
"--trustStorePassword=%s".format(config.ssl.trustStorePassword) "--trustStorePassword=${config.ssl.trustStorePassword}"
) )
process = p process = p

View File

@ -6,9 +6,12 @@ import java.nio.file.Paths
class ExplorerController : Controller() { class ExplorerController : Controller() {
private val jvm by inject<JVMConfig>() private val jvm by inject<JVMConfig>()
private val explorerPath = Paths.get("explorer", "node-explorer.jar").toAbsolutePath() private val explorerPath = Paths.get("explorer", "node-explorer.jar").toAbsolutePath()
init {
log.info("Explorer JAR: " + explorerPath)
}
internal fun execute(vararg args: String): Process { internal fun execute(vararg args: String): Process {
return jvm.execute(explorerPath, *args) return jvm.execute(explorerPath, *args)
} }
@ -17,7 +20,4 @@ class ExplorerController : Controller() {
return Explorer(this) return Explorer(this)
} }
init {
log.info("Explorer JAR: " + explorerPath)
}
} }

View File

@ -8,20 +8,19 @@ import tornadofx.Controller
class JVMConfig : Controller() { class JVMConfig : Controller() {
private val javaExe = if (UIUtil.isWindows) "java.exe" else "java" 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) 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<String> { fun commandFor(jarPath: Path, vararg args: String): Array<String> {
return arrayOf(javaPath.toString(), "-jar", jarPath.toString(), *args) return arrayOf(javaPath.toString(), "-jar", jarPath.toString(), *args)
} }
fun execute(jarPath: Path, vararg args: String): Process { fun execute(jarPath: Path, vararg args: String): Process {
return runtime.exec(commandFor(jarPath, *args)) return Runtime.getRuntime().exec(commandFor(jarPath, *args))
}
init {
log.info("Java executable: " + javaPath)
} }
} }

View File

@ -3,13 +3,10 @@ package net.corda.demobench.model
open class NetworkMapConfig(val legalName: String, val artemisPort: Int) { open class NetworkMapConfig(val legalName: String, val artemisPort: Int) {
private var keyValue = toKey(legalName) private var keyValue = toKey(legalName)
val key : String val key: String get() = keyValue
get() { return keyValue }
} }
private val WHITESPACE = "\\s++".toRegex() private val WHITESPACE = "\\s++".toRegex()
fun toKey(value: String): String { fun toKey(value: String) = value.replace(WHITESPACE, "").toLowerCase()
return value.replace(WHITESPACE, "").toLowerCase()
}

View File

@ -35,7 +35,7 @@ class NodeConfig(
override val keyStorePassword: String = "cordacadevpass" override val keyStorePassword: String = "cordacadevpass"
} }
val toFileConfig : Config val toFileConfig: Config
get() = ConfigFactory.empty() get() = ConfigFactory.empty()
.withValue("myLegalName", valueFor(legalName)) .withValue("myLegalName", valueFor(legalName))
.withValue("artemisAddress", addressValueFor(artemisPort)) .withValue("artemisAddress", addressValueFor(artemisPort))
@ -50,36 +50,27 @@ class NodeConfig(
.withValue("h2port", valueFor(h2Port)) .withValue("h2port", valueFor(h2Port))
.withValue("useTestClock", valueFor(true)) .withValue("useTestClock", valueFor(true))
val isCashIssuer : Boolean val isCashIssuer: Boolean
get() { get() = extraServices.any {
extraServices.forEach { it.startsWith("corda.issuer.")
if (it.startsWith("corda.issuer.")) {
return true
} }
}
return false
}
init { init {
userMap = mapOf<String, Any>( userMap = mapOf<String, Any>(
Pair<String, Any>("password", "letmein"), "password" to "letmein",
Pair<String, Any>("user", "guest"), "user" to "guest",
Pair<String, Any>("permissions", listOf( "permissions" to listOf(
"StartFlow.net.corda.flows.CashFlow", "StartFlow.net.corda.flows.CashFlow",
"StartFlow.net.corda.flows.IssuerFlow\$IssuanceRequester" "StartFlow.net.corda.flows.IssuerFlow\$IssuanceRequester"
)) )
) )
} }
} }
private fun <T> valueFor(any: T): ConfigValue? { private fun <T> valueFor(any: T): ConfigValue? = ConfigValueFactory.fromAnyRef(any)
return ConfigValueFactory.fromAnyRef(any)
}
private fun addressValueFor(port: Int): ConfigValue? { private fun addressValueFor(port: Int) = valueFor("localhost:$port")
return valueFor("localhost:%d".format(port))
}
private fun <T> optional(path: String, obj: T?, body: (c: Config, o: T) -> Config): Config { private fun <T> optional(path: String, obj: T?, body: (c: Config, o: T) -> Config): Config {
val config = ConfigFactory.empty() val config = ConfigFactory.empty()

View File

@ -11,7 +11,9 @@ import net.corda.demobench.pty.R3Pty
import tornadofx.Controller import tornadofx.Controller
class NodeController : 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 baseDir = Paths.get("work", localDir).toAbsolutePath()
private val pluginDir = Paths.get("plugins").toAbsolutePath() private val pluginDir = Paths.get("plugins").toAbsolutePath()
@ -29,6 +31,11 @@ class NodeController : Controller() {
private var networkMapConfig: NetworkMapConfig? = null private var networkMapConfig: NetworkMapConfig? = null
init {
log.info("Base directory: " + baseDir)
log.info("Corda JAR: " + cordaPath)
}
fun validate(nodeData: NodeData): NodeConfig? { fun validate(nodeData: NodeData): NodeConfig? {
val config = NodeConfig( val config = NodeConfig(
baseDir, baseDir,
@ -51,16 +58,11 @@ class NodeController : Controller() {
return config return config
} }
val nextPort: Int val nextPort: Int get() = port.andIncrement
get() { return port.andIncrement }
fun keyExists(key: String): Boolean { fun keyExists(key: String) = nodes.keys.contains(key)
return nodes.keys.contains(key)
}
fun nameExists(name: String): Boolean { fun nameExists(name: String) = keyExists(toKey(name))
return keyExists(toKey(name))
}
fun chooseNetworkMap(config: NodeConfig) { fun chooseNetworkMap(config: NodeConfig) {
if (networkMapConfig != null) { if (networkMapConfig != null) {
@ -104,8 +106,4 @@ class NodeController : Controller() {
get() = SimpleDateFormat("yyyyMMddHHmmss") get() = SimpleDateFormat("yyyyMMddHHmmss")
.format(Date(ManagementFactory.getRuntimeMXBean().startTime)) .format(Date(ManagementFactory.getRuntimeMXBean().startTime))
init {
log.info("Base directory: " + baseDir)
log.info("Corda JAR: " + cordaPath)
}
} }

View File

@ -12,17 +12,6 @@ class ServiceController : Controller() {
val services : List<String> val services : List<String>
get() = serviceSet get() = serviceSet
private fun loadConf(url: URL): List<String> {
val set = TreeSet<String>()
InputStreamReader(url.openStream()).readLines().forEach {
val service = it.trim()
set.add(service)
log.info("Supports: " + service)
}
return set.toList()
}
init { init {
/* /*
* Load our list of known extra Corda services. * Load our list of known extra Corda services.
@ -34,4 +23,18 @@ class ServiceController : Controller() {
loadConf(serviceConf) loadConf(serviceConf)
} }
} }
private fun loadConf(url: URL): List<String> {
val set = TreeSet<String>()
InputStreamReader(url.openStream()).useLines {
sq -> sq.forEach {
val service = it.trim()
set.add(service)
log.info("Supports: " + service)
}
}
return set.toList()
}
} }

View File

@ -10,16 +10,14 @@ import org.slf4j.LoggerFactory
class NodeRPC(config: NodeConfig, invoke: (ops: CordaRPCOps) -> Unit): AutoCloseable { class NodeRPC(config: NodeConfig, invoke: (ops: CordaRPCOps) -> Unit): AutoCloseable {
private val log = LoggerFactory.getLogger(NodeRPC::class.java) 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 rpcClient = CordaRPCClient(HostAndPort.fromParts("localhost", config.artemisPort), config.ssl)
private val timer = Timer() private val timer = Timer()
override fun close() {
timer.cancel()
rpcClient.close()
}
init { init {
val setupTask = object : TimerTask() { val setupTask = object : TimerTask() {
override fun run() { 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. // Wait 5 seconds for the node to start, and then poll once per second.
timer.schedule(setupTask, 5 * ONE_SECOND, ONE_SECOND) timer.schedule(setupTask, 5 * ONE_SECOND, ONE_SECOND)
} }
override fun close() {
timer.cancel()
rpcClient.close()
}
} }

View File

@ -14,8 +14,10 @@ class NodeTabView : Fragment() {
private val main by inject<DemoBenchView>() private val main by inject<DemoBenchView>()
private val INTEGER_FORMAT = DecimalFormat() private companion object Data {
private val NOT_NUMBER = "[^\\d]".toRegex() val INTEGER_FORMAT = DecimalFormat()
val NOT_NUMBER = "[^\\d]".toRegex()
}
private val model = NodeDataModel() private val model = NodeDataModel()
private val nodeController by inject<NodeController>() private val nodeController by inject<NodeController>()

View File

@ -38,6 +38,10 @@ class NodeTerminalView : Fragment() {
private var rpc: NodeRPC? = null private var rpc: NodeRPC? = null
private var pty: R3Pty? = null private var pty: R3Pty? = null
init {
root.vgrow = Priority.ALWAYS
}
fun open(config: NodeConfig) { fun open(config: NodeConfig) {
nodeName.text = config.legalName nodeName.text = config.legalName
p2pPort.value = config.artemisPort.toString() p2pPort.value = config.artemisPort.toString()
@ -75,27 +79,32 @@ class NodeTerminalView : Fragment() {
}) })
} }
rpc = NodeRPC(config, { ops -> /*
try { * Start RPC client that will update node statistics on UI.
val verifiedTx = ops.verifiedTransactions() */
val statesInVault = ops.vaultAndUpdates() rpc = launchRPC(config)
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 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() { fun close() {
explorer.close() explorer.close()
viewer.close() viewer.close()
@ -107,10 +116,6 @@ class NodeTerminalView : Fragment() {
// TODO - Force a repaint somehow? My naive attempts have not worked. // TODO - Force a repaint somehow? My naive attempts have not worked.
} }
init {
root.vgrow = Priority.ALWAYS
}
class TerminalSettingsProvider : DefaultSettingsProvider() { class TerminalSettingsProvider : DefaultSettingsProvider() {
override fun getDefaultStyle(): TextStyle { override fun getDefaultStyle(): TextStyle {
return TextStyle(TerminalColor.WHITE, TerminalColor.BLACK) return TextStyle(TerminalColor.WHITE, TerminalColor.BLACK)

View File

@ -2,9 +2,6 @@ package org.h2.server.web
import java.sql.Connection import java.sql.Connection
/**
*
*/
class LocalWebServer : WebServer() { class LocalWebServer : WebServer() {
/** /**