echo.server.pb.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * DO NOT EDIT.
  3. *
  4. * Generated by the protocol buffer compiler.
  5. * Source: echo.proto
  6. *
  7. */
  8. /*
  9. *
  10. * Copyright 2016, 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. var statusCode : Int = 0
  57. var statusMessage : String = "OK"
  58. var initialMetadata : Metadata = Metadata()
  59. var trailingMetadata : Metadata = Metadata()
  60. var receivedMetadata : Metadata = Metadata()
  61. }
  62. // Get (Unary)
  63. public class Echo_EchoGetSession : Echo_EchoSession {
  64. private var handler : gRPC.Handler
  65. private var provider : Echo_EchoProvider
  66. /// Create a session.
  67. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  68. self.handler = handler
  69. self.provider = provider
  70. }
  71. /// Run the session. Internal.
  72. fileprivate func run(queue:DispatchQueue) throws {
  73. try handler.receiveMessage(initialMetadata:initialMetadata) {(requestData) in
  74. if let requestData = requestData {
  75. let requestMessage = try Echo_EchoRequest(protobuf:requestData)
  76. let replyMessage = try self.provider.get(request:requestMessage, session: self)
  77. try self.handler.sendResponse(message:replyMessage.serializeProtobuf(),
  78. statusCode:self.statusCode,
  79. statusMessage:self.statusMessage,
  80. trailingMetadata:self.trailingMetadata)
  81. }
  82. }
  83. }
  84. }
  85. // Expand (Server Streaming)
  86. public class Echo_EchoExpandSession : Echo_EchoSession {
  87. private var handler : gRPC.Handler
  88. private var provider : Echo_EchoProvider
  89. /// Create a session.
  90. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  91. self.handler = handler
  92. self.provider = provider
  93. }
  94. /// Send a message. Nonblocking.
  95. public func Send(_ response: Echo_EchoResponse) throws {
  96. try handler.sendResponse(message:response.serializeProtobuf()) {}
  97. }
  98. /// Run the session. Internal.
  99. fileprivate func run(queue:DispatchQueue) throws {
  100. try self.handler.receiveMessage(initialMetadata:initialMetadata) {(requestData) in
  101. if let requestData = requestData {
  102. do {
  103. let requestMessage = try Echo_EchoRequest(protobuf:requestData)
  104. // to keep providers from blocking the server thread,
  105. // we dispatch them to another queue.
  106. queue.async {
  107. do {
  108. try self.provider.expand(request:requestMessage, session: self)
  109. try self.handler.sendStatus(statusCode:self.statusCode,
  110. statusMessage:self.statusMessage,
  111. trailingMetadata:self.trailingMetadata,
  112. completion:{})
  113. } catch (let error) {
  114. print("error: \(error)")
  115. }
  116. }
  117. } catch (let error) {
  118. print("error: \(error)")
  119. }
  120. }
  121. }
  122. }
  123. }
  124. // Collect (Client Streaming)
  125. public class Echo_EchoCollectSession : Echo_EchoSession {
  126. private var handler : gRPC.Handler
  127. private var provider : Echo_EchoProvider
  128. /// Create a session.
  129. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  130. self.handler = handler
  131. self.provider = provider
  132. }
  133. /// Receive a message. Blocks until a message is received or the client closes the connection.
  134. public func Receive() throws -> Echo_EchoRequest {
  135. let done = NSCondition()
  136. var requestMessage : Echo_EchoRequest?
  137. try self.handler.receiveMessage() {(requestData) in
  138. if let requestData = requestData {
  139. requestMessage = try? Echo_EchoRequest(protobuf:requestData)
  140. }
  141. done.lock()
  142. done.signal()
  143. done.unlock()
  144. }
  145. done.lock()
  146. done.wait()
  147. done.unlock()
  148. if requestMessage == nil {
  149. throw Echo_EchoServerError.endOfStream
  150. }
  151. return requestMessage!
  152. }
  153. /// Send a response and close the connection.
  154. public func SendAndClose(_ response: Echo_EchoResponse) throws {
  155. try self.handler.sendResponse(message:response.serializeProtobuf(),
  156. statusCode:self.statusCode,
  157. statusMessage:self.statusMessage,
  158. trailingMetadata:self.trailingMetadata)
  159. }
  160. /// Run the session. Internal.
  161. fileprivate func run(queue:DispatchQueue) throws {
  162. try self.handler.sendMetadata(initialMetadata:initialMetadata) {
  163. queue.async {
  164. do {
  165. try self.provider.collect(session:self)
  166. } catch (let error) {
  167. print("error \(error)")
  168. }
  169. }
  170. }
  171. }
  172. }
  173. // Update (Bidirectional Streaming)
  174. public class Echo_EchoUpdateSession : Echo_EchoSession {
  175. private var handler : gRPC.Handler
  176. private var provider : Echo_EchoProvider
  177. /// Create a session.
  178. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  179. self.handler = handler
  180. self.provider = provider
  181. }
  182. /// Receive a message. Blocks until a message is received or the client closes the connection.
  183. public func Receive() throws -> Echo_EchoRequest {
  184. let done = NSCondition()
  185. var requestMessage : Echo_EchoRequest?
  186. try self.handler.receiveMessage() {(requestData) in
  187. if let requestData = requestData {
  188. do {
  189. requestMessage = try Echo_EchoRequest(protobuf:requestData)
  190. } catch (let error) {
  191. print("error \(error)")
  192. }
  193. }
  194. done.lock()
  195. done.signal()
  196. done.unlock()
  197. }
  198. done.lock()
  199. done.wait()
  200. done.unlock()
  201. if let requestMessage = requestMessage {
  202. return requestMessage
  203. } else {
  204. throw Echo_EchoServerError.endOfStream
  205. }
  206. }
  207. /// Send a message. Nonblocking.
  208. public func Send(_ response: Echo_EchoResponse) throws {
  209. try handler.sendResponse(message:response.serializeProtobuf()) {}
  210. }
  211. /// Close a connection. Blocks until the connection is closed.
  212. public func Close() throws {
  213. let done = NSCondition()
  214. try self.handler.sendStatus(statusCode:self.statusCode,
  215. statusMessage:self.statusMessage,
  216. trailingMetadata:self.trailingMetadata) {
  217. done.lock()
  218. done.signal()
  219. done.unlock()
  220. }
  221. done.lock()
  222. done.wait()
  223. done.unlock()
  224. }
  225. /// Run the session. Internal.
  226. fileprivate func run(queue:DispatchQueue) throws {
  227. try self.handler.sendMetadata(initialMetadata:initialMetadata) {
  228. queue.async {
  229. do {
  230. try self.provider.update(session:self)
  231. } catch (let error) {
  232. print("error \(error)")
  233. }
  234. }
  235. }
  236. }
  237. }
  238. /// Main server for generated service
  239. public class Echo_EchoServer {
  240. private var address: String
  241. private var server: gRPC.Server
  242. private var provider: Echo_EchoProvider?
  243. /// Create a server that accepts insecure connections.
  244. public init(address:String,
  245. provider:Echo_EchoProvider) {
  246. gRPC.initialize()
  247. self.address = address
  248. self.provider = provider
  249. self.server = gRPC.Server(address:address)
  250. }
  251. /// Create a server that accepts secure connections.
  252. public init?(address:String,
  253. certificateURL:URL,
  254. keyURL:URL,
  255. provider:Echo_EchoProvider) {
  256. gRPC.initialize()
  257. self.address = address
  258. self.provider = provider
  259. guard
  260. let certificate = try? String(contentsOf: certificateURL),
  261. let key = try? String(contentsOf: keyURL)
  262. else {
  263. return nil
  264. }
  265. self.server = gRPC.Server(address:address, key:key, certs:certificate)
  266. }
  267. /// Start the server.
  268. public func start(queue:DispatchQueue = DispatchQueue.global()) {
  269. guard let provider = self.provider else {
  270. assert(false) // the server requires a provider
  271. }
  272. server.run {(handler) in
  273. print("Server received request to " + handler.host
  274. + " calling " + handler.method
  275. + " from " + handler.caller)
  276. do {
  277. switch handler.method {
  278. case "/echo.Echo/Get":
  279. try Echo_EchoGetSession(handler:handler, provider:provider).run(queue:queue)
  280. case "/echo.Echo/Expand":
  281. try Echo_EchoExpandSession(handler:handler, provider:provider).run(queue:queue)
  282. case "/echo.Echo/Collect":
  283. try Echo_EchoCollectSession(handler:handler, provider:provider).run(queue:queue)
  284. case "/echo.Echo/Update":
  285. try Echo_EchoUpdateSession(handler:handler, provider:provider).run(queue:queue)
  286. default:
  287. break // handle unknown requests
  288. }
  289. } catch (let error) {
  290. print("Server error: \(error)")
  291. }
  292. }
  293. }
  294. }