EchoProvider.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 NIO
  18. import GRPC
  19. import EchoModel
  20. public class EchoProvider: Echo_EchoProvider {
  21. public init() {}
  22. public func get(request: Echo_EchoRequest, context: StatusOnlyCallContext) -> EventLoopFuture<Echo_EchoResponse> {
  23. var response = Echo_EchoResponse()
  24. response.text = "Swift echo get: " + request.text
  25. return context.eventLoop.makeSucceededFuture(response)
  26. }
  27. public func expand(request: Echo_EchoRequest, context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<GRPCStatus> {
  28. var endOfSendOperationQueue = context.eventLoop.makeSucceededFuture(())
  29. let parts = request.text.components(separatedBy: " ")
  30. for (i, part) in parts.enumerated() {
  31. var response = Echo_EchoResponse()
  32. response.text = "Swift echo expand (\(i)): \(part)"
  33. endOfSendOperationQueue = endOfSendOperationQueue.flatMap { context.sendResponse(response) }
  34. }
  35. return endOfSendOperationQueue.map { GRPCStatus.ok }
  36. }
  37. public func collect(context: UnaryResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  38. var parts: [String] = []
  39. return context.eventLoop.makeSucceededFuture({ event in
  40. switch event {
  41. case .message(let message):
  42. parts.append(message.text)
  43. case .end:
  44. var response = Echo_EchoResponse()
  45. response.text = "Swift echo collect: " + parts.joined(separator: " ")
  46. context.responsePromise.succeed(response)
  47. }
  48. })
  49. }
  50. public func update(context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  51. var endOfSendOperationQueue = context.eventLoop.makeSucceededFuture(())
  52. var count = 0
  53. return context.eventLoop.makeSucceededFuture({ event in
  54. switch event {
  55. case .message(let message):
  56. var response = Echo_EchoResponse()
  57. response.text = "Swift echo update (\(count)): \(message.text)"
  58. endOfSendOperationQueue = endOfSendOperationQueue.flatMap { context.sendResponse(response) }
  59. count += 1
  60. case .end:
  61. endOfSendOperationQueue
  62. .map { GRPCStatus.ok }
  63. .cascade(to: context.statusPromise)
  64. }
  65. })
  66. }
  67. }