Skip to content

Commit

Permalink
Make RPC scaffolding available for Node
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy Lillehagen committed Feb 12, 2018
1 parent c250392 commit cca71e3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
22 changes: 22 additions & 0 deletions experimental/behave/src/main/kotlin/net/corda/behave/node/Node.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit cca71e3

Please sign in to comment.