EchoServer.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. import Foundation
  34. import gRPC
  35. import Darwin // for sleep()
  36. // This seemed like a nice idea but doesn't work because
  37. // specific message types are in the protocol signatures.
  38. // There are also functions in the Session classes that depend
  39. // on specific message types.
  40. protocol UnaryServer {
  41. func handle(message:Echo_EchoRequest) -> Echo_EchoResponse?
  42. }
  43. protocol ServerStreamingServer {
  44. func handle(session:ServerStreamingSession, message:Echo_EchoRequest) -> Void
  45. }
  46. protocol ClientStreamingServer {
  47. func handle(session:ClientStreamingSession, message:Echo_EchoRequest) -> Void
  48. func close(session:ClientStreamingSession)
  49. }
  50. protocol BidiStreamingServer {
  51. func handle(session:BidiStreamingSession, message:Echo_EchoRequest) -> Void
  52. }
  53. // nonstreaming
  54. class UnarySession : Session {
  55. var handler : Handler
  56. var server : UnaryServer
  57. init(handler:Handler, server: UnaryServer) {
  58. self.handler = handler
  59. self.server = server
  60. }
  61. func run() {
  62. do {
  63. try handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
  64. if let requestData = requestData {
  65. let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  66. if let replyMessage = self.server.handle(message:requestMessage) { // calling stub
  67. try self.handler.sendResponse(message:replyMessage.serializeProtobuf(),
  68. statusCode: 0,
  69. statusMessage: "OK",
  70. trailingMetadata:Metadata())
  71. }
  72. }
  73. }
  74. } catch (let callError) {
  75. print("grpc error: \(callError)")
  76. }
  77. }
  78. }
  79. // server streaming
  80. class ServerStreamingSession : Session {
  81. var handler : Handler
  82. var server : ServerStreamingServer
  83. init(handler:Handler, server: ServerStreamingServer) {
  84. self.handler = handler
  85. self.server = server
  86. }
  87. func sendMessage(message:Echo_EchoResponse) -> Void {
  88. try! handler.sendResponse(message:message.serializeProtobuf()) {}
  89. }
  90. func close() -> Void {
  91. try! self.handler.sendStatus(statusCode:0,
  92. statusMessage:"OK",
  93. trailingMetadata:Metadata(),
  94. completion:{
  95. })
  96. }
  97. func run() {
  98. do {
  99. try handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
  100. if let requestData = requestData {
  101. let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  102. self.server.handle(session: self, message:requestMessage)
  103. }
  104. }
  105. } catch (let callError) {
  106. print("grpc error: \(callError)")
  107. }
  108. }
  109. }
  110. // client streaming
  111. class ClientStreamingSession : Session {
  112. var handler : Handler
  113. var server : ClientStreamingServer
  114. init(handler:Handler, server: ClientStreamingServer) {
  115. self.handler = handler
  116. self.server = server
  117. }
  118. func sendMessage(message:Echo_EchoResponse) -> Void {
  119. try! self.handler.sendResponse(message:message.serializeProtobuf(),
  120. statusCode: 0,
  121. statusMessage: "OK",
  122. trailingMetadata: Metadata())
  123. }
  124. func waitForMessage() {
  125. do {
  126. try handler.receiveMessage() {(requestData) in
  127. if let requestData = requestData {
  128. let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  129. self.waitForMessage()
  130. self.server.handle(session:self, message:requestMessage)
  131. } else {
  132. // if we get an empty message (requestData == nil), we close the connection
  133. self.server.close(session:self)
  134. }
  135. }
  136. } catch (let error) {
  137. print(error)
  138. }
  139. }
  140. func run() {
  141. do {
  142. try self.handler.sendMetadata(initialMetadata:Metadata()) {
  143. self.waitForMessage()
  144. }
  145. } catch (let callError) {
  146. print("grpc error: \(callError)")
  147. }
  148. }
  149. }
  150. // fully streaming
  151. class BidiStreamingSession : Session {
  152. var handler : Handler
  153. var server : BidiStreamingServer
  154. init(handler:Handler, server: BidiStreamingServer) {
  155. self.handler = handler
  156. self.server = server
  157. }
  158. func sendMessage(message:Echo_EchoResponse) -> Void {
  159. try! handler.sendResponse(message:message.serializeProtobuf()) {}
  160. }
  161. func waitForMessage() {
  162. do {
  163. try handler.receiveMessage() {(requestData) in
  164. if let requestData = requestData {
  165. let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  166. self.waitForMessage()
  167. self.server.handle(session:self, message:requestMessage)
  168. } else {
  169. // if we get an empty message (requestData == nil), we close the connection
  170. try self.handler.sendStatus(statusCode: 0,
  171. statusMessage: "OK",
  172. trailingMetadata: Metadata())
  173. {
  174. self.handler.shutdown()
  175. }
  176. }
  177. }
  178. } catch (let error) {
  179. print(error)
  180. }
  181. }
  182. func run() {
  183. do {
  184. try self.handler.sendMetadata(initialMetadata:Metadata()) {
  185. self.waitForMessage()
  186. }
  187. } catch (let callError) {
  188. print("grpc error: \(callError)")
  189. }
  190. }
  191. }
  192. class EchoServer {
  193. private var address: String
  194. private var server: Server
  195. init(address:String, secure:Bool) {
  196. gRPC.initialize()
  197. self.address = address
  198. if secure {
  199. let certificateURL = URL(fileURLWithPath:"ssl.crt")
  200. let certificate = try! String(contentsOf: certificateURL)
  201. let keyURL = URL(fileURLWithPath:"ssl.key")
  202. let key = try! String(contentsOf: keyURL)
  203. self.server = gRPC.Server(address:address, key:key, certs:certificate)
  204. } else {
  205. self.server = gRPC.Server(address:address)
  206. }
  207. }
  208. func start() {
  209. print("Server Starting")
  210. print("GRPC version " + gRPC.version())
  211. server.run {(handler) in
  212. print("Server received request to " + handler.host
  213. + " calling " + handler.method
  214. + " from " + handler.caller)
  215. if (handler.method == "/echo.Echo/Get") {
  216. handler.session = UnarySession(handler:handler,
  217. server:EchoGetServer())
  218. handler.session.run()
  219. }
  220. else if (handler.method == "/echo.Echo/Expand") {
  221. handler.session = ServerStreamingSession(handler:handler,
  222. server:EchoExpandServer())
  223. handler.session.run()
  224. }
  225. else if (handler.method == "/echo.Echo/Collect") {
  226. handler.session = ClientStreamingSession(handler:handler,
  227. server:EchoCollectServer())
  228. handler.session.run()
  229. }
  230. else if (handler.method == "/echo.Echo/Update") {
  231. handler.session = BidiStreamingSession(handler:handler,
  232. server:EchoUpdateServer())
  233. handler.session.run()
  234. }
  235. }
  236. }
  237. }
  238. // The following code is for developer/users to edit.
  239. // Everything above these lines is intended to be preexisting or generated.
  240. class EchoGetServer : UnaryServer {
  241. func handle(message:Echo_EchoRequest) -> Echo_EchoResponse? {
  242. var reply = Echo_EchoResponse()
  243. reply.text = "Swift echo get: " + message.text
  244. return reply
  245. }
  246. }
  247. class EchoExpandServer : ServerStreamingServer {
  248. func handle(session:ServerStreamingSession, message:Echo_EchoRequest) -> Void {
  249. let parts = message.text.components(separatedBy: " ")
  250. var i = 0
  251. for part in parts {
  252. var reply = Echo_EchoResponse()
  253. reply.text = "Swift echo expand (\(i)): \(part)"
  254. session.sendMessage(message:reply)
  255. i += 1
  256. sleep(1)
  257. }
  258. session.close()
  259. }
  260. }
  261. class EchoCollectServer : ClientStreamingServer {
  262. var result = ""
  263. func handle(session:ClientStreamingSession, message:Echo_EchoRequest) -> Void {
  264. if result != "" {
  265. result += " "
  266. }
  267. result += message.text
  268. }
  269. func close(session:ClientStreamingSession) {
  270. var reply = Echo_EchoResponse()
  271. reply.text = "Swift echo collect: " + result
  272. session.sendMessage(message:reply)
  273. }
  274. }
  275. class EchoUpdateServer : BidiStreamingServer {
  276. var i = 0
  277. func handle(session:BidiStreamingSession, message:Echo_EchoRequest) -> Void {
  278. var reply = Echo_EchoResponse()
  279. reply.text = "Swift echo update (\(i)): \(message.text)"
  280. session.sendMessage(message:reply)
  281. i += 1
  282. }
  283. }