echo.server.pb.swift 10.0 KB

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