Moved IRS demo trade mode to the Kotlin demo code.

This commit is contained in:
Clinton Alexander 2016-06-01 16:06:52 +01:00
parent 639f97ff9c
commit 3d556c614f
2 changed files with 104 additions and 42 deletions

View File

@ -37,8 +37,7 @@ elif [[ "$mode" == "nodeB" ]]; then
done done
elif [[ "$mode" == "trade" && "$2" != "" ]]; then elif [[ "$mode" == "trade" && "$2" != "" ]]; then
tradeID=$2 tradeID=$2
echo "Uploading tradeID ${tradeID}" build/install/r3prototyping/bin/irsdemo --role=Trade $tradeId
sed "s/tradeXXX/${tradeID}/g" scripts/example-irs-trade.json | curl -H "Content-Type: application/json" -d @- http://localhost:31338/api/irs/deals
elif [[ "$mode" == "date" && "$2" != "" ]]; then elif [[ "$mode" == "date" && "$2" != "" ]]; then
demodate=$2 demodate=$2
echo "Setting demo date to ${demodate}" echo "Setting demo date to ${demodate}"

View File

@ -36,7 +36,8 @@ import kotlin.system.exitProcess
enum class IRSDemoRole { enum class IRSDemoRole {
NodeA, NodeA,
NodeB NodeB,
Trade
} }
class NodeParams() { class NodeParams() {
@ -64,6 +65,8 @@ fun main(args: Array<String>) {
val fakeTradeWithAddr = parser.accepts("fake-trade-with-address").withOptionalArg() val fakeTradeWithAddr = parser.accepts("fake-trade-with-address").withOptionalArg()
val fakeTradeWithIdentityFile = parser.accepts("fake-trade-with-identity-file").withOptionalArg() val fakeTradeWithIdentityFile = parser.accepts("fake-trade-with-identity-file").withOptionalArg()
val tradeIdArg = parser.nonOptions("Trade ID")
val options = try { val options = try {
parser.parse(*args) parser.parse(*args)
} catch (e: Exception) { } catch (e: Exception) {
@ -76,32 +79,63 @@ fun main(args: Array<String>) {
BriefLogFormatter.initVerbose("+demo.irsdemo", "+api-call", "+platform.deal", "-org.apache.activemq") BriefLogFormatter.initVerbose("+demo.irsdemo", "+api-call", "+platform.deal", "-org.apache.activemq")
val role = options.valueOf(roleArg)!! val role = options.valueOf(roleArg)!!
val nodeParams = when(role) { if(role == IRSDemoRole.Trade) {
IRSDemoRole.NodeA -> createNodeAParams() val tradeId : String? = options.valuesOf(tradeIdArg)[0]
IRSDemoRole.NodeB -> createNodeBParams() if(tradeId != null) {
} if(runTrade(tradeId)) {
exitProcess(0)
nodeParams.mapAddress = options.valueOf(networkMapNetAddr) } else {
if(options.has(dirArg)) { exitProcess(1)
nodeParams.dir = Paths.get(options.valueOf(dirArg)) }
} } else {
if(options.has(networkAddressArg)) { println("Please provide a trade ID")
nodeParams.address = options.valueOf(networkAddressArg) exitProcess(1)
} }
nodeParams.identityFile = if(options.has(networkMapIdentityFile)) {
Paths.get(options.valueOf(networkMapIdentityFile))
} else { } else {
nodeParams.dir.resolve("identity-public") val nodeParams = when (role) {
} IRSDemoRole.NodeA -> createNodeAParams()
if(options.has(fakeTradeWithIdentityFile)) { IRSDemoRole.NodeB -> createNodeBParams()
nodeParams.tradeWithIdentities = options.valuesOf(fakeTradeWithIdentityFile).map { Paths.get(it) } else -> {
} throw IllegalArgumentException()
if(options.has(fakeTradeWithAddr)) { }
nodeParams.tradeWithAddrs = options.valuesOf(fakeTradeWithAddr) }
}
runNode(nodeParams) nodeParams.mapAddress = options.valueOf(networkMapNetAddr)
exitProcess(0) if (options.has(dirArg)) {
nodeParams.dir = Paths.get(options.valueOf(dirArg))
}
if (options.has(networkAddressArg)) {
nodeParams.address = options.valueOf(networkAddressArg)
}
nodeParams.identityFile = if (options.has(networkMapIdentityFile)) {
Paths.get(options.valueOf(networkMapIdentityFile))
} else {
nodeParams.dir.resolve("identity-public")
}
if (options.has(fakeTradeWithIdentityFile)) {
nodeParams.tradeWithIdentities = options.valuesOf(fakeTradeWithIdentityFile).map { Paths.get(it) }
}
if (options.has(fakeTradeWithAddr)) {
nodeParams.tradeWithAddrs = options.valuesOf(fakeTradeWithAddr)
}
runNode(nodeParams)
exitProcess(0)
}
}
fun runTrade(tradeId : String) : Boolean {
println("Uploading tradeID " + tradeId)
val fileContents = Files.readAllBytes(Paths.get("scripts/example-irs-trade.json"))
val tradeFile = String(fileContents).replace("tradeXXX", tradeId)
var url = URL("http://localhost:31338/api/irs/deals")
if(postJson(url, tradeFile)) {
println("Trade sent")
return true
} else {
println("Trade failed to send")
return false
}
} }
fun runNode(nodeParams : NodeParams) : Unit { fun runNode(nodeParams : NodeParams) : Unit {
@ -127,26 +161,12 @@ fun runUploadRates() {
var timer : Timer? = null var timer : Timer? = null
timer = fixedRateTimer("upload-rates", false, 0, 5000, { timer = fixedRateTimer("upload-rates", false, 0, 5000, {
try { try {
val boundary = "===" + System.currentTimeMillis() + "===";
val url = URL("http://localhost:31341/upload/interest-rates") val url = URL("http://localhost:31341/upload/interest-rates")
val connection = url.openConnection() as HttpURLConnection if(uploadFile(url, fileContents)) {
connection.doOutput = true
connection.doInput = true;
connection.useCaches = false
connection.requestMethod = "POST"
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Cache-Control", "no-cache")
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
val outStream = DataOutputStream(connection.outputStream)
outStream.write(fileContents)
outStream.close()
if (connection.responseCode == 200) {
timer!!.cancel() timer!!.cancel()
println("Rates uploaded successfully") println("Rates uploaded successfully")
} else { } else {
print("Could not upload rates. Retrying in 5 seconds. ") print("Could not upload rates. Retrying in 5 seconds. ")
println("Status Code: " + connection + ". Mesage: " + connection.responseMessage)
} }
} catch (e: Exception) { } catch (e: Exception) {
println("Could not upload rates due to exception. Retrying in 5 seconds") println("Could not upload rates due to exception. Retrying in 5 seconds")
@ -154,6 +174,49 @@ fun runUploadRates() {
}) })
} }
fun postJson(url: URL, data: String) : Boolean {
val connection = url.openConnection() as HttpURLConnection
connection.doOutput = true
connection.useCaches = false
connection.requestMethod = "POST"
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Cache-Control", "no-cache")
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", data.length.toString())
val outStream = DataOutputStream(connection.outputStream)
outStream.writeBytes(data)
outStream.close()
if (connection.responseCode == 200) {
return true
} else {
println("Failed to post data. Status Code: " + connection + ". Mesage: " + connection.responseMessage)
return false
}
}
fun uploadFile(url: URL, file: ByteArray) : Boolean {
val boundary = "===" + System.currentTimeMillis() + "===";
val connection = url.openConnection() as HttpURLConnection
connection.doOutput = true
connection.doInput = true
connection.useCaches = false
connection.requestMethod = "POST"
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Cache-Control", "no-cache")
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
val outStream = DataOutputStream(connection.outputStream)
outStream.write(file)
outStream.close()
if (connection.responseCode == 200) {
return true
} else {
println("Could not upload file. Status Code: " + connection + ". Mesage: " + connection.responseMessage)
return false
}
}
fun createNodeAParams() : NodeParams { fun createNodeAParams() : NodeParams {
val params = NodeParams() val params = NodeParams()
params.dir = Paths.get("nodeA") params.dir = Paths.get("nodeA")