From e0b7fe56f90be8d95bd47a126dc7f53145e821ae Mon Sep 17 00:00:00 2001 From: Yuchen Dai Date: Fri, 6 Nov 2020 10:03:53 +0000 Subject: [PATCH] add internal related factory Signed-off-by: Yuchen Dai --- include/envoy/network/address.h | 4 ++ source/extensions/io_socket/BUILD | 27 ++++++++++ source/extensions/io_socket/address_map.h | 27 ++++++++++ .../io_socket/buffered_io_socket/BUILD | 14 ++++++ .../io_socket/buffered_io_socket/b_factory.cc | 1 + .../io_socket/buffered_io_socket/b_factory.h | 24 +++++++++ source/extensions/io_socket/config.h | 49 +++++++++++++++++++ .../io_socket/buffered_io_socket/BUILD | 9 ++++ .../buffered_io_socket/b_factory_test.cc | 38 ++++++++++++++ 9 files changed, 193 insertions(+) create mode 100644 source/extensions/io_socket/address_map.h create mode 100644 source/extensions/io_socket/buffered_io_socket/b_factory.cc create mode 100644 source/extensions/io_socket/buffered_io_socket/b_factory.h create mode 100644 source/extensions/io_socket/config.h create mode 100644 test/extensions/io_socket/buffered_io_socket/b_factory_test.cc diff --git a/include/envoy/network/address.h b/include/envoy/network/address.h index bd28205bd6305..ff49ea8841b28 100644 --- a/include/envoy/network/address.h +++ b/include/envoy/network/address.h @@ -134,6 +134,10 @@ class EnvoyInternalAddress { enum class Type { Ip, Pipe, EnvoyInternal }; +constexpr absl::string_view IpName = "Ip"; +constexpr absl::string_view PipeName = "Pipe"; +constexpr absl::string_view EnvoyInternalName = "EnvoyInternal"; + /** * Interface for all network addresses. */ diff --git a/source/extensions/io_socket/BUILD b/source/extensions/io_socket/BUILD index 90e061ad8da39..3c5ca2e172f75 100644 --- a/source/extensions/io_socket/BUILD +++ b/source/extensions/io_socket/BUILD @@ -1,8 +1,35 @@ load( "//bazel:envoy_build_system.bzl", + "envoy_cc_extension", + "envoy_cc_library", "envoy_extension_package", ) licenses(["notice"]) # Apache 2 envoy_extension_package() + +envoy_cc_extension( + name = "address_map", + hdrs = [ + "address_map.h", + ], + security_posture = "unknown", + status = "alpha", + deps = [ + "//include/envoy/network:address_interface", + ], +) + +envoy_cc_library( + name = "client_connection_factory", + hdrs = ["config.h"], + deps = [ + ":address_map", + "//include/envoy/config:typed_config_interface", + "//include/envoy/network:connection_interface", + "//include/envoy/registry", + "//source/common/common:assert_lib", + "//source/common/singleton:const_singleton", + ], +) diff --git a/source/extensions/io_socket/address_map.h b/source/extensions/io_socket/address_map.h new file mode 100644 index 0000000000000..1892715ab6d17 --- /dev/null +++ b/source/extensions/io_socket/address_map.h @@ -0,0 +1,27 @@ +#pragma once + +#include "envoy/network/address.h" + +namespace Envoy { +namespace Network { +namespace Address { + +class AddressMap { +public: + constexpr absl::string_view operator[](Type address_type) const { + switch (address_type) { + case Type::Ip: + return IpName; + case Type::Pipe: + return PipeName; + case Type::EnvoyInternal: + return EnvoyInternalName; + } + } +}; + +constexpr AddressMap addressMap; + +} // namespace Address +} // namespace Network +} // namespace Envoy \ No newline at end of file diff --git a/source/extensions/io_socket/buffered_io_socket/BUILD b/source/extensions/io_socket/buffered_io_socket/BUILD index a2dad3494947b..f97aaee61f667 100644 --- a/source/extensions/io_socket/buffered_io_socket/BUILD +++ b/source/extensions/io_socket/buffered_io_socket/BUILD @@ -1,6 +1,7 @@ load( "//bazel:envoy_build_system.bzl", "envoy_cc_extension", + "envoy_cc_library", "envoy_extension_package", ) @@ -38,3 +39,16 @@ envoy_cc_extension( "//source/common/network:default_socket_interface_lib", ], ) + +envoy_cc_library( + name = "b_factory", + srcs = [ + "b_factory.cc", + ], + hdrs = [ + "b_factory.h", + ], + deps = [ + "//source/extensions/io_socket:client_connection_factory", + ], +) diff --git a/source/extensions/io_socket/buffered_io_socket/b_factory.cc b/source/extensions/io_socket/buffered_io_socket/b_factory.cc new file mode 100644 index 0000000000000..395e9d86b7e42 --- /dev/null +++ b/source/extensions/io_socket/buffered_io_socket/b_factory.cc @@ -0,0 +1 @@ +#include "extensions/io_socket/buffered_io_socket/b_factory.h" diff --git a/source/extensions/io_socket/buffered_io_socket/b_factory.h b/source/extensions/io_socket/buffered_io_socket/b_factory.h new file mode 100644 index 0000000000000..f6eb40f554cc6 --- /dev/null +++ b/source/extensions/io_socket/buffered_io_socket/b_factory.h @@ -0,0 +1,24 @@ +#pragma once + +#include "envoy/network/address.h" +#include "extensions/io_socket/config.h" + +namespace Envoy { +namespace Extensions { +namespace IoSocket { + +class BioClientConnectionFactory : public ClientConnectionFactory { +public: + ~BioClientConnectionFactory() override = default; + + /** + * Create a particular client connection. + * @return ClientConnectionPtr the client connection. + */ + Network::ClientConnectionPtr createClientConnection() override { return nullptr; } + + std::string name() const override { return std::string(Network::Address::EnvoyInternalName); } +}; +} // namespace IoSocket +} // namespace Extensions +} // namespace Envoy \ No newline at end of file diff --git a/source/extensions/io_socket/config.h b/source/extensions/io_socket/config.h new file mode 100644 index 0000000000000..cb8153d71ef3d --- /dev/null +++ b/source/extensions/io_socket/config.h @@ -0,0 +1,49 @@ +#pragma once + +#include +#include + +#include "envoy/buffer/buffer.h" +#include "envoy/config/typed_config.h" +#include "envoy/registry/registry.h" +#include "envoy/network/connection.h" + +#include "extensions/io_socket/address_map.h" + +#include "common/common/assert.h" +#include "common/singleton/const_singleton.h" + +namespace Envoy { +namespace Extensions { +namespace IoSocket { + +class ClientConnectionFactory : public Envoy::Config::UntypedFactory { +public: + ~ClientConnectionFactory() override = default; + + /** + * Create a particular client connection. + * @return ClientConnectionPtr the client connection. + */ + virtual Network::ClientConnectionPtr createClientConnection() PURE; + + std::string category() const override { return "envoy.connection"; } + + /** + * Convenience method to lookup a factory by destination address type. + * @return ClientConnectionFactory& for the destiantion address. + */ + static ClientConnectionFactory* + getFactoryByAddress(Network::Address::InstanceConstSharedPtr& destination_address) { + if (destination_address == nullptr) { + return nullptr; + } + + return Registry::FactoryRegistry::getFactory( + Network::Address::addressMap[destination_address->type()]); + } +}; + +} // namespace IoSocket +} // namespace Extensions +} // namespace Envoy \ No newline at end of file diff --git a/test/extensions/io_socket/buffered_io_socket/BUILD b/test/extensions/io_socket/buffered_io_socket/BUILD index 82b16c32c7d01..8a89b462acf20 100644 --- a/test/extensions/io_socket/buffered_io_socket/BUILD +++ b/test/extensions/io_socket/buffered_io_socket/BUILD @@ -46,3 +46,12 @@ envoy_cc_test( "//test/test_common:utility_lib", ], ) + +envoy_cc_test( + name = "b_factory_test", + srcs = ["b_factory_test.cc"], + deps = [ + "//source/extensions/io_socket/buffered_io_socket:b_factory", + "//test/test_common:registry_lib", + ], +) diff --git a/test/extensions/io_socket/buffered_io_socket/b_factory_test.cc b/test/extensions/io_socket/buffered_io_socket/b_factory_test.cc new file mode 100644 index 0000000000000..65eeaaec1a273 --- /dev/null +++ b/test/extensions/io_socket/buffered_io_socket/b_factory_test.cc @@ -0,0 +1,38 @@ + +#include "common/network/address_impl.h" + +#include "extensions/io_socket/buffered_io_socket/b_factory.h" +#include "test/test_common/registry.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +using testing::_; +using testing::NiceMock; + +namespace Envoy { +namespace Extensions { +namespace IoSocket { +namespace BufferedIoSocket { +namespace { + +class FactoryTest : public testing::Test {}; + +TEST_F(FactoryTest, TestCreate) { + BioClientConnectionFactory factory; + Registry::InjectFactory registration(factory); + // TODO(lambdai): share_ptr covariance + auto address = std::dynamic_pointer_cast( + std::make_shared("abc")); + auto* f = ClientConnectionFactory::getFactoryByAddress(address); + // Registered. + ASSERT_NE(nullptr, f); + auto connection = f->createClientConnection(); + // Naive implemention. + ASSERT_EQ(nullptr, connection); +} +} // namespace +} // namespace BufferedIoSocket +} // namespace IoSocket +} // namespace Extensions +} // namespace Envoy \ No newline at end of file