From 389a4704001c8605d3eadaf02c0a348eb0106d6f Mon Sep 17 00:00:00 2001 From: Clinton Alexander Date: Wed, 1 Jun 2016 17:26:43 +0100 Subject: [PATCH] Moved IRS demo date setting functionality to Kotlin code. Removed now redundant demo script. --- scripts/irs-demo.sh | 56 -------------------- src/main/kotlin/com/r3corda/demos/IRSDemo.kt | 52 +++++++++++++++--- 2 files changed, 44 insertions(+), 64 deletions(-) delete mode 100755 scripts/irs-demo.sh diff --git a/scripts/irs-demo.sh b/scripts/irs-demo.sh deleted file mode 100755 index 8d633c25be..0000000000 --- a/scripts/irs-demo.sh +++ /dev/null @@ -1,56 +0,0 @@ -#!/bin/bash - -mode=$1 - -if [ ! -e ./gradlew ]; then - echo "Run from the root directory please" - exit 1 -fi - -if [ ! -d build/install/r3prototyping ] && [[ "$SKIP_INSTALL" == "" ]]; then - ./gradlew installDist -fi - -if [[ "$mode" == "nodeA" ]]; then - if [ ! -d nodeA ]; then - mkdir nodeA - echo "myLegalName = Bank A" >nodeA/config - fi - - RC=83 - while [ $RC -eq 83 ] - do - build/install/r3prototyping/bin/irsdemo --role=NodeA - RC=$? - done -elif [[ "$mode" == "nodeB" ]]; then - if [ ! -d nodeB ]; then - mkdir nodeB - echo "myLegalName = Bank B" >nodeB/config - fi - - RC=83 - while [ $RC -eq 83 ] - do - build/install/r3prototyping/bin/irsdemo --role=NodeB - RC=$? - done -elif [[ "$mode" == "trade" && "$2" != "" ]]; then - tradeID=$2 - build/install/r3prototyping/bin/irsdemo --role=Trade $tradeId -elif [[ "$mode" == "date" && "$2" != "" ]]; then - demodate=$2 - echo "Setting demo date to ${demodate}" - echo "\"$demodate\"" | curl -H "Content-Type: application/json" -X PUT -d @- http://localhost:31338/api/irs/demodate -else - echo "Run like this, one in each tab:" - echo - echo " scripts/irs-demo.sh nodeA" - echo " scripts/irs-demo.sh nodeB" - echo - echo "To upload a trade as e.g. trade10" - echo " scripts/irs-demo.sh trade trade10" - echo - echo "To set the demo date, and post fixings in the interval, to e.g. 2017-01-30" - echo " scripts/irs-demo.sh date 2017-01-30" -fi diff --git a/src/main/kotlin/com/r3corda/demos/IRSDemo.kt b/src/main/kotlin/com/r3corda/demos/IRSDemo.kt index 47c99940d6..db4b69f07c 100644 --- a/src/main/kotlin/com/r3corda/demos/IRSDemo.kt +++ b/src/main/kotlin/com/r3corda/demos/IRSDemo.kt @@ -26,6 +26,7 @@ import java.net.URL import java.nio.file.Files import java.nio.file.Path import java.nio.file.Paths +import java.time.LocalDateTime import java.util.* import kotlin.concurrent.fixedRateTimer import kotlin.system.exitProcess @@ -37,7 +38,8 @@ import kotlin.system.exitProcess enum class IRSDemoRole { NodeA, NodeB, - Trade + Trade, + Date } class NodeParams() { @@ -50,6 +52,12 @@ class NodeParams() { var uploadRates: Boolean = false } +/* + + echo "Setting demo date to ${demodate}" + echo "\"$demodate\"" | curl -H "Content-Type: application/json" -X PUT -d @- http://localhost:31338/api/irs/demodate + */ + fun main(args: Array) { val parser = OptionParser() @@ -66,6 +74,7 @@ fun main(args: Array) { val fakeTradeWithIdentityFile = parser.accepts("fake-trade-with-identity-file").withOptionalArg() val tradeIdArg = parser.nonOptions("Trade ID") + val dateArg = parser.nonOptions("Date") val options = try { parser.parse(*args) @@ -80,9 +89,9 @@ fun main(args: Array) { val role = options.valueOf(roleArg)!! if(role == IRSDemoRole.Trade) { - val tradeId : String? = options.valuesOf(tradeIdArg)[0] - if(tradeId != null) { - if(runTrade(tradeId)) { + val tradeId: String? = options.valuesOf(tradeIdArg)[0] + if (tradeId != null) { + if (runTrade(tradeId)) { exitProcess(0) } else { exitProcess(1) @@ -91,6 +100,14 @@ fun main(args: Array) { println("Please provide a trade ID") exitProcess(1) } + } else if(role == IRSDemoRole.Date) { + val dateStr: String? = options.valueOf(dateArg) + if(dateStr != null) { + runDateChange(dateStr) + } else { + println("Please provide a date") + exitProcess(1) + } } else { val nodeParams = when (role) { IRSDemoRole.NodeA -> createNodeAParams() @@ -124,7 +141,18 @@ fun main(args: Array) { } } -fun runTrade(tradeId : String) : Boolean { +fun runDateChange(date: String) : Boolean{ + var url = URL("http://localhost:31338/api/irs/demodate") + if(putJson(url, "\"" + date + "\"")) { + println("Date changed") + return true + } else { + println("Date failed to change") + return false + } +} + +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) @@ -174,11 +202,11 @@ fun runUploadRates() { }) } -fun postJson(url: URL, data: String) : Boolean { +fun sendJson(url: URL, data: String, method: String) : Boolean { val connection = url.openConnection() as HttpURLConnection connection.doOutput = true connection.useCaches = false - connection.requestMethod = "POST" + connection.requestMethod = method connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Cache-Control", "no-cache") connection.setRequestProperty("Content-Type", "application/json"); @@ -190,11 +218,19 @@ fun postJson(url: URL, data: String) : Boolean { if (connection.responseCode == 200) { return true } else { - println("Failed to post data. Status Code: " + connection + ". Mesage: " + connection.responseMessage) + println("Failed to " + method + " data. Status Code: " + connection + ". Mesage: " + connection.responseMessage) return false } } +fun putJson(url: URL, data: String) : Boolean { + return sendJson(url, data, "PUT") +} + +fun postJson(url: URL, data: String) : Boolean { + return sendJson(url, data, "POST") +} + fun uploadFile(url: URL, file: ByteArray) : Boolean { val boundary = "===" + System.currentTimeMillis() + "==="; val connection = url.openConnection() as HttpURLConnection