server.pb.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * DO NOT EDIT.
  3. *
  4. * Generated by the protocol buffer compiler.
  5. * Source: {{ protoFile.name }}
  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. public protocol Echo_EchoProvider {
  46. func get(request : Echo_EchoRequest) throws -> Echo_EchoResponse
  47. func collect(session : Echo_EchoCollectSession) throws
  48. func expand(request : Echo_EchoRequest, session : Echo_EchoExpandSession) throws
  49. func update(session : Echo_EchoUpdateSession) throws
  50. }
  51. // unary
  52. public class Echo_EchoGetSession {
  53. var handler : gRPC.Handler
  54. var provider : Echo_EchoProvider
  55. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  56. self.handler = handler
  57. self.provider = provider
  58. }
  59. fileprivate func run(queue:DispatchQueue) {
  60. do {
  61. try handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
  62. if let requestData = requestData {
  63. let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  64. let replyMessage = try! self.provider.get(request:requestMessage)
  65. try self.handler.sendResponse(message:replyMessage.serializeProtobuf(),
  66. statusCode: 0,
  67. statusMessage: "OK",
  68. trailingMetadata:Metadata())
  69. }
  70. }
  71. } catch (let callError) {
  72. print("grpc error: \(callError)")
  73. }
  74. }
  75. }
  76. // server streaming
  77. public class Echo_EchoExpandSession {
  78. var handler : gRPC.Handler
  79. var provider : Echo_EchoProvider
  80. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  81. self.handler = handler
  82. self.provider = provider
  83. }
  84. public func Send(_ response: Echo_EchoResponse) throws {
  85. try! handler.sendResponse(message:response.serializeProtobuf()) {}
  86. }
  87. fileprivate func run(queue:DispatchQueue) {
  88. do {
  89. try self.handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
  90. if let requestData = requestData {
  91. let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  92. // to keep providers from blocking the server thread,
  93. // we dispatch them to another queue.
  94. queue.async {
  95. try! self.provider.expand(request:requestMessage, session: self)
  96. try! self.handler.sendStatus(statusCode:0,
  97. statusMessage:"OK",
  98. trailingMetadata:Metadata(),
  99. completion:{})
  100. }
  101. }
  102. }
  103. } catch (let callError) {
  104. print("grpc error: \(callError)")
  105. }
  106. }
  107. }
  108. // client streaming
  109. public class Echo_EchoCollectSession {
  110. var handler : gRPC.Handler
  111. var provider : Echo_EchoProvider
  112. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  113. self.handler = handler
  114. self.provider = provider
  115. }
  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. public func SendAndClose(_ response: Echo_EchoResponse) throws {
  136. try! self.handler.sendResponse(message:response.serializeProtobuf(),
  137. statusCode: 0,
  138. statusMessage: "OK",
  139. trailingMetadata: Metadata())
  140. }
  141. fileprivate func run(queue:DispatchQueue) {
  142. do {
  143. print("EchoCollectSession run")
  144. try self.handler.sendMetadata(initialMetadata:Metadata()) {
  145. queue.async {
  146. try! self.provider.collect(session:self)
  147. }
  148. }
  149. } catch (let callError) {
  150. print("grpc error: \(callError)")
  151. }
  152. }
  153. }
  154. // fully streaming
  155. public class Echo_EchoUpdateSession {
  156. var handler : gRPC.Handler
  157. var provider : Echo_EchoProvider
  158. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  159. self.handler = handler
  160. self.provider = provider
  161. }
  162. public func Receive() throws -> Echo_EchoRequest {
  163. let done = NSCondition()
  164. var requestMessage : Echo_EchoRequest?
  165. try self.handler.receiveMessage() {(requestData) in
  166. if let requestData = requestData {
  167. requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  168. }
  169. done.lock()
  170. done.signal()
  171. done.unlock()
  172. }
  173. done.lock()
  174. done.wait()
  175. done.unlock()
  176. if requestMessage == nil {
  177. throw Echo_EchoServerError.endOfStream
  178. }
  179. return requestMessage!
  180. }
  181. public func Send(_ response: Echo_EchoResponse) throws {
  182. try handler.sendResponse(message:response.serializeProtobuf()) {}
  183. }
  184. public func Close() {
  185. let done = NSCondition()
  186. try! self.handler.sendStatus(statusCode: 0,
  187. statusMessage: "OK",
  188. trailingMetadata: Metadata()) {
  189. done.lock()
  190. done.signal()
  191. done.unlock()
  192. }
  193. done.lock()
  194. done.wait()
  195. done.unlock()
  196. }
  197. fileprivate func run(queue:DispatchQueue) {
  198. do {
  199. try self.handler.sendMetadata(initialMetadata:Metadata()) {
  200. queue.async {
  201. try! self.provider.update(session:self)
  202. }
  203. }
  204. } catch (let callError) {
  205. print("grpc error: \(callError)")
  206. }
  207. }
  208. }
  209. //
  210. // main server for generated service
  211. //
  212. public class Echo_EchoServer {
  213. private var address: String
  214. private var server: gRPC.Server
  215. public var provider: Echo_EchoProvider?
  216. public 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. public 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),
  232. let key = try? String(contentsOf: keyURL)
  233. else {
  234. return nil
  235. }
  236. self.server = gRPC.Server(address:address, key:key, certs:certificate)
  237. }
  238. public func start(queue:DispatchQueue = DispatchQueue.global()) {
  239. guard let provider = self.provider else {
  240. assert(false) // the server requires a provider
  241. }
  242. server.run {(handler) in
  243. print("Server received request to " + handler.host
  244. + " calling " + handler.method
  245. + " from " + handler.caller)
  246. switch handler.method {
  247. case "/echo.Echo/Get":
  248. Echo_EchoGetSession(handler:handler, provider:provider).run(queue:queue)
  249. case "/echo.Echo/Expand":
  250. Echo_EchoExpandSession(handler:handler, provider:provider).run(queue:queue)
  251. case "/echo.Echo/Collect":
  252. Echo_EchoCollectSession(handler:handler, provider:provider).run(queue:queue)
  253. case "/echo.Echo/Update":
  254. Echo_EchoUpdateSession(handler:handler, provider:provider).run(queue:queue)
  255. default:
  256. break // handle unknown requests
  257. }
  258. }
  259. }
  260. }