node: Load config from disk in Driver, further cleanup

This commit is contained in:
Andras Slemmer 2016-08-02 10:23:27 +01:00
parent 303858c3e4
commit 773d53b4c8
4 changed files with 65 additions and 38 deletions

View File

@ -7,10 +7,15 @@ import com.r3corda.core.node.NodeInfo
import com.r3corda.core.node.services.ServiceType
import com.r3corda.node.services.config.NodeConfiguration
import com.r3corda.node.services.messaging.ArtemisMessagingClient
import com.r3corda.node.services.config.NodeConfigurationFromConfig
import com.r3corda.node.services.config.copy
import com.r3corda.node.services.network.InMemoryNetworkMapCache
import com.r3corda.node.services.network.NetworkMapService
import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigParseOptions
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File
import java.net.ServerSocket
import java.net.Socket
import java.net.SocketException
@ -66,6 +71,7 @@ private val log: Logger = LoggerFactory.getLogger("Driver")
*/
fun <A> driver(
baseDirectory: String = "build/${getTimestampAsDirectoryName()}",
nodeConfigurationPath: String = "reference.conf",
quasarJarPath: String = "lib/quasar.jar",
portAllocation: PortAllocation = PortAllocation.Incremental(10000),
debugPortAllocation: PortAllocation = PortAllocation.Incremental(5005),
@ -75,6 +81,7 @@ fun <A> driver(
portAllocation = portAllocation,
debugPortAllocation = debugPortAllocation,
baseDirectory = baseDirectory,
nodeConfigurationPath = nodeConfigurationPath,
quasarJarPath = quasarJarPath
)
driverDsl.start()
@ -125,6 +132,7 @@ class DriverDSL(
private val portAllocation: PortAllocation,
private val debugPortAllocation: PortAllocation,
val baseDirectory: String,
val nodeConfigurationPath: String,
val quasarJarPath: String
) : DriverDSLInterface {
@ -134,15 +142,19 @@ class DriverDSL(
private lateinit var networkMapNodeInfo: NodeInfo
private val registeredProcesses = LinkedList<Process>()
val nodeConfiguration =
NodeConfigurationFromConfig(
ConfigFactory.parseResources(
nodeConfigurationPath,
ConfigParseOptions.defaults().setAllowMissing(false)
)
).copy(
myLegalName = "driver-artemis"
)
val messagingService = ArtemisMessagingClient(
Paths.get(baseDirectory, "driver-artemis"),
object : NodeConfiguration {
override val myLegalName = "driver-artemis"
override val exportJMXto = ""
override val nearestCity = "Zion"
override val keyStorePassword = "keypass"
override val trustStorePassword = "trustpass"
},
nodeConfiguration,
serverHostPort = networkMapAddress,
myHostPort = portAllocation.nextHostAndPort()
)
@ -178,7 +190,6 @@ class DriverDSL(
val apiAddress = portAllocation.nextHostAndPort()
val debugPort = debugPortAllocation.nextPort()
val name = providedName ?: "${pickA(name)}-${messagingAddress.port}"
val nearestCity = pickA(city)
val driverCliParams = NodeRunner.CliParams(
services = advertisedServices,
@ -188,7 +199,7 @@ class DriverDSL(
messagingAddress = messagingAddress,
apiAddress = apiAddress,
baseDirectory = baseDirectory,
nearestCity = nearestCity,
nodeConfigurationPath = nodeConfigurationPath,
legalName = name
)
registerProcess(startNode(driverCliParams, quasarJarPath, debugPort))
@ -228,6 +239,8 @@ class DriverDSL(
}
}
private fun startNetworkMapService() {
val apiAddress = portAllocation.nextHostAndPort()
val debugPort = debugPortAllocation.nextPort()
@ -239,7 +252,7 @@ class DriverDSL(
messagingAddress = networkMapAddress,
apiAddress = apiAddress,
baseDirectory = baseDirectory,
nearestCity = pickA(city),
nodeConfigurationPath = nodeConfigurationPath,
legalName = networkMapName
)
log.info("Starting network-map-service")
@ -248,12 +261,6 @@ class DriverDSL(
companion object {
val city = arrayOf(
"London",
"Paris",
"New York",
"Tokyo"
)
val name = arrayOf(
"Alice",
"Bob",

View File

@ -7,9 +7,12 @@ import com.r3corda.core.crypto.toBase58String
import com.r3corda.core.node.NodeInfo
import com.r3corda.core.node.services.ServiceType
import com.r3corda.node.internal.Node
import com.r3corda.node.services.config.NodeConfiguration
import com.r3corda.node.services.messaging.ArtemisMessagingClient
import com.r3corda.node.services.config.NodeConfigurationFromConfig
import com.r3corda.node.services.config.copy
import com.r3corda.node.services.network.NetworkMapService
import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigParseOptions
import joptsimple.ArgumentAcceptingOptionSpec
import joptsimple.OptionParser
import joptsimple.OptionSet
@ -45,18 +48,21 @@ class NodeRunner {
} else {
null
}
val nodeConfiguration =
NodeConfigurationFromConfig(
ConfigFactory.parseResources(
nodeConfigurationPath,
ConfigParseOptions.defaults().setAllowMissing(false)
)
).copy(
myLegalName = legalName
)
val node = Node(
dir = nodeDirectory,
p2pAddr = messagingAddress,
webServerAddr = apiAddress,
configuration = object : NodeConfiguration {
override val myLegalName = legalName
override val exportJMXto = ""
override val nearestCity = cliParams.nearestCity
override val keyStorePassword = "keypass"
override val trustStorePassword = "trustpass"
},
configuration = nodeConfiguration,
networkMapAddress = networkMapNodeInfo,
advertisedServices = services.toSet()
)
@ -75,8 +81,8 @@ class NodeRunner {
val messagingAddress: HostAndPort,
val apiAddress: HostAndPort,
val baseDirectory: String,
val legalName: String,
val nearestCity: String
val nodeConfigurationPath: String,
val legalName: String
) {
companion object {
@ -95,8 +101,8 @@ class NodeRunner {
parser.accepts("api-address").withRequiredArg().ofType(String::class.java)
val baseDirectory =
parser.accepts("base-directory").withRequiredArg().ofType(String::class.java)
val nearestCity =
parser.accepts("nearest-city").withRequiredArg().ofType(String::class.java)
val nodeConfigurationPath =
parser.accepts("node-configuration-path").withRequiredArg().ofType(String::class.java)
val legalName =
parser.accepts("legal-name").withRequiredArg().ofType(String::class.java)
@ -114,7 +120,7 @@ class NodeRunner {
val messagingAddress = requiredArgument(optionSet, messagingAddress)
val apiAddress = requiredArgument(optionSet, apiAddress)
val baseDirectory = requiredArgument(optionSet, baseDirectory)
val nearestCity = requiredArgument(optionSet, nearestCity)
val nodeConfigurationPath = requiredArgument(optionSet, nodeConfigurationPath)
val legalName = requiredArgument(optionSet, legalName)
return CliParams(
@ -125,8 +131,8 @@ class NodeRunner {
networkMapName = networkMapName,
networkMapPublicKey = networkMapPublicKey,
networkMapAddress = networkMapAddress?.let { HostAndPort.fromString(it) },
legalName = legalName,
nearestCity = nearestCity
nodeConfigurationPath = nodeConfigurationPath,
legalName = legalName
)
}
}
@ -153,8 +159,8 @@ class NodeRunner {
cliArguments.add(apiAddress.toString())
cliArguments.add("--base-directory")
cliArguments.add(baseDirectory.toString())
cliArguments.add("--nearest-city")
cliArguments.add(nearestCity)
cliArguments.add("--node-configuration-path")
cliArguments.add(nodeConfigurationPath)
cliArguments.add("--legal-name")
cliArguments.add(legalName)
return cliArguments
@ -162,7 +168,5 @@ class NodeRunner {
}
}
fun createNodeRunDirectory(directory: Path) {
directory.toFile().mkdirs()
}
fun createNodeRunDirectory(directory: Path) = directory.toFile().mkdirs()

View File

@ -97,3 +97,19 @@ class FullNodeConfiguration(conf: Config) : NodeConfiguration {
)
}
}
fun NodeConfiguration.copy(
myLegalName: String = this.myLegalName,
exportJMXto: String = this.exportJMXto,
nearestCity: String = this.nearestCity,
keyStorePassword: String = this.keyStorePassword,
trustStorePassword: String = this.trustStorePassword
): NodeConfiguration {
return object : NodeConfiguration {
override val myLegalName: String = myLegalName
override val exportJMXto: String = exportJMXto
override val nearestCity: String = nearestCity
override val keyStorePassword: String = keyStorePassword
override val trustStorePassword: String = trustStorePassword
}
}

View File

@ -13,7 +13,7 @@ class DriverTests {
fun simpleNodeStartupShutdownWorks() {
// Start a notary
val (handle, notaryNodeInfo) = driver(quasarPath = "../lib/quasar.jar") {
val (handle, notaryNodeInfo) = driver(quasarJarPath = "../lib/quasar.jar") {
startNode(setOf(NotaryService.Type), "TestNotary")
}
// Check that the node is registered in the network map