Make RPC scaffolding available for Node

This commit is contained in:
Tommy Lillehagen 2018-02-12 10:18:18 +00:00
parent c2503921ad
commit cca71e3d6e
2 changed files with 27 additions and 21 deletions

View File

@ -9,10 +9,15 @@ import net.corda.behave.logging.getLogger
import net.corda.behave.monitoring.PatternWatch
import net.corda.behave.node.configuration.*
import net.corda.behave.process.JarCommand
import net.corda.behave.seconds
import net.corda.behave.service.Service
import net.corda.behave.service.ServiceSettings
import net.corda.behave.ssh.MonitoringSSHClient
import net.corda.behave.ssh.SSHClient
import net.corda.client.rpc.CordaRPCClient
import net.corda.client.rpc.CordaRPCClientConfiguration
import net.corda.core.messaging.CordaRPCOps
import net.corda.core.utilities.NetworkHostAndPort
import org.apache.commons.io.FileUtils
import java.io.File
import java.time.Duration
@ -146,6 +151,23 @@ class Node(
}).start()
}
fun <T> rpc(action: (CordaRPCOps) -> T): T {
var result: T? = null
val user = config.users.first()
val address = config.nodeInterface
val targetHost = NetworkHostAndPort(address.host, address.rpcPort)
val config = CordaRPCClientConfiguration(
connectionMaxRetryInterval = 10.seconds
)
log.info("Establishing RPC connection to ${targetHost.host} on port ${targetHost.port} ...")
CordaRPCClient(targetHost, config).use(user.username, user.password) {
log.info("RPC connection to ${targetHost.host}:${targetHost.port} established")
val client = it.proxy
result = action(client)
}
return result ?: error("Failed to run RPC action")
}
override fun toString(): String {
return "Node(name = ${config.name}, version = ${config.distribution.version})"
}

View File

@ -3,11 +3,7 @@ package net.corda.behave.scenarios
import net.corda.behave.logging.getLogger
import net.corda.behave.network.Network
import net.corda.behave.node.Node
import net.corda.behave.seconds
import net.corda.client.rpc.CordaRPCClient
import net.corda.client.rpc.CordaRPCClientConfiguration
import net.corda.core.messaging.CordaRPCOps
import net.corda.core.utilities.NetworkHostAndPort
import org.assertj.core.api.Assertions.assertThat
class ScenarioState {
@ -54,29 +50,17 @@ class ScenarioState {
assertThat(network?.waitUntilRunning()).isTrue()
}
fun withNetwork(action: ScenarioState.() -> Unit) {
inline fun <T> withNetwork(action: ScenarioState.() -> T): T {
ensureNetworkIsRunning()
action()
return action()
}
fun <T> withClient(nodeName: String, action: (CordaRPCOps) -> T): T {
var result: T? = null
inline fun <T> withClient(nodeName: String, crossinline action: (CordaRPCOps) -> T): T {
withNetwork {
val node = node(nodeName)
val user = node.config.users.first()
val address = node.config.nodeInterface
val targetHost = NetworkHostAndPort(address.host, address.rpcPort)
val config = CordaRPCClientConfiguration(
connectionMaxRetryInterval = 10.seconds
)
log.info("Establishing RPC connection to ${targetHost.host} on port ${targetHost.port} ...")
CordaRPCClient(targetHost, config).use(user.username, user.password) {
log.info("RPC connection to ${targetHost.host}:${targetHost.port} established")
val client = it.proxy
result = action(client)
return node(nodeName).rpc {
action(it)
}
}
return result ?: error("Failed to run RPC action")
}
fun stopNetwork() {