echo.server.pb.swift 10.0 KB

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