Debian Codegraph @

src/core/lib/resolver/server_address.cc

raw
1 /*
2 *
3 * Copyright 2018 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/resolver/server_address.h"
22
23 #include <string.h>
24
25 #include <algorithm>
26 #include <memory>
27 #include <string>
28 #include <utility>
29 #include <vector>
30
31 #include "absl/status/status.h"
32 #include "absl/status/statusor.h"
33 #include "absl/strings/str_cat.h"
34 #include "absl/strings/str_format.h"
35 #include "absl/strings/str_join.h"
36
37 #include "src/core/lib/address_utils/sockaddr_utils.h"
38 #include "src/core/lib/channel/channel_args.h"
39
40 // IWYU pragma: no_include <sys/socket.h>
41
42 namespace grpc_core {
43
44 //
45 // ServerAddressWeightAttribute
46 //
47 const char* ServerAddressWeightAttribute::kServerAddressWeightAttributeKey =
48 "server_address_weight";
49
50 //
51 // ServerAddress
52 //
53
54 ServerAddress::ServerAddress(
55 const grpc_resolved_address& address, const ChannelArgs& args,
56 std::map<const char*, std::unique_ptr<AttributeInterface>> attributes)
57 : address_(address), args_(args), attributes_(std::move(attributes)) {}
58
59 ServerAddress::ServerAddress(
60 const void* address, size_t address_len, const ChannelArgs& args,
61 std::map<const char*, std::unique_ptr<AttributeInterface>> attributes)
62 : args_(args), attributes_(std::move(attributes)) {
63 memcpy(address_.addr, address, address_len);
64 address_.len = static_cast<socklen_t>(address_len);
65 }
66
67 ServerAddress::ServerAddress(const ServerAddress& other)
68 : address_(other.address_), args_(other.args_) {
69 for (const auto& p : other.attributes_) {
70 attributes_[p.first] = p.second->Copy();
71 }
72 }
73 ServerAddress& ServerAddress::operator=(const ServerAddress& other) {
74 if (&other == this) {
75 return *this;
76 }
77 address_ = other.address_;
78 args_ = other.args_;
79 attributes_.clear();
80 for (const auto& p : other.attributes_) {
81 attributes_[p.first] = p.second->Copy();
82 }
83 return *this;
84 }
85
86 ServerAddress::ServerAddress(ServerAddress&& other) noexcept
87 : address_(other.address_),
88 args_(std::move(other.args_)),
89 attributes_(std::move(other.attributes_)) {}
90
91 ServerAddress& ServerAddress::operator=(ServerAddress&& other) noexcept {
92 address_ = other.address_;
93 args_ = std::move(other.args_);
94 attributes_ = std::move(other.attributes_);
95 return *this;
96 }
97
98 namespace {
99
100 int CompareAttributes(
101 const std::map<const char*,
102 std::unique_ptr<ServerAddress::AttributeInterface>>&
103 attributes1,
104 const std::map<const char*,
105 std::unique_ptr<ServerAddress::AttributeInterface>>&
106 attributes2) {
107 auto it2 = attributes2.begin();
108 for (auto it1 = attributes1.begin(); it1 != attributes1.end(); ++it1) {
109 // attributes2 has fewer elements than attributes1
110 if (it2 == attributes2.end()) return -1;
111 // compare keys
112 int retval = strcmp(it1->first, it2->first);
113 if (retval != 0) return retval;
114 // compare values
115 retval = it1->second->Cmp(it2->second.get());
116 if (retval != 0) return retval;
117 ++it2;
118 }
119 // attributes1 has fewer elements than attributes2
120 if (it2 != attributes2.end()) return 1;
121 // equal
122 return 0;
123 }
124
125 } // namespace
126
127 int ServerAddress::Cmp(const ServerAddress& other) const {
128 if (address_.len > other.address_.len) return 1;
129 if (address_.len < other.address_.len) return -1;
130 int retval = memcmp(address_.addr, other.address_.addr, address_.len);
131 if (retval != 0) return retval;
132 retval = QsortCompare(args_, other.args_);
133 if (retval != 0) return retval;
134 return CompareAttributes(attributes_, other.attributes_);
135 }
136
137 const ServerAddress::AttributeInterface* ServerAddress::GetAttribute(
138 const char* key) const {
139 auto it = attributes_.find(key);
140 if (it == attributes_.end()) return nullptr;
141 return it->second.get();
142 }
143
144 // Returns a copy of the address with a modified attribute.
145 // If the new value is null, the attribute is removed.
146 ServerAddress ServerAddress::WithAttribute(
147 const char* key, std::unique_ptr<AttributeInterface> value) const {
148 ServerAddress address = *this;
149 if (value == nullptr) {
150 address.attributes_.erase(key);
151 } else {
152 address.attributes_[key] = std::move(value);
153 }
154 return address;
155 }
156
157 std::string ServerAddress::ToString() const {
158 auto addr_str = grpc_sockaddr_to_string(&address_, false);
159 std::vector<std::string> parts = {
160 addr_str.ok() ? addr_str.value() : addr_str.status().ToString(),
161 };
162 if (args_ != ChannelArgs()) {
163 parts.emplace_back(absl::StrCat("args=", args_.ToString()));
164 }
165 if (!attributes_.empty()) {
166 std::vector<std::string> attrs;
167 attrs.reserve(attributes_.size());
168 for (const auto& p : attributes_) {
169 attrs.emplace_back(absl::StrCat(p.first, "=", p.second->ToString()));
170 }
171 parts.emplace_back(
172 absl::StrCat("attributes={", absl::StrJoin(attrs, ", "), "}"));
173 }
174 return absl::StrJoin(parts, " ");
175 }
176
177 std::string ServerAddressWeightAttribute::ToString() const {
178 return absl::StrFormat("%d", weight_);
179 }
180
181 } // namespace grpc_core