Skip to content

Instantly share code, notes, and snippets.

View lankydan's full-sized avatar
👋

Dan Newton lankydan

👋
View GitHub Profile
@lankydan
lankydan / Server.kt
Created August 12, 2019 09:08
Corda + Ktor - gracefully disconnecting from node
fun NettyApplicationEngine.addShutdownHook() {
Runtime.getRuntime().addShutdownHook(Thread {
stop(1, 1, TimeUnit.SECONDS)
})
Thread.currentThread().join()
}
fun Application.addShutdownEvent(connection: CordaRPCConnection) {
environment.monitor.subscribe(ApplicationStopped) {
connection.notifyServerAndClose()
@lankydan
lankydan / Messages.kt
Created August 12, 2019 09:08
Corda + Ktor - endpoints
fun Routing.messages(proxy: CordaRPCOps) {
route("/messages") {
get("/") {
call.respond(
HttpStatusCode.OK,
proxy.vaultQueryBy<MessageState>().states.map { it.state.data })
}
post("/") {
val received = call.receive<Message>()
try {
@lankydan
lankydan / Server.kt
Created August 12, 2019 09:07
Corda + Ktor - jackson
fun ContentNegotiation.Configuration.cordaJackson(proxy: CordaRPCOps) {
val mapper: ObjectMapper = JacksonSupport.createDefaultMapper(proxy)
mapper.apply {
setDefaultPrettyPrinter(DefaultPrettyPrinter().apply {
indentArraysWith(DefaultPrettyPrinter.FixedSpaceIndenter.instance)
indentObjectsWith(DefaultIndenter(" ", "\n"))
})
}
val converter = JacksonConverter(mapper)
register(ContentType.Application.Json, converter)
@lankydan
lankydan / CordaConnection.kt
Created August 12, 2019 09:07
Corda + Ktor - connecting to the node
fun connectToNode(
host: String = System.getProperty("config.rpc.host"),
rpcPort: Int = System.getProperty("config.rpc.port").toInt(),
username: String = System.getProperty("config.rpc.username"),
password: String = System.getProperty("config.rpc.password")
): CordaRPCConnection {
val rpcAddress = NetworkHostAndPort(host, rpcPort)
val rpcClient = CordaRPCClient(rpcAddress)
return rpcClient.start(username, password)
}
@lankydan
lankydan / Server.kt
Created August 12, 2019 09:04
Corda + Ktor - server
fun main() {
embeddedServer(
Netty,
port = System.getProperty("server.port").toInt(),
module = Application::module
).start().addShutdownHook()
}
fun Application.module() {
val connection: CordaRPCConnection = connectToNode()
@lankydan
lankydan / build.gradle
Created August 12, 2019 09:03
Corda + Ktor - dependencies
buildscript {
ext.ktor_version = '1.2.2'
ext.kotlin_version_for_app = '1.3.41'
repositories {
mavenCentral()
jcenter()
}
dependencies {
@lankydan
lankydan / DoSomethingComplicatedThatYouCantDoViaRpc.kt
Created July 30, 2019 10:55
Corda flows can do anything - DoSomethingComplicatedThatYouCantDoViaRpc
@StartableByRPC
class DoSomethingComplicatedThatYouCantDoViaRpc(private val recipient: Party) :
FlowLogic<List<MessageSchema.PersistentMessage>>() {
override fun call(): List<MessageSchema.PersistentMessage> {
return serviceHub.withEntityManager {
createQuery(
"SELECT m FROM $TABLE_NAME m WHERE m.recipient = ?1",
MessageSchema.PersistentMessage::class.java
).setParameter(
1,
@lankydan
lankydan / ExecuteSomeInternalNodeLogicYouDontWantToDuplicateFlow.kt
Created July 30, 2019 10:55
Corda flows can do anything - ExecuteSomeInternalNodeLogicYouDontWantToDuplicateFlow
@StartableByRPC
class ExecuteSomeInternalNodeLogicYouDontWantToDuplicateFlow(private val recipient: Party) :
FlowLogic<List<MessageState>>() {
override fun call(): List<MessageState> {
return serviceHub.cordaService(MessageRepository::class.java)
.findAllMessagesByRecipient(recipient)
}
}
@CordaService
@lankydan
lankydan / GetMeSomeValueFromAService.kt
Last active July 30, 2019 10:54
Corda flows can do anything - GetMeSomeValueFromAService
@StartableByRPC
class GetMeSomeValueFromAService : FlowLogic<Long>() {
override fun call(): Long {
return serviceHub.cordaService(IncrementingService::class.java).counter
}
}
@CordaService
class IncrementingService(serviceHub: AppServiceHub) : SingletonSerializeAsToken() {
@lankydan
lankydan / StartFlow.kt
Created July 30, 2019 10:53
Corda flows can do anything - start flow
proxy.startFlow(::StupidSimpleQueryFlow, "asdas").returnValue.get()