echo.server.pb.swift 9.9 KB

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