ServerSessionBidirectionalStreaming.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 ServerSessionBidirectionalStreaming: ServerSession {
  20. func waitForSendOperationsToFinish()
  21. }
  22. open class ServerSessionBidirectionalStreamingBase<InputType: Message, OutputType: Message>: ServerSessionBase, ServerSessionBidirectionalStreaming {
  23. public typealias ProviderBlock = (ServerSessionBidirectionalStreamingBase) 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 receive() throws -> InputType {
  30. let sem = DispatchSemaphore(value: 0)
  31. var requestMessage: InputType?
  32. try handler.receiveMessage { requestData in
  33. if let requestData = requestData {
  34. do {
  35. requestMessage = try InputType(serializedData: requestData)
  36. } catch (let error) {
  37. print("error \(error)")
  38. }
  39. }
  40. sem.signal()
  41. }
  42. _ = sem.wait()
  43. if let requestMessage = requestMessage {
  44. return requestMessage
  45. } else {
  46. throw ServerError.endOfStream
  47. }
  48. }
  49. public func send(_ response: OutputType, completion: ((Error?) -> Void)?) throws {
  50. try handler.sendResponse(message: response.serializedData(), completion: completion)
  51. }
  52. public func close() throws {
  53. let sem = DispatchSemaphore(value: 0)
  54. try handler.sendStatus(statusCode: statusCode,
  55. statusMessage: statusMessage,
  56. trailingMetadata: trailingMetadata) { _ in sem.signal() }
  57. _ = sem.wait()
  58. }
  59. public func run(queue: DispatchQueue) throws {
  60. try handler.sendMetadata(initialMetadata: initialMetadata) { _ in
  61. queue.async {
  62. do {
  63. try self.providerBlock(self)
  64. } catch (let error) {
  65. print("error \(error)")
  66. }
  67. }
  68. }
  69. }
  70. public func waitForSendOperationsToFinish() {
  71. handler.call.messageQueueEmpty.wait()
  72. }
  73. }
  74. /// Simple fake implementation of ServerSessionBidirectionalStreaming that returns a previously-defined set of results
  75. /// and stores sent values for later verification.
  76. open class ServerSessionBidirectionalStreamingTestStub<InputType: Message, OutputType: Message>: ServerSessionTestStub, ServerSessionBidirectionalStreaming {
  77. open var inputs: [InputType] = []
  78. open var outputs: [OutputType] = []
  79. open func receive() throws -> InputType {
  80. if let input = inputs.first {
  81. inputs.removeFirst()
  82. return input
  83. } else {
  84. throw ServerError.endOfStream
  85. }
  86. }
  87. open func send(_ response: OutputType, completion _: ((Error?) -> Void)?) throws {
  88. outputs.append(response)
  89. }
  90. open func close() throws {}
  91. open func waitForSendOperationsToFinish() {}
  92. }