EchoProvider.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 EchoModel
  17. import GRPC
  18. import NIOCore
  19. import SwiftProtobuf
  20. public class EchoProvider: Echo_EchoProvider {
  21. public let interceptors: Echo_EchoServerInterceptorFactoryProtocol?
  22. public init(interceptors: Echo_EchoServerInterceptorFactoryProtocol? = nil) {
  23. self.interceptors = interceptors
  24. }
  25. public func get(
  26. request: Echo_EchoRequest,
  27. context: StatusOnlyCallContext
  28. ) -> EventLoopFuture<Echo_EchoResponse> {
  29. let response = Echo_EchoResponse.with {
  30. $0.text = "Swift echo get: " + request.text
  31. }
  32. return context.eventLoop.makeSucceededFuture(response)
  33. }
  34. public func expand(
  35. request: Echo_EchoRequest,
  36. context: StreamingResponseCallContext<Echo_EchoResponse>
  37. ) -> EventLoopFuture<GRPCStatus> {
  38. let responses = request.text.components(separatedBy: " ").lazy.enumerated().map { i, part in
  39. Echo_EchoResponse.with {
  40. $0.text = "Swift echo expand (\(i)): \(part)"
  41. }
  42. }
  43. context.sendResponses(responses, promise: nil)
  44. return context.eventLoop.makeSucceededFuture(.ok)
  45. }
  46. public func collect(
  47. context: UnaryResponseCallContext<Echo_EchoResponse>
  48. ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  49. var parts: [String] = []
  50. return context.eventLoop.makeSucceededFuture({ event in
  51. switch event {
  52. case let .message(message):
  53. parts.append(message.text)
  54. case .end:
  55. let response = Echo_EchoResponse.with {
  56. $0.text = "Swift echo collect: " + parts.joined(separator: " ")
  57. }
  58. context.responsePromise.succeed(response)
  59. }
  60. })
  61. }
  62. public func update(
  63. context: StreamingResponseCallContext<Echo_EchoResponse>
  64. ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  65. var count = 0
  66. return context.eventLoop.makeSucceededFuture({ event in
  67. switch event {
  68. case let .message(message):
  69. let response = Echo_EchoResponse.with {
  70. $0.text = "Swift echo update (\(count)): \(message.text)"
  71. }
  72. count += 1
  73. context.sendResponse(response, promise: nil)
  74. case .end:
  75. context.statusPromise.succeed(.ok)
  76. }
  77. })
  78. }
  79. }