EchoServer.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. print("SERVER RECEIVED CLOSE")
  170. // if we get an empty message (requestData == nil), we close the connection
  171. try self.handler.sendStatus(statusCode: 0,
  172. statusMessage: "OK",
  173. trailingMetadata: Metadata())
  174. {
  175. print("SERVER SENT CLOSE")
  176. }
  177. }
  178. }
  179. } catch (let error) {
  180. print(error)
  181. }
  182. }
  183. func run() {
  184. do {
  185. try self.handler.sendMetadata(initialMetadata:Metadata()) {
  186. self.waitForMessage()
  187. }
  188. } catch (let callError) {
  189. print("grpc error: \(callError)")
  190. }
  191. }
  192. }
  193. class EchoServer {
  194. private var address: String
  195. private var server: Server
  196. init(address:String, secure:Bool) {
  197. gRPC.initialize()
  198. self.address = address
  199. if secure {
  200. let certificateURL = URL(fileURLWithPath:"ssl.crt")
  201. let certificate = try! String(contentsOf: certificateURL)
  202. let keyURL = URL(fileURLWithPath:"ssl.key")
  203. let key = try! String(contentsOf: keyURL)
  204. self.server = gRPC.Server(address:address, key:key, certs:certificate)
  205. } else {
  206. self.server = gRPC.Server(address:address)
  207. }
  208. }
  209. func start() {
  210. print("Server Starting")
  211. print("GRPC version " + gRPC.version())
  212. server.run {(handler) in
  213. print("Server received request to " + handler.host
  214. + " calling " + handler.method
  215. + " from " + handler.caller)
  216. if (handler.method == "/echo.Echo/Get") {
  217. handler.session = UnarySession(handler:handler,
  218. server:EchoGetServer())
  219. handler.session.run()
  220. }
  221. else if (handler.method == "/echo.Echo/Expand") {
  222. handler.session = ServerStreamingSession(handler:handler,
  223. server:EchoExpandServer())
  224. handler.session.run()
  225. }
  226. else if (handler.method == "/echo.Echo/Collect") {
  227. handler.session = ClientStreamingSession(handler:handler,
  228. server:EchoCollectServer())
  229. handler.session.run()
  230. }
  231. else if (handler.method == "/echo.Echo/Update") {
  232. handler.session = BidiStreamingSession(handler:handler,
  233. server:EchoUpdateServer())
  234. handler.session.run()
  235. }
  236. }
  237. }
  238. }
  239. // The following code is for developer/users to edit.
  240. // Everything above these lines is intended to be preexisting or generated.
  241. class EchoGetServer : UnaryServer {
  242. func handle(message:Echo_EchoRequest) -> Echo_EchoResponse? {
  243. return Echo_EchoResponse(text: "Swift echo get: " + message.text)
  244. }
  245. }
  246. class EchoExpandServer : ServerStreamingServer {
  247. func handle(session:ServerStreamingSession, message:Echo_EchoRequest) -> Void {
  248. let parts = message.text.components(separatedBy: " ")
  249. var i = 0
  250. for part in parts {
  251. session.sendMessage(message:Echo_EchoResponse(text:"Swift echo expand (\(i)): \(part)"))
  252. i += 1
  253. sleep(1)
  254. }
  255. session.close()
  256. }
  257. }
  258. class EchoCollectServer : ClientStreamingServer {
  259. var result = ""
  260. func handle(session:ClientStreamingSession, message:Echo_EchoRequest) -> Void {
  261. if result != "" {
  262. result += " "
  263. }
  264. result += message.text
  265. }
  266. func close(session:ClientStreamingSession) {
  267. session.sendMessage(message:Echo_EchoResponse(text:"Swift echo collect: " + result))
  268. }
  269. }
  270. class EchoUpdateServer : BidiStreamingServer {
  271. var i = 0
  272. func handle(session:BidiStreamingSession, message:Echo_EchoRequest) -> Void {
  273. session.sendMessage(message:Echo_EchoResponse(text:"Swift echo update (\(i)): \(message.text)"))
  274. i += 1
  275. }
  276. }