From 639f97ff9c9368210efa2e63188be21fe278a354 Mon Sep 17 00:00:00 2001 From: Clinton Alexander Date: Wed, 1 Jun 2016 15:07:15 +0100 Subject: [PATCH] Rate uploading moved to IRSDemo.kt from irs-demo.sh --- scripts/irs-demo.sh | 10 +--- src/main/kotlin/com/r3corda/demos/IRSDemo.kt | 55 +++++++++++++++++--- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/scripts/irs-demo.sh b/scripts/irs-demo.sh index d6f3334ca4..2f163357e7 100755 --- a/scripts/irs-demo.sh +++ b/scripts/irs-demo.sh @@ -29,18 +29,10 @@ elif [[ "$mode" == "nodeB" ]]; then echo "myLegalName = Bank B" >nodeB/config fi - # enable job control - set -o monitor - RC=83 while [ $RC -eq 83 ] do - build/install/r3prototyping/bin/irsdemo --role=NodeB & - while ! curl -F rates=@scripts/example.rates.txt http://localhost:31341/upload/interest-rates; do - echo "Retry to upload interest rates to oracle after 5 seconds" - sleep 5 - done - fg %1 + build/install/r3prototyping/bin/irsdemo --role=NodeB RC=$? done elif [[ "$mode" == "trade" && "$2" != "" ]]; then diff --git a/src/main/kotlin/com/r3corda/demos/IRSDemo.kt b/src/main/kotlin/com/r3corda/demos/IRSDemo.kt index 67b0d91121..9955b2d901 100644 --- a/src/main/kotlin/com/r3corda/demos/IRSDemo.kt +++ b/src/main/kotlin/com/r3corda/demos/IRSDemo.kt @@ -20,9 +20,14 @@ import com.r3corda.demos.protocols.AutoOfferProtocol import com.r3corda.demos.protocols.ExitServerProtocol import com.r3corda.demos.protocols.UpdateBusinessDayProtocol import joptsimple.OptionParser +import java.io.DataOutputStream +import java.net.HttpURLConnection +import java.net.URL import java.nio.file.Files import java.nio.file.Path import java.nio.file.Paths +import java.util.* +import kotlin.concurrent.fixedRateTimer import kotlin.system.exitProcess // IRS DEMO @@ -35,12 +40,13 @@ enum class IRSDemoRole { } class NodeParams() { - public var dir : Path = Paths.get("") - public var address : String = "" - public var mapAddress: String = "" - public var identityFile: Path = Paths.get("") - public var tradeWithAddrs: List = listOf() - public var tradeWithIdentities: List = listOf() + var dir : Path = Paths.get("") + var address : String = "" + var mapAddress: String = "" + var identityFile: Path = Paths.get("") + var tradeWithAddrs: List = listOf() + var tradeWithIdentities: List = listOf() + var uploadRates: Boolean = false } fun main(args: Array) { @@ -105,6 +111,10 @@ fun runNode(nodeParams : NodeParams) : Unit { UpdateBusinessDayProtocol.Handler.register(node) ExitServerProtocol.Handler.register(node) + if(nodeParams.uploadRates) { + runUploadRates() + } + try { while (true) Thread.sleep(Long.MAX_VALUE) } catch(e: InterruptedException) { @@ -112,6 +122,38 @@ fun runNode(nodeParams : NodeParams) : Unit { } } +fun runUploadRates() { + val fileContents = Files.readAllBytes(Paths.get("scripts/example.rates.txt")) + var timer : Timer? = null + timer = fixedRateTimer("upload-rates", false, 0, 5000, { + try { + val boundary = "===" + System.currentTimeMillis() + "==="; + val url = URL("http://localhost:31341/upload/interest-rates") + 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(fileContents) + outStream.close() + + if (connection.responseCode == 200) { + timer!!.cancel() + println("Rates uploaded successfully") + } else { + print("Could not upload rates. Retrying in 5 seconds. ") + println("Status Code: " + connection + ". Mesage: " + connection.responseMessage) + } + } catch (e: Exception) { + println("Could not upload rates due to exception. Retrying in 5 seconds") + } + }) +} + fun createNodeAParams() : NodeParams { val params = NodeParams() params.dir = Paths.get("nodeA") @@ -127,6 +169,7 @@ fun createNodeBParams() : NodeParams { params.address = "localhost:31340" params.tradeWithAddrs = listOf("localhost") params.tradeWithIdentities = listOf(getRoleDir(IRSDemoRole.NodeA).resolve("identity-public")) + params.uploadRates = true return params }