EchoProvider.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 gRPC
  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. var i = 0
  30. for part in parts {
  31. var response = Echo_EchoResponse()
  32. response.text = "Swift echo expand (\(i)): \(part)"
  33. let sem = DispatchSemaphore(value: 0)
  34. try session.send(response) { _ in sem.signal() }
  35. _ = sem.wait(timeout: DispatchTime.distantFuture)
  36. i += 1
  37. sleep(1)
  38. }
  39. }
  40. // collect collects a sequence of messages and returns them concatenated when the caller closes.
  41. func collect(session: Echo_EchoCollectSession) throws {
  42. var parts: [String] = []
  43. while true {
  44. do {
  45. let request = try session.receive()
  46. parts.append(request.text)
  47. } catch ServerError.endOfStream {
  48. break
  49. } catch (let error) {
  50. print("\(error)")
  51. }
  52. }
  53. var response = Echo_EchoResponse()
  54. response.text = "Swift echo collect: " + parts.joined(separator: " ")
  55. try session.sendAndClose(response)
  56. }
  57. // update streams back messages as they are received in an input stream.
  58. func update(session: Echo_EchoUpdateSession) throws {
  59. var count = 0
  60. while true {
  61. do {
  62. let request = try session.receive()
  63. count += 1
  64. var response = Echo_EchoResponse()
  65. response.text = "Swift echo update (\(count)): \(request.text)"
  66. let sem = DispatchSemaphore(value: 0)
  67. try session.send(response) { _ in sem.signal() }
  68. _ = sem.wait(timeout: DispatchTime.distantFuture)
  69. } catch ServerError.endOfStream {
  70. break
  71. } catch (let error) {
  72. print("\(error)")
  73. }
  74. }
  75. try session.close()
  76. }
  77. }