echo.grpc.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * DO NOT EDIT.
  3. *
  4. * Generated by the protocol buffer compiler.
  5. * Source: echo.proto
  6. *
  7. */
  8. /*
  9. * Copyright 2018, gRPC Authors All rights reserved.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. import Foundation
  24. import Dispatch
  25. import gRPC
  26. import SwiftProtobuf
  27. /// Type for errors thrown from generated client code.
  28. internal enum Echo_EchoClientError : Error {
  29. case endOfStream
  30. case invalidMessageReceived
  31. case error(c: CallResult)
  32. }
  33. internal protocol Echo_EchoGetCall: ClientCallUnary { }
  34. fileprivate final class Echo_EchoGetCallImpl: ClientCallUnaryImpl<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoGetCall {
  35. override class var method: String { return "/echo.Echo/Get" }
  36. }
  37. internal protocol Echo_EchoExpandCall: ClientCallServerStreamingBase {
  38. /// Call this to wait for a result. Blocking.
  39. func receive() throws -> Echo_EchoResponse
  40. /// Call this to wait for a result. Nonblocking.
  41. func receive(completion: @escaping (Echo_EchoResponse?, ClientError?) -> Void) throws
  42. }
  43. fileprivate final class Echo_EchoExpandCallImpl: ClientCallServerStreamingImpl<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoExpandCall {
  44. override class var method: String { return "/echo.Echo/Expand" }
  45. }
  46. class Echo_EchoExpandCallTestStub: ClientCallServerStreamingTestStub<Echo_EchoResponse>, Echo_EchoExpandCall {
  47. override class var method: String { return "/echo.Echo/Expand" }
  48. }
  49. internal protocol Echo_EchoCollectCall: ClientCallClientStreamingBase {
  50. /// Call this to send each message in the request stream. Nonblocking.
  51. func send(_ message: Echo_EchoRequest, errorHandler: @escaping (Error) -> Void) throws
  52. /// Call this to close the connection and wait for a response. Blocking.
  53. func closeAndReceive() throws -> Echo_EchoResponse
  54. /// Call this to close the connection and wait for a response. Nonblocking.
  55. func closeAndReceive(completion: @escaping (Echo_EchoResponse?, ClientError?) -> Void) throws
  56. }
  57. fileprivate final class Echo_EchoCollectCallImpl: ClientCallClientStreamingImpl<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectCall {
  58. override class var method: String { return "/echo.Echo/Collect" }
  59. }
  60. /// Simple fake implementation of Echo_EchoCollectCall
  61. /// stores sent values for later verification and finall returns a previously-defined result.
  62. class Echo_EchoCollectCallTestStub: ClientCallClientStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectCall {
  63. override class var method: String { return "/echo.Echo/Collect" }
  64. }
  65. internal protocol Echo_EchoUpdateCall: ClientCallBidirectionalStreamingBase {
  66. /// Call this to wait for a result. Blocking.
  67. func receive() throws -> Echo_EchoResponse
  68. /// Call this to wait for a result. Nonblocking.
  69. func receive(completion: @escaping (Echo_EchoResponse?, ClientError?) -> Void) throws
  70. /// Call this to send each message in the request stream.
  71. func send(_ message: Echo_EchoRequest, errorHandler: @escaping (Error) -> Void) throws
  72. /// Call this to close the sending connection. Blocking.
  73. func closeSend() throws
  74. /// Call this to close the sending connection. Nonblocking.
  75. func closeSend(completion: (() -> Void)?) throws
  76. }
  77. fileprivate final class Echo_EchoUpdateCallImpl: ClientCallBidirectionalStreamingImpl<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateCall {
  78. override class var method: String { return "/echo.Echo/Update" }
  79. }
  80. class Echo_EchoUpdateCallTestStub: ClientCallBidirectionalStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateCall {
  81. override class var method: String { return "/echo.Echo/Update" }
  82. }
  83. /// Instantiate Echo_EchoServiceImpl, then call methods of this protocol to make API calls.
  84. internal protocol Echo_EchoService {
  85. var channel: Channel { get }
  86. /// This metadata will be sent with all requests.
  87. var metadata: Metadata { get }
  88. /// This property allows the service host name to be overridden.
  89. /// For example, it can be used to make calls to "localhost:8080"
  90. /// appear to be to "example.com".
  91. var host : String { get }
  92. /// This property allows the service timeout to be overridden.
  93. var timeout : TimeInterval { get }
  94. /// Synchronous. Unary.
  95. func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse
  96. /// Asynchronous. Unary.
  97. func get(_ request: Echo_EchoRequest,
  98. completion: @escaping (Echo_EchoResponse?, CallResult)->()) throws -> Echo_EchoGetCall
  99. /// Asynchronous. Server-streaming.
  100. /// Send the initial message.
  101. /// Use methods on the returned object to get streamed responses.
  102. func expand(_ request: Echo_EchoRequest, completion: ((CallResult)->())?)
  103. throws -> Echo_EchoExpandCall
  104. /// Asynchronous. Client-streaming.
  105. /// Use methods on the returned object to stream messages and
  106. /// to close the connection and wait for a final response.
  107. func collect(completion: ((CallResult)->())?)
  108. throws -> Echo_EchoCollectCall
  109. /// Asynchronous. Bidirectional-streaming.
  110. /// Use methods on the returned object to stream messages,
  111. /// to wait for replies, and to close the connection.
  112. func update(completion: ((CallResult)->())?)
  113. throws -> Echo_EchoUpdateCall
  114. }
  115. internal final class Echo_EchoServiceClient: Echo_EchoService {
  116. internal private(set) var channel: Channel
  117. internal var metadata : Metadata
  118. internal var host : String {
  119. get {
  120. return self.channel.host
  121. }
  122. set {
  123. self.channel.host = newValue
  124. }
  125. }
  126. internal var timeout : TimeInterval {
  127. get {
  128. return self.channel.timeout
  129. }
  130. set {
  131. self.channel.timeout = newValue
  132. }
  133. }
  134. /// Create a client.
  135. internal init(address: String, secure: Bool = true) {
  136. gRPC.initialize()
  137. channel = Channel(address:address, secure:secure)
  138. metadata = Metadata()
  139. }
  140. /// Create a client that makes secure connections with a custom certificate and (optional) hostname.
  141. internal init(address: String, certificates: String, host: String?) {
  142. gRPC.initialize()
  143. channel = Channel(address:address, certificates:certificates, host:host)
  144. metadata = Metadata()
  145. }
  146. /// Synchronous. Unary.
  147. internal func get(_ request: Echo_EchoRequest)
  148. throws
  149. -> Echo_EchoResponse {
  150. return try Echo_EchoGetCallImpl(channel).run(request:request, metadata:metadata)
  151. }
  152. /// Asynchronous. Unary.
  153. internal func get(_ request: Echo_EchoRequest,
  154. completion: @escaping (Echo_EchoResponse?, CallResult)->())
  155. throws
  156. -> Echo_EchoGetCall {
  157. return try Echo_EchoGetCallImpl(channel).start(request:request,
  158. metadata:metadata,
  159. completion:completion)
  160. }
  161. /// Asynchronous. Server-streaming.
  162. /// Send the initial message.
  163. /// Use methods on the returned object to get streamed responses.
  164. internal func expand(_ request: Echo_EchoRequest, completion: ((CallResult)->())?)
  165. throws
  166. -> Echo_EchoExpandCall {
  167. return try Echo_EchoExpandCallImpl(channel).start(request:request, metadata:metadata, completion:completion)
  168. }
  169. /// Asynchronous. Client-streaming.
  170. /// Use methods on the returned object to stream messages and
  171. /// to close the connection and wait for a final response.
  172. internal func collect(completion: ((CallResult)->())?)
  173. throws
  174. -> Echo_EchoCollectCall {
  175. return try Echo_EchoCollectCallImpl(channel).start(metadata:metadata, completion:completion)
  176. }
  177. /// Asynchronous. Bidirectional-streaming.
  178. /// Use methods on the returned object to stream messages,
  179. /// to wait for replies, and to close the connection.
  180. internal func update(completion: ((CallResult)->())?)
  181. throws
  182. -> Echo_EchoUpdateCall {
  183. return try Echo_EchoUpdateCallImpl(channel).start(metadata:metadata, completion:completion)
  184. }
  185. }
  186. /// Simple fake implementation of Echo_EchoService that returns a previously-defined set of results
  187. /// and stores request values passed into it for later verification.
  188. /// Note: completion blocks are NOT called with this default implementation, and asynchronous unary calls are NOT implemented!
  189. class Echo_EchoServiceTestStub: Echo_EchoService {
  190. var channel: Channel { fatalError("not implemented") }
  191. var metadata = Metadata()
  192. var host = ""
  193. var timeout: TimeInterval = 0
  194. var getRequests: [Echo_EchoRequest] = []
  195. var getResponses: [Echo_EchoResponse] = []
  196. func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
  197. getRequests.append(request)
  198. defer { getResponses.removeFirst() }
  199. return getResponses.first!
  200. }
  201. func get(_ request: Echo_EchoRequest,
  202. completion: @escaping (Echo_EchoResponse?, CallResult)->()) throws -> Echo_EchoGetCall {
  203. fatalError("not implemented")
  204. }
  205. var expandRequests: [Echo_EchoRequest] = []
  206. var expandCalls: [Echo_EchoExpandCall] = []
  207. func expand(_ request: Echo_EchoRequest, completion: ((CallResult)->())?)
  208. throws -> Echo_EchoExpandCall {
  209. expandRequests.append(request)
  210. defer { expandCalls.removeFirst() }
  211. return expandCalls.first!
  212. }
  213. var collectCalls: [Echo_EchoCollectCall] = []
  214. func collect(completion: ((CallResult)->())?)
  215. throws -> Echo_EchoCollectCall {
  216. defer { collectCalls.removeFirst() }
  217. return collectCalls.first!
  218. }
  219. var updateCalls: [Echo_EchoUpdateCall] = []
  220. func update(completion: ((CallResult)->())?)
  221. throws -> Echo_EchoUpdateCall {
  222. defer { updateCalls.removeFirst() }
  223. return updateCalls.first!
  224. }
  225. }
  226. /// To build a server, implement a class that conforms to this protocol.
  227. internal protocol Echo_EchoProvider {
  228. func get(request : Echo_EchoRequest, session : Echo_EchoGetSession) throws -> Echo_EchoResponse
  229. func expand(request : Echo_EchoRequest, session : Echo_EchoExpandSession) throws
  230. func collect(session : Echo_EchoCollectSession) throws
  231. func update(session : Echo_EchoUpdateSession) throws
  232. }
  233. internal protocol Echo_EchoGetSession: ServerSessionUnary { }
  234. fileprivate final class Echo_EchoGetSessionImpl: ServerSessionUnaryImpl<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoGetSession { }
  235. class Echo_EchoGetSessionTestStub: ServerSessionUnaryTestStub, Echo_EchoGetSession { }
  236. internal protocol Echo_EchoExpandSession: ServerSessionServerStreaming {
  237. /// Send a message. Nonblocking.
  238. func send(_ response: Echo_EchoResponse, completion: ((Bool) -> Void)?) throws
  239. }
  240. fileprivate final class Echo_EchoExpandSessionImpl: ServerSessionServerStreamingImpl<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoExpandSession { }
  241. class Echo_EchoExpandSessionTestStub: ServerSessionServerStreamingTestStub<Echo_EchoResponse>, Echo_EchoExpandSession { }
  242. internal protocol Echo_EchoCollectSession: ServerSessionClientStreaming {
  243. /// Receive a message. Blocks until a message is received or the client closes the connection.
  244. func receive() throws -> Echo_EchoRequest
  245. /// Send a response and close the connection.
  246. func sendAndClose(_ response: Echo_EchoResponse) throws
  247. }
  248. fileprivate final class Echo_EchoCollectSessionImpl: ServerSessionClientStreamingImpl<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectSession { }
  249. class Echo_EchoCollectSessionTestStub: ServerSessionClientStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectSession { }
  250. internal protocol Echo_EchoUpdateSession: ServerSessionBidirectionalStreaming {
  251. /// Receive a message. Blocks until a message is received or the client closes the connection.
  252. func receive() throws -> Echo_EchoRequest
  253. /// Send a message. Nonblocking.
  254. func send(_ response: Echo_EchoResponse, completion: ((Bool) -> Void)?) throws
  255. /// Close a connection. Blocks until the connection is closed.
  256. func close() throws
  257. }
  258. fileprivate final class Echo_EchoUpdateSessionImpl: ServerSessionBidirectionalStreamingImpl<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateSession { }
  259. class Echo_EchoUpdateSessionTestStub: ServerSessionBidirectionalStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateSession { }
  260. /// Main server for generated service
  261. internal final class Echo_EchoServer {
  262. private var address: String
  263. private var server: Server
  264. private var provider: Echo_EchoProvider?
  265. /// Create a server that accepts insecure connections.
  266. internal init(address:String,
  267. provider:Echo_EchoProvider) {
  268. gRPC.initialize()
  269. self.address = address
  270. self.provider = provider
  271. self.server = Server(address:address)
  272. }
  273. /// Create a server that accepts secure connections.
  274. internal init?(address:String,
  275. certificateURL:URL,
  276. keyURL:URL,
  277. provider:Echo_EchoProvider) {
  278. gRPC.initialize()
  279. self.address = address
  280. self.provider = provider
  281. guard
  282. let certificate = try? String(contentsOf: certificateURL, encoding: .utf8),
  283. let key = try? String(contentsOf: keyURL, encoding: .utf8)
  284. else {
  285. return nil
  286. }
  287. self.server = Server(address:address, key:key, certs:certificate)
  288. }
  289. /// Start the server.
  290. internal func start(queue:DispatchQueue = DispatchQueue.global()) {
  291. guard let provider = self.provider else {
  292. fatalError() // the server requires a provider
  293. }
  294. server.run {(handler) in
  295. let unwrappedHost = handler.host ?? "(nil)"
  296. let unwrappedMethod = handler.method ?? "(nil)"
  297. let unwrappedCaller = handler.caller ?? "(nil)"
  298. print("Server received request to " + unwrappedHost
  299. + " calling " + unwrappedMethod
  300. + " from " + unwrappedCaller
  301. + " with " + handler.requestMetadata.description)
  302. do {
  303. switch unwrappedMethod {
  304. case "/echo.Echo/Get":
  305. try Echo_EchoGetSessionImpl(
  306. handler: handler,
  307. providerBlock: { try provider.get(request: $0, session: $1 as! Echo_EchoGetSessionImpl) })
  308. .run(queue:queue)
  309. case "/echo.Echo/Expand":
  310. try Echo_EchoExpandSessionImpl(
  311. handler: handler,
  312. providerBlock: { try provider.expand(request: $0, session: $1 as! Echo_EchoExpandSessionImpl) })
  313. .run(queue:queue)
  314. case "/echo.Echo/Collect":
  315. try Echo_EchoCollectSessionImpl(
  316. handler: handler,
  317. providerBlock: { try provider.collect(session: $0 as! Echo_EchoCollectSessionImpl) })
  318. .run(queue:queue)
  319. case "/echo.Echo/Update":
  320. try Echo_EchoUpdateSessionImpl(
  321. handler: handler,
  322. providerBlock: { try provider.update(session: $0 as! Echo_EchoUpdateSessionImpl) })
  323. .run(queue:queue)
  324. default:
  325. // handle unknown requests
  326. try handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
  327. try handler.sendResponse(statusCode:.unimplemented,
  328. statusMessage:"unknown method " + unwrappedMethod,
  329. trailingMetadata:Metadata())
  330. }
  331. }
  332. } catch (let error) {
  333. print("Server error: \(error)")
  334. }
  335. }
  336. }
  337. }