OperationGroup.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #if SWIFT_PACKAGE
  34. import CgRPC
  35. #endif
  36. /// A collection of gRPC operations
  37. internal class OperationGroup {
  38. /// A mutex for synchronizing tag generation
  39. static let tagMutex = Mutex()
  40. /// Used to generate unique tags for OperationGroups
  41. private static var nextTag : Int64 = 1
  42. /// Automatically-assigned tag that is used by the completion queue that watches this group.
  43. internal var tag : Int64
  44. /// The call associated with the operation group. Retained while the operations are running.
  45. private var call : Call
  46. /// An array of operation objects that are passed into the initializer.
  47. private var operations : [Operation]
  48. /// An array of observers used to watch the operation
  49. private var underlyingObservers : [UnsafeMutableRawPointer] = []
  50. /// Pointer to underlying C representation
  51. internal var underlyingOperations : UnsafeMutableRawPointer?
  52. /// Completion handler that is called when the group completes
  53. internal var completion : ((OperationGroup) throws -> Void)
  54. /// Indicates that the OperationGroup completed successfully
  55. internal var success : Bool = false
  56. /// Creates the underlying observer needed to run an operation
  57. ///
  58. /// - Parameter: operation: the operation to observe
  59. /// - Returns: the observer
  60. private func underlyingObserverForOperation(operation: Operation) -> UnsafeMutableRawPointer {
  61. var underlyingObserver : UnsafeMutableRawPointer
  62. switch operation {
  63. case .sendInitialMetadata(let metadata):
  64. underlyingObserver = cgrpc_observer_create_send_initial_metadata(metadata.underlyingArray)!
  65. case .sendMessage(let message):
  66. underlyingObserver = cgrpc_observer_create_send_message()!
  67. cgrpc_observer_send_message_set_message(underlyingObserver, message.underlyingByteBuffer)
  68. case .sendCloseFromClient:
  69. underlyingObserver = cgrpc_observer_create_send_close_from_client()!
  70. case .sendStatusFromServer(let statusCode, let statusMessage, let metadata):
  71. underlyingObserver = cgrpc_observer_create_send_status_from_server(metadata.underlyingArray)!
  72. cgrpc_observer_send_status_from_server_set_status(underlyingObserver, Int32(statusCode))
  73. cgrpc_observer_send_status_from_server_set_status_details(underlyingObserver, statusMessage)
  74. case .receiveInitialMetadata:
  75. underlyingObserver = cgrpc_observer_create_recv_initial_metadata()!
  76. case .receiveMessage:
  77. underlyingObserver = cgrpc_observer_create_recv_message()!
  78. case .receiveStatusOnClient:
  79. underlyingObserver = cgrpc_observer_create_recv_status_on_client()!
  80. case .receiveCloseOnServer:
  81. underlyingObserver = cgrpc_observer_create_recv_close_on_server()!
  82. }
  83. return underlyingObserver
  84. }
  85. /// Initializes an OperationGroup representation
  86. ///
  87. /// - Parameter operations: an array of operations
  88. init(call: Call,
  89. operations: [Operation],
  90. completion: @escaping ((OperationGroup) throws -> Void)) {
  91. self.call = call
  92. self.operations = operations
  93. self.completion = completion
  94. // set tag to a unique value (per execution)
  95. OperationGroup.tagMutex.lock()
  96. self.tag = OperationGroup.nextTag
  97. OperationGroup.nextTag += 1
  98. OperationGroup.tagMutex.unlock()
  99. // create underlying observers and operations
  100. self.underlyingOperations = cgrpc_operations_create()
  101. cgrpc_operations_reserve_space_for_operations(self.underlyingOperations, Int32(operations.count))
  102. for operation in operations {
  103. let underlyingObserver = self.underlyingObserverForOperation(operation: operation)
  104. self.underlyingObservers.append(underlyingObserver)
  105. cgrpc_operations_add_operation(self.underlyingOperations, underlyingObserver)
  106. }
  107. }
  108. deinit {
  109. for underlyingObserver in underlyingObservers {
  110. cgrpc_observer_destroy(underlyingObserver);
  111. }
  112. cgrpc_operations_destroy(underlyingOperations);
  113. }
  114. /// WARNING: The following assumes that at most one operation of each type is in the group.
  115. /// Gets the message that was received
  116. ///
  117. /// - Returns: message
  118. internal func receivedMessage() -> ByteBuffer? {
  119. for (i, operation) in operations.enumerated() {
  120. switch (operation) {
  121. case .receiveMessage:
  122. if let b = cgrpc_observer_recv_message_get_message(underlyingObservers[i]) {
  123. return ByteBuffer(underlyingByteBuffer:b)
  124. } else {
  125. return nil
  126. }
  127. default: continue
  128. }
  129. }
  130. return nil
  131. }
  132. /// Gets initial metadata that was received
  133. ///
  134. /// - Returns: metadata
  135. internal func receivedInitialMetadata() -> Metadata? {
  136. for (i, operation) in operations.enumerated() {
  137. switch (operation) {
  138. case .receiveInitialMetadata:
  139. return Metadata(underlyingArray:cgrpc_observer_recv_initial_metadata_get_metadata(underlyingObservers[i]));
  140. default:
  141. continue
  142. }
  143. }
  144. return nil
  145. }
  146. /// Gets a status code that was received
  147. ///
  148. /// - Returns: status code
  149. internal func receivedStatusCode() -> Int? {
  150. for (i, operation) in operations.enumerated() {
  151. switch (operation) {
  152. case .receiveStatusOnClient:
  153. return cgrpc_observer_recv_status_on_client_get_status(underlyingObservers[i])
  154. default:
  155. continue
  156. }
  157. }
  158. return nil
  159. }
  160. /// Gets a status message that was received
  161. ///
  162. /// - Returns: status message
  163. internal func receivedStatusMessage() -> String? {
  164. for (i, operation) in operations.enumerated() {
  165. switch (operation) {
  166. case .receiveStatusOnClient:
  167. return String(cString:cgrpc_observer_recv_status_on_client_get_status_details(underlyingObservers[i]),
  168. encoding:String.Encoding.utf8)!
  169. default:
  170. continue
  171. }
  172. }
  173. return nil
  174. }
  175. /// Gets trailing metadata that was received
  176. ///
  177. /// - Returns: metadata
  178. internal func receivedTrailingMetadata() -> Metadata? {
  179. for (i, operation) in operations.enumerated() {
  180. switch (operation) {
  181. case .receiveStatusOnClient:
  182. return Metadata(underlyingArray:cgrpc_observer_recv_status_on_client_get_metadata(underlyingObservers[i]));
  183. default:
  184. continue
  185. }
  186. }
  187. return nil
  188. }
  189. }