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

View File

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

View File

@ -6,9 +6,12 @@ import java.nio.file.Paths
class ExplorerController : Controller() {
private val jvm by inject<JVMConfig>()
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)
}
}

View File

@ -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<String> {
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))
}
}

View File

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

View File

@ -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<String, Any>(
Pair<String, Any>("password", "letmein"),
Pair<String, Any>("user", "guest"),
Pair<String, Any>("permissions", listOf(
"password" to "letmein",
"user" to "guest",
"permissions" to listOf(
"StartFlow.net.corda.flows.CashFlow",
"StartFlow.net.corda.flows.IssuerFlow\$IssuanceRequester"
))
)
)
}
}
private fun <T> valueFor(any: T): ConfigValue? {
return ConfigValueFactory.fromAnyRef(any)
}
private fun <T> 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 <T> optional(path: String, obj: T?, body: (c: Config, o: T) -> Config): Config {
val config = ConfigFactory.empty()

View File

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

View File

@ -12,17 +12,6 @@ class ServiceController : Controller() {
val services : List<String>
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 {
/*
* Load our list of known extra Corda services.
@ -34,4 +23,18 @@ class ServiceController : Controller() {
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 {
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()
}
}

View File

@ -14,8 +14,10 @@ class NodeTabView : Fragment() {
private val main by inject<DemoBenchView>()
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<NodeController>()

View File

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

View File

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