1 //
2 //
3 // Copyright 2017 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/handshaker/proxy_mapper_registry.h"
22 
23 #include <algorithm>
24 #include <memory>
25 #include <utility>
26 #include <vector>
27 
28 #include "absl/types/optional.h"
29 
30 namespace grpc_core {
31 
Register(bool at_start,std::unique_ptr<ProxyMapperInterface> mapper)32 void ProxyMapperRegistry::Builder::Register(
33     bool at_start, std::unique_ptr<ProxyMapperInterface> mapper) {
34   if (at_start) {
35     mappers_.insert(mappers_.begin(), std::move(mapper));
36   } else {
37     mappers_.emplace_back(std::move(mapper));
38   }
39 }
40 
Build()41 ProxyMapperRegistry ProxyMapperRegistry::Builder::Build() {
42   ProxyMapperRegistry registry;
43   registry.mappers_ = std::move(mappers_);
44   return registry;
45 }
46 
MapName(absl::string_view server_uri,ChannelArgs * args) const47 absl::optional<std::string> ProxyMapperRegistry::MapName(
48     absl::string_view server_uri, ChannelArgs* args) const {
49   ChannelArgs args_backup = *args;
50   for (const auto& mapper : mappers_) {
51     *args = args_backup;
52     auto r = mapper->MapName(server_uri, args);
53     if (r.has_value()) return r;
54   }
55   *args = args_backup;
56   return absl::nullopt;
57 }
58 
MapAddress(const grpc_resolved_address & address,ChannelArgs * args) const59 absl::optional<grpc_resolved_address> ProxyMapperRegistry::MapAddress(
60     const grpc_resolved_address& address, ChannelArgs* args) const {
61   ChannelArgs args_backup = *args;
62   for (const auto& mapper : mappers_) {
63     *args = args_backup;
64     auto r = mapper->MapAddress(address, args);
65     if (r.has_value()) return r;
66   }
67   *args = args_backup;
68   return absl::nullopt;
69 }
70 
71 }  // namespace grpc_core
72