mirror of
https://github.com/corda/corda.git
synced 2024-12-23 23:02:29 +00:00
node: Add apiAddress constructor parameter
This commit is contained in:
parent
429d8aab74
commit
e54dad9a8b
@ -25,6 +25,7 @@ import org.glassfish.jersey.server.ServerProperties
|
||||
import org.glassfish.jersey.servlet.ServletContainer
|
||||
import java.io.RandomAccessFile
|
||||
import java.lang.management.ManagementFactory
|
||||
import java.net.InetSocketAddress
|
||||
import java.nio.channels.FileLock
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
@ -52,7 +53,7 @@ class ConfigurationException(message: String) : Exception(message)
|
||||
* Listed clientAPI classes are assumed to have to take a single APIServer constructor parameter
|
||||
* @param clock The clock used within the node and by all protocols etc
|
||||
*/
|
||||
class Node(dir: Path, val p2pAddr: HostAndPort, configuration: NodeConfiguration,
|
||||
class Node(dir: Path, val p2pAddr: HostAndPort, val webServerAddr: HostAndPort, configuration: NodeConfiguration,
|
||||
networkMapAddress: NodeInfo?, advertisedServices: Set<ServiceType>,
|
||||
clock: Clock = NodeClock(),
|
||||
val clientAPIs: List<Class<*>> = listOf()) : AbstractNode(dir, configuration, networkMapAddress, advertisedServices, clock) {
|
||||
@ -71,9 +72,6 @@ class Node(dir: Path, val p2pAddr: HostAndPort, configuration: NodeConfiguration
|
||||
// when our process shuts down, but we try in stop() anyway just to be nice.
|
||||
private var nodeFileLock: FileLock? = null
|
||||
|
||||
// TODO: Move to node config file
|
||||
private var webServerPort: Int = p2pAddr.port + 1
|
||||
|
||||
override fun makeMessagingService(): MessagingService = ArtemisMessagingService(dir, p2pAddr, serverThread)
|
||||
|
||||
override fun startMessagingService() {
|
||||
@ -81,14 +79,9 @@ class Node(dir: Path, val p2pAddr: HostAndPort, configuration: NodeConfiguration
|
||||
(net as ArtemisMessagingService).start()
|
||||
}
|
||||
|
||||
fun setWebServerPort(port: Int): Node {
|
||||
webServerPort = port
|
||||
return this
|
||||
}
|
||||
|
||||
private fun initWebServer(): Server {
|
||||
// Note that the web server handlers will all run concurrently, and not on the node thread.
|
||||
val server = Server(webServerPort)
|
||||
val server = Server(InetSocketAddress(webServerAddr.hostText, webServerAddr.port))
|
||||
|
||||
val handlerCollection = HandlerCollection()
|
||||
|
||||
|
@ -52,7 +52,7 @@ private fun startNode(dir: Path,
|
||||
"--dir", dir.toString(),
|
||||
"--network-address", nodeAddr.toString(),
|
||||
"--network-map-address", networkMapAddr.toString(),
|
||||
"--api-address", apiAddr.port.toString())
|
||||
"--api-address", apiAddr.toString())
|
||||
val proc = spawn("com.r3corda.demos.IRSDemoKt", args, "IRSDemo$nodeType")
|
||||
ensureNodeStartsOrKill(proc, apiAddr)
|
||||
return proc
|
||||
|
@ -11,10 +11,11 @@ import kotlin.test.assertEquals
|
||||
class TraderDemoTest {
|
||||
@Test fun `runs trader demo`() {
|
||||
val buyerAddr = freeLocalHostAndPort()
|
||||
val buyerApiAddr = freeLocalHostAndPort()
|
||||
var nodeProc: Process? = null
|
||||
try {
|
||||
cleanupFiles()
|
||||
nodeProc = runBuyer(buyerAddr)
|
||||
nodeProc = runBuyer(buyerAddr, buyerApiAddr)
|
||||
runSeller(buyerAddr)
|
||||
} finally {
|
||||
nodeProc?.destroy()
|
||||
@ -23,9 +24,13 @@ class TraderDemoTest {
|
||||
}
|
||||
}
|
||||
|
||||
private fun runBuyer(buyerAddr: HostAndPort): Process {
|
||||
private fun runBuyer(buyerAddr: HostAndPort, buyerApiAddr: HostAndPort): Process {
|
||||
println("Running Buyer")
|
||||
val args = listOf("--role", "BUYER", "--network-address", buyerAddr.toString())
|
||||
val args = listOf(
|
||||
"--role", "BUYER",
|
||||
"--network-address", buyerAddr.toString(),
|
||||
"--api-address", buyerApiAddr.toString()
|
||||
)
|
||||
val proc = spawn("com.r3corda.demos.TraderDemoKt", args, "TradeDemoBuyer")
|
||||
ensureNodeStartsOrKill(proc, buyerAddr)
|
||||
return proc
|
||||
@ -34,10 +39,13 @@ private fun runBuyer(buyerAddr: HostAndPort): Process {
|
||||
private fun runSeller(buyerAddr: HostAndPort) {
|
||||
println("Running Seller")
|
||||
val sellerAddr = freeLocalHostAndPort()
|
||||
val sellerApiAddr = freeLocalHostAndPort()
|
||||
val args = listOf(
|
||||
"--role", "SELLER",
|
||||
"--network-address", sellerAddr.toString(),
|
||||
"--other-network-address", buyerAddr.toString())
|
||||
"--api-address", sellerApiAddr.toString(),
|
||||
"--other-network-address", buyerAddr.toString()
|
||||
)
|
||||
val proc = spawn("com.r3corda.demos.TraderDemoKt", args, "TradeDemoSeller")
|
||||
assertExitOrKill(proc);
|
||||
assertEquals(proc.exitValue(), 0)
|
||||
@ -48,4 +56,4 @@ private fun cleanupFiles() {
|
||||
val dir = Paths.get("trader-demo")
|
||||
println("Erasing " + dir)
|
||||
dir.toFile().deleteRecursively()
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import com.google.common.net.HostAndPort
|
||||
import com.typesafe.config.ConfigFactory
|
||||
import com.r3corda.core.crypto.Party
|
||||
import com.r3corda.core.logElapsedTime
|
||||
import com.r3corda.core.messaging.MessageRecipients
|
||||
import com.r3corda.core.messaging.SingleMessageRecipient
|
||||
import com.r3corda.node.internal.Node
|
||||
import com.r3corda.node.services.config.NodeConfiguration
|
||||
@ -12,7 +11,6 @@ import com.r3corda.node.services.config.NodeConfigurationFromConfig
|
||||
import com.r3corda.core.node.NodeInfo
|
||||
import com.r3corda.node.services.network.NetworkMapService
|
||||
import com.r3corda.node.services.clientapi.NodeInterestRates
|
||||
import com.r3corda.node.services.transactions.NotaryService
|
||||
import com.r3corda.core.node.services.ServiceType
|
||||
import com.r3corda.node.services.messaging.ArtemisMessagingService
|
||||
import com.r3corda.core.serialization.deserialize
|
||||
@ -23,12 +21,10 @@ import com.r3corda.demos.protocols.ExitServerProtocol
|
||||
import com.r3corda.demos.protocols.UpdateBusinessDayProtocol
|
||||
import com.r3corda.node.internal.AbstractNode
|
||||
import com.r3corda.node.internal.testing.MockNetwork
|
||||
import com.r3corda.node.services.network.InMemoryMessagingNetwork
|
||||
import com.r3corda.node.services.transactions.SimpleNotaryService
|
||||
import joptsimple.OptionParser
|
||||
import joptsimple.OptionSet
|
||||
import joptsimple.OptionSpec
|
||||
import joptsimple.OptionSpecBuilder
|
||||
import java.io.DataOutputStream
|
||||
import java.io.File
|
||||
import java.net.HttpURLConnection
|
||||
@ -63,7 +59,7 @@ private class NodeParams() {
|
||||
var dir : Path = Paths.get("")
|
||||
var address : HostAndPort = HostAndPort.fromString("localhost").withDefaultPort(Node.DEFAULT_PORT)
|
||||
var mapAddress: String = ""
|
||||
var apiPort : Int = Node.DEFAULT_PORT + 1
|
||||
var apiAddress: HostAndPort = HostAndPort.fromString("localhost").withDefaultPort(Node.DEFAULT_PORT + 1)
|
||||
var identityFile: Path = Paths.get("")
|
||||
var tradeWithAddrs: List<String> = listOf()
|
||||
var tradeWithIdentities: List<Path> = listOf()
|
||||
@ -74,7 +70,7 @@ private class NodeParams() {
|
||||
private class DemoArgs() {
|
||||
lateinit var roleArg: OptionSpec<IRSDemoRole>
|
||||
lateinit var networkAddressArg: OptionSpec<String>
|
||||
lateinit var apiPort: OptionSpec<Int>
|
||||
lateinit var apiAddressArg: OptionSpec<String>
|
||||
lateinit var dirArg: OptionSpec<String>
|
||||
lateinit var networkMapIdentityFile: OptionSpec<String>
|
||||
lateinit var networkMapNetAddr: OptionSpec<String>
|
||||
@ -181,7 +177,7 @@ private fun setupArgs(parser: OptionParser): DemoArgs {
|
||||
|
||||
args.roleArg = parser.accepts("role").withRequiredArg().ofType(IRSDemoRole::class.java).required()
|
||||
args.networkAddressArg = parser.accepts("network-address").withOptionalArg()
|
||||
args.apiPort = parser.accepts("api-address").withOptionalArg().ofType(Int::class.java)
|
||||
args.apiAddressArg = parser.accepts("api-address").withOptionalArg()
|
||||
args.dirArg = parser.accepts("directory").withOptionalArg()
|
||||
args.networkMapIdentityFile = parser.accepts("network-map-identity-file").withOptionalArg()
|
||||
args.networkMapNetAddr = parser.accepts("network-map-address").withRequiredArg().defaultsTo("localhost")
|
||||
@ -240,10 +236,8 @@ private fun configureNodeParams(role: IRSDemoRole, args: DemoArgs, options: Opti
|
||||
if (options.has(args.networkAddressArg)) {
|
||||
nodeParams.address = HostAndPort.fromString(options.valueOf(args.networkAddressArg)).withDefaultPort(Node.DEFAULT_PORT)
|
||||
}
|
||||
if (options.has(args.apiPort)) {
|
||||
nodeParams.apiPort = options.valueOf(args.apiPort)
|
||||
} else if (options.has(args.networkAddressArg)) {
|
||||
nodeParams.apiPort = nodeParams.address.port + 1
|
||||
if (options.has(args.apiAddressArg)) {
|
||||
nodeParams.apiAddress = HostAndPort.fromString(options.valueOf(args.apiAddressArg)).withDefaultPort(Node.DEFAULT_PORT + 1)
|
||||
}
|
||||
|
||||
nodeParams.identityFile = if (options.has(args.networkMapIdentityFile)) {
|
||||
@ -274,7 +268,7 @@ private fun runNode(nodeParams: NodeParams): Unit {
|
||||
ExitServerProtocol.Handler.register(node)
|
||||
|
||||
if (nodeParams.uploadRates) {
|
||||
runUploadRates(HostAndPort.fromString("localhost:${nodeParams.apiPort}"))
|
||||
runUploadRates(nodeParams.apiAddress)
|
||||
}
|
||||
|
||||
try {
|
||||
@ -301,9 +295,10 @@ private fun startNode(params : NodeParams, networkMap: SingleMessageRecipient, r
|
||||
nodeInfo(networkMap, params.identityFile, setOf(NetworkMapService.Type, SimpleNotaryService.Type))
|
||||
}
|
||||
|
||||
val node = logElapsedTime("Node startup") { Node(params.dir, params.address, config, networkMapId,
|
||||
advertisedServices, DemoClock(),
|
||||
listOf(InterestRateSwapAPI::class.java)).setWebServerPort(params.apiPort).start() }
|
||||
val node = logElapsedTime("Node startup") {
|
||||
Node(params.dir, params.address, params.apiAddress, config, networkMapId, advertisedServices, DemoClock(),
|
||||
listOf(InterestRateSwapAPI::class.java)).start()
|
||||
}
|
||||
|
||||
// TODO: This should all be replaced by the identity service being updated
|
||||
// as the network map changes.
|
||||
@ -424,7 +419,7 @@ private fun createNodeAParams() : NodeParams {
|
||||
val params = NodeParams()
|
||||
params.dir = Paths.get("nodeA")
|
||||
params.address = HostAndPort.fromString("localhost").withDefaultPort(Node.DEFAULT_PORT)
|
||||
params.apiPort = Node.DEFAULT_PORT + 1
|
||||
params.apiAddress = HostAndPort.fromString("localhost").withDefaultPort(Node.DEFAULT_PORT + 1)
|
||||
params.tradeWithAddrs = listOf("localhost:31340")
|
||||
params.tradeWithIdentities = listOf(getRoleDir(IRSDemoRole.NodeB).resolve(AbstractNode.PUBLIC_IDENTITY_FILE_NAME))
|
||||
params.defaultLegalName = "Bank A"
|
||||
@ -435,7 +430,7 @@ private fun createNodeBParams() : NodeParams {
|
||||
val params = NodeParams()
|
||||
params.dir = Paths.get("nodeB")
|
||||
params.address = HostAndPort.fromString("localhost:31340")
|
||||
params.apiPort = 31341
|
||||
params.apiAddress = HostAndPort.fromString("localhost:31341")
|
||||
params.tradeWithAddrs = listOf("localhost")
|
||||
params.tradeWithIdentities = listOf(getRoleDir(IRSDemoRole.NodeA).resolve(AbstractNode.PUBLIC_IDENTITY_FILE_NAME))
|
||||
params.defaultLegalName = "Bank B"
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.r3corda.demos
|
||||
|
||||
import com.google.common.net.HostAndPort
|
||||
import com.r3corda.contracts.cash.Cash
|
||||
import com.r3corda.core.contracts.*
|
||||
import com.r3corda.core.crypto.Party
|
||||
@ -71,7 +72,9 @@ fun main(args: Array<String>) {
|
||||
override val nearestCity: String = "Atlantis"
|
||||
}
|
||||
|
||||
val node = logElapsedTime("Node startup") { Node(dir, myNetAddr, config, networkMapAddress,
|
||||
val apiAddr = HostAndPort.fromParts(myNetAddr.hostText, myNetAddr.port + 1)
|
||||
|
||||
val node = logElapsedTime("Node startup") { Node(dir, myNetAddr, apiAddr, config, networkMapAddress,
|
||||
advertisedServices, DemoClock(),
|
||||
listOf(InterestRateSwapAPI::class.java)).setup().start() }
|
||||
|
||||
@ -89,4 +92,4 @@ fun main(args: Array<String>) {
|
||||
println()
|
||||
print(Emoji.renderIfSupported(tx.toWireTransaction()))
|
||||
println(tx.toSignedTransaction().sigs)
|
||||
}
|
||||
}
|
||||
|
@ -77,6 +77,7 @@ fun runTraderDemo(args: Array<String>): Int {
|
||||
val roleArg = parser.accepts("role").withRequiredArg().ofType(Role::class.java).required()
|
||||
val myNetworkAddress = parser.accepts("network-address").withRequiredArg().defaultsTo("localhost")
|
||||
val theirNetworkAddress = parser.accepts("other-network-address").withRequiredArg().defaultsTo("localhost")
|
||||
val apiNetworkAddress = parser.accepts("api-address").withRequiredArg().defaultsTo("localhost")
|
||||
|
||||
val options = try {
|
||||
parser.parse(*args)
|
||||
@ -100,6 +101,7 @@ fun runTraderDemo(args: Array<String>): Int {
|
||||
Role.SELLER -> 31337
|
||||
}
|
||||
)
|
||||
val apiNetAddr = HostAndPort.fromString(options.valueOf(apiNetworkAddress)).withDefaultPort(myNetAddr.port + 1)
|
||||
|
||||
// Suppress the Artemis MQ noise, and activate the demo logging.
|
||||
//
|
||||
@ -143,7 +145,7 @@ fun runTraderDemo(args: Array<String>): Int {
|
||||
|
||||
// And now construct then start the node object. It takes a little while.
|
||||
val node = logElapsedTime("Node startup") {
|
||||
Node(directory, myNetAddr, config, networkMapId, advertisedServices).setup().start()
|
||||
Node(directory, myNetAddr, apiNetAddr, config, networkMapId, advertisedServices).setup().start()
|
||||
}
|
||||
|
||||
// TODO: Replace with a separate trusted cash issuer
|
||||
|
Loading…
Reference in New Issue
Block a user