ServerSessionServerStreaming.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 ServerSessionServerStreaming: ServerSession {
  20. func waitForSendOperationsToFinish()
  21. }
  22. open class ServerSessionServerStreamingBase<InputType: Message, OutputType: Message>: ServerSessionBase, ServerSessionServerStreaming {
  23. public typealias ProviderBlock = (InputType, ServerSessionServerStreamingBase) throws -> Void
  24. private var providerBlock: ProviderBlock
  25. public init(handler: Handler, providerBlock: @escaping ProviderBlock) {
  26. self.providerBlock = providerBlock
  27. super.init(handler: handler)
  28. }
  29. public func send(_ response: OutputType, completion: ((Error?) -> Void)?) throws {
  30. try handler.sendResponse(message: response.serializedData(), completion: completion)
  31. }
  32. public func run(queue: DispatchQueue) throws {
  33. try handler.receiveMessage(initialMetadata: initialMetadata) { requestData in
  34. // TODO(danielalm): Unify this behavior with `ServerSessionBidirectionalStreamingBase.run()`.
  35. if let requestData = requestData {
  36. do {
  37. let requestMessage = try InputType(serializedData: requestData)
  38. // to keep providers from blocking the server thread,
  39. // we dispatch them to another queue.
  40. queue.async {
  41. do {
  42. try self.providerBlock(requestMessage, self)
  43. try self.handler.sendStatus(statusCode: self.statusCode,
  44. statusMessage: self.statusMessage,
  45. trailingMetadata: self.trailingMetadata,
  46. completion: nil)
  47. } catch (let error) {
  48. print("error: \(error)")
  49. }
  50. }
  51. } catch (let error) {
  52. print("error: \(error)")
  53. }
  54. }
  55. }
  56. }
  57. public func waitForSendOperationsToFinish() {
  58. handler.call.messageQueueEmpty.wait()
  59. }
  60. }
  61. /// Simple fake implementation of ServerSessionServerStreaming that returns a previously-defined set of results
  62. /// and stores sent values for later verification.
  63. open class ServerSessionServerStreamingTestStub<OutputType: Message>: ServerSessionTestStub, ServerSessionServerStreaming {
  64. open var outputs: [OutputType] = []
  65. open func send(_ response: OutputType, completion _: ((Error?) -> Void)?) throws {
  66. outputs.append(response)
  67. }
  68. open func close() throws {}
  69. open func waitForSendOperationsToFinish() {}
  70. }