EchoService.swift 8.1 KB

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