This is an automated email from the ASF dual-hosted git repository.
wangdan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pegasus.git
The following commit(s) were added to refs/heads/master by this push:
new fe0e0284d feat(FQDN): Implement of struct rpc_host_port (#1430)
fe0e0284d is described below
commit fe0e0284daab6afc544fa21e06ef95bb9291f88c
Author: liguohao <[email protected]>
AuthorDate: Tue Apr 11 14:44:30 2023 +0800
feat(FQDN): Implement of struct rpc_host_port (#1430)
https://github.com/apache/incubator-pegasus/issues/1404
Implement of class `rpc_host_port` which is base on class `rpc_address`.
The purpose is to replace address with host_port achieve to be able to
communicate through fqdn not only ip.
---
src/runtime/rpc/rpc_host_port.cpp | 101 ++++++++++++++++++++++++++++++++++
src/runtime/rpc/rpc_host_port.h | 106 ++++++++++++++++++++++++++++++++++++
src/runtime/test/host_port_test.cpp | 77 ++++++++++++++++++++++++++
3 files changed, 284 insertions(+)
diff --git a/src/runtime/rpc/rpc_host_port.cpp
b/src/runtime/rpc/rpc_host_port.cpp
new file mode 100644
index 000000000..9c71a7f26
--- /dev/null
+++ b/src/runtime/rpc/rpc_host_port.cpp
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <netinet/in.h>
+#include <utility>
+
+#include "fmt/core.h"
+#include "runtime/rpc/rpc_host_port.h"
+#include "utils/utils.h"
+
+namespace dsn {
+
+const host_port host_port::s_invalid_host_port;
+
+host_port::host_port(std::string host, uint16_t port)
+ : _host(std::move(host)), _port(port), _type(HOST_TYPE_IPV4)
+{
+ CHECK_NE_MSG(rpc_address::ipv4_from_host(_host.c_str()), 0, "invalid
hostname: {}", _host);
+}
+
+host_port::host_port(rpc_address addr)
+{
+ switch (addr.type()) {
+ case HOST_TYPE_IPV4: {
+ CHECK(utils::hostname_from_ip(htonl(addr.ip()), &_host),
+ "invalid address {}",
+ addr.ipv4_str());
+ _port = addr.port();
+ } break;
+ case HOST_TYPE_GROUP:
+ CHECK(false, "type HOST_TYPE_GROUP not support!");
+ default:
+ break;
+ }
+ _type = addr.type();
+}
+
+void host_port::reset()
+{
+ switch (type()) {
+ case HOST_TYPE_IPV4:
+ _host.clear();
+ _port = 0;
+ break;
+ case HOST_TYPE_GROUP:
+ CHECK(false, "type HOST_TYPE_GROUP not support!");
+ default:
+ break;
+ }
+ _type = HOST_TYPE_INVALID;
+}
+
+host_port &host_port::operator=(const host_port &other)
+{
+ if (this == &other) {
+ return *this;
+ }
+
+ reset();
+ switch (other.type()) {
+ case HOST_TYPE_IPV4:
+ _host = other.host();
+ _port = other.port();
+ break;
+ case HOST_TYPE_GROUP:
+ CHECK(false, "type HOST_TYPE_GROUP not support!");
+ default:
+ break;
+ }
+ _type = other.type();
+ return *this;
+}
+
+std::string host_port::to_string() const
+{
+ switch (type()) {
+ case HOST_TYPE_IPV4:
+ return fmt::format("{}:{}", _host, _port);
+ case HOST_TYPE_GROUP:
+ CHECK(false, "type HOST_TYPE_GROUP not support!");
+ default:
+ return "invalid address";
+ }
+}
+} // namespace dsn
diff --git a/src/runtime/rpc/rpc_host_port.h b/src/runtime/rpc/rpc_host_port.h
new file mode 100644
index 000000000..864a43f0e
--- /dev/null
+++ b/src/runtime/rpc/rpc_host_port.h
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+// IWYU pragma: no_include <experimental/string_view>
+#include <stdint.h>
+#include <cstddef>
+#include <iosfwd>
+#include <string>
+
+#include "runtime/rpc/rpc_address.h"
+#include "utils/fmt_logging.h"
+
+namespace dsn {
+
+class host_port
+{
+public:
+ static const host_port s_invalid_host_port;
+ explicit host_port() = default;
+ explicit host_port(std::string host, uint16_t port);
+ explicit host_port(rpc_address addr);
+
+ host_port(const host_port &other) { *this = other; }
+ host_port &operator=(const host_port &other);
+
+ void reset();
+ ~host_port() { reset(); }
+
+ dsn_host_type_t type() const { return _type; }
+ const std::string &host() const { return _host; }
+ uint16_t port() const { return _port; }
+
+ bool is_invalid() const { return _type == HOST_TYPE_INVALID; }
+
+ std::string to_string() const;
+
+ friend std::ostream &operator<<(std::ostream &os, const host_port &hp)
+ {
+ return os << hp.to_string();
+ }
+
+private:
+ std::string _host;
+ uint16_t _port = 0;
+ dsn_host_type_t _type = HOST_TYPE_INVALID;
+};
+
+inline bool operator==(const host_port &hp1, const host_port &hp2)
+{
+ if (&hp1 == &hp2) {
+ return true;
+ }
+
+ if (hp1.type() != hp2.type()) {
+ return false;
+ }
+
+ switch (hp1.type()) {
+ case HOST_TYPE_IPV4:
+ return hp1.host() == hp2.host() && hp1.port() == hp2.port();
+ case HOST_TYPE_GROUP:
+ CHECK(false, "type HOST_TYPE_GROUP not support!");
+ default:
+ return true;
+ }
+}
+
+inline bool operator!=(const host_port &hp1, const host_port &hp2) { return
!(hp1 == hp2); }
+
+} // namespace dsn
+
+namespace std {
+template <>
+struct hash<::dsn::host_port>
+{
+ size_t operator()(const ::dsn::host_port &hp) const
+ {
+ switch (hp.type()) {
+ case HOST_TYPE_IPV4:
+ return std::hash<std::string>()(hp.host()) ^
std::hash<uint16_t>()(hp.port());
+ case HOST_TYPE_GROUP:
+ CHECK(false, "type HOST_TYPE_GROUP not support!");
+ default:
+ return 0;
+ }
+ }
+};
+} // namespace std
diff --git a/src/runtime/test/host_port_test.cpp
b/src/runtime/test/host_port_test.cpp
new file mode 100644
index 000000000..e1adc74de
--- /dev/null
+++ b/src/runtime/test/host_port_test.cpp
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <gtest/gtest.h>
+#include <gtest/gtest-message.h>
+#include <gtest/gtest-test-part.h>
+#include <string>
+
+#include "runtime/rpc/rpc_address.h"
+#include "runtime/rpc/rpc_host_port.h"
+
+namespace dsn {
+
+TEST(host_port_test, host_port_to_string)
+{
+ {
+ host_port hp = host_port("localhost", 8080);
+ ASSERT_EQ(std::string("localhost:8080"), hp.to_string());
+ }
+
+ {
+ host_port hp;
+ ASSERT_EQ(std::string("invalid address"), hp.to_string());
+ }
+}
+
+TEST(host_port_test, host_port_build)
+{
+ host_port hp = host_port("localhost", 8080);
+ ASSERT_EQ(HOST_TYPE_IPV4, hp.type());
+ ASSERT_EQ(8080, hp.port());
+ ASSERT_EQ("localhost", hp.host());
+
+ {
+ rpc_address addr = rpc_address("localhost", 8080);
+ host_port hp1(addr);
+ ASSERT_EQ(hp, hp1);
+ }
+}
+
+TEST(host_port_test, operators)
+{
+ host_port hp("localhost", 8080);
+ ASSERT_EQ(hp, hp);
+
+ {
+ host_port new_hp(hp);
+ ASSERT_EQ(hp, new_hp);
+ }
+
+ {
+ host_port new_hp("localhost", 8081);
+ ASSERT_NE(hp, new_hp);
+ }
+
+ host_port hp2;
+ ASSERT_NE(hp, hp2);
+ ASSERT_FALSE(hp.is_invalid());
+ ASSERT_TRUE(hp2.is_invalid());
+}
+} // namespace dsn
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]