echo.server.pb.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * DO NOT EDIT.
  3. *
  4. * Generated by the protocol buffer compiler.
  5. * Source: echo.proto
  6. *
  7. */
  8. /*
  9. *
  10. * Copyright 2017, Google Inc.
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are
  15. * met:
  16. *
  17. * * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * * Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following disclaimer
  21. * in the documentation and/or other materials provided with the
  22. * distribution.
  23. * * Neither the name of Google Inc. nor the names of its
  24. * contributors may be used to endorse or promote products derived from
  25. * this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. import Foundation
  41. import gRPC
  42. import Dispatch
  43. /// Type for errors thrown from generated server code.
  44. public enum Echo_EchoServerError : Error {
  45. case endOfStream
  46. }
  47. /// To build a server, implement a class that conforms to this protocol.
  48. public protocol Echo_EchoProvider {
  49. func get(request : Echo_EchoRequest, session : Echo_EchoGetSession) throws -> Echo_EchoResponse
  50. func expand(request : Echo_EchoRequest, session : Echo_EchoExpandSession) throws
  51. func collect(session : Echo_EchoCollectSession) throws
  52. func update(session : Echo_EchoUpdateSession) throws
  53. }
  54. /// Common properties available in each service session.
  55. public class Echo_EchoSession {
  56. fileprivate var handler : gRPC.Handler
  57. public var requestMetadata : Metadata { return handler.requestMetadata }
  58. public var statusCode : Int = 0
  59. public var statusMessage : String = "OK"
  60. public var initialMetadata : Metadata = Metadata()
  61. public var trailingMetadata : Metadata = Metadata()
  62. fileprivate init(handler:gRPC.Handler) {
  63. self.handler = handler
  64. }
  65. }
  66. // Get (Unary)
  67. public class Echo_EchoGetSession : Echo_EchoSession {
  68. private var provider : Echo_EchoProvider
  69. /// Create a session.
  70. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  71. self.provider = provider
  72. super.init(handler:handler)
  73. }
  74. /// Run the session. Internal.
  75. fileprivate func run(queue:DispatchQueue) throws {
  76. try handler.receiveMessage(initialMetadata:initialMetadata) {(requestData) in
  77. if let requestData = requestData {
  78. let requestMessage = try Echo_EchoRequest(protobuf:requestData)
  79. let replyMessage = try self.provider.get(request:requestMessage, session: self)
  80. try self.handler.sendResponse(message:replyMessage.serializeProtobuf(),
  81. statusCode:self.statusCode,
  82. statusMessage:self.statusMessage,
  83. trailingMetadata:self.trailingMetadata)
  84. }
  85. }
  86. }
  87. }
  88. // Expand (Server Streaming)
  89. public class Echo_EchoExpandSession : Echo_EchoSession {
  90. private var provider : Echo_EchoProvider
  91. /// Create a session.
  92. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  93. self.provider = provider
  94. super.init(handler:handler)
  95. }
  96. /// Send a message. Nonblocking.
  97. public func send(_ response: Echo_EchoResponse) throws {
  98. try handler.sendResponse(message:response.serializeProtobuf()) {}
  99. }
  100. /// Run the session. Internal.
  101. fileprivate func run(queue:DispatchQueue) throws {
  102. try self.handler.receiveMessage(initialMetadata:initialMetadata) {(requestData) in
  103. if let requestData = requestData {
  104. do {
  105. let requestMessage = try Echo_EchoRequest(protobuf:requestData)
  106. // to keep providers from blocking the server thread,
  107. // we dispatch them to another queue.
  108. queue.async {
  109. do {
  110. try self.provider.expand(request:requestMessage, session: self)
  111. try self.handler.sendStatus(statusCode:self.statusCode,
  112. statusMessage:self.statusMessage,
  113. trailingMetadata:self.trailingMetadata,
  114. completion:{})
  115. } catch (let error) {
  116. print("error: \(error)")
  117. }
  118. }
  119. } catch (let error) {
  120. print("error: \(error)")
  121. }
  122. }
  123. }
  124. }
  125. }
  126. // Collect (Client Streaming)
  127. public class Echo_EchoCollectSession : Echo_EchoSession {
  128. private var provider : Echo_EchoProvider
  129. /// Create a session.
  130. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  131. self.provider = provider
  132. super.init(handler:handler)
  133. }
  134. /// Receive a message. Blocks until a message is received or the client closes the connection.
  135. public func receive() throws -> Echo_EchoRequest {
  136. let latch = CountDownLatch(1)
  137. var requestMessage : Echo_EchoRequest?
  138. try self.handler.receiveMessage() {(requestData) in
  139. if let requestData = requestData {
  140. requestMessage = try? Echo_EchoRequest(protobuf:requestData)
  141. }
  142. latch.signal()
  143. }
  144. latch.wait()
  145. if requestMessage == nil {
  146. throw Echo_EchoServerError.endOfStream
  147. }
  148. return requestMessage!
  149. }
  150. /// Send a response and close the connection.
  151. public func sendAndClose(_ response: Echo_EchoResponse) throws {
  152. try self.handler.sendResponse(message:response.serializeProtobuf(),
  153. statusCode:self.statusCode,
  154. statusMessage:self.statusMessage,
  155. trailingMetadata:self.trailingMetadata)
  156. }
  157. /// Run the session. Internal.
  158. fileprivate func run(queue:DispatchQueue) throws {
  159. try self.handler.sendMetadata(initialMetadata:initialMetadata) {
  160. queue.async {
  161. do {
  162. try self.provider.collect(session:self)
  163. } catch (let error) {
  164. print("error \(error)")
  165. }
  166. }
  167. }
  168. }
  169. }
  170. // Update (Bidirectional Streaming)
  171. public class Echo_EchoUpdateSession : Echo_EchoSession {
  172. private var provider : Echo_EchoProvider
  173. /// Create a session.
  174. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  175. self.provider = provider
  176. super.init(handler:handler)
  177. }
  178. /// Receive a message. Blocks until a message is received or the client closes the connection.
  179. public func receive() throws -> Echo_EchoRequest {
  180. let latch = CountDownLatch(1)
  181. var requestMessage : Echo_EchoRequest?
  182. try self.handler.receiveMessage() {(requestData) in
  183. if let requestData = requestData {
  184. do {
  185. requestMessage = try Echo_EchoRequest(protobuf:requestData)
  186. } catch (let error) {
  187. print("error \(error)")
  188. }
  189. }
  190. latch.signal()
  191. }
  192. latch.wait()
  193. if let requestMessage = requestMessage {
  194. return requestMessage
  195. } else {
  196. throw Echo_EchoServerError.endOfStream
  197. }
  198. }
  199. /// Send a message. Nonblocking.
  200. public func send(_ response: Echo_EchoResponse) throws {
  201. try handler.sendResponse(message:response.serializeProtobuf()) {}
  202. }
  203. /// Close a connection. Blocks until the connection is closed.
  204. public func close() throws {
  205. let latch = CountDownLatch(1)
  206. try self.handler.sendStatus(statusCode:self.statusCode,
  207. statusMessage:self.statusMessage,
  208. trailingMetadata:self.trailingMetadata) {
  209. latch.signal()
  210. }
  211. latch.wait()
  212. }
  213. /// Run the session. Internal.
  214. fileprivate func run(queue:DispatchQueue) throws {
  215. try self.handler.sendMetadata(initialMetadata:initialMetadata) {
  216. queue.async {
  217. do {
  218. try self.provider.update(session:self)
  219. } catch (let error) {
  220. print("error \(error)")
  221. }
  222. }
  223. }
  224. }
  225. }
  226. /// Main server for generated service
  227. public class Echo_EchoServer {
  228. private var address: String
  229. private var server: gRPC.Server
  230. private var provider: Echo_EchoProvider?
  231. /// Create a server that accepts insecure connections.
  232. public init(address:String,
  233. provider:Echo_EchoProvider) {
  234. gRPC.initialize()
  235. self.address = address
  236. self.provider = provider
  237. self.server = gRPC.Server(address:address)
  238. }
  239. /// Create a server that accepts secure connections.
  240. public init?(address:String,
  241. certificateURL:URL,
  242. keyURL:URL,
  243. provider:Echo_EchoProvider) {
  244. gRPC.initialize()
  245. self.address = address
  246. self.provider = provider
  247. guard
  248. let certificate = try? String(contentsOf: certificateURL),
  249. let key = try? String(contentsOf: keyURL)
  250. else {
  251. return nil
  252. }
  253. self.server = gRPC.Server(address:address, key:key, certs:certificate)
  254. }
  255. /// Start the server.
  256. public func start(queue:DispatchQueue = DispatchQueue.global()) {
  257. guard let provider = self.provider else {
  258. assert(false) // the server requires a provider
  259. }
  260. server.run {(handler) in
  261. print("Server received request to " + handler.host
  262. + " calling " + handler.method
  263. + " from " + handler.caller
  264. + " with " + String(describing:handler.requestMetadata) )
  265. do {
  266. switch handler.method {
  267. case "/echo.Echo/Get":
  268. try Echo_EchoGetSession(handler:handler, provider:provider).run(queue:queue)
  269. case "/echo.Echo/Expand":
  270. try Echo_EchoExpandSession(handler:handler, provider:provider).run(queue:queue)
  271. case "/echo.Echo/Collect":
  272. try Echo_EchoCollectSession(handler:handler, provider:provider).run(queue:queue)
  273. case "/echo.Echo/Update":
  274. try Echo_EchoUpdateSession(handler:handler, provider:provider).run(queue:queue)
  275. default:
  276. break // handle unknown requests
  277. }
  278. } catch (let error) {
  279. print("Server error: \(error)")
  280. }
  281. }
  282. }
  283. }