EchoService.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. enum EchoResult {
  37. case Response(r: Echo_EchoResponse)
  38. // these last two should be merged
  39. case CallResult(c: CallResult)
  40. case Error(s: String)
  41. }
  42. public class EchoGetCall {
  43. var call : Call
  44. init(_ channel: Channel) {
  45. self.call = channel.makeCall("/echo.Echo/Get")
  46. }
  47. // Call this with the message to send,
  48. // the callback will be called after the request is received.
  49. func perform(request: Echo_EchoRequest,
  50. callback:@escaping (EchoResult) -> Void)
  51. -> Void {
  52. let requestMessageData = try! request.serializeProtobuf()
  53. let requestMetadata = Metadata()
  54. try! call.perform(message: requestMessageData,
  55. metadata: requestMetadata)
  56. {(callResult) in
  57. print("Client received status \(callResult.statusCode) \(callResult.statusMessage!)")
  58. if let messageData = callResult.resultData {
  59. let responseMessage = try! Echo_EchoResponse(protobuf:messageData)
  60. callback(EchoResult.Response(r: responseMessage))
  61. } else {
  62. callback(EchoResult.CallResult(c: callResult))
  63. }
  64. }
  65. }
  66. }
  67. public class EchoExpandCall {
  68. var call : Call
  69. init(_ channel: Channel) {
  70. self.call = channel.makeCall("/echo.Echo/Expand")
  71. }
  72. // Call this once with the message to send,
  73. // the callback will be called after the request is initiated.
  74. func perform(request: Echo_EchoRequest,
  75. callback:@escaping (CallResult) -> Void)
  76. -> Void {
  77. let requestMessageData = try! request.serializeProtobuf()
  78. let requestMetadata = Metadata()
  79. try! call.startServerStreaming(message: requestMessageData,
  80. metadata: requestMetadata)
  81. {(callResult) in
  82. callback(callResult)
  83. }
  84. }
  85. func Recv() -> EchoResult {
  86. let done = NSCondition()
  87. var result : EchoResult!
  88. try! call.receiveMessage() {(data) in
  89. if let data = data {
  90. if let responseMessage = try? Echo_EchoResponse(protobuf:data) {
  91. result = EchoResult.Response(r: responseMessage)
  92. } else {
  93. result = EchoResult.Error(s: "INVALID RESPONSE")
  94. }
  95. } else {
  96. result = EchoResult.Error(s: "EOM")
  97. }
  98. done.lock()
  99. done.signal()
  100. done.unlock()
  101. }
  102. done.lock()
  103. done.wait()
  104. done.unlock()
  105. return result
  106. }
  107. }
  108. public class EchoCollectCall {
  109. var call : Call
  110. init(_ channel: Channel) {
  111. self.call = channel.makeCall("/echo.Echo/Collect")
  112. }
  113. // Call this to start a call.
  114. func start(metadata:Metadata, completion:@escaping (() -> Void)) throws {
  115. try self.call.start(metadata: metadata, completion:completion)
  116. }
  117. // Call this to send each message in the request stream.
  118. func Send(_ message: Echo_EchoRequest) {
  119. let messageData = try! message.serializeProtobuf()
  120. _ = call.sendMessage(data:messageData)
  121. }
  122. func CloseAndRecv() -> EchoResult {
  123. let done = NSCondition()
  124. var result : EchoResult!
  125. do {
  126. try self.receiveMessage() {(responseMessage) in
  127. if let responseMessage = responseMessage {
  128. result = EchoResult.Response(r: responseMessage)
  129. } else {
  130. result = EchoResult.Error(s: "INVALID RESPONSE")
  131. }
  132. done.lock()
  133. done.signal()
  134. done.unlock()
  135. }
  136. } catch (let error) {
  137. print("ERROR A: \(error)")
  138. }
  139. do {
  140. try call.close(completion:{
  141. print("closed")
  142. })
  143. } catch (let error) {
  144. print("ERROR B: \(error)")
  145. }
  146. done.lock()
  147. done.wait()
  148. done.unlock()
  149. return result
  150. }
  151. // Call this to receive a message.
  152. // The callback will be called when a message is received.
  153. // call this again from the callback to wait for another message.
  154. func receiveMessage(callback:@escaping (Echo_EchoResponse?) throws -> Void)
  155. throws {
  156. try call.receiveMessage() {(data) in
  157. guard let data = data else {
  158. try callback(nil)
  159. return
  160. }
  161. guard
  162. let responseMessage = try? Echo_EchoResponse(protobuf:data)
  163. else {
  164. return
  165. }
  166. try callback(responseMessage)
  167. }
  168. }
  169. }
  170. public class EchoUpdateCall {
  171. var call : Call
  172. init(_ channel: Channel) {
  173. self.call = channel.makeCall("/echo.Echo/Update")
  174. }
  175. func start(metadata:Metadata, completion:@escaping (() -> Void)) throws {
  176. try self.call.start(metadata: metadata, completion:completion)
  177. }
  178. func receiveMessage(callback:@escaping (Echo_EchoResponse?) throws -> Void) throws {
  179. try call.receiveMessage() {(data) in
  180. if let data = data {
  181. if let responseMessage = try? Echo_EchoResponse(protobuf:data) {
  182. try callback(responseMessage)
  183. } else {
  184. try callback(nil) // error, bad data
  185. }
  186. } else {
  187. try callback(nil)
  188. }
  189. }
  190. }
  191. func Recv() -> EchoResult {
  192. let done = NSCondition()
  193. var result : EchoResult!
  194. try! self.receiveMessage() {responseMessage in
  195. if let responseMessage = responseMessage {
  196. result = EchoResult.Response(r: responseMessage)
  197. } else {
  198. result = EchoResult.Error(s: "EOM")
  199. }
  200. done.lock()
  201. done.signal()
  202. done.unlock()
  203. }
  204. done.lock()
  205. done.wait()
  206. done.unlock()
  207. return result
  208. }
  209. func Send(message:Echo_EchoRequest) {
  210. let messageData = try! message.serializeProtobuf()
  211. _ = call.sendMessage(data:messageData)
  212. }
  213. func CloseSend() {
  214. let done = NSCondition()
  215. try! call.close() {
  216. done.lock()
  217. done.signal()
  218. done.unlock()
  219. }
  220. done.lock()
  221. done.wait()
  222. done.unlock()
  223. }
  224. }
  225. public class EchoService {
  226. public var channel: Channel
  227. public init(address: String) {
  228. channel = Channel(address:address)
  229. }
  230. public init(address: String, certificates: String?, host: String?) {
  231. channel = Channel(address:address, certificates:certificates, host:host)
  232. }
  233. func get(_ requestMessage: Echo_EchoRequest) -> EchoResult {
  234. let call = EchoGetCall(channel)
  235. let done = NSCondition()
  236. var finalResult : EchoResult!
  237. call.perform(request:requestMessage) {(result) in
  238. finalResult = result
  239. done.lock()
  240. done.signal()
  241. done.unlock()
  242. }
  243. done.lock()
  244. done.wait()
  245. done.unlock()
  246. return finalResult
  247. }
  248. func expand(_ requestMessage: Echo_EchoRequest) -> EchoExpandCall {
  249. let call = EchoExpandCall(channel)
  250. call.perform(request:requestMessage) {response in }
  251. return call
  252. }
  253. func collect() -> EchoCollectCall {
  254. let call = EchoCollectCall(channel)
  255. try! call.start(metadata:Metadata(), completion:{})
  256. return call
  257. }
  258. func update() -> EchoUpdateCall {
  259. let call = EchoUpdateCall(channel)
  260. try! call.start(metadata:Metadata(), completion:{})
  261. return call
  262. }
  263. }