echo.client.pb.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. // all code that follows is to-be-generated
  34. import Foundation
  35. import gRPC
  36. public enum Echo_EchoClientError : Error {
  37. case endOfStream
  38. case invalidMessageReceived
  39. case error(c: CallResult)
  40. }
  41. //
  42. // Unary GET
  43. //
  44. public class Echo_EchoGetCall {
  45. var call : Call
  46. fileprivate init(_ channel: Channel) {
  47. self.call = channel.makeCall("/echo.Echo/Get")
  48. }
  49. fileprivate func run(request: Echo_EchoRequest,
  50. metadata: Metadata) throws -> Echo_EchoResponse {
  51. let done = NSCondition()
  52. var callResult : CallResult!
  53. var responseMessage : Echo_EchoResponse?
  54. let requestMessageData = try! request.serializeProtobuf()
  55. try! call.perform(message: requestMessageData,
  56. metadata: metadata)
  57. {(_callResult) in
  58. callResult = _callResult
  59. if let messageData = callResult.resultData {
  60. responseMessage = try? Echo_EchoResponse(protobuf:messageData)
  61. }
  62. done.lock()
  63. done.signal()
  64. done.unlock()
  65. }
  66. done.lock()
  67. done.wait()
  68. done.unlock()
  69. if let responseMessage = responseMessage {
  70. return responseMessage
  71. } else {
  72. throw Echo_EchoClientError.error(c: callResult)
  73. }
  74. }
  75. }
  76. //
  77. // Server-streaming EXPAND
  78. //
  79. public class Echo_EchoExpandCall {
  80. var call : Call
  81. fileprivate init(_ channel: Channel) {
  82. self.call = channel.makeCall("/echo.Echo/Expand")
  83. }
  84. // Call this once with the message to send.
  85. fileprivate func run(request: Echo_EchoRequest, metadata: Metadata) throws -> Echo_EchoExpandCall {
  86. let requestMessageData = try! request.serializeProtobuf()
  87. try! call.startServerStreaming(message: requestMessageData,
  88. metadata: metadata,
  89. completion:{(CallResult) in })
  90. return self
  91. }
  92. // Call this to wait for a result. Blocks.
  93. public func Receive() throws -> Echo_EchoResponse {
  94. var returnError : Echo_EchoClientError?
  95. var returnMessage : Echo_EchoResponse!
  96. let done = NSCondition()
  97. do {
  98. try call.receiveMessage() {(data) in
  99. if let data = data {
  100. returnMessage = try? Echo_EchoResponse(protobuf:data)
  101. if returnMessage == nil {
  102. returnError = Echo_EchoClientError.invalidMessageReceived
  103. }
  104. } else {
  105. returnError = Echo_EchoClientError.endOfStream
  106. }
  107. done.lock()
  108. done.signal()
  109. done.unlock()
  110. }
  111. done.lock()
  112. done.wait()
  113. done.unlock()
  114. }
  115. if let returnError = returnError {
  116. throw returnError
  117. }
  118. return returnMessage
  119. }
  120. }
  121. //
  122. // Client-streaming COLLECT
  123. //
  124. public class Echo_EchoCollectCall {
  125. var call : Call
  126. fileprivate init(_ channel: Channel) {
  127. self.call = channel.makeCall("/echo.Echo/Collect")
  128. }
  129. // Call this to start a call.
  130. fileprivate func run(metadata:Metadata) throws -> Echo_EchoCollectCall {
  131. try self.call.start(metadata: metadata, completion:{})
  132. return self
  133. }
  134. // Call this to send each message in the request stream.
  135. public func Send(_ message: Echo_EchoRequest) {
  136. let messageData = try! message.serializeProtobuf()
  137. _ = call.sendMessage(data:messageData)
  138. }
  139. // Call this to close the connection and wait for a response. Blocks.
  140. public func CloseAndReceive() throws -> Echo_EchoResponse {
  141. var returnError : Echo_EchoClientError?
  142. var returnMessage : Echo_EchoResponse!
  143. let done = NSCondition()
  144. do {
  145. try self.receiveMessage() {(responseMessage) in
  146. if let responseMessage = responseMessage {
  147. returnMessage = responseMessage
  148. } else {
  149. returnError = Echo_EchoClientError.invalidMessageReceived
  150. }
  151. done.lock()
  152. done.signal()
  153. done.unlock()
  154. }
  155. try call.close(completion:{
  156. print("closed")
  157. })
  158. done.lock()
  159. done.wait()
  160. done.unlock()
  161. } catch (let error) {
  162. print("ERROR B: \(error)")
  163. }
  164. if let returnError = returnError {
  165. throw returnError
  166. }
  167. return returnMessage
  168. }
  169. // Call this to receive a message.
  170. // The callback will be called when a message is received.
  171. // call this again from the callback to wait for another message.
  172. fileprivate func receiveMessage(callback:@escaping (Echo_EchoResponse?) throws -> Void)
  173. throws {
  174. try call.receiveMessage() {(data) in
  175. guard let data = data else {
  176. try callback(nil)
  177. return
  178. }
  179. guard
  180. let responseMessage = try? Echo_EchoResponse(protobuf:data)
  181. else {
  182. return
  183. }
  184. try callback(responseMessage)
  185. }
  186. }
  187. }
  188. //
  189. // Bidirectional-streaming UPDATE
  190. //
  191. public class Echo_EchoUpdateCall {
  192. var call : Call
  193. fileprivate init(_ channel: Channel) {
  194. self.call = channel.makeCall("/echo.Echo/Update")
  195. }
  196. fileprivate func run(metadata:Metadata) throws -> Echo_EchoUpdateCall {
  197. try self.call.start(metadata: metadata, completion:{})
  198. return self
  199. }
  200. fileprivate func receiveMessage(callback:@escaping (Echo_EchoResponse?) throws -> Void) throws {
  201. try call.receiveMessage() {(data) in
  202. if let data = data {
  203. if let responseMessage = try? Echo_EchoResponse(protobuf:data) {
  204. try callback(responseMessage)
  205. } else {
  206. try callback(nil) // error, bad data
  207. }
  208. } else {
  209. try callback(nil)
  210. }
  211. }
  212. }
  213. public func Receive() throws -> Echo_EchoResponse {
  214. var returnError : Echo_EchoClientError?
  215. var returnMessage : Echo_EchoResponse!
  216. let done = NSCondition()
  217. do {
  218. try call.receiveMessage() {(data) in
  219. if let data = data {
  220. returnMessage = try? Echo_EchoResponse(protobuf:data)
  221. if returnMessage == nil {
  222. returnError = Echo_EchoClientError.invalidMessageReceived
  223. }
  224. } else {
  225. returnError = Echo_EchoClientError.endOfStream
  226. }
  227. done.lock()
  228. done.signal()
  229. done.unlock()
  230. }
  231. done.lock()
  232. done.wait()
  233. done.unlock()
  234. }
  235. if let returnError = returnError {
  236. throw returnError
  237. }
  238. return returnMessage
  239. }
  240. public func Send(_ message:Echo_EchoRequest) {
  241. let messageData = try! message.serializeProtobuf()
  242. _ = call.sendMessage(data:messageData)
  243. }
  244. public func CloseSend() {
  245. let done = NSCondition()
  246. try! call.close() {
  247. done.lock()
  248. done.signal()
  249. done.unlock()
  250. }
  251. done.lock()
  252. done.wait()
  253. done.unlock()
  254. }
  255. }
  256. // Call methods of this class to make API calls.
  257. public class Echo_EchoService {
  258. private var channel: Channel
  259. public var metadata : Metadata
  260. public var host : String {
  261. get {
  262. return self.channel.host
  263. }
  264. set {
  265. self.channel.host = newValue
  266. }
  267. }
  268. public init(address: String) {
  269. gRPC.initialize()
  270. channel = Channel(address:address)
  271. metadata = Metadata()
  272. }
  273. public init(address: String, certificates: String?, host: String?) {
  274. gRPC.initialize()
  275. channel = Channel(address:address, certificates:certificates, host:host)
  276. metadata = Metadata()
  277. }
  278. // Synchronous. Unary.
  279. public func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
  280. return try Echo_EchoGetCall(channel).run(request:request, metadata:metadata)
  281. }
  282. // Asynchronous. Server-streaming.
  283. // Send the initial message.
  284. // Use methods on the returned object to get streamed responses.
  285. public func expand(_ request: Echo_EchoRequest) throws -> Echo_EchoExpandCall {
  286. return try Echo_EchoExpandCall(channel).run(request:request, metadata:metadata)
  287. }
  288. // Asynchronous. Client-streaming.
  289. // Use methods on the returned object to stream messages and
  290. // to close the connection and wait for a final response.
  291. public func collect() throws -> Echo_EchoCollectCall {
  292. return try Echo_EchoCollectCall(channel).run(metadata:metadata)
  293. }
  294. // Asynchronous. Bidirectional-streaming.
  295. // Use methods on the returned object to stream messages,
  296. // to wait for replies, and to close the connection.
  297. public func update() throws -> Echo_EchoUpdateCall {
  298. return try Echo_EchoUpdateCall(channel).run(metadata:metadata)
  299. }
  300. }