EchoProvider.swift 2.8 KB

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