EmbeddedGRPCChannel.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2020, 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 NIO
  17. import NIOHTTP2
  18. import Logging
  19. // This is currently intended for internal testing only.
  20. class EmbeddedGRPCChannel: GRPCChannel {
  21. let embeddedChannel: EmbeddedChannel
  22. let multiplexer: EventLoopFuture<HTTP2StreamMultiplexer>
  23. let logger: Logger
  24. let scheme: String
  25. let authority: String
  26. let errorDelegate: ClientErrorDelegate?
  27. func close() -> EventLoopFuture<Void> {
  28. return embeddedChannel.close()
  29. }
  30. var eventLoop: EventLoop {
  31. return self.embeddedChannel.eventLoop
  32. }
  33. init(errorDelegate: ClientErrorDelegate? = nil) {
  34. let embeddedChannel = EmbeddedChannel()
  35. self.embeddedChannel = embeddedChannel
  36. let logger = Logger(subsystem: .clientChannel)
  37. self.logger = logger
  38. self.multiplexer = embeddedChannel.configureGRPCClient(
  39. errorDelegate: errorDelegate,
  40. logger: logger
  41. ).flatMap {
  42. embeddedChannel.pipeline.handler(type: HTTP2StreamMultiplexer.self)
  43. }
  44. self.scheme = "http"
  45. self.authority = "localhost"
  46. self.errorDelegate = errorDelegate
  47. }
  48. func makeUnaryCall<Request: GRPCPayload, Response: GRPCPayload>(
  49. path: String,
  50. request: Request,
  51. callOptions: CallOptions
  52. ) -> UnaryCall<Request, Response> where Request : GRPCPayload, Response : GRPCPayload {
  53. return UnaryCall(
  54. path: path,
  55. scheme: self.scheme,
  56. authority: self.authority,
  57. callOptions: callOptions,
  58. eventLoop: self.eventLoop,
  59. multiplexer: self.multiplexer,
  60. errorDelegate: self.errorDelegate,
  61. logger: self.logger,
  62. request: request
  63. )
  64. }
  65. func makeClientStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  66. path: String,
  67. callOptions: CallOptions
  68. ) -> ClientStreamingCall<Request, Response> {
  69. return ClientStreamingCall(
  70. path: path,
  71. scheme: self.scheme,
  72. authority: self.authority,
  73. callOptions: callOptions,
  74. eventLoop: self.eventLoop,
  75. multiplexer: self.multiplexer,
  76. errorDelegate: self.errorDelegate,
  77. logger: self.logger
  78. )
  79. }
  80. func makeServerStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  81. path: String,
  82. request: Request,
  83. callOptions: CallOptions,
  84. handler: @escaping (Response) -> Void
  85. ) -> ServerStreamingCall<Request, Response> {
  86. return ServerStreamingCall(
  87. path: path,
  88. scheme: self.scheme,
  89. authority: self.authority,
  90. callOptions: callOptions,
  91. eventLoop: self.eventLoop,
  92. multiplexer: self.multiplexer,
  93. errorDelegate: self.errorDelegate,
  94. logger: self.logger,
  95. request: request,
  96. handler: handler
  97. )
  98. }
  99. func makeBidirectionalStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  100. path: String,
  101. callOptions: CallOptions,
  102. handler: @escaping (Response) -> Void
  103. ) -> BidirectionalStreamingCall<Request, Response> {
  104. return BidirectionalStreamingCall(
  105. path: path,
  106. scheme: self.scheme,
  107. authority: self.authority,
  108. callOptions: callOptions,
  109. eventLoop: self.eventLoop,
  110. multiplexer: self.multiplexer,
  111. errorDelegate: self.errorDelegate,
  112. logger: self.logger,
  113. handler: handler
  114. )
  115. }
  116. }