mirror of
https://github.com/corda/corda.git
synced 2025-06-13 04:38:19 +00:00
docs: add docs/source/example-code module, tutorial on the Client RPC API
This commit is contained in:
@ -0,0 +1,73 @@
|
||||
package com.r3corda.docs
|
||||
|
||||
import com.google.common.net.HostAndPort
|
||||
import com.r3corda.client.CordaRPCClient
|
||||
import org.graphstream.graph.Edge
|
||||
import org.graphstream.graph.Node
|
||||
import org.graphstream.graph.implementations.SingleGraph
|
||||
import java.nio.file.Paths
|
||||
import java.util.concurrent.CompletableFuture
|
||||
|
||||
/**
|
||||
* This is example code for the Client RPC API tutorial. The START/END comments are important and used by the documentation!
|
||||
*/
|
||||
|
||||
// START 1
|
||||
enum class PrintOrVisualise {
|
||||
Print,
|
||||
Visualise
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
if (args.size < 2) {
|
||||
throw IllegalArgumentException("Usage: <binary> <node address> [Print|Visualise]")
|
||||
}
|
||||
val nodeAddress = HostAndPort.fromString(args[0])
|
||||
val printOrVisualise = PrintOrVisualise.valueOf(args[1])
|
||||
val certificatesPath = Paths.get("build/trader-demo/buyer/certificates")
|
||||
// END 1
|
||||
|
||||
// START 2
|
||||
val client = CordaRPCClient(nodeAddress, certificatesPath)
|
||||
client.start()
|
||||
val proxy = client.proxy()
|
||||
// END 2
|
||||
|
||||
// START 3
|
||||
val (transactions, futureTransactions) = proxy.verifiedTransactions()
|
||||
// END 3
|
||||
|
||||
// START 4
|
||||
when (printOrVisualise) {
|
||||
PrintOrVisualise.Print -> {
|
||||
futureTransactions.startWith(transactions).subscribe { transaction ->
|
||||
println("NODE ${transaction.id}")
|
||||
transaction.tx.inputs.forEach { input ->
|
||||
println("EDGE ${input.txhash} ${transaction.id}")
|
||||
}
|
||||
}
|
||||
CompletableFuture<Unit>().get() // block indefinitely
|
||||
}
|
||||
// END 4
|
||||
// START 5
|
||||
PrintOrVisualise.Visualise -> {
|
||||
val graph = SingleGraph("transactions")
|
||||
transactions.forEach { transaction ->
|
||||
graph.addNode<Node>("${transaction.id}")
|
||||
}
|
||||
transactions.forEach { transaction ->
|
||||
transaction.tx.inputs.forEach { ref ->
|
||||
graph.addEdge<Edge>("$ref", "${ref.txhash}", "${transaction.id}")
|
||||
}
|
||||
}
|
||||
futureTransactions.subscribe { transaction ->
|
||||
graph.addNode<Node>("${transaction.id}")
|
||||
transaction.tx.inputs.forEach { ref ->
|
||||
graph.addEdge<Edge>("${ref}", "${ref.txhash}", "${transaction.id}")
|
||||
}
|
||||
}
|
||||
graph.display()
|
||||
}
|
||||
}
|
||||
}
|
||||
// END 5
|
Reference in New Issue
Block a user