EchoProviderNIO.swift 2.7 KB

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