Skip to content

Instantly share code, notes, and snippets.

@jinqian
Last active December 9, 2019 15:51
Show Gist options
  • Save jinqian/dc99c09658c45050329da8ad0bf7c27a to your computer and use it in GitHub Desktop.
Save jinqian/dc99c09658c45050329da8ad0bf7c27a to your computer and use it in GitHub Desktop.
PokedexServer.kt
class PokedexServer {
private val port = 50052
private var server: Server? = null
@Throws(IOException::class)
private fun start() {
//...
server = ServerBuilder.forPort(port)
.addService(PokedexImpl())
.build()
.start()
Runtime.getRuntime().addShutdownHook(object : Thread() {
override fun run() {
System.err.println("*** shutting down gRPC server since JVM is shutting down")
this@PokedexServer.stop()
System.err.println("*** server shut down")
}
})
}
private fun stop() {
server?.shutdown()
}
internal class PokedexImpl() : PokedexGrpc.PokedexImplBase() {
override fun getPokemon(request: PokedexRequest, responseObserver: StreamObserver<PokedexReply>) {
val englishName = request.englishName
// logic of find the pokemon info...
val reply = PokedexReply.newBuilder()
.setId("pokemon ID" )
.setFrenchName("pokemon French name")
.setType("pokemon type")
.setImageUrl("pokemon image URL")
.build()
responseObserver.onNext(reply)
responseObserver.onCompleted()
}
}
companion object {
@Throws(IOException::class, InterruptedException::class)
@JvmStatic
fun main(args: Array<String>) {
val server = PokedexServer()
server.start()
server.blockUntilShutdown()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment