echo.client.pb.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * DO NOT EDIT.
  3. *
  4. * Generated by the protocol buffer compiler.
  5. * Source: echo.proto
  6. *
  7. */
  8. /*
  9. *
  10. * Copyright 2016, Google Inc.
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are
  15. * met:
  16. *
  17. * * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * * Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following disclaimer
  21. * in the documentation and/or other materials provided with the
  22. * distribution.
  23. * * Neither the name of Google Inc. nor the names of its
  24. * contributors may be used to endorse or promote products derived from
  25. * this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. import Foundation
  41. import gRPC
  42. public enum Echo_EchoClientError : Error {
  43. case endOfStream
  44. case invalidMessageReceived
  45. case error(c: CallResult)
  46. }
  47. // Get (Unary)
  48. public class Echo_EchoGetCall {
  49. var call : Call
  50. /// Create a call.
  51. fileprivate init(_ channel: Channel) {
  52. self.call = channel.makeCall("/echo.Echo/Get")
  53. }
  54. /// Run the call. Blocks until the reply is received.
  55. fileprivate func run(request: Echo_EchoRequest,
  56. metadata: Metadata) throws -> Echo_EchoResponse {
  57. let done = NSCondition()
  58. var callResult : CallResult!
  59. var responseMessage : Echo_EchoResponse?
  60. let requestMessageData = try! request.serializeProtobuf()
  61. try! call.perform(message: requestMessageData,
  62. metadata: metadata)
  63. {(_callResult) in
  64. callResult = _callResult
  65. if let messageData = callResult.resultData {
  66. responseMessage = try? Echo_EchoResponse(protobuf:messageData)
  67. }
  68. done.lock()
  69. done.signal()
  70. done.unlock()
  71. }
  72. done.lock()
  73. done.wait()
  74. done.unlock()
  75. if let responseMessage = responseMessage {
  76. return responseMessage
  77. } else {
  78. throw Echo_EchoClientError.error(c: callResult)
  79. }
  80. }
  81. }
  82. // Expand (Server Streaming)
  83. public class Echo_EchoExpandCall {
  84. var call : Call
  85. /// Create a call.
  86. fileprivate init(_ channel: Channel) {
  87. self.call = channel.makeCall("/echo.Echo/Expand")
  88. }
  89. // Call this once with the message to send.
  90. fileprivate func run(request: Echo_EchoRequest, metadata: Metadata) throws -> Echo_EchoExpandCall {
  91. let requestMessageData = try! request.serializeProtobuf()
  92. try! call.startServerStreaming(message: requestMessageData,
  93. metadata: metadata,
  94. completion:{(CallResult) in })
  95. return self
  96. }
  97. // Call this to wait for a result. Blocks.
  98. public func Receive() throws -> Echo_EchoResponse {
  99. var returnError : Echo_EchoClientError?
  100. var returnMessage : Echo_EchoResponse!
  101. let done = NSCondition()
  102. do {
  103. try call.receiveMessage() {(data) in
  104. if let data = data {
  105. returnMessage = try? Echo_EchoResponse(protobuf:data)
  106. if returnMessage == nil {
  107. returnError = Echo_EchoClientError.invalidMessageReceived
  108. }
  109. } else {
  110. returnError = Echo_EchoClientError.endOfStream
  111. }
  112. done.lock()
  113. done.signal()
  114. done.unlock()
  115. }
  116. done.lock()
  117. done.wait()
  118. done.unlock()
  119. }
  120. if let returnError = returnError {
  121. throw returnError
  122. }
  123. return returnMessage
  124. }
  125. }
  126. // Collect (Client Streaming)
  127. public class Echo_EchoCollectCall {
  128. var call : Call
  129. /// Create a call.
  130. fileprivate init(_ channel: Channel) {
  131. self.call = channel.makeCall("/echo.Echo/Collect")
  132. }
  133. // Call this to start a call.
  134. fileprivate func run(metadata:Metadata) throws -> Echo_EchoCollectCall {
  135. try self.call.start(metadata: metadata, completion:{})
  136. return self
  137. }
  138. // Call this to send each message in the request stream.
  139. public func Send(_ message: Echo_EchoRequest) {
  140. let messageData = try! message.serializeProtobuf()
  141. _ = call.sendMessage(data:messageData)
  142. }
  143. // Call this to close the connection and wait for a response. Blocks.
  144. public func CloseAndReceive() throws -> Echo_EchoResponse {
  145. var returnError : Echo_EchoClientError?
  146. var returnMessage : Echo_EchoResponse!
  147. let done = NSCondition()
  148. do {
  149. try self.receiveMessage() {(responseMessage) in
  150. if let responseMessage = responseMessage {
  151. returnMessage = responseMessage
  152. } else {
  153. returnError = Echo_EchoClientError.invalidMessageReceived
  154. }
  155. done.lock()
  156. done.signal()
  157. done.unlock()
  158. }
  159. try call.close(completion:{
  160. print("closed")
  161. })
  162. done.lock()
  163. done.wait()
  164. done.unlock()
  165. } catch (let error) {
  166. print("ERROR B: \(error)")
  167. }
  168. if let returnError = returnError {
  169. throw returnError
  170. }
  171. return returnMessage
  172. }
  173. // Call this to receive a message.
  174. // The callback will be called when a message is received.
  175. // call this again from the callback to wait for another message.
  176. fileprivate func receiveMessage(callback:@escaping (Echo_EchoResponse?) throws -> Void)
  177. throws {
  178. try call.receiveMessage() {(data) in
  179. guard let data = data else {
  180. try callback(nil)
  181. return
  182. }
  183. guard
  184. let responseMessage = try? Echo_EchoResponse(protobuf:data)
  185. else {
  186. return
  187. }
  188. try callback(responseMessage)
  189. }
  190. }
  191. }
  192. // Update (Bidirectional Streaming)
  193. public class Echo_EchoUpdateCall {
  194. var call : Call
  195. /// Create a call.
  196. fileprivate init(_ channel: Channel) {
  197. self.call = channel.makeCall("/echo.Echo/Update")
  198. }
  199. fileprivate func run(metadata:Metadata) throws -> Echo_EchoUpdateCall {
  200. try self.call.start(metadata: metadata, completion:{})
  201. return self
  202. }
  203. fileprivate func receiveMessage(callback:@escaping (Echo_EchoResponse?) throws -> Void) throws {
  204. try call.receiveMessage() {(data) in
  205. if let data = data {
  206. if let responseMessage = try? Echo_EchoResponse(protobuf:data) {
  207. try callback(responseMessage)
  208. } else {
  209. try callback(nil) // error, bad data
  210. }
  211. } else {
  212. try callback(nil)
  213. }
  214. }
  215. }
  216. public func Receive() throws -> Echo_EchoResponse {
  217. var returnError : Echo_EchoClientError?
  218. var returnMessage : Echo_EchoResponse!
  219. let done = NSCondition()
  220. do {
  221. try call.receiveMessage() {(data) in
  222. if let data = data {
  223. returnMessage = try? Echo_EchoResponse(protobuf:data)
  224. if returnMessage == nil {
  225. returnError = Echo_EchoClientError.invalidMessageReceived
  226. }
  227. } else {
  228. returnError = Echo_EchoClientError.endOfStream
  229. }
  230. done.lock()
  231. done.signal()
  232. done.unlock()
  233. }
  234. done.lock()
  235. done.wait()
  236. done.unlock()
  237. }
  238. if let returnError = returnError {
  239. throw returnError
  240. }
  241. return returnMessage
  242. }
  243. public func Send(_ message:Echo_EchoRequest) {
  244. let messageData = try! message.serializeProtobuf()
  245. _ = call.sendMessage(data:messageData)
  246. }
  247. public func CloseSend() {
  248. let done = NSCondition()
  249. try! call.close() {
  250. done.lock()
  251. done.signal()
  252. done.unlock()
  253. }
  254. done.lock()
  255. done.wait()
  256. done.unlock()
  257. }
  258. }
  259. // Call methods of this class to make API calls.
  260. public class Echo_EchoService {
  261. private var channel: Channel
  262. public var metadata : Metadata
  263. public var host : String {
  264. get {
  265. return self.channel.host
  266. }
  267. set {
  268. self.channel.host = newValue
  269. }
  270. }
  271. public init(address: String) {
  272. gRPC.initialize()
  273. channel = Channel(address:address)
  274. metadata = Metadata()
  275. }
  276. public init(address: String, certificates: String?, host: String?) {
  277. gRPC.initialize()
  278. channel = Channel(address:address, certificates:certificates, host:host)
  279. metadata = Metadata()
  280. }
  281. // Synchronous. Unary.
  282. public func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
  283. return try Echo_EchoGetCall(channel).run(request:request, metadata:metadata)
  284. }
  285. // Asynchronous. Server-streaming.
  286. // Send the initial message.
  287. // Use methods on the returned object to get streamed responses.
  288. public func expand(_ request: Echo_EchoRequest) throws -> Echo_EchoExpandCall {
  289. return try Echo_EchoExpandCall(channel).run(request:request, metadata:metadata)
  290. }
  291. // Asynchronous. Client-streaming.
  292. // Use methods on the returned object to stream messages and
  293. // to close the connection and wait for a final response.
  294. public func collect() throws -> Echo_EchoCollectCall {
  295. return try Echo_EchoCollectCall(channel).run(metadata:metadata)
  296. }
  297. // Asynchronous. Bidirectional-streaming.
  298. // Use methods on the returned object to stream messages,
  299. // to wait for replies, and to close the connection.
  300. public func update() throws -> Echo_EchoUpdateCall {
  301. return try Echo_EchoUpdateCall(channel).run(metadata:metadata)
  302. }
  303. }