|
| 1 | +/* |
| 2 | + * Copyright 2017 47 Degrees, LLC. <http://www.47deg.com> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package freestyle.rpc |
| 18 | +package server.handlers |
| 19 | + |
| 20 | +import cats.data.Kleisli |
| 21 | +import freestyle.rpc.server.{GrpcServer, GrpcServerOps} |
| 22 | +import freestyle.Capture |
| 23 | +import io.grpc.{Server, ServerServiceDefinition} |
| 24 | + |
| 25 | +import scala.collection.JavaConverters._ |
| 26 | +import scala.concurrent.duration.TimeUnit |
| 27 | + |
| 28 | +class GrpcServerHandler[F[_]](implicit C: Capture[F]) |
| 29 | + extends GrpcServer.Handler[GrpcServerOps[F, ?]] { |
| 30 | + |
| 31 | + def start: GrpcServerOps[F, Server] = { |
| 32 | + |
| 33 | + Runtime.getRuntime.addShutdownHook(new Thread() { |
| 34 | + override def run(): Unit = { |
| 35 | + shutdown |
| 36 | + (): Unit |
| 37 | + } |
| 38 | + }) |
| 39 | + |
| 40 | + captureWithServer(_.start()) |
| 41 | + } |
| 42 | + |
| 43 | + def getPort: GrpcServerOps[F, Int] = captureWithServer(_.getPort) |
| 44 | + |
| 45 | + def getServices: GrpcServerOps[F, List[ServerServiceDefinition]] = |
| 46 | + captureWithServer(_.getServices.asScala.toList) |
| 47 | + |
| 48 | + def getImmutableServices: GrpcServerOps[F, List[ServerServiceDefinition]] = |
| 49 | + captureWithServer(_.getImmutableServices.asScala.toList) |
| 50 | + |
| 51 | + def getMutableServices: GrpcServerOps[F, List[ServerServiceDefinition]] = |
| 52 | + captureWithServer(_.getMutableServices.asScala.toList) |
| 53 | + |
| 54 | + def shutdown: GrpcServerOps[F, Server] = captureWithServer(_.shutdown()) |
| 55 | + |
| 56 | + def shutdownNow: GrpcServerOps[F, Server] = captureWithServer(_.shutdownNow()) |
| 57 | + |
| 58 | + def isShutdown: GrpcServerOps[F, Boolean] = captureWithServer(_.isShutdown) |
| 59 | + |
| 60 | + def isTerminated: GrpcServerOps[F, Boolean] = captureWithServer(_.isTerminated) |
| 61 | + |
| 62 | + def awaitTerminationTimeout(timeout: Long, unit: TimeUnit): GrpcServerOps[F, Boolean] = |
| 63 | + captureWithServer(_.awaitTermination(timeout, unit)) |
| 64 | + |
| 65 | + def awaitTermination: GrpcServerOps[F, Unit] = captureWithServer(_.awaitTermination()) |
| 66 | + |
| 67 | + private[this] def captureWithServer[A](f: Server => A): GrpcServerOps[F, A] = |
| 68 | + Kleisli(s => C.capture(f(s))) |
| 69 | + |
| 70 | +} |
0 commit comments