Generator-Server.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright 2018, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. import SwiftProtobuf
  18. import SwiftProtobufPluginLibrary
  19. extension Generator {
  20. internal func printServer() {
  21. printServerProtocol()
  22. for method in service.methods {
  23. self.method = method
  24. switch streamingType(method) {
  25. case .unary:
  26. printServerMethodUnary()
  27. case .clientStreaming:
  28. printServerMethodClientStreaming()
  29. case .serverStreaming:
  30. printServerMethodServerStreaming()
  31. case .bidirectionalStreaming:
  32. printServerMethodBidirectional()
  33. }
  34. println()
  35. }
  36. println()
  37. printServerMainClass()
  38. }
  39. private func printServerProtocol() {
  40. println("/// To build a server, implement a class that conforms to this protocol.")
  41. println("\(access) protocol \(providerName) {")
  42. indent()
  43. for method in service.methods {
  44. self.method = method
  45. switch streamingType(method) {
  46. case .unary:
  47. println("func \(methodFunctionName)(request: \(methodInputName), session: \(methodSessionName)) throws -> \(methodOutputName)")
  48. case .serverStreaming:
  49. println("func \(methodFunctionName)(request: \(methodInputName), session: \(methodSessionName)) throws")
  50. case .clientStreaming:
  51. println("func \(methodFunctionName)(session: \(methodSessionName)) throws")
  52. case .bidirectionalStreaming:
  53. println("func \(methodFunctionName)(session: \(methodSessionName)) throws")
  54. }
  55. }
  56. outdent()
  57. println("}")
  58. println()
  59. }
  60. private func printServerMainClass() {
  61. println("/// Main server for generated service")
  62. println("\(access) final class \(serverName): ServiceServer {")
  63. indent()
  64. println("private let provider: \(providerName)")
  65. println()
  66. println("\(access) init(address: String, provider: \(providerName)) {")
  67. indent()
  68. println("self.provider = provider")
  69. println("super.init(address: address)")
  70. outdent()
  71. println("}")
  72. println()
  73. println("\(access) init?(address: String, certificateURL: URL, keyURL: URL, provider: \(providerName)) {")
  74. indent()
  75. println("self.provider = provider")
  76. println("super.init(address: address, certificateURL: certificateURL, keyURL: keyURL)")
  77. outdent()
  78. println("}")
  79. println()
  80. println("/// Start the server.")
  81. println("\(access) override func handleMethod(_ method: String, handler: Handler, queue: DispatchQueue) throws -> Bool {")
  82. indent()
  83. println("let provider = self.provider")
  84. println("switch method {")
  85. for method in service.methods {
  86. self.method = method
  87. println("case \(methodPath):")
  88. indent()
  89. switch streamingType(method) {
  90. case .unary, .serverStreaming:
  91. println("try \(methodSessionName)Base(")
  92. indent()
  93. println("handler: handler,")
  94. println("providerBlock: { try provider.\(methodFunctionName)(request: $0, session: $1 as! \(methodSessionName)Base) })")
  95. indent()
  96. println(".run(queue: queue)")
  97. outdent()
  98. outdent()
  99. default:
  100. println("try \(methodSessionName)Base(")
  101. indent()
  102. println("handler: handler,")
  103. println("providerBlock: { try provider.\(methodFunctionName)(session: $0 as! \(methodSessionName)Base) })")
  104. indent()
  105. println(".run(queue: queue)")
  106. outdent()
  107. outdent()
  108. }
  109. println("return true")
  110. outdent()
  111. }
  112. println("default:")
  113. indent()
  114. println("return false")
  115. outdent()
  116. println("}")
  117. outdent()
  118. println("}")
  119. outdent()
  120. println("}")
  121. println()
  122. }
  123. private func printServerMethodUnary() {
  124. println("\(access) protocol \(methodSessionName): ServerSessionUnary {}")
  125. println()
  126. println("fileprivate final class \(methodSessionName)Base: ServerSessionUnaryBase<\(methodInputName), \(methodOutputName)>, \(methodSessionName) {}")
  127. if options.generateTestStubs {
  128. println()
  129. println("class \(methodSessionName)TestStub: ServerSessionUnaryTestStub, \(methodSessionName) {}")
  130. }
  131. }
  132. private func printServerMethodClientStreaming() {
  133. println("\(access) protocol \(methodSessionName): ServerSessionClientStreaming {")
  134. indent()
  135. println("/// Receive a message. Blocks until a message is received or the client closes the connection.")
  136. println("func receive() throws -> \(methodInputName)")
  137. println()
  138. println("/// Send a response and close the connection.")
  139. println("func sendAndClose(_ response: \(methodOutputName)) throws")
  140. outdent()
  141. println("}")
  142. println()
  143. println("fileprivate final class \(methodSessionName)Base: ServerSessionClientStreamingBase<\(methodInputName), \(methodOutputName)>, \(methodSessionName) {}")
  144. if options.generateTestStubs {
  145. println()
  146. println("class \(methodSessionName)TestStub: ServerSessionClientStreamingTestStub<\(methodInputName), \(methodOutputName)>, \(methodSessionName) {}")
  147. }
  148. }
  149. private func printServerMethodServerStreaming() {
  150. println("\(access) protocol \(methodSessionName): ServerSessionServerStreaming {")
  151. indent()
  152. println("/// Send a message. Nonblocking.")
  153. println("func send(_ response: \(methodOutputName), completion: ((Bool) -> Void)?) throws")
  154. outdent()
  155. println("}")
  156. println()
  157. println("fileprivate final class \(methodSessionName)Base: ServerSessionServerStreamingBase<\(methodInputName), \(methodOutputName)>, \(methodSessionName) {}")
  158. if options.generateTestStubs {
  159. println()
  160. println("class \(methodSessionName)TestStub: ServerSessionServerStreamingTestStub<\(methodOutputName)>, \(methodSessionName) {}")
  161. }
  162. }
  163. private func printServerMethodBidirectional() {
  164. println("\(access) protocol \(methodSessionName): ServerSessionBidirectionalStreaming {")
  165. indent()
  166. println("/// Receive a message. Blocks until a message is received or the client closes the connection.")
  167. println("func receive() throws -> \(methodInputName)")
  168. println()
  169. println("/// Send a message. Nonblocking.")
  170. println("func send(_ response: \(methodOutputName), completion: ((Bool) -> Void)?) throws")
  171. println()
  172. println("/// Close a connection. Blocks until the connection is closed.")
  173. println("func close() throws")
  174. outdent()
  175. println("}")
  176. println()
  177. println("fileprivate final class \(methodSessionName)Base: ServerSessionBidirectionalStreamingBase<\(methodInputName), \(methodOutputName)>, \(methodSessionName) {}")
  178. if options.generateTestStubs {
  179. println()
  180. println("class \(methodSessionName)TestStub: ServerSessionBidirectionalStreamingTestStub<\(methodInputName), \(methodOutputName)>, \(methodSessionName) {}")
  181. }
  182. }
  183. }