ClientCallClientStreaming.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 (OutputType?, ClientError?) -> Void) throws {
  35. do {
  36. try call.closeAndReceiveMessage { responseData in
  37. if let responseData = responseData,
  38. let response = try? OutputType(serializedData: responseData) {
  39. completion(response, nil)
  40. } else {
  41. completion(nil, .invalidMessageReceived)
  42. }
  43. }
  44. } catch (let error) {
  45. throw error
  46. }
  47. }
  48. public func closeAndReceive() throws -> OutputType {
  49. var returnError: ClientError?
  50. var returnResponse: OutputType!
  51. let sem = DispatchSemaphore(value: 0)
  52. do {
  53. try closeAndReceive { response, error in
  54. returnResponse = response
  55. returnError = error
  56. sem.signal()
  57. }
  58. _ = sem.wait()
  59. } catch (let error) {
  60. throw error
  61. }
  62. if let returnError = returnError {
  63. throw returnError
  64. }
  65. return returnResponse
  66. }
  67. public func waitForSendOperationsToFinish() {
  68. call.messageQueueEmpty.wait()
  69. }
  70. }
  71. /// Simple fake implementation of ClientCallClientStreamingBase that
  72. /// stores sent values for later verification and finally returns a previously-defined result.
  73. open class ClientCallClientStreamingTestStub<InputType: Message, OutputType: Message>: ClientCallClientStreaming {
  74. open class var method: String { fatalError("needs to be overridden") }
  75. open var inputs: [InputType] = []
  76. open var output: OutputType?
  77. public init() {}
  78. open func send(_ message: InputType, completion _: @escaping (Error?) -> Void) throws {
  79. inputs.append(message)
  80. }
  81. open func closeAndReceive(completion: @escaping (OutputType?, ClientError?) -> Void) throws {
  82. completion(output!, nil)
  83. }
  84. open func closeAndReceive() throws -> OutputType {
  85. return output!
  86. }
  87. open func waitForSendOperationsToFinish() {}
  88. open func cancel() {}
  89. }