echo.server.pb.swift 9.4 KB

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