echo.server.pb.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. var handler : gRPC.Handler
  55. 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) {
  63. do {
  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. } catch (let callError) {
  75. print("grpc error: \(callError)")
  76. }
  77. }
  78. }
  79. // Expand (Server Streaming)
  80. public class Echo_EchoExpandSession {
  81. var handler : gRPC.Handler
  82. var provider : Echo_EchoProvider
  83. /// Create a session.
  84. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  85. self.handler = handler
  86. self.provider = provider
  87. }
  88. /// Send a message. Nonblocking.
  89. public func Send(_ response: Echo_EchoResponse) throws {
  90. try! handler.sendResponse(message:response.serializeProtobuf()) {}
  91. }
  92. /// Run the session. Internal.
  93. fileprivate func run(queue:DispatchQueue) {
  94. do {
  95. try self.handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
  96. if let requestData = requestData {
  97. let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  98. // to keep providers from blocking the server thread,
  99. // we dispatch them to another queue.
  100. queue.async {
  101. try! self.provider.expand(request:requestMessage, session: self)
  102. try! self.handler.sendStatus(statusCode:0,
  103. statusMessage:"OK",
  104. trailingMetadata:Metadata(),
  105. completion:{})
  106. }
  107. }
  108. }
  109. } catch (let callError) {
  110. print("grpc error: \(callError)")
  111. }
  112. }
  113. }
  114. // Collect (Client Streaming)
  115. public class Echo_EchoCollectSession {
  116. var handler : gRPC.Handler
  117. 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) {
  152. do {
  153. try self.handler.sendMetadata(initialMetadata:Metadata()) {
  154. queue.async {
  155. try! self.provider.collect(session:self)
  156. }
  157. }
  158. } catch (let callError) {
  159. print("grpc error: \(callError)")
  160. }
  161. }
  162. }
  163. // Update (Bidirectional Streaming)
  164. public class Echo_EchoUpdateSession {
  165. var handler : gRPC.Handler
  166. 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. requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  179. }
  180. done.lock()
  181. done.signal()
  182. done.unlock()
  183. }
  184. done.lock()
  185. done.wait()
  186. done.unlock()
  187. if requestMessage == nil {
  188. throw Echo_EchoServerError.endOfStream
  189. }
  190. return requestMessage!
  191. }
  192. /// Send a message. Nonblocking.
  193. public func Send(_ response: Echo_EchoResponse) throws {
  194. try handler.sendResponse(message:response.serializeProtobuf()) {}
  195. }
  196. /// Close a connection. Blocks until the connection is closed.
  197. public func Close() {
  198. let done = NSCondition()
  199. try! self.handler.sendStatus(statusCode: 0,
  200. statusMessage: "OK",
  201. trailingMetadata: Metadata()) {
  202. done.lock()
  203. done.signal()
  204. done.unlock()
  205. }
  206. done.lock()
  207. done.wait()
  208. done.unlock()
  209. }
  210. /// Run the session. Internal.
  211. fileprivate func run(queue:DispatchQueue) {
  212. do {
  213. try self.handler.sendMetadata(initialMetadata:Metadata()) {
  214. queue.async {
  215. try! self.provider.update(session:self)
  216. }
  217. }
  218. } catch (let callError) {
  219. print("grpc error: \(callError)")
  220. }
  221. }
  222. }
  223. /// Main server for generated service
  224. public class Echo_EchoServer {
  225. private var address: String
  226. private var server: gRPC.Server
  227. public var provider: Echo_EchoProvider?
  228. /// Create a server that accepts insecure connections.
  229. public init(address:String,
  230. provider:Echo_EchoProvider) {
  231. gRPC.initialize()
  232. self.address = address
  233. self.provider = provider
  234. self.server = gRPC.Server(address:address)
  235. }
  236. /// Create a server that accepts secure connections.
  237. public init?(address:String,
  238. certificateURL:URL,
  239. keyURL:URL,
  240. provider:Echo_EchoProvider) {
  241. gRPC.initialize()
  242. self.address = address
  243. self.provider = provider
  244. guard
  245. let certificate = try? String(contentsOf: certificateURL),
  246. let key = try? String(contentsOf: keyURL)
  247. else {
  248. return nil
  249. }
  250. self.server = gRPC.Server(address:address, key:key, certs:certificate)
  251. }
  252. /// Start the server.
  253. public func start(queue:DispatchQueue = DispatchQueue.global()) {
  254. guard let provider = self.provider else {
  255. assert(false) // the server requires a provider
  256. }
  257. server.run {(handler) in
  258. print("Server received request to " + handler.host
  259. + " calling " + handler.method
  260. + " from " + handler.caller)
  261. switch handler.method {
  262. case "/echo.Echo/Get":
  263. Echo_EchoGetSession(handler:handler, provider:provider).run(queue:queue)
  264. case "/echo.Echo/Expand":
  265. Echo_EchoExpandSession(handler:handler, provider:provider).run(queue:queue)
  266. case "/echo.Echo/Collect":
  267. Echo_EchoCollectSession(handler:handler, provider:provider).run(queue:queue)
  268. case "/echo.Echo/Update":
  269. Echo_EchoUpdateSession(handler:handler, provider:provider).run(queue:queue)
  270. default:
  271. break // handle unknown requests
  272. }
  273. }
  274. }
  275. }