echo.server.pb.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. // all code that follows is to-be-generated
  34. import Foundation
  35. import gRPC
  36. public enum Echo_EchoServerError : Error {
  37. case endOfStream
  38. }
  39. public protocol Echo_EchoProvider {
  40. func get(request : Echo_EchoRequest) throws -> Echo_EchoResponse
  41. func collect(session : Echo_EchoCollectSession) throws
  42. func expand(request : Echo_EchoRequest, session : Echo_EchoExpandSession) throws
  43. func update(session : Echo_EchoUpdateSession) throws
  44. }
  45. // unary
  46. public class Echo_EchoGetSession {
  47. var handler : gRPC.Handler
  48. var provider : Echo_EchoProvider
  49. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  50. self.handler = handler
  51. self.provider = provider
  52. }
  53. fileprivate func run(queue:DispatchQueue) {
  54. do {
  55. try handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
  56. if let requestData = requestData {
  57. let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  58. let replyMessage = try! self.provider.get(request:requestMessage)
  59. try self.handler.sendResponse(message:replyMessage.serializeProtobuf(),
  60. statusCode: 0,
  61. statusMessage: "OK",
  62. trailingMetadata:Metadata())
  63. }
  64. }
  65. } catch (let callError) {
  66. print("grpc error: \(callError)")
  67. }
  68. }
  69. }
  70. // server streaming
  71. public class Echo_EchoExpandSession {
  72. var handler : gRPC.Handler
  73. var provider : Echo_EchoProvider
  74. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  75. self.handler = handler
  76. self.provider = provider
  77. }
  78. public func Send(_ response: Echo_EchoResponse) throws {
  79. try! handler.sendResponse(message:response.serializeProtobuf()) {}
  80. }
  81. fileprivate func run(queue:DispatchQueue) {
  82. do {
  83. try self.handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
  84. if let requestData = requestData {
  85. let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  86. // to keep providers from blocking the server thread,
  87. // we dispatch them to another queue.
  88. queue.async {
  89. try! self.provider.expand(request:requestMessage, session: self)
  90. try! self.handler.sendStatus(statusCode:0,
  91. statusMessage:"OK",
  92. trailingMetadata:Metadata(),
  93. completion:{})
  94. }
  95. }
  96. }
  97. } catch (let callError) {
  98. print("grpc error: \(callError)")
  99. }
  100. }
  101. }
  102. // client streaming
  103. public class Echo_EchoCollectSession {
  104. var handler : gRPC.Handler
  105. var provider : Echo_EchoProvider
  106. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  107. self.handler = handler
  108. self.provider = provider
  109. }
  110. public func Receive() throws -> Echo_EchoRequest {
  111. let done = NSCondition()
  112. var requestMessage : Echo_EchoRequest?
  113. try self.handler.receiveMessage() {(requestData) in
  114. if let requestData = requestData {
  115. requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  116. }
  117. done.lock()
  118. done.signal()
  119. done.unlock()
  120. }
  121. done.lock()
  122. done.wait()
  123. done.unlock()
  124. if requestMessage == nil {
  125. throw Echo_EchoServerError.endOfStream
  126. }
  127. return requestMessage!
  128. }
  129. public func SendAndClose(_ response: Echo_EchoResponse) throws {
  130. try! self.handler.sendResponse(message:response.serializeProtobuf(),
  131. statusCode: 0,
  132. statusMessage: "OK",
  133. trailingMetadata: Metadata())
  134. }
  135. fileprivate func run(queue:DispatchQueue) {
  136. do {
  137. print("EchoCollectSession run")
  138. try self.handler.sendMetadata(initialMetadata:Metadata()) {
  139. queue.async {
  140. try! self.provider.collect(session:self)
  141. }
  142. }
  143. } catch (let callError) {
  144. print("grpc error: \(callError)")
  145. }
  146. }
  147. }
  148. // fully streaming
  149. public class Echo_EchoUpdateSession {
  150. var handler : gRPC.Handler
  151. var provider : Echo_EchoProvider
  152. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  153. self.handler = handler
  154. self.provider = provider
  155. }
  156. public func Receive() throws -> Echo_EchoRequest {
  157. let done = NSCondition()
  158. var requestMessage : Echo_EchoRequest?
  159. try self.handler.receiveMessage() {(requestData) in
  160. if let requestData = requestData {
  161. requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  162. }
  163. done.lock()
  164. done.signal()
  165. done.unlock()
  166. }
  167. done.lock()
  168. done.wait()
  169. done.unlock()
  170. if requestMessage == nil {
  171. throw Echo_EchoServerError.endOfStream
  172. }
  173. return requestMessage!
  174. }
  175. public func Send(_ response: Echo_EchoResponse) throws {
  176. try handler.sendResponse(message:response.serializeProtobuf()) {}
  177. }
  178. public func Close() {
  179. let done = NSCondition()
  180. try! self.handler.sendStatus(statusCode: 0,
  181. statusMessage: "OK",
  182. trailingMetadata: Metadata()) {
  183. done.lock()
  184. done.signal()
  185. done.unlock()
  186. }
  187. done.lock()
  188. done.wait()
  189. done.unlock()
  190. }
  191. fileprivate func run(queue:DispatchQueue) {
  192. do {
  193. try self.handler.sendMetadata(initialMetadata:Metadata()) {
  194. queue.async {
  195. try! self.provider.update(session:self)
  196. }
  197. }
  198. } catch (let callError) {
  199. print("grpc error: \(callError)")
  200. }
  201. }
  202. }
  203. //
  204. // main server for generated service
  205. //
  206. public class Echo_EchoServer {
  207. private var address: String
  208. private var server: gRPC.Server
  209. public var provider: Echo_EchoProvider?
  210. public init(address:String,
  211. provider:Echo_EchoProvider) {
  212. gRPC.initialize()
  213. self.address = address
  214. self.provider = provider
  215. self.server = gRPC.Server(address:address)
  216. }
  217. public init?(address:String,
  218. certificateURL:URL,
  219. keyURL:URL,
  220. provider:Echo_EchoProvider) {
  221. gRPC.initialize()
  222. self.address = address
  223. self.provider = provider
  224. guard
  225. let certificate = try? String(contentsOf: certificateURL),
  226. let key = try? String(contentsOf: keyURL)
  227. else {
  228. return nil
  229. }
  230. self.server = gRPC.Server(address:address, key:key, certs:certificate)
  231. }
  232. public func start(queue:DispatchQueue = DispatchQueue.global()) {
  233. guard let provider = self.provider else {
  234. assert(false) // the server requires a provider
  235. }
  236. server.run {(handler) in
  237. print("Server received request to " + handler.host
  238. + " calling " + handler.method
  239. + " from " + handler.caller)
  240. switch handler.method {
  241. case "/echo.Echo/Get":
  242. Echo_EchoGetSession(handler:handler, provider:provider).run(queue:queue)
  243. case "/echo.Echo/Expand":
  244. Echo_EchoExpandSession(handler:handler, provider:provider).run(queue:queue)
  245. case "/echo.Echo/Collect":
  246. Echo_EchoCollectSession(handler:handler, provider:provider).run(queue:queue)
  247. case "/echo.Echo/Update":
  248. Echo_EchoUpdateSession(handler:handler, provider:provider).run(queue:queue)
  249. default:
  250. break // handle unknown requests
  251. }
  252. }
  253. }
  254. }