Common.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2021, 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 GRPC
  17. import NIO
  18. func makeEchoServer(
  19. group: EventLoopGroup,
  20. host: String = "127.0.0.1",
  21. port: Int = 0,
  22. interceptors: Echo_EchoServerInterceptorFactoryProtocol? = nil
  23. ) -> EventLoopFuture<Server> {
  24. return Server.insecure(group: group)
  25. .withServiceProviders([MinimalEchoProvider(interceptors: interceptors)])
  26. .bind(host: host, port: port)
  27. }
  28. func makeClientConnection(
  29. group: EventLoopGroup,
  30. host: String = "127.0.0.1",
  31. port: Int
  32. ) -> ClientConnection {
  33. return ClientConnection.insecure(group: group)
  34. .connect(host: host, port: port)
  35. }
  36. func makeEchoClientInterceptors(count: Int) -> Echo_EchoClientInterceptorFactoryProtocol? {
  37. let factory = EchoClientInterceptors()
  38. for _ in 0 ..< count {
  39. factory.register { NoOpEchoClientInterceptor() }
  40. }
  41. return factory
  42. }
  43. func makeEchoServerInterceptors(count: Int) -> Echo_EchoServerInterceptorFactoryProtocol? {
  44. let factory = EchoServerInterceptors()
  45. for _ in 0 ..< count {
  46. factory.register { NoOpEchoServerInterceptor() }
  47. }
  48. return factory
  49. }
  50. final class EchoClientInterceptors: Echo_EchoClientInterceptorFactoryProtocol {
  51. internal typealias Factory = () -> ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>
  52. private var factories: [Factory] = []
  53. internal init(_ factories: Factory...) {
  54. self.factories = factories
  55. }
  56. internal func register(_ factory: @escaping Factory) {
  57. self.factories.append(factory)
  58. }
  59. private func makeInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  60. return self.factories.map { $0() }
  61. }
  62. func makeGetInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  63. return self.makeInterceptors()
  64. }
  65. func makeExpandInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  66. return self.makeInterceptors()
  67. }
  68. func makeCollectInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  69. return self.makeInterceptors()
  70. }
  71. func makeUpdateInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  72. return self.makeInterceptors()
  73. }
  74. }
  75. internal final class EchoServerInterceptors: Echo_EchoServerInterceptorFactoryProtocol {
  76. internal typealias Factory = () -> ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>
  77. private var factories: [Factory] = []
  78. internal init(_ factories: Factory...) {
  79. self.factories = factories
  80. }
  81. internal func register(_ factory: @escaping Factory) {
  82. self.factories.append(factory)
  83. }
  84. private func makeInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  85. return self.factories.map { $0() }
  86. }
  87. func makeGetInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  88. return self.makeInterceptors()
  89. }
  90. func makeExpandInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  91. return self.makeInterceptors()
  92. }
  93. func makeCollectInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  94. return self.makeInterceptors()
  95. }
  96. func makeUpdateInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  97. return self.makeInterceptors()
  98. }
  99. }
  100. final class NoOpEchoClientInterceptor: ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse> {}
  101. final class NoOpEchoServerInterceptor: ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse> {}