echo.grpc.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. internal protocol Echo_EchoGetCall: ClientCallUnary { }
  34. fileprivate final class Echo_EchoGetCallImpl: ClientCallUnaryImpl<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoGetCall {
  35. override class var method: String { return "/echo.Echo/Get" }
  36. }
  37. internal protocol Echo_EchoExpandCall: ClientCallServerStreamingBase {
  38. /// Call this to wait for a result. Blocking.
  39. func receive() throws -> Echo_EchoResponse
  40. /// Call this to wait for a result. Nonblocking.
  41. func receive(completion: @escaping (Echo_EchoResponse?, ClientError?) -> Void) throws
  42. }
  43. fileprivate final class Echo_EchoExpandCallImpl: ClientCallServerStreamingImpl<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoExpandCall {
  44. override class var method: String { return "/echo.Echo/Expand" }
  45. }
  46. class Echo_EchoExpandCallTestStub: ClientCallServerStreamingTestStub<Echo_EchoResponse>, Echo_EchoExpandCall {
  47. override class var method: String { return "/echo.Echo/Expand" }
  48. }
  49. internal protocol Echo_EchoCollectCall: ClientCallClientStreamingBase {
  50. /// Call this to send each message in the request stream. Nonblocking.
  51. func send(_ message: Echo_EchoRequest, errorHandler: @escaping (Error) -> Void) throws
  52. /// Call this to close the connection and wait for a response. Blocking.
  53. func closeAndReceive() throws -> Echo_EchoResponse
  54. /// Call this to close the connection and wait for a response. Nonblocking.
  55. func closeAndReceive(completion: @escaping (Echo_EchoResponse?, ClientError?) -> Void) throws
  56. }
  57. fileprivate final class Echo_EchoCollectCallImpl: ClientCallClientStreamingImpl<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectCall {
  58. override class var method: String { return "/echo.Echo/Collect" }
  59. }
  60. /// Simple fake implementation of Echo_EchoCollectCall
  61. /// stores sent values for later verification and finall returns a previously-defined result.
  62. class Echo_EchoCollectCallTestStub: ClientCallClientStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectCall {
  63. override class var method: String { return "/echo.Echo/Collect" }
  64. }
  65. internal protocol Echo_EchoUpdateCall: ClientCallBidirectionalStreamingBase {
  66. /// Call this to wait for a result. Blocking.
  67. func receive() throws -> Echo_EchoResponse
  68. /// Call this to wait for a result. Nonblocking.
  69. func receive(completion: @escaping (Echo_EchoResponse?, ClientError?) -> Void) throws
  70. /// Call this to send each message in the request stream.
  71. func send(_ message: Echo_EchoRequest, errorHandler: @escaping (Error) -> Void) throws
  72. /// Call this to close the sending connection. Blocking.
  73. func closeSend() throws
  74. /// Call this to close the sending connection. Nonblocking.
  75. func closeSend(completion: (() -> Void)?) throws
  76. }
  77. fileprivate final class Echo_EchoUpdateCallImpl: ClientCallBidirectionalStreamingImpl<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateCall {
  78. override class var method: String { return "/echo.Echo/Update" }
  79. }
  80. class Echo_EchoUpdateCallTestStub: ClientCallBidirectionalStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateCall {
  81. override class var method: String { return "/echo.Echo/Update" }
  82. }
  83. /// Instantiate Echo_EchoServiceImpl, then call methods of this protocol to make API calls.
  84. internal protocol Echo_EchoService {
  85. var channel: Channel { get }
  86. /// This metadata will be sent with all requests.
  87. var metadata: Metadata { get }
  88. /// This property allows the service host name to be overridden.
  89. /// For example, it can be used to make calls to "localhost:8080"
  90. /// appear to be to "example.com".
  91. var host : String { get }
  92. /// This property allows the service timeout to be overridden.
  93. var timeout : TimeInterval { get }
  94. /// Synchronous. Unary.
  95. func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse
  96. /// Asynchronous. Unary.
  97. func get(_ request: Echo_EchoRequest,
  98. completion: @escaping (Echo_EchoResponse?, CallResult)->()) throws -> Echo_EchoGetCall
  99. /// Asynchronous. Server-streaming.
  100. /// Send the initial message.
  101. /// Use methods on the returned object to get streamed responses.
  102. func expand(_ request: Echo_EchoRequest, completion: ((CallResult)->())?)
  103. throws -> Echo_EchoExpandCall
  104. /// Asynchronous. Client-streaming.
  105. /// Use methods on the returned object to stream messages and
  106. /// to close the connection and wait for a final response.
  107. func collect(completion: ((CallResult)->())?)
  108. throws -> Echo_EchoCollectCall
  109. /// Asynchronous. Bidirectional-streaming.
  110. /// Use methods on the returned object to stream messages,
  111. /// to wait for replies, and to close the connection.
  112. func update(completion: ((CallResult)->())?)
  113. throws -> Echo_EchoUpdateCall
  114. }
  115. internal final class Echo_EchoServiceClient: Echo_EchoService {
  116. internal private(set) var channel: Channel
  117. internal var metadata : Metadata
  118. internal var host : String {
  119. get {
  120. return self.channel.host
  121. }
  122. set {
  123. self.channel.host = newValue
  124. }
  125. }
  126. internal var timeout : TimeInterval {
  127. get {
  128. return self.channel.timeout
  129. }
  130. set {
  131. self.channel.timeout = newValue
  132. }
  133. }
  134. /// Create a client.
  135. internal init(address: String, secure: Bool = true) {
  136. gRPC.initialize()
  137. channel = Channel(address:address, secure:secure)
  138. metadata = Metadata()
  139. }
  140. /// Create a client that makes secure connections with a custom certificate and (optional) hostname.
  141. internal init(address: String, certificates: String, host: String?) {
  142. gRPC.initialize()
  143. channel = Channel(address:address, certificates:certificates, host:host)
  144. metadata = Metadata()
  145. }
  146. /// Synchronous. Unary.
  147. internal func get(_ request: Echo_EchoRequest)
  148. throws
  149. -> Echo_EchoResponse {
  150. return try Echo_EchoGetCallImpl(channel).run(request:request, metadata:metadata)
  151. }
  152. /// Asynchronous. Unary.
  153. internal func get(_ request: Echo_EchoRequest,
  154. completion: @escaping (Echo_EchoResponse?, CallResult)->())
  155. throws
  156. -> Echo_EchoGetCall {
  157. return try Echo_EchoGetCallImpl(channel).start(request:request,
  158. metadata:metadata,
  159. completion:completion)
  160. }
  161. /// Asynchronous. Server-streaming.
  162. /// Send the initial message.
  163. /// Use methods on the returned object to get streamed responses.
  164. internal func expand(_ request: Echo_EchoRequest, completion: ((CallResult)->())?)
  165. throws
  166. -> Echo_EchoExpandCall {
  167. return try Echo_EchoExpandCallImpl(channel).start(request:request, metadata:metadata, completion:completion)
  168. }
  169. /// Asynchronous. Client-streaming.
  170. /// Use methods on the returned object to stream messages and
  171. /// to close the connection and wait for a final response.
  172. internal func collect(completion: ((CallResult)->())?)
  173. throws
  174. -> Echo_EchoCollectCall {
  175. return try Echo_EchoCollectCallImpl(channel).start(metadata:metadata, completion:completion)
  176. }
  177. /// Asynchronous. Bidirectional-streaming.
  178. /// Use methods on the returned object to stream messages,
  179. /// to wait for replies, and to close the connection.
  180. internal func update(completion: ((CallResult)->())?)
  181. throws
  182. -> Echo_EchoUpdateCall {
  183. return try Echo_EchoUpdateCallImpl(channel).start(metadata:metadata, completion:completion)
  184. }
  185. }
  186. /// Simple fake implementation of Echo_EchoService that returns a previously-defined set of results
  187. /// and stores request values passed into it for later verification.
  188. /// Note: completion blocks are NOT called with this default implementation, and asynchronous unary calls are NOT implemented!
  189. class Echo_EchoServiceTestStub: Echo_EchoService {
  190. var channel: Channel { fatalError("not implemented") }
  191. var metadata = Metadata()
  192. var host = ""
  193. var timeout: TimeInterval = 0
  194. var getRequests: [Echo_EchoRequest] = []
  195. var getResponses: [Echo_EchoResponse] = []
  196. func get(_ request: Echo_EchoRequest) throws -> Echo_EchoResponse {
  197. getRequests.append(request)
  198. defer { getResponses.removeFirst() }
  199. return getResponses.first!
  200. }
  201. func get(_ request: Echo_EchoRequest,
  202. completion: @escaping (Echo_EchoResponse?, CallResult)->()) throws -> Echo_EchoGetCall {
  203. fatalError("not implemented")
  204. }
  205. var expandRequests: [Echo_EchoRequest] = []
  206. var expandCalls: [Echo_EchoExpandCall] = []
  207. func expand(_ request: Echo_EchoRequest, completion: ((CallResult)->())?)
  208. throws -> Echo_EchoExpandCall {
  209. expandRequests.append(request)
  210. defer { expandCalls.removeFirst() }
  211. return expandCalls.first!
  212. }
  213. var collectCalls: [Echo_EchoCollectCall] = []
  214. func collect(completion: ((CallResult)->())?)
  215. throws -> Echo_EchoCollectCall {
  216. defer { collectCalls.removeFirst() }
  217. return collectCalls.first!
  218. }
  219. var updateCalls: [Echo_EchoUpdateCall] = []
  220. func update(completion: ((CallResult)->())?)
  221. throws -> Echo_EchoUpdateCall {
  222. defer { updateCalls.removeFirst() }
  223. return updateCalls.first!
  224. }
  225. }
  226. /// Type for errors thrown from generated server code.
  227. internal enum Echo_EchoServerError : Error {
  228. case endOfStream
  229. }
  230. /// To build a server, implement a class that conforms to this protocol.
  231. internal protocol Echo_EchoProvider {
  232. func get(request : Echo_EchoRequest, session : Echo_EchoGetSession) throws -> Echo_EchoResponse
  233. func expand(request : Echo_EchoRequest, session : Echo_EchoExpandSession) throws
  234. func collect(session : Echo_EchoCollectSession) throws
  235. func update(session : Echo_EchoUpdateSession) throws
  236. }
  237. /// Common properties available in each service session.
  238. internal protocol Echo_EchoSession {
  239. var requestMetadata : Metadata { get }
  240. var statusCode : StatusCode { get }
  241. var statusMessage : String { get }
  242. var initialMetadata : Metadata { get }
  243. var trailingMetadata : Metadata { get }
  244. }
  245. fileprivate class Echo_EchoSessionImpl: Echo_EchoSession {
  246. var handler : Handler
  247. var requestMetadata : Metadata { return handler.requestMetadata }
  248. var statusCode : StatusCode = .ok
  249. var statusMessage : String = "OK"
  250. var initialMetadata : Metadata = Metadata()
  251. var trailingMetadata : Metadata = Metadata()
  252. init(handler:Handler) {
  253. self.handler = handler
  254. }
  255. }
  256. class Echo_EchoSessionTestStub: Echo_EchoSession {
  257. var requestMetadata = Metadata()
  258. var statusCode = StatusCode.ok
  259. var statusMessage = "OK"
  260. var initialMetadata = Metadata()
  261. var trailingMetadata = Metadata()
  262. }
  263. // Get (Unary Streaming)
  264. internal protocol Echo_EchoGetSession : Echo_EchoSession { }
  265. fileprivate final class Echo_EchoGetSessionImpl : Echo_EchoSessionImpl, Echo_EchoGetSession {
  266. private var provider : Echo_EchoProvider
  267. /// Create a session.
  268. init(handler:Handler, provider: Echo_EchoProvider) {
  269. self.provider = provider
  270. super.init(handler:handler)
  271. }
  272. /// Run the session. Internal.
  273. func run(queue:DispatchQueue) throws {
  274. try handler.receiveMessage(initialMetadata:initialMetadata) {(requestData) in
  275. if let requestData = requestData {
  276. let requestMessage = try Echo_EchoRequest(serializedData:requestData)
  277. let replyMessage = try self.provider.get(request:requestMessage, session: self)
  278. try self.handler.sendResponse(message:replyMessage.serializedData(),
  279. statusCode:self.statusCode,
  280. statusMessage:self.statusMessage,
  281. trailingMetadata:self.trailingMetadata)
  282. }
  283. }
  284. }
  285. }
  286. /// Trivial fake implementation of Echo_EchoGetSession.
  287. class Echo_EchoGetSessionTestStub : Echo_EchoSessionTestStub, Echo_EchoGetSession { }
  288. // Expand (Server Streaming)
  289. internal protocol Echo_EchoExpandSession : Echo_EchoSession {
  290. /// Send a message. Nonblocking.
  291. func send(_ response: Echo_EchoResponse, completion: ((Bool)->())?) throws
  292. }
  293. fileprivate final class Echo_EchoExpandSessionImpl : Echo_EchoSessionImpl, Echo_EchoExpandSession {
  294. private var provider : Echo_EchoProvider
  295. /// Create a session.
  296. init(handler:Handler, provider: Echo_EchoProvider) {
  297. self.provider = provider
  298. super.init(handler:handler)
  299. }
  300. func send(_ response: Echo_EchoResponse, completion: ((Bool)->())?) throws {
  301. try handler.sendResponse(message:response.serializedData(), completion: completion)
  302. }
  303. /// Run the session. Internal.
  304. func run(queue:DispatchQueue) throws {
  305. try self.handler.receiveMessage(initialMetadata:initialMetadata) {(requestData) in
  306. if let requestData = requestData {
  307. do {
  308. let requestMessage = try Echo_EchoRequest(serializedData:requestData)
  309. // to keep providers from blocking the server thread,
  310. // we dispatch them to another queue.
  311. queue.async {
  312. do {
  313. try self.provider.expand(request:requestMessage, session: self)
  314. try self.handler.sendStatus(statusCode:self.statusCode,
  315. statusMessage:self.statusMessage,
  316. trailingMetadata:self.trailingMetadata,
  317. completion:nil)
  318. } catch (let error) {
  319. print("error: \(error)")
  320. }
  321. }
  322. } catch (let error) {
  323. print("error: \(error)")
  324. }
  325. }
  326. }
  327. }
  328. }
  329. /// Simple fake implementation of Echo_EchoExpandSession that returns a previously-defined set of results
  330. /// and stores sent values for later verification.
  331. class Echo_EchoExpandSessionTestStub : Echo_EchoSessionTestStub, Echo_EchoExpandSession {
  332. var outputs: [Echo_EchoResponse] = []
  333. func send(_ response: Echo_EchoResponse, completion: ((Bool)->())?) throws {
  334. outputs.append(response)
  335. }
  336. func close() throws { }
  337. }
  338. // Collect (Client Streaming)
  339. internal protocol Echo_EchoCollectSession : Echo_EchoSession {
  340. /// Receive a message. Blocks until a message is received or the client closes the connection.
  341. func receive() throws -> Echo_EchoRequest
  342. /// Send a response and close the connection.
  343. func sendAndClose(_ response: Echo_EchoResponse) throws
  344. }
  345. fileprivate final class Echo_EchoCollectSessionImpl : Echo_EchoSessionImpl, Echo_EchoCollectSession {
  346. private var provider : Echo_EchoProvider
  347. /// Create a session.
  348. init(handler:Handler, provider: Echo_EchoProvider) {
  349. self.provider = provider
  350. super.init(handler:handler)
  351. }
  352. func receive() throws -> Echo_EchoRequest {
  353. let sem = DispatchSemaphore(value: 0)
  354. var requestMessage : Echo_EchoRequest?
  355. try self.handler.receiveMessage() {(requestData) in
  356. if let requestData = requestData {
  357. requestMessage = try? Echo_EchoRequest(serializedData:requestData)
  358. }
  359. sem.signal()
  360. }
  361. _ = sem.wait(timeout: DispatchTime.distantFuture)
  362. if requestMessage == nil {
  363. throw Echo_EchoServerError.endOfStream
  364. }
  365. return requestMessage!
  366. }
  367. func sendAndClose(_ response: Echo_EchoResponse) throws {
  368. try self.handler.sendResponse(message:response.serializedData(),
  369. statusCode:self.statusCode,
  370. statusMessage:self.statusMessage,
  371. trailingMetadata:self.trailingMetadata)
  372. }
  373. /// Run the session. Internal.
  374. func run(queue:DispatchQueue) throws {
  375. try self.handler.sendMetadata(initialMetadata:initialMetadata) { _ in
  376. queue.async {
  377. do {
  378. try self.provider.collect(session:self)
  379. } catch (let error) {
  380. print("error \(error)")
  381. }
  382. }
  383. }
  384. }
  385. }
  386. /// Simple fake implementation of Echo_EchoCollectSession that returns a previously-defined set of results
  387. /// and stores sent values for later verification.
  388. class Echo_EchoCollectSessionTestStub: Echo_EchoSessionTestStub, Echo_EchoCollectSession {
  389. var inputs: [Echo_EchoRequest] = []
  390. var output: Echo_EchoResponse?
  391. func receive() throws -> Echo_EchoRequest {
  392. if let input = inputs.first {
  393. inputs.removeFirst()
  394. return input
  395. } else {
  396. throw Echo_EchoClientError.endOfStream
  397. }
  398. }
  399. func sendAndClose(_ response: Echo_EchoResponse) throws {
  400. output = response
  401. }
  402. func close() throws { }
  403. }
  404. // Update (Bidirectional Streaming)
  405. internal protocol Echo_EchoUpdateSession : Echo_EchoSession {
  406. /// Receive a message. Blocks until a message is received or the client closes the connection.
  407. func receive() throws -> Echo_EchoRequest
  408. /// Send a message. Nonblocking.
  409. func send(_ response: Echo_EchoResponse, completion: ((Bool)->())?) throws
  410. /// Close a connection. Blocks until the connection is closed.
  411. func close() throws
  412. }
  413. fileprivate final class Echo_EchoUpdateSessionImpl : Echo_EchoSessionImpl, Echo_EchoUpdateSession {
  414. private var provider : Echo_EchoProvider
  415. /// Create a session.
  416. init(handler:Handler, provider: Echo_EchoProvider) {
  417. self.provider = provider
  418. super.init(handler:handler)
  419. }
  420. func receive() throws -> Echo_EchoRequest {
  421. let sem = DispatchSemaphore(value: 0)
  422. var requestMessage : Echo_EchoRequest?
  423. try self.handler.receiveMessage() {(requestData) in
  424. if let requestData = requestData {
  425. do {
  426. requestMessage = try Echo_EchoRequest(serializedData:requestData)
  427. } catch (let error) {
  428. print("error \(error)")
  429. }
  430. }
  431. sem.signal()
  432. }
  433. _ = sem.wait(timeout: DispatchTime.distantFuture)
  434. if let requestMessage = requestMessage {
  435. return requestMessage
  436. } else {
  437. throw Echo_EchoServerError.endOfStream
  438. }
  439. }
  440. func send(_ response: Echo_EchoResponse, completion: ((Bool)->())?) throws {
  441. try handler.sendResponse(message:response.serializedData(), completion: completion)
  442. }
  443. func close() throws {
  444. let sem = DispatchSemaphore(value: 0)
  445. try self.handler.sendStatus(statusCode:self.statusCode,
  446. statusMessage:self.statusMessage,
  447. trailingMetadata:self.trailingMetadata) { _ in sem.signal() }
  448. _ = sem.wait(timeout: DispatchTime.distantFuture)
  449. }
  450. /// Run the session. Internal.
  451. func run(queue:DispatchQueue) throws {
  452. try self.handler.sendMetadata(initialMetadata:initialMetadata) { _ in
  453. queue.async {
  454. do {
  455. try self.provider.update(session:self)
  456. } catch (let error) {
  457. print("error \(error)")
  458. }
  459. }
  460. }
  461. }
  462. }
  463. /// Simple fake implementation of Echo_EchoUpdateSession that returns a previously-defined set of results
  464. /// and stores sent values for later verification.
  465. class Echo_EchoUpdateSessionTestStub : Echo_EchoSessionTestStub, Echo_EchoUpdateSession {
  466. var inputs: [Echo_EchoRequest] = []
  467. var outputs: [Echo_EchoResponse] = []
  468. func receive() throws -> Echo_EchoRequest {
  469. if let input = inputs.first {
  470. inputs.removeFirst()
  471. return input
  472. } else {
  473. throw Echo_EchoClientError.endOfStream
  474. }
  475. }
  476. func send(_ response: Echo_EchoResponse, completion: ((Bool)->())?) throws {
  477. outputs.append(response)
  478. }
  479. func close() throws { }
  480. }
  481. /// Main server for generated service
  482. internal final class Echo_EchoServer {
  483. private var address: String
  484. private var server: Server
  485. private var provider: Echo_EchoProvider?
  486. /// Create a server that accepts insecure connections.
  487. internal init(address:String,
  488. provider:Echo_EchoProvider) {
  489. gRPC.initialize()
  490. self.address = address
  491. self.provider = provider
  492. self.server = Server(address:address)
  493. }
  494. /// Create a server that accepts secure connections.
  495. internal init?(address:String,
  496. certificateURL:URL,
  497. keyURL:URL,
  498. provider:Echo_EchoProvider) {
  499. gRPC.initialize()
  500. self.address = address
  501. self.provider = provider
  502. guard
  503. let certificate = try? String(contentsOf: certificateURL, encoding: .utf8),
  504. let key = try? String(contentsOf: keyURL, encoding: .utf8)
  505. else {
  506. return nil
  507. }
  508. self.server = Server(address:address, key:key, certs:certificate)
  509. }
  510. /// Start the server.
  511. internal func start(queue:DispatchQueue = DispatchQueue.global()) {
  512. guard let provider = self.provider else {
  513. fatalError() // the server requires a provider
  514. }
  515. server.run {(handler) in
  516. let unwrappedHost = handler.host ?? "(nil)"
  517. let unwrappedMethod = handler.method ?? "(nil)"
  518. let unwrappedCaller = handler.caller ?? "(nil)"
  519. print("Server received request to " + unwrappedHost
  520. + " calling " + unwrappedMethod
  521. + " from " + unwrappedCaller
  522. + " with " + handler.requestMetadata.description)
  523. do {
  524. switch unwrappedMethod {
  525. case "/echo.Echo/Get":
  526. try Echo_EchoGetSessionImpl(handler:handler, provider:provider).run(queue:queue)
  527. case "/echo.Echo/Expand":
  528. try Echo_EchoExpandSessionImpl(handler:handler, provider:provider).run(queue:queue)
  529. case "/echo.Echo/Collect":
  530. try Echo_EchoCollectSessionImpl(handler:handler, provider:provider).run(queue:queue)
  531. case "/echo.Echo/Update":
  532. try Echo_EchoUpdateSessionImpl(handler:handler, provider:provider).run(queue:queue)
  533. default:
  534. // handle unknown requests
  535. try handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
  536. try handler.sendResponse(statusCode:.unimplemented,
  537. statusMessage:"unknown method " + unwrappedMethod,
  538. trailingMetadata:Metadata())
  539. }
  540. }
  541. } catch (let error) {
  542. print("Server error: \(error)")
  543. }
  544. }
  545. }
  546. }