echo.grpc.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //
  2. // DO NOT EDIT.
  3. //
  4. // Generated by the protocol buffer compiler.
  5. // Source: echo.proto
  6. //
  7. //
  8. // Copyright 2018, gRPC Authors All rights reserved.
  9. //
  10. // Licensed under the Apache License, Version 2.0 (the "License");
  11. // you may not use this file except in compliance with the License.
  12. // You may obtain a copy of the License at
  13. //
  14. // http://www.apache.org/licenses/LICENSE-2.0
  15. //
  16. // Unless required by applicable law or agreed to in writing, software
  17. // distributed under the License is distributed on an "AS IS" BASIS,
  18. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. // See the License for the specific language governing permissions and
  20. // limitations under the License.
  21. //
  22. import Foundation
  23. import Dispatch
  24. import SwiftGRPC
  25. import SwiftProtobuf
  26. internal protocol Echo_EchoGetCall: ClientCallUnary {}
  27. fileprivate final class Echo_EchoGetCallBase: ClientCallUnaryBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoGetCall {
  28. override class var method: String { return "/echo.Echo/Get" }
  29. }
  30. internal protocol Echo_EchoExpandCall: ClientCallServerStreaming {
  31. /// Do not call this directly, call `receive()` in the protocol extension below instead.
  32. func _receive(timeout: DispatchTime) throws -> Echo_EchoResponse?
  33. /// Call this to wait for a result. Nonblocking.
  34. func receive(completion: @escaping (ResultOrRPCError<Echo_EchoResponse?>) -> Void) throws
  35. }
  36. internal extension Echo_EchoExpandCall {
  37. /// Call this to wait for a result. Blocking.
  38. func receive(timeout: DispatchTime = .distantFuture) throws -> Echo_EchoResponse? { return try self._receive(timeout: timeout) }
  39. }
  40. fileprivate final class Echo_EchoExpandCallBase: ClientCallServerStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoExpandCall {
  41. override class var method: String { return "/echo.Echo/Expand" }
  42. }
  43. class Echo_EchoExpandCallTestStub: ClientCallServerStreamingTestStub<Echo_EchoResponse>, Echo_EchoExpandCall {
  44. override class var method: String { return "/echo.Echo/Expand" }
  45. }
  46. internal protocol Echo_EchoCollectCall: ClientCallClientStreaming {
  47. /// Send a message to the stream. Nonblocking.
  48. func send(_ message: Echo_EchoRequest, completion: @escaping (Error?) -> Void) throws
  49. /// Do not call this directly, call `send()` in the protocol extension below instead.
  50. func _send(_ message: Echo_EchoRequest, timeout: DispatchTime) throws
  51. /// Call this to close the connection and wait for a response. Blocking.
  52. func closeAndReceive() throws -> Echo_EchoResponse
  53. /// Call this to close the connection and wait for a response. Nonblocking.
  54. func closeAndReceive(completion: @escaping (ResultOrRPCError<Echo_EchoResponse>) -> Void) throws
  55. }
  56. internal extension Echo_EchoCollectCall {
  57. /// Send a message to the stream and wait for the send operation to finish. Blocking.
  58. func send(_ message: Echo_EchoRequest, timeout: DispatchTime = .distantFuture) throws { try self._send(message, timeout: timeout) }
  59. }
  60. fileprivate final class Echo_EchoCollectCallBase: ClientCallClientStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectCall {
  61. override class var method: String { return "/echo.Echo/Collect" }
  62. }
  63. /// Simple fake implementation of Echo_EchoCollectCall
  64. /// stores sent values for later verification and finall returns a previously-defined result.
  65. class Echo_EchoCollectCallTestStub: ClientCallClientStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectCall {
  66. override class var method: String { return "/echo.Echo/Collect" }
  67. }
  68. internal protocol Echo_EchoUpdateCall: ClientCallBidirectionalStreaming {
  69. /// Do not call this directly, call `receive()` in the protocol extension below instead.
  70. func _receive(timeout: DispatchTime) throws -> Echo_EchoResponse?
  71. /// Call this to wait for a result. Nonblocking.
  72. func receive(completion: @escaping (ResultOrRPCError<Echo_EchoResponse?>) -> Void) throws
  73. /// Send a message to the stream. Nonblocking.
  74. func send(_ message: Echo_EchoRequest, completion: @escaping (Error?) -> Void) throws
  75. /// Do not call this directly, call `send()` in the protocol extension below instead.
  76. func _send(_ message: Echo_EchoRequest, timeout: DispatchTime) throws
  77. /// Call this to close the sending connection. Blocking.
  78. func closeSend() throws
  79. /// Call this to close the sending connection. Nonblocking.
  80. func closeSend(completion: (() -> Void)?) throws
  81. }
  82. internal extension Echo_EchoUpdateCall {
  83. /// Call this to wait for a result. Blocking.
  84. func receive(timeout: DispatchTime = .distantFuture) throws -> Echo_EchoResponse? { return try self._receive(timeout: timeout) }
  85. }
  86. internal extension Echo_EchoUpdateCall {
  87. /// Send a message to the stream and wait for the send operation to finish. Blocking.
  88. func send(_ message: Echo_EchoRequest, timeout: DispatchTime = .distantFuture) throws { try self._send(message, timeout: timeout) }
  89. }
  90. fileprivate final class Echo_EchoUpdateCallBase: ClientCallBidirectionalStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateCall {
  91. override class var method: String { return "/echo.Echo/Update" }
  92. }
  93. class Echo_EchoUpdateCallTestStub: ClientCallBidirectionalStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateCall {
  94. override class var method: String { return "/echo.Echo/Update" }
  95. }
  96. /// Instantiate Echo_EchoServiceClient, then call methods of this protocol to make API calls.
  97. internal protocol Echo_EchoService: ServiceClient {
  98. /// Synchronous. Unary.
  99. func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse
  100. /// Asynchronous. Unary.
  101. func get(_ request: Echo_EchoRequest, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall
  102. /// Asynchronous. Server-streaming.
  103. /// Send the initial message.
  104. /// Use methods on the returned object to get streamed responses.
  105. func expand(_ request: Echo_EchoRequest, completion: ((CallResult) -> Void)?) throws -> Echo_EchoExpandCall
  106. /// Asynchronous. Client-streaming.
  107. /// Use methods on the returned object to stream messages and
  108. /// to close the connection and wait for a final response.
  109. func collect(completion: ((CallResult) -> Void)?) throws -> Echo_EchoCollectCall
  110. /// Asynchronous. Bidirectional-streaming.
  111. /// Use methods on the returned object to stream messages,
  112. /// to wait for replies, and to close the connection.
  113. func update(completion: ((CallResult) -> Void)?) throws -> Echo_EchoUpdateCall
  114. }
  115. internal final class Echo_EchoServiceClient: ServiceClientBase, Echo_EchoService {
  116. /// Synchronous. Unary.
  117. internal func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
  118. return try Echo_EchoGetCallBase(channel)
  119. .run(request: request, metadata: metadata)
  120. }
  121. /// Asynchronous. Unary.
  122. internal func get(_ request: Echo_EchoRequest, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall {
  123. return try Echo_EchoGetCallBase(channel)
  124. .start(request: request, metadata: metadata, completion: completion)
  125. }
  126. /// Asynchronous. Server-streaming.
  127. /// Send the initial message.
  128. /// Use methods on the returned object to get streamed responses.
  129. internal func expand(_ request: Echo_EchoRequest, completion: ((CallResult) -> Void)?) throws -> Echo_EchoExpandCall {
  130. return try Echo_EchoExpandCallBase(channel)
  131. .start(request: request, metadata: metadata, completion: completion)
  132. }
  133. /// Asynchronous. Client-streaming.
  134. /// Use methods on the returned object to stream messages and
  135. /// to close the connection and wait for a final response.
  136. internal func collect(completion: ((CallResult) -> Void)?) throws -> Echo_EchoCollectCall {
  137. return try Echo_EchoCollectCallBase(channel)
  138. .start(metadata: metadata, completion: completion)
  139. }
  140. /// Asynchronous. Bidirectional-streaming.
  141. /// Use methods on the returned object to stream messages,
  142. /// to wait for replies, and to close the connection.
  143. internal func update(completion: ((CallResult) -> Void)?) throws -> Echo_EchoUpdateCall {
  144. return try Echo_EchoUpdateCallBase(channel)
  145. .start(metadata: metadata, completion: completion)
  146. }
  147. }
  148. class Echo_EchoServiceTestStub: ServiceClientTestStubBase, Echo_EchoService {
  149. var getRequests: [Echo_EchoRequest] = []
  150. var getResponses: [Echo_EchoResponse] = []
  151. func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
  152. getRequests.append(request)
  153. defer { getResponses.removeFirst() }
  154. return getResponses.first!
  155. }
  156. func get(_ request: Echo_EchoRequest, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall {
  157. fatalError("not implemented")
  158. }
  159. var expandRequests: [Echo_EchoRequest] = []
  160. var expandCalls: [Echo_EchoExpandCall] = []
  161. func expand(_ request: Echo_EchoRequest, completion: ((CallResult) -> Void)?) throws -> Echo_EchoExpandCall {
  162. expandRequests.append(request)
  163. defer { expandCalls.removeFirst() }
  164. return expandCalls.first!
  165. }
  166. var collectCalls: [Echo_EchoCollectCall] = []
  167. func collect(completion: ((CallResult) -> Void)?) throws -> Echo_EchoCollectCall {
  168. defer { collectCalls.removeFirst() }
  169. return collectCalls.first!
  170. }
  171. var updateCalls: [Echo_EchoUpdateCall] = []
  172. func update(completion: ((CallResult) -> Void)?) throws -> Echo_EchoUpdateCall {
  173. defer { updateCalls.removeFirst() }
  174. return updateCalls.first!
  175. }
  176. }
  177. /// To build a server, implement a class that conforms to this protocol.
  178. internal protocol Echo_EchoProvider {
  179. func get(request: Echo_EchoRequest, session: Echo_EchoGetSession) throws -> Echo_EchoResponse
  180. func expand(request: Echo_EchoRequest, session: Echo_EchoExpandSession) throws
  181. func collect(session: Echo_EchoCollectSession) throws
  182. func update(session: Echo_EchoUpdateSession) throws
  183. }
  184. internal protocol Echo_EchoGetSession: ServerSessionUnary {}
  185. fileprivate final class Echo_EchoGetSessionBase: ServerSessionUnaryBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoGetSession {}
  186. class Echo_EchoGetSessionTestStub: ServerSessionUnaryTestStub, Echo_EchoGetSession {}
  187. internal protocol Echo_EchoExpandSession: ServerSessionServerStreaming {
  188. /// Send a message to the stream. Nonblocking.
  189. func send(_ message: Echo_EchoResponse, completion: @escaping (Error?) -> Void) throws
  190. /// Do not call this directly, call `send()` in the protocol extension below instead.
  191. func _send(_ message: Echo_EchoResponse, timeout: DispatchTime) throws
  192. /// Close the connection and send the status. Non-blocking.
  193. /// You MUST call this method once you are done processing the request.
  194. func close(withStatus status: ServerStatus, completion: (() -> Void)?) throws
  195. }
  196. internal extension Echo_EchoExpandSession {
  197. /// Send a message to the stream and wait for the send operation to finish. Blocking.
  198. func send(_ message: Echo_EchoResponse, timeout: DispatchTime = .distantFuture) throws { try self._send(message, timeout: timeout) }
  199. }
  200. fileprivate final class Echo_EchoExpandSessionBase: ServerSessionServerStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoExpandSession {}
  201. class Echo_EchoExpandSessionTestStub: ServerSessionServerStreamingTestStub<Echo_EchoResponse>, Echo_EchoExpandSession {}
  202. internal protocol Echo_EchoCollectSession: ServerSessionClientStreaming {
  203. /// Do not call this directly, call `receive()` in the protocol extension below instead.
  204. func _receive(timeout: DispatchTime) throws -> Echo_EchoRequest?
  205. /// Call this to wait for a result. Nonblocking.
  206. func receive(completion: @escaping (ResultOrRPCError<Echo_EchoRequest?>) -> Void) throws
  207. /// You MUST call one of these two methods once you are done processing the request.
  208. /// Close the connection and send a single result. Non-blocking.
  209. func sendAndClose(response: Echo_EchoResponse, status: ServerStatus, completion: (() -> Void)?) throws
  210. /// Close the connection and send an error. Non-blocking.
  211. /// Use this method if you encountered an error that makes it impossible to send a response.
  212. /// Accordingly, it does not make sense to call this method with a status of `.ok`.
  213. func sendErrorAndClose(status: ServerStatus, completion: (() -> Void)?) throws
  214. }
  215. internal extension Echo_EchoCollectSession {
  216. /// Call this to wait for a result. Blocking.
  217. func receive(timeout: DispatchTime = .distantFuture) throws -> Echo_EchoRequest? { return try self._receive(timeout: timeout) }
  218. }
  219. fileprivate final class Echo_EchoCollectSessionBase: ServerSessionClientStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectSession {}
  220. class Echo_EchoCollectSessionTestStub: ServerSessionClientStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectSession {}
  221. internal protocol Echo_EchoUpdateSession: ServerSessionBidirectionalStreaming {
  222. /// Do not call this directly, call `receive()` in the protocol extension below instead.
  223. func _receive(timeout: DispatchTime) throws -> Echo_EchoRequest?
  224. /// Call this to wait for a result. Nonblocking.
  225. func receive(completion: @escaping (ResultOrRPCError<Echo_EchoRequest?>) -> Void) throws
  226. /// Send a message to the stream. Nonblocking.
  227. func send(_ message: Echo_EchoResponse, completion: @escaping (Error?) -> Void) throws
  228. /// Do not call this directly, call `send()` in the protocol extension below instead.
  229. func _send(_ message: Echo_EchoResponse, timeout: DispatchTime) throws
  230. /// Close the connection and send the status. Non-blocking.
  231. /// You MUST call this method once you are done processing the request.
  232. func close(withStatus status: ServerStatus, completion: (() -> Void)?) throws
  233. }
  234. internal extension Echo_EchoUpdateSession {
  235. /// Call this to wait for a result. Blocking.
  236. func receive(timeout: DispatchTime = .distantFuture) throws -> Echo_EchoRequest? { return try self._receive(timeout: timeout) }
  237. }
  238. internal extension Echo_EchoUpdateSession {
  239. /// Send a message to the stream and wait for the send operation to finish. Blocking.
  240. func send(_ message: Echo_EchoResponse, timeout: DispatchTime = .distantFuture) throws { try self._send(message, timeout: timeout) }
  241. }
  242. fileprivate final class Echo_EchoUpdateSessionBase: ServerSessionBidirectionalStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateSession {}
  243. class Echo_EchoUpdateSessionTestStub: ServerSessionBidirectionalStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateSession {}
  244. /// Main server for generated service
  245. internal final class Echo_EchoServer: ServiceServer {
  246. private let provider: Echo_EchoProvider
  247. internal init(address: String, provider: Echo_EchoProvider) {
  248. self.provider = provider
  249. super.init(address: address)
  250. }
  251. internal init?(address: String, certificateURL: URL, keyURL: URL, provider: Echo_EchoProvider) {
  252. self.provider = provider
  253. super.init(address: address, certificateURL: certificateURL, keyURL: keyURL)
  254. }
  255. internal init?(address: String, certificateString: String, keyString: String, provider: Echo_EchoProvider) {
  256. self.provider = provider
  257. super.init(address: address, certificateString: certificateString, keyString: keyString)
  258. }
  259. /// Start the server.
  260. internal override func handleMethod(_ method: String, handler: Handler) throws -> Bool {
  261. let provider = self.provider
  262. switch method {
  263. case "/echo.Echo/Get":
  264. try Echo_EchoGetSessionBase(
  265. handler: handler,
  266. providerBlock: { try provider.get(request: $0, session: $1 as! Echo_EchoGetSessionBase) })
  267. .run()
  268. return true
  269. case "/echo.Echo/Expand":
  270. try Echo_EchoExpandSessionBase(
  271. handler: handler,
  272. providerBlock: { try provider.expand(request: $0, session: $1 as! Echo_EchoExpandSessionBase) })
  273. .run()
  274. return true
  275. case "/echo.Echo/Collect":
  276. try Echo_EchoCollectSessionBase(
  277. handler: handler,
  278. providerBlock: { try provider.collect(session: $0 as! Echo_EchoCollectSessionBase) })
  279. .run()
  280. return true
  281. case "/echo.Echo/Update":
  282. try Echo_EchoUpdateSessionBase(
  283. handler: handler,
  284. providerBlock: { try provider.update(session: $0 as! Echo_EchoUpdateSessionBase) })
  285. .run()
  286. return true
  287. default:
  288. return false
  289. }
  290. }
  291. }