echo.server.pb.swift 9.8 KB

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