2
0

CallHandlerFactory.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright 2020, gRPC Authors All rights reserved.
  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. import NIO
  17. import SwiftProtobuf
  18. // We can't use a 'where' clause on 'init's to constrain the generic requirements of a type. Instead
  19. // we'll use static methods on this factory.
  20. public enum CallHandlerFactory {
  21. public typealias UnaryContext<Response> = UnaryResponseCallContext<Response>
  22. public typealias UnaryEventObserver<Request, Response> = (Request) -> EventLoopFuture<Response>
  23. public static func makeUnary<Request: Message, Response: Message>(
  24. callHandlerContext: CallHandlerContext,
  25. interceptors: [ServerInterceptor<Request, Response>] = [],
  26. eventObserverFactory: @escaping (UnaryContext<Response>)
  27. -> UnaryEventObserver<Request, Response>
  28. ) -> UnaryCallHandler<Request, Response> {
  29. return UnaryCallHandler(
  30. serializer: ProtobufSerializer(),
  31. deserializer: ProtobufDeserializer(),
  32. callHandlerContext: callHandlerContext,
  33. interceptors: interceptors,
  34. eventObserverFactory: eventObserverFactory
  35. )
  36. }
  37. public static func makeUnary<Request: GRPCPayload, Response: GRPCPayload>(
  38. callHandlerContext: CallHandlerContext,
  39. interceptors: [ServerInterceptor<Request, Response>] = [],
  40. eventObserverFactory: @escaping (UnaryContext<Response>)
  41. -> UnaryEventObserver<Request, Response>
  42. ) -> UnaryCallHandler<Request, Response> {
  43. return UnaryCallHandler(
  44. serializer: GRPCPayloadSerializer(),
  45. deserializer: GRPCPayloadDeserializer(),
  46. callHandlerContext: callHandlerContext,
  47. interceptors: interceptors,
  48. eventObserverFactory: eventObserverFactory
  49. )
  50. }
  51. public typealias ClientStreamingContext<Response> = UnaryResponseCallContext<Response>
  52. public typealias ClientStreamingEventObserver<Request> =
  53. EventLoopFuture<(StreamEvent<Request>) -> Void>
  54. public static func makeClientStreaming<Request: Message, Response: Message>(
  55. callHandlerContext: CallHandlerContext,
  56. interceptors: [ServerInterceptor<Request, Response>] = [],
  57. eventObserverFactory: @escaping (ClientStreamingContext<Response>)
  58. -> ClientStreamingEventObserver<Request>
  59. ) -> ClientStreamingCallHandler<Request, Response> {
  60. return ClientStreamingCallHandler(
  61. serializer: ProtobufSerializer(),
  62. deserializer: ProtobufDeserializer(),
  63. callHandlerContext: callHandlerContext,
  64. interceptors: interceptors,
  65. eventObserverFactory: eventObserverFactory
  66. )
  67. }
  68. public static func makeClientStreaming<Request: GRPCPayload, Response: GRPCPayload>(
  69. callHandlerContext: CallHandlerContext,
  70. interceptors: [ServerInterceptor<Request, Response>] = [],
  71. eventObserverFactory: @escaping (ClientStreamingContext<Response>)
  72. -> ClientStreamingEventObserver<Request>
  73. ) -> ClientStreamingCallHandler<Request, Response> {
  74. return ClientStreamingCallHandler(
  75. serializer: GRPCPayloadSerializer(),
  76. deserializer: GRPCPayloadDeserializer(),
  77. callHandlerContext: callHandlerContext,
  78. interceptors: interceptors,
  79. eventObserverFactory: eventObserverFactory
  80. )
  81. }
  82. public typealias ServerStreamingContext<Response> = StreamingResponseCallContext<Response>
  83. public typealias ServerStreamingEventObserver<Request> = (Request) -> EventLoopFuture<GRPCStatus>
  84. public static func makeServerStreaming<Request: Message, Response: Message>(
  85. callHandlerContext: CallHandlerContext,
  86. interceptors: [ServerInterceptor<Request, Response>] = [],
  87. eventObserverFactory: @escaping (ServerStreamingContext<Response>)
  88. -> ServerStreamingEventObserver<Request>
  89. ) -> ServerStreamingCallHandler<Request, Response> {
  90. return ServerStreamingCallHandler(
  91. serializer: ProtobufSerializer(),
  92. deserializer: ProtobufDeserializer(),
  93. callHandlerContext: callHandlerContext,
  94. interceptors: interceptors,
  95. eventObserverFactory: eventObserverFactory
  96. )
  97. }
  98. public static func makeServerStreaming<Request: GRPCPayload, Response: GRPCPayload>(
  99. callHandlerContext: CallHandlerContext,
  100. interceptors: [ServerInterceptor<Request, Response>] = [],
  101. eventObserverFactory: @escaping (ServerStreamingContext<Response>)
  102. -> ServerStreamingEventObserver<Request>
  103. ) -> ServerStreamingCallHandler<Request, Response> {
  104. return ServerStreamingCallHandler(
  105. serializer: GRPCPayloadSerializer(),
  106. deserializer: GRPCPayloadDeserializer(),
  107. callHandlerContext: callHandlerContext,
  108. interceptors: interceptors,
  109. eventObserverFactory: eventObserverFactory
  110. )
  111. }
  112. public typealias BidirectionalStreamingContext<Response> = StreamingResponseCallContext<Response>
  113. public typealias BidirectionalStreamingEventObserver<Request> =
  114. EventLoopFuture<(StreamEvent<Request>) -> Void>
  115. public static func makeBidirectionalStreaming<Request: Message, Response: Message>(
  116. callHandlerContext: CallHandlerContext,
  117. interceptors: [ServerInterceptor<Request, Response>] = [],
  118. eventObserverFactory: @escaping (BidirectionalStreamingContext<Response>)
  119. -> BidirectionalStreamingEventObserver<Request>
  120. ) -> BidirectionalStreamingCallHandler<Request, Response> {
  121. return BidirectionalStreamingCallHandler(
  122. serializer: ProtobufSerializer(),
  123. deserializer: ProtobufDeserializer(),
  124. callHandlerContext: callHandlerContext,
  125. interceptors: interceptors,
  126. eventObserverFactory: eventObserverFactory
  127. )
  128. }
  129. public static func makeBidirectionalStreaming<Request: GRPCPayload, Response: GRPCPayload>(
  130. callHandlerContext: CallHandlerContext,
  131. interceptors: [ServerInterceptor<Request, Response>] = [],
  132. eventObserverFactory: @escaping (BidirectionalStreamingContext<Response>)
  133. -> BidirectionalStreamingEventObserver<Request>
  134. ) -> BidirectionalStreamingCallHandler<Request, Response> {
  135. return BidirectionalStreamingCallHandler(
  136. serializer: GRPCPayloadSerializer(),
  137. deserializer: GRPCPayloadDeserializer(),
  138. callHandlerContext: callHandlerContext,
  139. interceptors: interceptors,
  140. eventObserverFactory: eventObserverFactory
  141. )
  142. }
  143. }