EchoProvider.swift 2.7 KB

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