EchoProvider.swift 3.3 KB

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