Merged in demo-cliargs-description (pull request #216)

Demo cliargs description
This commit is contained in:
Andras Slemmer 2016-07-12 15:54:12 +01:00
commit 54042db7bc

View File

@ -82,7 +82,6 @@ sealed class CliParams {
val apiAddress: HostAndPort, val apiAddress: HostAndPort,
val mapAddress: String, val mapAddress: String,
val identityFile: Path, val identityFile: Path,
val tradeWithAddrs: List<String>,
val tradeWithIdentities: List<Path>, val tradeWithIdentities: List<Path>,
val uploadRates: Boolean, val uploadRates: Boolean,
val defaultLegalName: String, val defaultLegalName: String,
@ -105,6 +104,11 @@ sealed class CliParams {
val dateString: String val dateString: String
) : CliParams() ) : CliParams()
/**
* Corresponds to --help.
*/
object Help : CliParams()
companion object { companion object {
val defaultBaseDirectory = "./build/irs-demo" val defaultBaseDirectory = "./build/irs-demo"
@ -156,11 +160,6 @@ sealed class CliParams {
} else { } else {
dir.resolve(AbstractNode.PUBLIC_IDENTITY_FILE_NAME) dir.resolve(AbstractNode.PUBLIC_IDENTITY_FILE_NAME)
}, },
tradeWithAddrs = if (options.has(CliParamsSpec.fakeTradeWithAddr)) {
options.valuesOf(CliParamsSpec.fakeTradeWithAddr)
} else {
listOf("localhost:${defaultNetworkPort(node.other)}")
},
tradeWithIdentities = if (options.has(CliParamsSpec.fakeTradeWithIdentityFile)) { tradeWithIdentities = if (options.has(CliParamsSpec.fakeTradeWithIdentityFile)) {
options.valuesOf(CliParamsSpec.fakeTradeWithIdentityFile).map { Paths.get(it) } options.valuesOf(CliParamsSpec.fakeTradeWithIdentityFile).map { Paths.get(it) }
} else { } else {
@ -203,7 +202,10 @@ sealed class CliParams {
} }
fun parse(options: OptionSet): CliParams { fun parse(options: OptionSet): CliParams {
val role = options.valueOf(CliParamsSpec.roleArg)!! if (options.has(CliParamsSpec.help)) {
return Help
}
val role: IRSDemoRole = options.valueOf(CliParamsSpec.roleArg) ?: throw IllegalArgumentException("Please provide a role")
return when (role) { return when (role) {
IRSDemoRole.SetupNodeA -> parseSetupNode(options, IRSDemoNode.NodeA) IRSDemoRole.SetupNodeA -> parseSetupNode(options, IRSDemoNode.NodeA)
IRSDemoRole.SetupNodeB -> parseSetupNode(options, IRSDemoNode.NodeB) IRSDemoRole.SetupNodeB -> parseSetupNode(options, IRSDemoNode.NodeB)
@ -230,15 +232,28 @@ enum class IRSDemoNode {
object CliParamsSpec { object CliParamsSpec {
val parser = OptionParser() val parser = OptionParser()
val roleArg = parser.accepts("role").withRequiredArg().ofType(IRSDemoRole::class.java) val roleArg = parser.accepts("role")
val networkAddressArg = parser.accepts("network-address").withOptionalArg().ofType(String::class.java) .withRequiredArg().ofType(IRSDemoRole::class.java)
val apiAddressArg = parser.accepts("api-address").withOptionalArg().ofType(String::class.java) val networkAddressArg =
val baseDirectoryArg = parser.accepts("base-directory").withOptionalArg().defaultsTo(CliParams.defaultBaseDirectory) parser.accepts("network-address", "The p2p networking address to use")
val networkMapIdentityFile = parser.accepts("network-map-identity-file").withOptionalArg() .withOptionalArg().ofType(String::class.java)
val networkMapNetAddr = parser.accepts("network-map-address").withRequiredArg().defaultsTo("localhost") val apiAddressArg =
val fakeTradeWithAddr = parser.accepts("fake-trade-with-address").withOptionalArg() parser.accepts("api-address", "The address to expose the HTTP API on")
val fakeTradeWithIdentityFile = parser.accepts("fake-trade-with-identity-file").withOptionalArg() .withOptionalArg().ofType(String::class.java)
val baseDirectoryArg =
parser.accepts("base-directory", "The directory to put all files under")
.withOptionalArg().defaultsTo(CliParams.defaultBaseDirectory)
val networkMapIdentityFile =
parser.accepts("network-map-identity-file", "The file containing the Party info of the network map")
.withOptionalArg()
val networkMapNetAddr =
parser.accepts("network-map-address", "The address of the network map")
.withRequiredArg().defaultsTo("localhost")
val fakeTradeWithIdentityFile =
parser.accepts("fake-trade-with-identity-file", "Extra identities to be registered with the identity service")
.withOptionalArg()
val nonOptions = parser.nonOptions() val nonOptions = parser.nonOptions()
val help = parser.accepts("help", "Prints this help").forHelp()
} }
class IRSDemoPluginRegistry : CordaPluginRegistry { class IRSDemoPluginRegistry : CordaPluginRegistry {
@ -274,6 +289,10 @@ fun runIRSDemo(args: Array<String>): Int {
is CliParams.RunNode -> runNode(cliParams) is CliParams.RunNode -> runNode(cliParams)
is CliParams.Trade -> runTrade(cliParams) is CliParams.Trade -> runTrade(cliParams)
is CliParams.DateChange -> runDateChange(cliParams) is CliParams.DateChange -> runDateChange(cliParams)
is CliParams.Help -> {
printHelp(CliParamsSpec.parser)
0
}
} }
} }
@ -306,11 +325,8 @@ private fun runNode(cliParams: CliParams.RunNode): Int {
try { try {
val networkMap = createRecipient(cliParams.mapAddress) val networkMap = createRecipient(cliParams.mapAddress)
val destinations = cliParams.tradeWithAddrs.map({
createRecipient(it)
})
val node = startNode(cliParams, networkMap, destinations) val node = startNode(cliParams, networkMap)
// Register handlers for the demo // Register handlers for the demo
AutoOfferProtocol.Handler.register(node) AutoOfferProtocol.Handler.register(node)
UpdateBusinessDayProtocol.Handler.register(node) UpdateBusinessDayProtocol.Handler.register(node)
@ -366,7 +382,7 @@ private fun createRecipient(addr: String) : SingleMessageRecipient {
return ArtemisMessagingService.makeRecipient(hostAndPort) return ArtemisMessagingService.makeRecipient(hostAndPort)
} }
private fun startNode(params: CliParams.RunNode, networkMap: SingleMessageRecipient, recipients: List<SingleMessageRecipient>) : Node { private fun startNode(params: CliParams.RunNode, networkMap: SingleMessageRecipient) : Node {
val config = getNodeConfig(params) val config = getNodeConfig(params)
val advertisedServices: Set<ServiceType> val advertisedServices: Set<ServiceType>
val networkMapId = val networkMapId =
@ -387,21 +403,18 @@ private fun startNode(params: CliParams.RunNode, networkMap: SingleMessageRecipi
// TODO: This should all be replaced by the identity service being updated // TODO: This should all be replaced by the identity service being updated
// as the network map changes. // as the network map changes.
if (params.tradeWithAddrs.size != params.tradeWithIdentities.size) { for (identityFile in params.tradeWithIdentities) {
throw IllegalArgumentException("Different number of peer addresses (${params.tradeWithAddrs.size}) and identities (${params.tradeWithIdentities.size})") node.services.identityService.registerIdentity(parsePartyFromFile(identityFile))
}
for ((recipient, identityFile) in recipients.zip(params.tradeWithIdentities)) {
val peerId = nodeInfo(recipient, identityFile)
node.services.identityService.registerIdentity(peerId.identity)
} }
return node return node
} }
private fun parsePartyFromFile(path: Path) = Files.readAllBytes(path).deserialize<Party>()
private fun nodeInfo(recipient: SingleMessageRecipient, identityFile: Path, advertisedServices: Set<ServiceType> = emptySet()): NodeInfo { private fun nodeInfo(recipient: SingleMessageRecipient, identityFile: Path, advertisedServices: Set<ServiceType> = emptySet()): NodeInfo {
try { try {
val path = identityFile val party = parsePartyFromFile(identityFile)
val party = Files.readAllBytes(path).deserialize<Party>()
return NodeInfo(recipient, party, advertisedServices) return NodeInfo(recipient, party, advertisedServices)
} catch (e: Exception) { } catch (e: Exception) {
println("Could not find identify file $identityFile.") println("Could not find identify file $identityFile.")
@ -466,8 +479,9 @@ private fun createDefaultConfigFile(configFile: File, legalName: String) {
} }
private fun printHelp(parser: OptionParser) { private fun printHelp(parser: OptionParser) {
val roleList = IRSDemoRole.values().joinToString(separator = "|") { it.toString() }
println(""" println("""
Usage: irsdemo --role [NodeA|NodeB|Trade|Date] [<TradeName>|<DateValue>] [options] Usage: irsdemo --role $roleList [<TradeName>|<DateValue>] [options]
Please refer to the documentation in docs/build/index.html for more info. Please refer to the documentation in docs/build/index.html for more info.
""".trimIndent()) """.trimIndent())