EchoProvider.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2016, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Dispatch
  17. import Foundation
  18. import SwiftGRPC
  19. class EchoProvider: Echo_EchoProvider {
  20. // get returns requests as they were received.
  21. func get(request: Echo_EchoRequest, session _: Echo_EchoGetSession) throws -> Echo_EchoResponse {
  22. var response = Echo_EchoResponse()
  23. response.text = "Swift echo get: " + request.text
  24. return response
  25. }
  26. // expand splits a request into words and returns each word in a separate message.
  27. func expand(request: Echo_EchoRequest, session: Echo_EchoExpandSession) throws {
  28. let parts = request.text.components(separatedBy: " ")
  29. for (i, part) in parts.enumerated() {
  30. var response = Echo_EchoResponse()
  31. response.text = "Swift echo expand (\(i)): \(part)"
  32. try session.send(response) {
  33. if let error = $0 {
  34. print("expand error: \(error)")
  35. }
  36. }
  37. }
  38. session.waitForSendOperationsToFinish()
  39. try session.close(withStatus: .ok, completion: nil)
  40. }
  41. // collect collects a sequence of messages and returns them concatenated when the caller closes.
  42. func collect(session: Echo_EchoCollectSession) throws {
  43. var parts: [String] = []
  44. while true {
  45. do {
  46. guard let request = try session.receive()
  47. else { break } // End of stream
  48. parts.append(request.text)
  49. } catch {
  50. print("collect error: \(error)")
  51. break
  52. }
  53. }
  54. var response = Echo_EchoResponse()
  55. response.text = "Swift echo collect: " + parts.joined(separator: " ")
  56. try session.sendAndClose(response: response, status: .ok, completion: nil)
  57. }
  58. // update streams back messages as they are received in an input stream.
  59. func update(session: Echo_EchoUpdateSession) throws {
  60. var count = 0
  61. while true {
  62. do {
  63. guard let request = try session.receive()
  64. else { break } // End of stream
  65. var response = Echo_EchoResponse()
  66. response.text = "Swift echo update (\(count)): \(request.text)"
  67. count += 1
  68. try session.send(response) {
  69. if let error = $0 {
  70. print("update error: \(error)")
  71. }
  72. }
  73. } catch {
  74. print("update error: \(error)")
  75. break
  76. }
  77. }
  78. session.waitForSendOperationsToFinish()
  79. try session.close(withStatus: .ok, completion: nil)
  80. }
  81. }