2
0

Client.swift 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. /// A gRPC Client
  38. public class Client {
  39. /// Pointer to underlying C representation
  40. private var underlyingClient: UnsafeMutableRawPointer
  41. /// Completion queue for client call operations
  42. private var completionQueue: CompletionQueue
  43. /// Initializes a gRPC client
  44. ///
  45. /// - Parameter address: the address of the server to be called
  46. public init(address: String) {
  47. underlyingClient = cgrpc_client_create(address)
  48. completionQueue = CompletionQueue(underlyingCompletionQueue:cgrpc_client_completion_queue(underlyingClient))
  49. completionQueue.name = "Client" // only for debugging
  50. self.completionQueue.run() {} // start a loop that watches the client's completion queue
  51. }
  52. /// Initializes a gRPC client
  53. ///
  54. /// - Parameter address: the address of the server to be called
  55. public init(address: String, certificates: String?, host: String?) {
  56. if certificates == nil {
  57. let bundle = Bundle(for: Client.self)
  58. let url = bundle.url(forResource: "roots", withExtension: "pem")!
  59. let data = try! Data(contentsOf: url)
  60. let s = String(data: data, encoding: .ascii)
  61. underlyingClient = cgrpc_client_create_secure(address, s, host)
  62. } else {
  63. underlyingClient = cgrpc_client_create_secure(address, certificates, host)
  64. }
  65. completionQueue = CompletionQueue(underlyingCompletionQueue:cgrpc_client_completion_queue(underlyingClient))
  66. completionQueue.name = "Client" // only for debugging
  67. self.completionQueue.run() {} // start a loop that watches the client's completion queue
  68. }
  69. deinit {
  70. cgrpc_client_destroy(underlyingClient)
  71. }
  72. /// Constructs a Call object to make a gRPC API call
  73. ///
  74. /// - Parameter host: the gRPC host name for the call
  75. /// - Parameter method: the gRPC method name for the call
  76. /// - Parameter timeout: a timeout value in seconds
  77. /// - Returns: a Call object that can be used to perform the request
  78. public func makeCall(host:String, method:String, timeout:TimeInterval) -> Call {
  79. let call = cgrpc_client_create_call(underlyingClient, method, host, timeout)!
  80. return Call(underlyingCall:call, owned:true, completionQueue:self.completionQueue)
  81. }
  82. }