echo.server.pb.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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_EchoHandler {
  40. func Get(request : Echo_EchoRequest) throws -> Echo_EchoResponse
  41. func Collect(session : Echo_EchoCollectSession) throws -> Void
  42. func Expand(request : Echo_EchoRequest, session : Echo_EchoExpandSession) throws -> Void
  43. func Update(session : Echo_EchoUpdateSession) throws -> Void
  44. }
  45. // unary
  46. public class Echo_EchoGetSession {
  47. var connection : gRPC.Handler
  48. var handler : Echo_EchoHandler
  49. fileprivate init(connection:gRPC.Handler, handler: Echo_EchoHandler) {
  50. self.connection = connection
  51. self.handler = handler
  52. }
  53. fileprivate func run(queue:DispatchQueue) {
  54. do {
  55. try connection.receiveMessage(initialMetadata:Metadata()) {(requestData) in
  56. if let requestData = requestData {
  57. let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  58. let replyMessage = try! self.handler.Get(request:requestMessage)
  59. try self.connection.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 connection : gRPC.Handler
  73. var handler : Echo_EchoHandler
  74. fileprivate init(connection:gRPC.Handler, handler: Echo_EchoHandler) {
  75. self.connection = connection
  76. self.handler = handler
  77. }
  78. public func Send(_ response: Echo_EchoResponse) throws {
  79. try! connection.sendResponse(message:response.serializeProtobuf()) {}
  80. }
  81. fileprivate func run(queue:DispatchQueue) {
  82. do {
  83. try self.connection.receiveMessage(initialMetadata:Metadata()) {(requestData) in
  84. if let requestData = requestData {
  85. let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  86. // to keep handlers from blocking the server thread,
  87. // we dispatch them to another queue.
  88. queue.async {
  89. try! self.handler.Expand(request:requestMessage, session: self)
  90. try! self.connection.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 connection : gRPC.Handler
  105. var handler : Echo_EchoHandler
  106. fileprivate init(connection:gRPC.Handler, handler: Echo_EchoHandler) {
  107. self.connection = connection
  108. self.handler = handler
  109. }
  110. public func Receive() throws -> Echo_EchoRequest {
  111. print("collect awaiting message")
  112. let done = NSCondition()
  113. var requestMessage : Echo_EchoRequest?
  114. try self.connection.receiveMessage() {(requestData) in
  115. print("collect received message")
  116. if let requestData = requestData {
  117. requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  118. }
  119. done.lock()
  120. done.signal()
  121. done.unlock()
  122. }
  123. done.lock()
  124. done.wait()
  125. done.unlock()
  126. if requestMessage == nil {
  127. throw Echo_EchoServerError.endOfStream
  128. }
  129. return requestMessage!
  130. }
  131. public func SendAndClose(_ response: Echo_EchoResponse) throws {
  132. try! self.connection.sendResponse(message:response.serializeProtobuf(),
  133. statusCode: 0,
  134. statusMessage: "OK",
  135. trailingMetadata: Metadata())
  136. }
  137. fileprivate func run(queue:DispatchQueue) {
  138. do {
  139. print("EchoCollectSession run")
  140. try self.connection.sendMetadata(initialMetadata:Metadata()) {
  141. queue.async {
  142. try! self.handler.Collect(session:self)
  143. }
  144. }
  145. } catch (let callError) {
  146. print("grpc error: \(callError)")
  147. }
  148. }
  149. }
  150. // fully streaming
  151. public class Echo_EchoUpdateSession {
  152. var connection : gRPC.Handler
  153. var handler : Echo_EchoHandler
  154. fileprivate init(connection:gRPC.Handler, handler: Echo_EchoHandler) {
  155. self.connection = connection
  156. self.handler = handler
  157. }
  158. public func Receive() throws -> Echo_EchoRequest {
  159. print("update awaiting message")
  160. let done = NSCondition()
  161. var requestMessage : Echo_EchoRequest?
  162. try self.connection.receiveMessage() {(requestData) in
  163. print("update received message")
  164. if let requestData = requestData {
  165. requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  166. }
  167. done.lock()
  168. done.signal()
  169. done.unlock()
  170. }
  171. done.lock()
  172. done.wait()
  173. done.unlock()
  174. if requestMessage == nil {
  175. throw Echo_EchoServerError.endOfStream
  176. }
  177. return requestMessage!
  178. }
  179. public func Send(_ response: Echo_EchoResponse) throws {
  180. try connection.sendResponse(message:response.serializeProtobuf()) {}
  181. }
  182. public func Close() {
  183. let done = NSCondition()
  184. try! self.connection.sendStatus(statusCode: 0,
  185. statusMessage: "OK",
  186. trailingMetadata: Metadata()) {
  187. done.lock()
  188. done.signal()
  189. done.unlock()
  190. }
  191. done.lock()
  192. done.wait()
  193. done.unlock()
  194. }
  195. fileprivate func run(queue:DispatchQueue) {
  196. do {
  197. try self.connection.sendMetadata(initialMetadata:Metadata()) {
  198. queue.async {
  199. try! self.handler.Update(session:self)
  200. }
  201. }
  202. } catch (let callError) {
  203. print("grpc error: \(callError)")
  204. }
  205. }
  206. }
  207. //
  208. // main server for generated service
  209. //
  210. public class Echo_EchoServer {
  211. private var address: String
  212. private var server: gRPC.Server
  213. public var handler: Echo_EchoHandler!
  214. public init(address:String,
  215. handler:Echo_EchoHandler) {
  216. gRPC.initialize()
  217. self.address = address
  218. self.handler = handler
  219. self.server = gRPC.Server(address:address)
  220. }
  221. public init?(address:String,
  222. certificateURL:URL,
  223. keyURL:URL,
  224. handler:Echo_EchoHandler) {
  225. gRPC.initialize()
  226. self.address = address
  227. self.handler = handler
  228. guard
  229. let certificate = try? String(contentsOf: certificateURL),
  230. let key = try? String(contentsOf: keyURL)
  231. else {
  232. return nil
  233. }
  234. self.server = gRPC.Server(address:address, key:key, certs:certificate)
  235. }
  236. public func start(queue:DispatchQueue = DispatchQueue.global()) {
  237. guard let handler = self.handler else {
  238. assert(false) // the server requires a handler
  239. }
  240. server.run {(connection) in
  241. print("Server received request to " + connection.host
  242. + " calling " + connection.method
  243. + " from " + connection.caller)
  244. switch connection.method {
  245. case "/echo.Echo/Get":
  246. Echo_EchoGetSession(connection:connection, handler:handler).run(queue:queue)
  247. case "/echo.Echo/Expand":
  248. Echo_EchoExpandSession(connection:connection, handler:handler).run(queue:queue)
  249. case "/echo.Echo/Collect":
  250. Echo_EchoCollectSession(connection:connection, handler:handler).run(queue:queue)
  251. case "/echo.Echo/Update":
  252. Echo_EchoUpdateSession(connection:connection, handler:handler).run(queue:queue)
  253. default:
  254. break // handle unknown requests
  255. }
  256. }
  257. }
  258. }