2
0

Generator-Server.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. }
  23. private func printServerProtocol() {
  24. println("/// To build a server, implement a class that conforms to this protocol.")
  25. println("\(access) protocol \(providerName): CallHandlerProvider {")
  26. indent()
  27. for method in service.methods {
  28. self.method = method
  29. switch streamingType(method) {
  30. case .unary:
  31. println(self.method.protoSourceComments(), newline: false)
  32. println("func \(methodFunctionName)(request: \(methodInputName), context: StatusOnlyCallContext) -> EventLoopFuture<\(methodOutputName)>")
  33. case .serverStreaming:
  34. println(self.method.protoSourceComments(), newline: false)
  35. println("func \(methodFunctionName)(request: \(methodInputName), context: StreamingResponseCallContext<\(methodOutputName)>) -> EventLoopFuture<GRPCStatus>")
  36. case .clientStreaming:
  37. println(self.method.protoSourceComments(), newline: false)
  38. println("func \(methodFunctionName)(context: UnaryResponseCallContext<\(methodOutputName)>) -> EventLoopFuture<(StreamEvent<\(methodInputName)>) -> Void>")
  39. case .bidirectionalStreaming:
  40. println(self.method.protoSourceComments(), newline: false)
  41. println("func \(methodFunctionName)(context: StreamingResponseCallContext<\(methodOutputName)>) -> EventLoopFuture<(StreamEvent<\(methodInputName)>) -> Void>")
  42. }
  43. }
  44. outdent()
  45. println("}")
  46. println()
  47. println("extension \(providerName) {")
  48. indent()
  49. println("\(access) var serviceName: String { return \"\(servicePath)\" }")
  50. println()
  51. println("/// Determines, calls and returns the appropriate request handler, depending on the request's method.")
  52. println("/// Returns nil for methods not handled by this service.")
  53. println("\(access) func handleMethod(_ methodName: String, callHandlerContext: CallHandlerContext) -> GRPCCallHandler? {")
  54. indent()
  55. println("switch methodName {")
  56. for method in service.methods {
  57. self.method = method
  58. println("case \"\(method.name)\":")
  59. indent()
  60. let callHandlerType: String
  61. switch streamingType(method) {
  62. case .unary: callHandlerType = "CallHandlerFactory.makeUnary"
  63. case .serverStreaming: callHandlerType = "CallHandlerFactory.makeServerStreaming"
  64. case .clientStreaming: callHandlerType = "CallHandlerFactory.makeClientStreaming"
  65. case .bidirectionalStreaming: callHandlerType = "CallHandlerFactory.makeBidirectionalStreaming"
  66. }
  67. println("return \(callHandlerType)(callHandlerContext: callHandlerContext) { context in")
  68. indent()
  69. switch streamingType(method) {
  70. case .unary, .serverStreaming:
  71. println("return { request in")
  72. indent()
  73. println("self.\(methodFunctionName)(request: request, context: context)")
  74. outdent()
  75. println("}")
  76. case .clientStreaming, .bidirectionalStreaming:
  77. println("return self.\(methodFunctionName)(context: context)")
  78. }
  79. outdent()
  80. println("}")
  81. outdent()
  82. println()
  83. }
  84. println("default: return nil")
  85. println("}")
  86. outdent()
  87. println("}")
  88. outdent()
  89. println("}")
  90. println()
  91. }
  92. }