2
0

echo.grpc.swift 15 KB

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