echo.grpc.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * DO NOT EDIT.
  3. *
  4. * Generated by the protocol buffer compiler.
  5. * Source: echo.proto
  6. *
  7. */
  8. /*
  9. * Copyright 2018, gRPC Authors All rights reserved.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. import Foundation
  24. import Dispatch
  25. import gRPC
  26. import SwiftProtobuf
  27. /// Type for errors thrown from generated client code.
  28. internal enum Echo_EchoClientError : Error {
  29. case endOfStream
  30. case invalidMessageReceived
  31. case error(c: CallResult)
  32. }
  33. /// Get (Unary)
  34. internal protocol Echo_EchoGetCall {
  35. /// Cancel the call.
  36. func cancel()
  37. }
  38. /// Get (Unary)
  39. fileprivate final class Echo_EchoGetCallImpl: Echo_EchoGetCall {
  40. private var call : Call
  41. /// Create a call.
  42. init(_ channel: Channel) {
  43. self.call = channel.makeCall("/echo.Echo/Get")
  44. }
  45. /// Run the call. Blocks until the reply is received.
  46. /// - Throws: `BinaryEncodingError` if encoding fails. `CallError` if fails to call. `Echo_EchoClientError` if receives no response.
  47. func run(request: Echo_EchoRequest,
  48. metadata: Metadata) throws -> Echo_EchoResponse {
  49. let sem = DispatchSemaphore(value: 0)
  50. var returnCallResult : CallResult!
  51. var returnResponse : Echo_EchoResponse?
  52. _ = try start(request:request, metadata:metadata) {response, callResult in
  53. returnResponse = response
  54. returnCallResult = callResult
  55. sem.signal()
  56. }
  57. _ = sem.wait(timeout: DispatchTime.distantFuture)
  58. if let returnResponse = returnResponse {
  59. return returnResponse
  60. } else {
  61. throw Echo_EchoClientError.error(c: returnCallResult)
  62. }
  63. }
  64. /// Start the call. Nonblocking.
  65. /// - Throws: `BinaryEncodingError` if encoding fails. `CallError` if fails to call.
  66. func start(request: Echo_EchoRequest,
  67. metadata: Metadata,
  68. completion: @escaping (Echo_EchoResponse?, CallResult)->())
  69. throws -> Echo_EchoGetCall {
  70. let requestData = try request.serializedData()
  71. try call.start(.unary,
  72. metadata:metadata,
  73. message:requestData)
  74. {(callResult) in
  75. if let responseData = callResult.resultData,
  76. let response = try? Echo_EchoResponse(serializedData:responseData) {
  77. completion(response, callResult)
  78. } else {
  79. completion(nil, callResult)
  80. }
  81. }
  82. return self
  83. }
  84. func cancel() {
  85. call.cancel()
  86. }
  87. }
  88. /// Expand (Server Streaming)
  89. internal protocol Echo_EchoExpandCall {
  90. /// Call this to wait for a result. Blocking.
  91. func receive() throws -> Echo_EchoResponse
  92. /// Call this to wait for a result. Nonblocking.
  93. func receive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws
  94. /// Cancel the call.
  95. func cancel()
  96. }
  97. internal extension Echo_EchoExpandCall {
  98. func receive() throws -> Echo_EchoResponse {
  99. var returnError : Echo_EchoClientError?
  100. var returnResponse : Echo_EchoResponse!
  101. let sem = DispatchSemaphore(value: 0)
  102. do {
  103. try receive() {response, error in
  104. returnResponse = response
  105. returnError = error
  106. sem.signal()
  107. }
  108. _ = sem.wait(timeout: DispatchTime.distantFuture)
  109. }
  110. if let returnError = returnError {
  111. throw returnError
  112. }
  113. return returnResponse
  114. }
  115. }
  116. fileprivate final class Echo_EchoExpandCallImpl: Echo_EchoExpandCall {
  117. private var call : Call
  118. /// Create a call.
  119. init(_ channel: Channel) {
  120. self.call = channel.makeCall("/echo.Echo/Expand")
  121. }
  122. /// Call this once with the message to send. Nonblocking.
  123. func start(request: Echo_EchoRequest,
  124. metadata: Metadata,
  125. completion: @escaping (CallResult) -> ())
  126. throws -> Echo_EchoExpandCall {
  127. let requestData = try request.serializedData()
  128. try call.start(.serverStreaming,
  129. metadata:metadata,
  130. message:requestData,
  131. completion:completion)
  132. return self
  133. }
  134. func receive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws {
  135. do {
  136. try call.receiveMessage() {(responseData) in
  137. if let responseData = responseData {
  138. if let response = try? Echo_EchoResponse(serializedData:responseData) {
  139. completion(response, nil)
  140. } else {
  141. completion(nil, Echo_EchoClientError.invalidMessageReceived)
  142. }
  143. } else {
  144. completion(nil, Echo_EchoClientError.endOfStream)
  145. }
  146. }
  147. }
  148. }
  149. /// Cancel the call.
  150. func cancel() {
  151. call.cancel()
  152. }
  153. }
  154. /// Collect (Client Streaming)
  155. internal protocol Echo_EchoCollectCall {
  156. /// Call this to send each message in the request stream. Nonblocking.
  157. func send(_ message:Echo_EchoRequest, errorHandler:@escaping (Error)->()) throws
  158. /// Call this to close the connection and wait for a response. Blocking.
  159. func closeAndReceive() throws -> Echo_EchoResponse
  160. /// Call this to close the connection and wait for a response. Nonblocking.
  161. func closeAndReceive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws
  162. /// Cancel the call.
  163. func cancel()
  164. }
  165. internal extension Echo_EchoCollectCall {
  166. func closeAndReceive() throws -> Echo_EchoResponse {
  167. var returnError : Echo_EchoClientError?
  168. var returnResponse : Echo_EchoResponse!
  169. let sem = DispatchSemaphore(value: 0)
  170. do {
  171. try closeAndReceive() {response, error in
  172. returnResponse = response
  173. returnError = error
  174. sem.signal()
  175. }
  176. _ = sem.wait(timeout: DispatchTime.distantFuture)
  177. } catch (let error) {
  178. throw error
  179. }
  180. if let returnError = returnError {
  181. throw returnError
  182. }
  183. return returnResponse
  184. }
  185. }
  186. fileprivate final class Echo_EchoCollectCallImpl: Echo_EchoCollectCall {
  187. private var call : Call
  188. /// Create a call.
  189. init(_ channel: Channel) {
  190. self.call = channel.makeCall("/echo.Echo/Collect")
  191. }
  192. /// Call this to start a call. Nonblocking.
  193. func start(metadata:Metadata, completion:@escaping (CallResult)->())
  194. throws -> Echo_EchoCollectCall {
  195. try self.call.start(.clientStreaming, metadata:metadata, completion:completion)
  196. return self
  197. }
  198. func send(_ message:Echo_EchoRequest, errorHandler:@escaping (Error)->()) throws {
  199. let messageData = try message.serializedData()
  200. try call.sendMessage(data:messageData, errorHandler:errorHandler)
  201. }
  202. func closeAndReceive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws {
  203. do {
  204. try call.receiveMessage() {(responseData) in
  205. if let responseData = responseData,
  206. let response = try? Echo_EchoResponse(serializedData:responseData) {
  207. completion(response, nil)
  208. } else {
  209. completion(nil, Echo_EchoClientError.invalidMessageReceived)
  210. }
  211. }
  212. try call.close(completion:{})
  213. } catch (let error) {
  214. throw error
  215. }
  216. }
  217. func cancel() {
  218. call.cancel()
  219. }
  220. }
  221. /// Update (Bidirectional Streaming)
  222. internal protocol Echo_EchoUpdateCall {
  223. /// Call this to wait for a result. Blocking.
  224. func receive() throws -> Echo_EchoResponse
  225. /// Call this to wait for a result. Nonblocking.
  226. func receive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws
  227. /// Call this to send each message in the request stream.
  228. func send(_ message:Echo_EchoRequest, errorHandler:@escaping (Error)->()) throws
  229. /// Call this to close the sending connection. Blocking.
  230. func closeSend() throws
  231. /// Call this to close the sending connection. Nonblocking.
  232. func closeSend(completion:@escaping ()->()) throws
  233. /// Cancel the call.
  234. func cancel()
  235. }
  236. internal extension Echo_EchoUpdateCall {
  237. func receive() throws -> Echo_EchoResponse {
  238. var returnError : Echo_EchoClientError?
  239. var returnMessage : Echo_EchoResponse!
  240. let sem = DispatchSemaphore(value: 0)
  241. do {
  242. try receive() {response, error in
  243. returnMessage = response
  244. returnError = error
  245. sem.signal()
  246. }
  247. _ = sem.wait(timeout: DispatchTime.distantFuture)
  248. }
  249. if let returnError = returnError {
  250. throw returnError
  251. }
  252. return returnMessage
  253. }
  254. func closeSend() throws {
  255. let sem = DispatchSemaphore(value: 0)
  256. try closeSend() {
  257. sem.signal()
  258. }
  259. _ = sem.wait(timeout: DispatchTime.distantFuture)
  260. }
  261. }
  262. fileprivate final class Echo_EchoUpdateCallImpl: Echo_EchoUpdateCall {
  263. private var call : Call
  264. /// Create a call.
  265. init(_ channel: Channel) {
  266. self.call = channel.makeCall("/echo.Echo/Update")
  267. }
  268. /// Call this to start a call. Nonblocking.
  269. func start(metadata:Metadata, completion:@escaping (CallResult)->())
  270. throws -> Echo_EchoUpdateCall {
  271. try self.call.start(.bidiStreaming, metadata:metadata, completion:completion)
  272. return self
  273. }
  274. func receive(completion:@escaping (Echo_EchoResponse?, Echo_EchoClientError?)->()) throws {
  275. do {
  276. try call.receiveMessage() {(data) in
  277. if let data = data {
  278. if let returnMessage = try? Echo_EchoResponse(serializedData:data) {
  279. completion(returnMessage, nil)
  280. } else {
  281. completion(nil, Echo_EchoClientError.invalidMessageReceived)
  282. }
  283. } else {
  284. completion(nil, Echo_EchoClientError.endOfStream)
  285. }
  286. }
  287. }
  288. }
  289. func send(_ message:Echo_EchoRequest, errorHandler:@escaping (Error)->()) throws {
  290. let messageData = try message.serializedData()
  291. try call.sendMessage(data:messageData, errorHandler:errorHandler)
  292. }
  293. func closeSend(completion:@escaping ()->()) throws {
  294. try call.close() {
  295. completion()
  296. }
  297. }
  298. func cancel() {
  299. call.cancel()
  300. }
  301. }
  302. /// Instantiate Echo_EchoServiceImpl, then call methods of this protocol to make API calls.
  303. internal protocol Echo_EchoService {
  304. var channel: Channel { get }
  305. /// This metadata will be sent with all requests.
  306. var metadata: Metadata { get }
  307. /// This property allows the service host name to be overridden.
  308. /// For example, it can be used to make calls to "localhost:8080"
  309. /// appear to be to "example.com".
  310. var host : String { get }
  311. /// This property allows the service timeout to be overridden.
  312. var timeout : TimeInterval { get }
  313. /// Synchronous. Unary.
  314. func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse
  315. /// Asynchronous. Unary.
  316. func get(_ request: Echo_EchoRequest,
  317. completion: @escaping (Echo_EchoResponse?, CallResult)->()) throws -> Echo_EchoGetCall
  318. /// Asynchronous. Server-streaming.
  319. /// Send the initial message.
  320. /// Use methods on the returned object to get streamed responses.
  321. func expand(_ request: Echo_EchoRequest, completion: @escaping (CallResult)->())
  322. throws -> Echo_EchoExpandCall
  323. /// Asynchronous. Client-streaming.
  324. /// Use methods on the returned object to stream messages and
  325. /// to close the connection and wait for a final response.
  326. func collect(completion: @escaping (CallResult)->())
  327. throws -> Echo_EchoCollectCall
  328. /// Asynchronous. Bidirectional-streaming.
  329. /// Use methods on the returned object to stream messages,
  330. /// to wait for replies, and to close the connection.
  331. func update(completion: @escaping (CallResult)->())
  332. throws -> Echo_EchoUpdateCall
  333. }
  334. internal final class Echo_EchoServiceClient: Echo_EchoService {
  335. internal private(set) var channel: Channel
  336. internal var metadata : Metadata
  337. internal var host : String {
  338. get {
  339. return self.channel.host
  340. }
  341. set {
  342. self.channel.host = newValue
  343. }
  344. }
  345. internal var timeout : TimeInterval {
  346. get {
  347. return self.channel.timeout
  348. }
  349. set {
  350. self.channel.timeout = newValue
  351. }
  352. }
  353. /// Create a client.
  354. internal init(address: String, secure: Bool = true) {
  355. gRPC.initialize()
  356. channel = Channel(address:address, secure:secure)
  357. metadata = Metadata()
  358. }
  359. /// Create a client that makes secure connections with a custom certificate and (optional) hostname.
  360. internal init(address: String, certificates: String, host: String?) {
  361. gRPC.initialize()
  362. channel = Channel(address:address, certificates:certificates, host:host)
  363. metadata = Metadata()
  364. }
  365. /// Synchronous. Unary.
  366. internal func get(_ request: Echo_EchoRequest)
  367. throws
  368. -> Echo_EchoResponse {
  369. return try Echo_EchoGetCallImpl(channel).run(request:request, metadata:metadata)
  370. }
  371. /// Asynchronous. Unary.
  372. internal func get(_ request: Echo_EchoRequest,
  373. completion: @escaping (Echo_EchoResponse?, CallResult)->())
  374. throws
  375. -> Echo_EchoGetCall {
  376. return try Echo_EchoGetCallImpl(channel).start(request:request,
  377. metadata:metadata,
  378. completion:completion)
  379. }
  380. /// Asynchronous. Server-streaming.
  381. /// Send the initial message.
  382. /// Use methods on the returned object to get streamed responses.
  383. internal func expand(_ request: Echo_EchoRequest, completion: @escaping (CallResult)->())
  384. throws
  385. -> Echo_EchoExpandCall {
  386. return try Echo_EchoExpandCallImpl(channel).start(request:request, metadata:metadata, completion:completion)
  387. }
  388. /// Asynchronous. Client-streaming.
  389. /// Use methods on the returned object to stream messages and
  390. /// to close the connection and wait for a final response.
  391. internal func collect(completion: @escaping (CallResult)->())
  392. throws
  393. -> Echo_EchoCollectCall {
  394. return try Echo_EchoCollectCallImpl(channel).start(metadata:metadata, completion:completion)
  395. }
  396. /// Asynchronous. Bidirectional-streaming.
  397. /// Use methods on the returned object to stream messages,
  398. /// to wait for replies, and to close the connection.
  399. internal func update(completion: @escaping (CallResult)->())
  400. throws
  401. -> Echo_EchoUpdateCall {
  402. return try Echo_EchoUpdateCallImpl(channel).start(metadata:metadata, completion:completion)
  403. }
  404. }
  405. /// Type for errors thrown from generated server code.
  406. internal enum Echo_EchoServerError : Error {
  407. case endOfStream
  408. }
  409. /// To build a server, implement a class that conforms to this protocol.
  410. internal protocol Echo_EchoProvider {
  411. func get(request : Echo_EchoRequest, session : Echo_EchoGetSession) throws -> Echo_EchoResponse
  412. func expand(request : Echo_EchoRequest, session : Echo_EchoExpandSession) throws
  413. func collect(session : Echo_EchoCollectSession) throws
  414. func update(session : Echo_EchoUpdateSession) throws
  415. }
  416. /// Common properties available in each service session.
  417. internal protocol Echo_EchoSession {
  418. var requestMetadata : Metadata { get }
  419. var statusCode : StatusCode { get }
  420. var statusMessage : String { get }
  421. var initialMetadata : Metadata { get }
  422. var trailingMetadata : Metadata { get }
  423. }
  424. fileprivate class Echo_EchoSessionImpl: Echo_EchoSession {
  425. var handler : Handler
  426. var requestMetadata : Metadata { return handler.requestMetadata }
  427. var statusCode : StatusCode = .ok
  428. var statusMessage : String = "OK"
  429. var initialMetadata : Metadata = Metadata()
  430. var trailingMetadata : Metadata = Metadata()
  431. init(handler:Handler) {
  432. self.handler = handler
  433. }
  434. }
  435. // Get (Unary Streaming)
  436. internal protocol Echo_EchoGetSession : Echo_EchoSession { }
  437. fileprivate final class Echo_EchoGetSessionImpl : Echo_EchoSessionImpl, Echo_EchoGetSession {
  438. private var provider : Echo_EchoProvider
  439. /// Create a session.
  440. init(handler:Handler, provider: Echo_EchoProvider) {
  441. self.provider = provider
  442. super.init(handler:handler)
  443. }
  444. /// Run the session. Internal.
  445. func run(queue:DispatchQueue) throws {
  446. try handler.receiveMessage(initialMetadata:initialMetadata) {(requestData) in
  447. if let requestData = requestData {
  448. let requestMessage = try Echo_EchoRequest(serializedData:requestData)
  449. let replyMessage = try self.provider.get(request:requestMessage, session: self)
  450. try self.handler.sendResponse(message:replyMessage.serializedData(),
  451. statusCode:self.statusCode,
  452. statusMessage:self.statusMessage,
  453. trailingMetadata:self.trailingMetadata)
  454. }
  455. }
  456. }
  457. }
  458. // Expand (Server Streaming)
  459. internal protocol Echo_EchoExpandSession : Echo_EchoSession {
  460. /// Send a message. Nonblocking.
  461. func send(_ response: Echo_EchoResponse, completion: @escaping ()->()) throws
  462. }
  463. fileprivate final class Echo_EchoExpandSessionImpl : Echo_EchoSessionImpl, Echo_EchoExpandSession {
  464. private var provider : Echo_EchoProvider
  465. /// Create a session.
  466. init(handler:Handler, provider: Echo_EchoProvider) {
  467. self.provider = provider
  468. super.init(handler:handler)
  469. }
  470. func send(_ response: Echo_EchoResponse, completion: @escaping ()->()) throws {
  471. try handler.sendResponse(message:response.serializedData()) {completion()}
  472. }
  473. /// Run the session. Internal.
  474. func run(queue:DispatchQueue) throws {
  475. try self.handler.receiveMessage(initialMetadata:initialMetadata) {(requestData) in
  476. if let requestData = requestData {
  477. do {
  478. let requestMessage = try Echo_EchoRequest(serializedData:requestData)
  479. // to keep providers from blocking the server thread,
  480. // we dispatch them to another queue.
  481. queue.async {
  482. do {
  483. try self.provider.expand(request:requestMessage, session: self)
  484. try self.handler.sendStatus(statusCode:self.statusCode,
  485. statusMessage:self.statusMessage,
  486. trailingMetadata:self.trailingMetadata,
  487. completion:{})
  488. } catch (let error) {
  489. print("error: \(error)")
  490. }
  491. }
  492. } catch (let error) {
  493. print("error: \(error)")
  494. }
  495. }
  496. }
  497. }
  498. }
  499. // Collect (Client Streaming)
  500. internal protocol Echo_EchoCollectSession : Echo_EchoSession {
  501. /// Receive a message. Blocks until a message is received or the client closes the connection.
  502. func receive() throws -> Echo_EchoRequest
  503. /// Send a response and close the connection.
  504. func sendAndClose(_ response: Echo_EchoResponse) throws
  505. }
  506. fileprivate final class Echo_EchoCollectSessionImpl : Echo_EchoSessionImpl, Echo_EchoCollectSession {
  507. private var provider : Echo_EchoProvider
  508. /// Create a session.
  509. init(handler:Handler, provider: Echo_EchoProvider) {
  510. self.provider = provider
  511. super.init(handler:handler)
  512. }
  513. func receive() throws -> Echo_EchoRequest {
  514. let sem = DispatchSemaphore(value: 0)
  515. var requestMessage : Echo_EchoRequest?
  516. try self.handler.receiveMessage() {(requestData) in
  517. if let requestData = requestData {
  518. requestMessage = try? Echo_EchoRequest(serializedData:requestData)
  519. }
  520. sem.signal()
  521. }
  522. _ = sem.wait(timeout: DispatchTime.distantFuture)
  523. if requestMessage == nil {
  524. throw Echo_EchoServerError.endOfStream
  525. }
  526. return requestMessage!
  527. }
  528. func sendAndClose(_ response: Echo_EchoResponse) throws {
  529. try self.handler.sendResponse(message:response.serializedData(),
  530. statusCode:self.statusCode,
  531. statusMessage:self.statusMessage,
  532. trailingMetadata:self.trailingMetadata)
  533. }
  534. /// Run the session. Internal.
  535. func run(queue:DispatchQueue) throws {
  536. try self.handler.sendMetadata(initialMetadata:initialMetadata) {
  537. queue.async {
  538. do {
  539. try self.provider.collect(session:self)
  540. } catch (let error) {
  541. print("error \(error)")
  542. }
  543. }
  544. }
  545. }
  546. }
  547. // Update (Bidirectional Streaming)
  548. internal protocol Echo_EchoUpdateSession : Echo_EchoSession {
  549. /// Receive a message. Blocks until a message is received or the client closes the connection.
  550. func receive() throws -> Echo_EchoRequest
  551. /// Send a message. Nonblocking.
  552. func send(_ response: Echo_EchoResponse, completion: @escaping ()->()) throws
  553. /// Close a connection. Blocks until the connection is closed.
  554. func close() throws
  555. }
  556. fileprivate final class Echo_EchoUpdateSessionImpl : Echo_EchoSessionImpl, Echo_EchoUpdateSession {
  557. private var provider : Echo_EchoProvider
  558. /// Create a session.
  559. init(handler:Handler, provider: Echo_EchoProvider) {
  560. self.provider = provider
  561. super.init(handler:handler)
  562. }
  563. func receive() throws -> Echo_EchoRequest {
  564. let sem = DispatchSemaphore(value: 0)
  565. var requestMessage : Echo_EchoRequest?
  566. try self.handler.receiveMessage() {(requestData) in
  567. if let requestData = requestData {
  568. do {
  569. requestMessage = try Echo_EchoRequest(serializedData:requestData)
  570. } catch (let error) {
  571. print("error \(error)")
  572. }
  573. }
  574. sem.signal()
  575. }
  576. _ = sem.wait(timeout: DispatchTime.distantFuture)
  577. if let requestMessage = requestMessage {
  578. return requestMessage
  579. } else {
  580. throw Echo_EchoServerError.endOfStream
  581. }
  582. }
  583. func send(_ response: Echo_EchoResponse, completion: @escaping ()->()) throws {
  584. try handler.sendResponse(message:response.serializedData()) {completion()}
  585. }
  586. func close() throws {
  587. let sem = DispatchSemaphore(value: 0)
  588. try self.handler.sendStatus(statusCode:self.statusCode,
  589. statusMessage:self.statusMessage,
  590. trailingMetadata:self.trailingMetadata) {
  591. sem.signal()
  592. }
  593. _ = sem.wait(timeout: DispatchTime.distantFuture)
  594. }
  595. /// Run the session. Internal.
  596. func run(queue:DispatchQueue) throws {
  597. try self.handler.sendMetadata(initialMetadata:initialMetadata) {
  598. queue.async {
  599. do {
  600. try self.provider.update(session:self)
  601. } catch (let error) {
  602. print("error \(error)")
  603. }
  604. }
  605. }
  606. }
  607. }
  608. /// Main server for generated service
  609. internal final class Echo_EchoServer {
  610. private var address: String
  611. private var server: Server
  612. private var provider: Echo_EchoProvider?
  613. /// Create a server that accepts insecure connections.
  614. internal init(address:String,
  615. provider:Echo_EchoProvider) {
  616. gRPC.initialize()
  617. self.address = address
  618. self.provider = provider
  619. self.server = Server(address:address)
  620. }
  621. /// Create a server that accepts secure connections.
  622. internal init?(address:String,
  623. certificateURL:URL,
  624. keyURL:URL,
  625. provider:Echo_EchoProvider) {
  626. gRPC.initialize()
  627. self.address = address
  628. self.provider = provider
  629. guard
  630. let certificate = try? String(contentsOf: certificateURL, encoding: .utf8),
  631. let key = try? String(contentsOf: keyURL, encoding: .utf8)
  632. else {
  633. return nil
  634. }
  635. self.server = Server(address:address, key:key, certs:certificate)
  636. }
  637. /// Start the server.
  638. internal func start(queue:DispatchQueue = DispatchQueue.global()) {
  639. guard let provider = self.provider else {
  640. fatalError() // the server requires a provider
  641. }
  642. server.run {(handler) in
  643. let unwrappedHost = handler.host ?? "(nil)"
  644. let unwrappedMethod = handler.method ?? "(nil)"
  645. let unwrappedCaller = handler.caller ?? "(nil)"
  646. print("Server received request to " + unwrappedHost
  647. + " calling " + unwrappedMethod
  648. + " from " + unwrappedCaller
  649. + " with " + handler.requestMetadata.description)
  650. do {
  651. switch unwrappedMethod {
  652. case "/echo.Echo/Get":
  653. try Echo_EchoGetSessionImpl(handler:handler, provider:provider).run(queue:queue)
  654. case "/echo.Echo/Expand":
  655. try Echo_EchoExpandSessionImpl(handler:handler, provider:provider).run(queue:queue)
  656. case "/echo.Echo/Collect":
  657. try Echo_EchoCollectSessionImpl(handler:handler, provider:provider).run(queue:queue)
  658. case "/echo.Echo/Update":
  659. try Echo_EchoUpdateSessionImpl(handler:handler, provider:provider).run(queue:queue)
  660. default:
  661. // handle unknown requests
  662. try handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
  663. try handler.sendResponse(statusCode:.unimplemented,
  664. statusMessage:"unknown method " + unwrappedMethod,
  665. trailingMetadata:Metadata())
  666. }
  667. }
  668. } catch (let error) {
  669. print("Server error: \(error)")
  670. }
  671. }
  672. }
  673. }