ClientCallClientStreaming.swift 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 Dispatch
  17. import Foundation
  18. import SwiftProtobuf
  19. public protocol ClientCallClientStreaming: ClientCall {
  20. func waitForSendOperationsToFinish()
  21. // TODO: Move the other, message type-dependent, methods into this protocol. At the moment, this is not possible,
  22. // as the protocol would then have an associated type requirement (and become pretty much unusable in the process).
  23. }
  24. open class ClientCallClientStreamingBase<InputType: Message, OutputType: Message>: ClientCallBase, ClientCallClientStreaming {
  25. /// Call this to start a call. Nonblocking.
  26. public func start(metadata: Metadata, completion: ((CallResult) -> Void)?) throws -> Self {
  27. try call.start(.clientStreaming, metadata: metadata, completion: completion)
  28. return self
  29. }
  30. public func send(_ message: InputType, completion: @escaping (Error?) -> Void) throws {
  31. let messageData = try message.serializedData()
  32. try call.sendMessage(data: messageData, completion: completion)
  33. }
  34. public func closeAndReceive(completion: @escaping (ResultOrRPCError<OutputType>) -> Void) throws {
  35. try call.closeAndReceiveMessage { callResult in
  36. guard let responseData = callResult.resultData else {
  37. completion(.error(.callError(callResult))); return
  38. }
  39. if let response = try? OutputType(serializedData: responseData) {
  40. completion(.result(response))
  41. } else {
  42. completion(.error(.invalidMessageReceived))
  43. }
  44. }
  45. }
  46. public func closeAndReceive() throws -> OutputType {
  47. var result: ResultOrRPCError<OutputType>?
  48. let sem = DispatchSemaphore(value: 0)
  49. try closeAndReceive {
  50. result = $0
  51. sem.signal()
  52. }
  53. _ = sem.wait()
  54. switch result! {
  55. case .result(let response): return response
  56. case .error(let error): throw error
  57. }
  58. }
  59. public func waitForSendOperationsToFinish() {
  60. call.messageQueueEmpty.wait()
  61. }
  62. }
  63. /// Simple fake implementation of ClientCallClientStreamingBase that
  64. /// stores sent values for later verification and finally returns a previously-defined result.
  65. open class ClientCallClientStreamingTestStub<InputType: Message, OutputType: Message>: ClientCallClientStreaming {
  66. open class var method: String { fatalError("needs to be overridden") }
  67. open var inputs: [InputType] = []
  68. open var output: OutputType?
  69. public init() {}
  70. open func send(_ message: InputType, completion _: @escaping (Error?) -> Void) throws {
  71. inputs.append(message)
  72. }
  73. open func closeAndReceive(completion: @escaping (ResultOrRPCError<OutputType>) -> Void) throws {
  74. completion(.result(output!))
  75. }
  76. open func closeAndReceive() throws -> OutputType {
  77. return output!
  78. }
  79. open func waitForSendOperationsToFinish() {}
  80. open func cancel() {}
  81. }