Generator-Server.swift 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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("func \(methodFunctionName)(request: \(methodInputName), context: StatusOnlyCallContext) -> EventLoopFuture<\(methodOutputName)>")
  32. case .serverStreaming:
  33. println("func \(methodFunctionName)(request: \(methodInputName), context: StreamingResponseCallContext<\(methodOutputName)>) -> EventLoopFuture<GRPCStatus>")
  34. case .clientStreaming:
  35. println("func \(methodFunctionName)(context: UnaryResponseCallContext<\(methodOutputName)>) -> EventLoopFuture<(StreamEvent<\(methodInputName)>) -> Void>")
  36. case .bidirectionalStreaming:
  37. println("func \(methodFunctionName)(context: StreamingResponseCallContext<\(methodOutputName)>) -> EventLoopFuture<(StreamEvent<\(methodInputName)>) -> Void>")
  38. }
  39. }
  40. outdent()
  41. println("}")
  42. println()
  43. println("extension \(providerName) {")
  44. indent()
  45. println("\(access) var serviceName: String { return \"\(servicePath)\" }")
  46. println()
  47. println("/// Determines, calls and returns the appropriate request handler, depending on the request's method.")
  48. println("/// Returns nil for methods not handled by this service.")
  49. println("\(access) func handleMethod(_ methodName: String, callHandlerContext: CallHandlerContext) -> GRPCCallHandler? {")
  50. indent()
  51. println("switch methodName {")
  52. for method in service.methods {
  53. self.method = method
  54. println("case \"\(method.name)\":")
  55. indent()
  56. let callHandlerType: String
  57. switch streamingType(method) {
  58. case .unary: callHandlerType = "UnaryCallHandler"
  59. case .serverStreaming: callHandlerType = "ServerStreamingCallHandler"
  60. case .clientStreaming: callHandlerType = "ClientStreamingCallHandler"
  61. case .bidirectionalStreaming: callHandlerType = "BidirectionalStreamingCallHandler"
  62. }
  63. println("return \(callHandlerType)(callHandlerContext: callHandlerContext) { context in")
  64. indent()
  65. switch streamingType(method) {
  66. case .unary, .serverStreaming:
  67. println("return { request in")
  68. indent()
  69. println("self.\(methodFunctionName)(request: request, context: context)")
  70. outdent()
  71. println("}")
  72. case .clientStreaming, .bidirectionalStreaming:
  73. println("return self.\(methodFunctionName)(context: context)")
  74. }
  75. outdent()
  76. println("}")
  77. outdent()
  78. println()
  79. }
  80. println("default: return nil")
  81. println("}")
  82. outdent()
  83. println("}")
  84. outdent()
  85. println("}")
  86. println()
  87. }
  88. }