ServerSessionClientStreaming.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 ServerSessionClientStreaming: ServerSession {}
  20. open class ServerSessionClientStreamingBase<InputType: Message, OutputType: Message>: ServerSessionBase, ServerSessionClientStreaming {
  21. public typealias ProviderBlock = (ServerSessionClientStreamingBase) throws -> Void
  22. private var providerBlock: ProviderBlock
  23. public init(handler: Handler, providerBlock: @escaping ProviderBlock) {
  24. self.providerBlock = providerBlock
  25. super.init(handler: handler)
  26. }
  27. public func receive() throws -> InputType {
  28. let sem = DispatchSemaphore(value: 0)
  29. var requestMessage: InputType?
  30. try handler.receiveMessage { requestData in
  31. if let requestData = requestData {
  32. requestMessage = try? InputType(serializedData: requestData)
  33. }
  34. sem.signal()
  35. }
  36. _ = sem.wait()
  37. if requestMessage == nil {
  38. throw ServerError.endOfStream
  39. }
  40. return requestMessage!
  41. }
  42. public func sendAndClose(_ response: OutputType) throws {
  43. try handler.sendResponse(message: response.serializedData(),
  44. statusCode: statusCode,
  45. statusMessage: statusMessage,
  46. trailingMetadata: trailingMetadata)
  47. }
  48. public func run(queue: DispatchQueue) throws {
  49. try handler.sendMetadata(initialMetadata: initialMetadata) { _ in
  50. queue.async {
  51. do {
  52. try self.providerBlock(self)
  53. } catch (let error) {
  54. print("error \(error)")
  55. }
  56. }
  57. }
  58. }
  59. }
  60. /// Simple fake implementation of ServerSessionClientStreaming that returns a previously-defined result
  61. /// and stores sent values for later verification.
  62. open class ServerSessionClientStreamingTestStub<InputType: Message, OutputType: Message>: ServerSessionTestStub, ServerSessionClientStreaming {
  63. open var inputs: [InputType] = []
  64. open var output: OutputType?
  65. open func receive() throws -> InputType {
  66. if let input = inputs.first {
  67. inputs.removeFirst()
  68. return input
  69. } else {
  70. throw ServerError.endOfStream
  71. }
  72. }
  73. open func sendAndClose(_ response: OutputType) throws {
  74. output = response
  75. }
  76. open func close() throws {}
  77. }