echo.client.pb.swift 9.6 KB

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