EchoService.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. import Foundation
  34. import gRPC
  35. // all code that follows is to-be-generated
  36. public class EchoGetCall {
  37. var call : Call
  38. init(_ call: Call) {
  39. self.call = call
  40. }
  41. // Call this with the message to send,
  42. // the callback will be called after the request is received.
  43. func perform(request: Echo_EchoRequest,
  44. callback:@escaping (CallResult, Echo_EchoResponse?) -> Void)
  45. -> Void {
  46. let requestMessageData = try! request.serializeProtobuf()
  47. let requestMetadata = Metadata()
  48. try! call.perform(message: requestMessageData,
  49. metadata: requestMetadata)
  50. {(callResult) in
  51. print("Client received status \(callResult.statusCode) \(callResult.statusMessage!)")
  52. if let messageData = callResult.resultData {
  53. let responseMessage = try! Echo_EchoResponse(protobuf:messageData)
  54. callback(callResult, responseMessage)
  55. } else {
  56. callback(callResult, nil)
  57. }
  58. }
  59. }
  60. }
  61. public class EchoExpandCall {
  62. var call : Call
  63. init(_ call: Call) {
  64. self.call = call
  65. }
  66. // Call this once with the message to send,
  67. // the callback will be called after the request is initiated.
  68. func perform(request: Echo_EchoRequest,
  69. callback:@escaping (CallResult, Echo_EchoResponse?) -> Void)
  70. -> Void {
  71. let requestMessageData = try! request.serializeProtobuf()
  72. let requestMetadata = Metadata()
  73. try! call.startServerStreaming(message: requestMessageData,
  74. metadata: requestMetadata)
  75. {(callResult) in
  76. //print("Client received status \(callResult.statusCode): \(callResult.statusMessage!)")
  77. }
  78. }
  79. // Call this to receive a message.
  80. // The callback will be called when a message is received.
  81. // call this again from the callback to wait for another message.
  82. func receiveMessage(callback:@escaping (Echo_EchoResponse?) throws -> Void) throws {
  83. try call.receiveMessage() {(data) in
  84. if let data = data {
  85. if let responseMessage = try? Echo_EchoResponse(protobuf:data) {
  86. try callback(responseMessage)
  87. } else {
  88. try callback(nil)
  89. }
  90. } else {
  91. try callback(nil)
  92. }
  93. }
  94. }
  95. }
  96. public class EchoCollectCall {
  97. var call : Call
  98. init(_ call: Call) {
  99. self.call = call
  100. }
  101. // Call this to start a call.
  102. func start(metadata:Metadata) throws {
  103. try self.call.start(metadata: metadata)
  104. }
  105. // Call this to send each message in the request stream.
  106. func sendMessage(message: Echo_EchoRequest) {
  107. let messageData = try! message.serializeProtobuf()
  108. _ = call.sendMessage(data:messageData)
  109. }
  110. // Call this to receive a message.
  111. // The callback will be called when a message is received.
  112. // call this again from the callback to wait for another message.
  113. func receiveMessage(callback:@escaping (Echo_EchoResponse?) throws -> Void)
  114. throws {
  115. try call.receiveMessage() {(data) in
  116. guard
  117. let responseMessage = try? Echo_EchoResponse(protobuf:data)
  118. else {
  119. return
  120. }
  121. try callback(responseMessage)
  122. }
  123. }
  124. func close(completion:@escaping (() -> Void)) throws {
  125. try call.close(completion:completion)
  126. }
  127. }
  128. public class EchoUpdateCall {
  129. var call : Call
  130. init(_ call: Call) {
  131. self.call = call
  132. }
  133. func start(metadata:Metadata, completion:@escaping (() -> Void)) throws {
  134. try self.call.start(metadata: metadata, completion:completion)
  135. }
  136. func receiveMessage(callback:@escaping (Echo_EchoResponse?) throws -> Void) throws {
  137. try call.receiveMessage() {(data) in
  138. guard let data = data
  139. else {
  140. return
  141. }
  142. guard
  143. let responseMessage = try? Echo_EchoResponse(protobuf:data)
  144. else {
  145. return
  146. }
  147. try callback(responseMessage)
  148. }
  149. }
  150. func sendMessage(message: Echo_EchoRequest) {
  151. let messageData = try! message.serializeProtobuf()
  152. _ = call.sendMessage(data:messageData)
  153. }
  154. func close(completion:@escaping (() -> Void)) throws {
  155. try call.close(completion:completion)
  156. }
  157. }
  158. public class EchoService {
  159. public var channel: Channel
  160. public init(address: String) {
  161. channel = Channel(address:address)
  162. }
  163. public init(address: String, certificates: String?, host: String?) {
  164. channel = Channel(address:address, certificates:certificates, host:host)
  165. }
  166. func get() -> EchoGetCall {
  167. return EchoGetCall(channel.makeCall("/echo.Echo/Get"))
  168. }
  169. func expand() -> EchoExpandCall {
  170. return EchoExpandCall(channel.makeCall("/echo.Echo/Expand"))
  171. }
  172. func collect() -> EchoCollectCall {
  173. return EchoCollectCall(channel.makeCall("/echo.Echo/Collect"))
  174. }
  175. func update() -> EchoUpdateCall {
  176. return EchoUpdateCall(channel.makeCall("/echo.Echo/Update"))
  177. }
  178. }