echo.server.pb.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * DO NOT EDIT.
  3. *
  4. * Generated by the protocol buffer compiler.
  5. * Source: echo.proto
  6. *
  7. */
  8. /*
  9. * Copyright 2017, gRPC Authors All rights reserved.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. import Foundation
  24. import Dispatch
  25. import gRPC
  26. /// Type for errors thrown from generated server code.
  27. internal enum Echo_EchoServerError : Error {
  28. case endOfStream
  29. }
  30. /// To build a server, implement a class that conforms to this protocol.
  31. internal protocol Echo_EchoProvider {
  32. func get(request : Echo_EchoRequest, session : Echo_EchoGetSession) throws -> Echo_EchoResponse
  33. func expand(request : Echo_EchoRequest, session : Echo_EchoExpandSession) throws
  34. func collect(session : Echo_EchoCollectSession) throws
  35. func update(session : Echo_EchoUpdateSession) throws
  36. }
  37. /// Common properties available in each service session.
  38. internal class Echo_EchoSession {
  39. fileprivate var handler : gRPC.Handler
  40. internal var requestMetadata : Metadata { return handler.requestMetadata }
  41. internal var statusCode : Int = 0
  42. internal var statusMessage : String = "OK"
  43. internal var initialMetadata : Metadata = Metadata()
  44. internal var trailingMetadata : Metadata = Metadata()
  45. fileprivate init(handler:gRPC.Handler) {
  46. self.handler = handler
  47. }
  48. }
  49. // Get (Unary)
  50. internal class Echo_EchoGetSession : Echo_EchoSession {
  51. private var provider : Echo_EchoProvider
  52. /// Create a session.
  53. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  54. self.provider = provider
  55. super.init(handler:handler)
  56. }
  57. /// Run the session. Internal.
  58. fileprivate func run(queue:DispatchQueue) throws {
  59. try handler.receiveMessage(initialMetadata:initialMetadata) {(requestData) in
  60. if let requestData = requestData {
  61. let requestMessage = try Echo_EchoRequest(serializedData:requestData)
  62. let replyMessage = try self.provider.get(request:requestMessage, session: self)
  63. try self.handler.sendResponse(message:replyMessage.serializedData(),
  64. statusCode:self.statusCode,
  65. statusMessage:self.statusMessage,
  66. trailingMetadata:self.trailingMetadata)
  67. }
  68. }
  69. }
  70. }
  71. // Expand (Server Streaming)
  72. internal class Echo_EchoExpandSession : Echo_EchoSession {
  73. private var provider : Echo_EchoProvider
  74. /// Create a session.
  75. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  76. self.provider = provider
  77. super.init(handler:handler)
  78. }
  79. /// Send a message. Nonblocking.
  80. internal func send(_ response: Echo_EchoResponse) throws {
  81. try handler.sendResponse(message:response.serializedData()) {}
  82. }
  83. /// Run the session. Internal.
  84. fileprivate func run(queue:DispatchQueue) throws {
  85. try self.handler.receiveMessage(initialMetadata:initialMetadata) {(requestData) in
  86. if let requestData = requestData {
  87. do {
  88. let requestMessage = try Echo_EchoRequest(serializedData:requestData)
  89. // to keep providers from blocking the server thread,
  90. // we dispatch them to another queue.
  91. queue.async {
  92. do {
  93. try self.provider.expand(request:requestMessage, session: self)
  94. try self.handler.sendStatus(statusCode:self.statusCode,
  95. statusMessage:self.statusMessage,
  96. trailingMetadata:self.trailingMetadata,
  97. completion:{})
  98. } catch (let error) {
  99. print("error: \(error)")
  100. }
  101. }
  102. } catch (let error) {
  103. print("error: \(error)")
  104. }
  105. }
  106. }
  107. }
  108. }
  109. // Collect (Client Streaming)
  110. internal class Echo_EchoCollectSession : Echo_EchoSession {
  111. private var provider : Echo_EchoProvider
  112. /// Create a session.
  113. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  114. self.provider = provider
  115. super.init(handler:handler)
  116. }
  117. /// Receive a message. Blocks until a message is received or the client closes the connection.
  118. internal func receive() throws -> Echo_EchoRequest {
  119. let sem = DispatchSemaphore(value: 0)
  120. var requestMessage : Echo_EchoRequest?
  121. try self.handler.receiveMessage() {(requestData) in
  122. if let requestData = requestData {
  123. requestMessage = try? Echo_EchoRequest(serializedData:requestData)
  124. }
  125. sem.signal()
  126. }
  127. _ = sem.wait(timeout: DispatchTime.distantFuture)
  128. if requestMessage == nil {
  129. throw Echo_EchoServerError.endOfStream
  130. }
  131. return requestMessage!
  132. }
  133. /// Send a response and close the connection.
  134. internal func sendAndClose(_ response: Echo_EchoResponse) throws {
  135. try self.handler.sendResponse(message:response.serializedData(),
  136. statusCode:self.statusCode,
  137. statusMessage:self.statusMessage,
  138. trailingMetadata:self.trailingMetadata)
  139. }
  140. /// Run the session. Internal.
  141. fileprivate func run(queue:DispatchQueue) throws {
  142. try self.handler.sendMetadata(initialMetadata:initialMetadata) {
  143. queue.async {
  144. do {
  145. try self.provider.collect(session:self)
  146. } catch (let error) {
  147. print("error \(error)")
  148. }
  149. }
  150. }
  151. }
  152. }
  153. // Update (Bidirectional Streaming)
  154. internal class Echo_EchoUpdateSession : Echo_EchoSession {
  155. private var provider : Echo_EchoProvider
  156. /// Create a session.
  157. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  158. self.provider = provider
  159. super.init(handler:handler)
  160. }
  161. /// Receive a message. Blocks until a message is received or the client closes the connection.
  162. internal func receive() throws -> Echo_EchoRequest {
  163. let sem = DispatchSemaphore(value: 0)
  164. var requestMessage : Echo_EchoRequest?
  165. try self.handler.receiveMessage() {(requestData) in
  166. if let requestData = requestData {
  167. do {
  168. requestMessage = try Echo_EchoRequest(serializedData:requestData)
  169. } catch (let error) {
  170. print("error \(error)")
  171. }
  172. }
  173. sem.signal()
  174. }
  175. _ = sem.wait(timeout: DispatchTime.distantFuture)
  176. if let requestMessage = requestMessage {
  177. return requestMessage
  178. } else {
  179. throw Echo_EchoServerError.endOfStream
  180. }
  181. }
  182. /// Send a message. Nonblocking.
  183. internal func send(_ response: Echo_EchoResponse) throws {
  184. try handler.sendResponse(message:response.serializedData()) {}
  185. }
  186. /// Close a connection. Blocks until the connection is closed.
  187. internal func close() throws {
  188. let sem = DispatchSemaphore(value: 0)
  189. try self.handler.sendStatus(statusCode:self.statusCode,
  190. statusMessage:self.statusMessage,
  191. trailingMetadata:self.trailingMetadata) {
  192. sem.signal()
  193. }
  194. _ = sem.wait(timeout: DispatchTime.distantFuture)
  195. }
  196. /// Run the session. Internal.
  197. fileprivate func run(queue:DispatchQueue) throws {
  198. try self.handler.sendMetadata(initialMetadata:initialMetadata) {
  199. queue.async {
  200. do {
  201. try self.provider.update(session:self)
  202. } catch (let error) {
  203. print("error \(error)")
  204. }
  205. }
  206. }
  207. }
  208. }
  209. /// Main server for generated service
  210. internal class Echo_EchoServer {
  211. private var address: String
  212. private var server: gRPC.Server
  213. private var provider: Echo_EchoProvider?
  214. /// Create a server that accepts insecure connections.
  215. internal init(address:String,
  216. provider:Echo_EchoProvider) {
  217. gRPC.initialize()
  218. self.address = address
  219. self.provider = provider
  220. self.server = gRPC.Server(address:address)
  221. }
  222. /// Create a server that accepts secure connections.
  223. internal init?(address:String,
  224. certificateURL:URL,
  225. keyURL:URL,
  226. provider:Echo_EchoProvider) {
  227. gRPC.initialize()
  228. self.address = address
  229. self.provider = provider
  230. guard
  231. let certificate = try? String(contentsOf: certificateURL, encoding: .utf8),
  232. let key = try? String(contentsOf: keyURL, encoding: .utf8)
  233. else {
  234. return nil
  235. }
  236. self.server = gRPC.Server(address:address, key:key, certs:certificate)
  237. }
  238. /// Start the server.
  239. internal func start(queue:DispatchQueue = DispatchQueue.global()) {
  240. guard let provider = self.provider else {
  241. assert(false) // the server requires a provider
  242. }
  243. server.run {(handler) in
  244. print("Server received request to " + handler.host
  245. + " calling " + handler.method
  246. + " from " + handler.caller
  247. + " with " + String(describing:handler.requestMetadata) )
  248. do {
  249. switch handler.method {
  250. case "/echo.Echo/Get":
  251. try Echo_EchoGetSession(handler:handler, provider:provider).run(queue:queue)
  252. case "/echo.Echo/Expand":
  253. try Echo_EchoExpandSession(handler:handler, provider:provider).run(queue:queue)
  254. case "/echo.Echo/Collect":
  255. try Echo_EchoCollectSession(handler:handler, provider:provider).run(queue:queue)
  256. case "/echo.Echo/Update":
  257. try Echo_EchoUpdateSession(handler:handler, provider:provider).run(queue:queue)
  258. default:
  259. break // handle unknown requests
  260. }
  261. } catch (let error) {
  262. print("Server error: \(error)")
  263. }
  264. }
  265. }
  266. }