echo.server.pb.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. public protocol Echo_EchoProvider {
  46. func get(request : Echo_EchoRequest) throws -> Echo_EchoResponse
  47. func expand(request : Echo_EchoRequest, session : Echo_EchoExpandSession) throws
  48. func collect(session : Echo_EchoCollectSession) 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. try self.handler.sendMetadata(initialMetadata:Metadata()) {
  144. queue.async {
  145. try! self.provider.collect(session:self)
  146. }
  147. }
  148. } catch (let callError) {
  149. print("grpc error: \(callError)")
  150. }
  151. }
  152. }
  153. // fully streaming
  154. public class Echo_EchoUpdateSession {
  155. var handler : gRPC.Handler
  156. var provider : Echo_EchoProvider
  157. fileprivate init(handler:gRPC.Handler, provider: Echo_EchoProvider) {
  158. self.handler = handler
  159. self.provider = provider
  160. }
  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. public func Send(_ response: Echo_EchoResponse) throws {
  181. try handler.sendResponse(message:response.serializeProtobuf()) {}
  182. }
  183. public func Close() {
  184. let done = NSCondition()
  185. try! self.handler.sendStatus(statusCode: 0,
  186. statusMessage: "OK",
  187. trailingMetadata: Metadata()) {
  188. done.lock()
  189. done.signal()
  190. done.unlock()
  191. }
  192. done.lock()
  193. done.wait()
  194. done.unlock()
  195. }
  196. fileprivate func run(queue:DispatchQueue) {
  197. do {
  198. try self.handler.sendMetadata(initialMetadata:Metadata()) {
  199. queue.async {
  200. try! self.provider.update(session:self)
  201. }
  202. }
  203. } catch (let callError) {
  204. print("grpc error: \(callError)")
  205. }
  206. }
  207. }
  208. //
  209. // main server for generated service
  210. //
  211. public class Echo_EchoServer {
  212. private var address: String
  213. private var server: gRPC.Server
  214. public var provider: Echo_EchoProvider?
  215. public 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. public init?(address:String,
  223. certificateURL:URL,
  224. keyURL:URL,
  225. provider:Echo_EchoProvider) {
  226. gRPC.initialize()
  227. self.address = address
  228. self.provider = provider
  229. guard
  230. let certificate = try? String(contentsOf: certificateURL),
  231. let key = try? String(contentsOf: keyURL)
  232. else {
  233. return nil
  234. }
  235. self.server = gRPC.Server(address:address, key:key, certs:certificate)
  236. }
  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. switch handler.method {
  246. case "/echo.Echo/Get":
  247. Echo_EchoGetSession(handler:handler, provider:provider).run(queue:queue)
  248. case "/echo.Echo/Expand":
  249. Echo_EchoExpandSession(handler:handler, provider:provider).run(queue:queue)
  250. case "/echo.Echo/Collect":
  251. Echo_EchoCollectSession(handler:handler, provider:provider).run(queue:queue)
  252. case "/echo.Echo/Update":
  253. Echo_EchoUpdateSession(handler:handler, provider:provider).run(queue:queue)
  254. default:
  255. break // handle unknown requests
  256. }
  257. }
  258. }
  259. }