CompletionQueue.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. import Foundation
  37. public enum CompletionType {
  38. case queueShutdown
  39. case queueTimeout
  40. case complete
  41. case unknown
  42. static func completionType(grpcCompletionType value: grpc_completion_type) -> CompletionType {
  43. switch(value) {
  44. case GRPC_QUEUE_SHUTDOWN:
  45. return .queueShutdown
  46. case GRPC_QUEUE_TIMEOUT:
  47. return .queueTimeout
  48. case GRPC_OP_COMPLETE:
  49. return .complete
  50. default:
  51. return .unknown
  52. }
  53. }
  54. }
  55. public struct CompletionEvent {
  56. public var type: CompletionType
  57. public var success: Int32
  58. public var tag: Int64
  59. init(_ event: grpc_event) {
  60. type = CompletionType.completionType(grpcCompletionType: event.type)
  61. success = event.success
  62. tag = cgrpc_event_tag(event)
  63. }
  64. }
  65. /// A gRPC Completion Queue
  66. class CompletionQueue {
  67. var name : String!
  68. /// Pointer to underlying C representation
  69. private var underlyingCompletionQueue : UnsafeMutableRawPointer!
  70. /// Operation groups that are awaiting completion, keyed by tag
  71. var operationGroups : [Int64 : OperationGroup] = [:]
  72. /// Initializes a CompletionQueue
  73. ///
  74. /// - Parameter cq: the underlying C representation
  75. init(underlyingCompletionQueue: UnsafeMutableRawPointer) {
  76. self.underlyingCompletionQueue = underlyingCompletionQueue // NOT OWNED, so we don't dealloc it
  77. }
  78. /// Waits for an event to complete
  79. ///
  80. /// - Parameter timeout: a timeout value in seconds
  81. /// - Returns: a grpc_completion_type code indicating the result of waiting
  82. public func waitForCompletion(timeout: Double) -> CompletionEvent {
  83. let event = cgrpc_completion_queue_get_next_event(underlyingCompletionQueue, timeout);
  84. return CompletionEvent(event)
  85. }
  86. /// Run a completion queue
  87. public func run(completion:@escaping () -> Void) {
  88. DispatchQueue.global().async {
  89. var running = true
  90. while (running) {
  91. let event = cgrpc_completion_queue_get_next_event(self.underlyingCompletionQueue, -1.0)
  92. switch (event.type) {
  93. case GRPC_OP_COMPLETE:
  94. let tag = cgrpc_event_tag(event)
  95. if let operations = self.operationGroups[tag] {
  96. print("[\(self.name)] event success=\(event.success)")
  97. if event.success == 0 {
  98. print("something bad happened")
  99. } else {
  100. operations.completion(event.success == 1)
  101. }
  102. self.operationGroups[tag] = nil
  103. }
  104. break
  105. case GRPC_QUEUE_SHUTDOWN:
  106. running = false
  107. break
  108. case GRPC_QUEUE_TIMEOUT:
  109. break
  110. default:
  111. break
  112. }
  113. }
  114. DispatchQueue.main.async {
  115. completion()
  116. }
  117. }
  118. }
  119. /// Shutdown a completion queue
  120. public func shutdown() -> Void {
  121. cgrpc_completion_queue_shutdown(underlyingCompletionQueue)
  122. }
  123. }