EchoProvider.swift 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. class EchoProvider : Echo_EchoProvider {
  35. // get returns requests as they were received.
  36. func get(request : Echo_EchoRequest, session : Echo_EchoGetSession) throws -> Echo_EchoResponse {
  37. var response = Echo_EchoResponse()
  38. response.text = "Swift echo get: " + request.text
  39. return response
  40. }
  41. // expand splits a request into words and returns each word in a separate message.
  42. func expand(request : Echo_EchoRequest, session : Echo_EchoExpandSession) throws -> Void {
  43. let parts = request.text.components(separatedBy: " ")
  44. var i = 0
  45. for part in parts {
  46. var response = Echo_EchoResponse()
  47. response.text = "Swift echo expand (\(i)): \(part)"
  48. try session.send(response)
  49. i += 1
  50. sleep(1)
  51. }
  52. }
  53. // collect collects a sequence of messages and returns them concatenated when the caller closes.
  54. func collect(session : Echo_EchoCollectSession) throws -> Void {
  55. var parts : [String] = []
  56. while true {
  57. do {
  58. let request = try session.receive()
  59. parts.append(request.text)
  60. } catch Echo_EchoServerError.endOfStream {
  61. break
  62. } catch (let error) {
  63. print("\(error)")
  64. }
  65. }
  66. var response = Echo_EchoResponse()
  67. response.text = "Swift echo collect: " + parts.joined(separator: " ")
  68. try session.sendAndClose(response)
  69. }
  70. // update streams back messages as they are received in an input stream.
  71. func update(session : Echo_EchoUpdateSession) throws -> Void {
  72. var count = 0
  73. while true {
  74. do {
  75. let request = try session.receive()
  76. count += 1
  77. var response = Echo_EchoResponse()
  78. response.text = "Swift echo update (\(count)): \(request.text)"
  79. try session.send(response)
  80. } catch Echo_EchoServerError.endOfStream {
  81. break
  82. } catch (let error) {
  83. print("\(error)")
  84. }
  85. }
  86. try session.close()
  87. }
  88. }