Merged in irs-demo-against-standalone (pull request #326)

Allow IRS demo to be executed against standalone nodes by adding rates upload as a role so doesn't need to be done by node launcher, but can instead be run independently from the command line.
This commit is contained in:
Rick Parker 2016-09-06 09:47:17 +01:00
commit ac35673074

View File

@ -1,6 +1,8 @@
package com.r3corda.demos
import com.google.common.net.HostAndPort
import com.google.common.util.concurrent.ListenableFuture
import com.google.common.util.concurrent.SettableFuture
import com.r3corda.contracts.InterestRateSwap
import com.r3corda.core.crypto.Party
import com.r3corda.core.logElapsedTime
@ -19,13 +21,13 @@ import com.r3corda.demos.utilities.putJson
import com.r3corda.demos.utilities.uploadFile
import com.r3corda.node.internal.AbstractNode
import com.r3corda.node.internal.Node
import com.r3corda.testing.node.MockNetwork
import com.r3corda.node.services.clientapi.NodeInterestRates
import com.r3corda.node.services.config.NodeConfiguration
import com.r3corda.node.services.config.NodeConfigurationFromConfig
import com.r3corda.node.services.messaging.ArtemisMessagingClient
import com.r3corda.node.services.network.NetworkMapService
import com.r3corda.node.services.transactions.SimpleNotaryService
import com.r3corda.testing.node.MockNetwork
import joptsimple.OptionParser
import joptsimple.OptionSet
import org.apache.commons.io.IOUtils
@ -56,7 +58,8 @@ enum class IRSDemoRole {
NodeA,
NodeB,
Trade,
Date
Date,
Rates
}
/**
@ -105,6 +108,13 @@ sealed class CliParams {
val dateString: String
) : CliParams()
/**
* Corresponds to role 'Rates'.
*/
class UploadRates(
val apiAddress: HostAndPort
) : CliParams()
/**
* Corresponds to --help.
*/
@ -202,6 +212,15 @@ sealed class CliParams {
)
}
private fun parseRatesUpload(options: OptionSet): UploadRates {
return UploadRates(
apiAddress = HostAndPort.fromString(options.valueOf(
CliParamsSpec.apiAddressArg.defaultsTo("localhost:${defaultApiPort(IRSDemoNode.NodeB)}")
))
)
}
fun parse(options: OptionSet): CliParams {
if (options.has(CliParamsSpec.help)) {
return Help
@ -214,6 +233,7 @@ sealed class CliParams {
IRSDemoRole.NodeB -> parseRunNode(options, IRSDemoNode.NodeB)
IRSDemoRole.Trade -> parseTrade(options)
IRSDemoRole.Date -> parseDateChange(options)
IRSDemoRole.Rates -> parseRatesUpload(options)
}
}
}
@ -293,6 +313,7 @@ fun runIRSDemo(args: Array<String>): Int {
is CliParams.RunNode -> runNode(cliParams)
is CliParams.Trade -> runTrade(cliParams)
is CliParams.DateChange -> runDateChange(cliParams)
is CliParams.UploadRates -> runUploadRates(cliParams)
is CliParams.Help -> {
printHelp(CliParamsSpec.parser)
0
@ -372,6 +393,8 @@ private fun runTrade(cliParams: CliParams.Trade): Int {
}
}
fun runUploadRates(cliParams: CliParams.UploadRates) = runUploadRates(cliParams.apiAddress).get()
private fun createRecipient(addr: String): SingleMessageRecipient {
val hostAndPort = HostAndPort.fromString(addr).withDefaultPort(Node.DEFAULT_PORT)
return ArtemisMessagingClient.makeNetworkMapAddress(hostAndPort)
@ -411,23 +434,27 @@ private fun nodeInfo(recipient: SingleMessageRecipient, identityFile: Path, adve
}
}
private fun runUploadRates(host: HostAndPort) {
private fun runUploadRates(host: HostAndPort): ListenableFuture<Int> {
// Note: the getResourceAsStream is an ugly hack to get the jvm to search in the right location
val fileContents = IOUtils.toString(CliParams::class.java.getResourceAsStream("example.rates.txt"))
var timer: Timer? = null
val result = SettableFuture.create<Int>()
timer = fixedRateTimer("upload-rates", false, 0, 5000, {
try {
val url = URL("http://${host.toString()}/upload/interest-rates")
if (uploadFile(url, fileContents)) {
timer!!.cancel()
log.info("Rates uploaded successfully")
result.set(0)
} else {
log.error("Could not upload rates. Retrying in 5 seconds. ")
result.set(1)
}
} catch (e: Exception) {
log.error("Could not upload rates due to exception. Retrying in 5 seconds")
}
})
return result
}
private fun getNodeConfig(cliParams: CliParams.RunNode): NodeConfiguration {