echo.server.pb.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. 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 done = NSCondition()
  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. done.lock()
  143. done.signal()
  144. done.unlock()
  145. }
  146. done.lock()
  147. done.wait()
  148. done.unlock()
  149. if requestMessage == nil {
  150. throw Echo_EchoServerError.endOfStream
  151. }
  152. return requestMessage!
  153. }
  154. /// Send a response and close the connection.
  155. public func SendAndClose(_ response: Echo_EchoResponse) throws {
  156. try self.handler.sendResponse(message:response.serializeProtobuf(),
  157. statusCode:self.statusCode,
  158. statusMessage:self.statusMessage,
  159. trailingMetadata:self.trailingMetadata)
  160. }
  161. /// Run the session. Internal.
  162. fileprivate func run(queue:DispatchQueue) throws {
  163. try self.handler.sendMetadata(initialMetadata:initialMetadata) {
  164. queue.async {
  165. do {
  166. try self.provider.collect(session:self)
  167. } catch (let error) {
  168. print("error \(error)")
  169. }
  170. }
  171. }
  172. }
  173. }
  174. // Update (Bidirectional Streaming)
  175. public class Echo_EchoUpdateSession : Echo_EchoSession {
  176. private var provider : Echo_EchoProvider
  177. /// Create a session.
  178. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  179. self.provider = provider
  180. super.init(handler:handler)
  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. + " with " + String(describing:handler.requestMetadata) )
  277. do {
  278. switch handler.method {
  279. case "/echo.Echo/Get":
  280. try Echo_EchoGetSession(handler:handler, provider:provider).run(queue:queue)
  281. case "/echo.Echo/Expand":
  282. try Echo_EchoExpandSession(handler:handler, provider:provider).run(queue:queue)
  283. case "/echo.Echo/Collect":
  284. try Echo_EchoCollectSession(handler:handler, provider:provider).run(queue:queue)
  285. case "/echo.Echo/Update":
  286. try Echo_EchoUpdateSession(handler:handler, provider:provider).run(queue:queue)
  287. default:
  288. break // handle unknown requests
  289. }
  290. } catch (let error) {
  291. print("Server error: \(error)")
  292. }
  293. }
  294. }
  295. }