diff --git a/node/src/main/kotlin/com/r3corda/node/internal/AbstractNode.kt b/node/src/main/kotlin/com/r3corda/node/internal/AbstractNode.kt index bc6044beb3..7d5b00a219 100644 --- a/node/src/main/kotlin/com/r3corda/node/internal/AbstractNode.kt +++ b/node/src/main/kotlin/com/r3corda/node/internal/AbstractNode.kt @@ -163,6 +163,14 @@ abstract class AbstractNode(val dir: Path, val configuration: NodeConfiguration, return this } + /** + * Run any tasks that are needed to ensure the node is in a correct state before running start() + */ + open fun setup(): AbstractNode { + createNodeDir() + return this + } + private fun buildAdvertisedServices() { val serviceTypes = info.advertisedServices if (NetworkMapService.Type in serviceTypes) makeNetworkMapService() @@ -327,4 +335,10 @@ abstract class AbstractNode(val dir: Path, val configuration: NodeConfiguration, } return NodeAttachmentService(attachmentsDir, services.monitoringService.metrics) } + + protected fun createNodeDir() { + if (!Files.exists(dir)) { + Files.createDirectories(dir) + } + } } diff --git a/node/src/main/kotlin/com/r3corda/node/internal/Node.kt b/node/src/main/kotlin/com/r3corda/node/internal/Node.kt index 7ce76b0e18..6c3ddbdda4 100644 --- a/node/src/main/kotlin/com/r3corda/node/internal/Node.kt +++ b/node/src/main/kotlin/com/r3corda/node/internal/Node.kt @@ -26,6 +26,7 @@ import org.glassfish.jersey.servlet.ServletContainer import java.io.RandomAccessFile import java.lang.management.ManagementFactory import java.nio.channels.FileLock +import java.nio.file.Files import java.nio.file.Path import java.time.Clock import javax.management.ObjectName @@ -152,6 +153,11 @@ class Node(dir: Path, val p2pAddr: HostAndPort, configuration: NodeConfiguration return this } + override fun setup(): Node { + super.setup() + return this + } + override fun stop() { webServer.stop() super.stop() @@ -177,6 +183,7 @@ class Node(dir: Path, val p2pAddr: HostAndPort, configuration: NodeConfiguration println("Shut that other node down and try again. It may have process ID ${file.readText()}") System.exit(1) } + nodeFileLock = l val ourProcessID: String = ManagementFactory.getRuntimeMXBean().name.split("@")[0] f.setLength(0) diff --git a/node/src/main/kotlin/com/r3corda/node/internal/testing/MockNode.kt b/node/src/main/kotlin/com/r3corda/node/internal/testing/MockNode.kt index de4ba90666..3ca7678788 100644 --- a/node/src/main/kotlin/com/r3corda/node/internal/testing/MockNode.kt +++ b/node/src/main/kotlin/com/r3corda/node/internal/testing/MockNode.kt @@ -122,7 +122,7 @@ class MockNetwork(private val networkSendManuallyPumped: Boolean = false, override val nearestCity: String = "Atlantis" } val node = nodeFactory.create(path, config, this, networkMapAddress, advertisedServices.toSet(), id, keyPair) - if (start) node.start() + if (start) node.setup().start() _nodes.add(node) return node } diff --git a/src/main/kotlin/com/r3corda/demos/IRSDemo.kt b/src/main/kotlin/com/r3corda/demos/IRSDemo.kt index abecd82aef..e1d270b6bb 100644 --- a/src/main/kotlin/com/r3corda/demos/IRSDemo.kt +++ b/src/main/kotlin/com/r3corda/demos/IRSDemo.kt @@ -401,7 +401,7 @@ private fun startNode(params : NodeParams) : Node { val node = logElapsedTime("Node startup") { Node(params.dir, myNetAddr, config, networkMapId, advertisedServices, DemoClock(), - listOf(InterestRateSwapAPI::class.java)).start() } + listOf(InterestRateSwapAPI::class.java)).setup().start() } // TODO: This should all be replaced by the identity service being updated // as the network map changes. diff --git a/src/main/kotlin/com/r3corda/demos/RateFixDemo.kt b/src/main/kotlin/com/r3corda/demos/RateFixDemo.kt index e8d981e047..ee5f7fa4de 100644 --- a/src/main/kotlin/com/r3corda/demos/RateFixDemo.kt +++ b/src/main/kotlin/com/r3corda/demos/RateFixDemo.kt @@ -48,12 +48,7 @@ fun main(args: Array) { // Suppress the Artemis MQ noise, and activate the demo logging. BriefLogFormatter.initVerbose("+demo.ratefix", "-org.apache.activemq") - // TODO: Move this into the AbstractNode class. val dir = Paths.get(options.valueOf(dirArg)) - if (!Files.exists(dir)) { - Files.createDirectory(dir) - } - val networkMapAddr = ArtemisMessagingService.makeRecipient(options.valueOf(networkMapAddrArg)) val networkMapIdentity = Files.readAllBytes(Paths.get(options.valueOf(networkMapIdentityArg))).deserialize() val networkMapAddress = NodeInfo(networkMapAddr, networkMapIdentity) @@ -78,7 +73,7 @@ fun main(args: Array) { val node = logElapsedTime("Node startup") { Node(dir, myNetAddr, config, networkMapAddress, advertisedServices, DemoClock(), - listOf(InterestRateSwapAPI::class.java)).start() } + listOf(InterestRateSwapAPI::class.java)).setup().start() } val notary = node.services.networkMapCache.notaryNodes[0] diff --git a/src/main/kotlin/com/r3corda/demos/TraderDemo.kt b/src/main/kotlin/com/r3corda/demos/TraderDemo.kt index 20fb0d73fe..dbac9df7e2 100644 --- a/src/main/kotlin/com/r3corda/demos/TraderDemo.kt +++ b/src/main/kotlin/com/r3corda/demos/TraderDemo.kt @@ -95,7 +95,7 @@ fun main(args: Array) { // for protocols will change in future. BriefLogFormatter.initVerbose("+demo.buyer", "+demo.seller", "-org.apache.activemq") - val directory = setupDirectory(role) + val directory = Paths.get(DIRNAME, role.name.toLowerCase()) // Override the default config file (which you can find in the file "reference.conf") to give each node a name. val config = run { @@ -131,7 +131,7 @@ fun main(args: Array) { // And now construct then start the node object. It takes a little while. val node = logElapsedTime("Node startup") { - Node(directory, myNetAddr, config, networkMapId, advertisedServices).start() + Node(directory, myNetAddr, config, networkMapId, advertisedServices).setup().start() } // TODO: Replace with a separate trusted cash issuer @@ -149,12 +149,6 @@ fun main(args: Array) { } } -fun setupDirectory(mode: Role): Path { - val directory = Paths.get(DIRNAME, mode.name.toLowerCase()) - Files.createDirectories(directory) - return directory -} - fun parseOptions(args: Array, parser: OptionParser): OptionSet { try { return parser.parse(*args)