EchoServer.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. class EchoGetSession : Session {
  36. var handler : Handler
  37. var server : EchoGetServer
  38. init(handler:Handler, server: EchoGetServer) {
  39. self.handler = handler
  40. self.server = server
  41. }
  42. func run() {
  43. do {
  44. try handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
  45. if let requestData = requestData {
  46. let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  47. if let replyMessage = self.server.handle(message:requestMessage) { // calling stub
  48. try self.handler.sendResponse(message:replyMessage.serializeProtobuf(),
  49. statusCode: 0,
  50. statusMessage: "OK",
  51. trailingMetadata:Metadata())
  52. }
  53. }
  54. }
  55. } catch (let callError) {
  56. print("grpc error: \(callError)")
  57. }
  58. }
  59. }
  60. class EchoUpdateSession : Session {
  61. var handler : Handler
  62. var server : EchoUpdateServer
  63. init(handler:Handler, server: EchoUpdateServer) {
  64. self.handler = handler
  65. self.server = server
  66. }
  67. func sendMessage(message:Echo_EchoResponse) -> Void {
  68. try! handler.sendResponse(message:message.serializeProtobuf()) {}
  69. }
  70. func waitForMessage() {
  71. do {
  72. try handler.receiveMessage() {(requestData) in
  73. if let requestData = requestData {
  74. let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
  75. self.waitForMessage()
  76. self.server.handle(session:self, message:requestMessage)
  77. } else {
  78. // if we get an empty message (requestData == nil), we close the connection
  79. try self.handler.sendStatus(statusCode: 0,
  80. statusMessage: "OK",
  81. trailingMetadata: Metadata())
  82. {
  83. self.handler.shutdown()
  84. }
  85. }
  86. }
  87. } catch (let error) {
  88. print(error)
  89. }
  90. }
  91. func run() {
  92. do {
  93. try self.handler.sendMetadata(initialMetadata:Metadata()) {
  94. self.waitForMessage()
  95. }
  96. } catch (let callError) {
  97. print("grpc error: \(callError)")
  98. }
  99. }
  100. }
  101. class EchoServer {
  102. private var address: String
  103. private var server: Server
  104. init(address:String, secure:Bool) {
  105. gRPC.initialize()
  106. self.address = address
  107. if secure {
  108. let certificateURL = Bundle.main.url(forResource: "ssl", withExtension: "crt")!
  109. let certificate = try! String(contentsOf: certificateURL)
  110. let keyURL = Bundle.main.url(forResource: "ssl", withExtension: "key")!
  111. let key = try! String(contentsOf: keyURL)
  112. self.server = gRPC.Server(address:address, key:key, certs:certificate)
  113. } else {
  114. self.server = gRPC.Server(address:address)
  115. }
  116. }
  117. func start() {
  118. print("Server Starting")
  119. print("GRPC version " + gRPC.version())
  120. server.run {(handler) in
  121. print("Server received request to " + handler.host
  122. + " calling " + handler.method
  123. + " from " + handler.caller)
  124. if (handler.method == "/echo.Echo/Get") {
  125. handler.session = EchoGetSession(handler:handler,
  126. server:EchoGetServer())
  127. handler.session.run()
  128. }
  129. if (handler.method == "/echo.Echo/Update") {
  130. handler.session = EchoUpdateSession(handler:handler,
  131. server:EchoUpdateServer())
  132. handler.session.run()
  133. }
  134. }
  135. }
  136. }
  137. // The following code is for developer/users to edit.
  138. // Everything above these lines is intended to be preexisting or generated.
  139. class EchoGetServer {
  140. func handle(message:Echo_EchoRequest) -> Echo_EchoResponse? {
  141. var reply = Echo_EchoResponse()
  142. reply.text = "Swift nonstreaming echo " + message.text
  143. return reply
  144. }
  145. }
  146. class EchoUpdateServer {
  147. func handle(session:EchoUpdateSession, message:Echo_EchoRequest) -> Void {
  148. var reply = Echo_EchoResponse()
  149. reply.text = "Swift streaming echo " + message.text
  150. session.sendMessage(message:reply)
  151. }
  152. }