1 //
2 // Copyright 2019 gRPC authors.
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
17 #include <grpc/support/port_platform.h>
18
19 #include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.h"
20
21 #include "src/core/lib/channel/channel_args.h"
22 #include "src/core/lib/gpr/useful.h"
23
24 // Channel arg key for the list of balancer addresses.
25 #define GRPC_ARG_GRPCLB_BALANCER_ADDRESSES "grpc.grpclb_balancer_addresses"
26
27 namespace grpc_core {
28
29 namespace {
30
BalancerAddressesArgCopy(void * p)31 void* BalancerAddressesArgCopy(void* p) {
32 ServerAddressList* address_list = static_cast<ServerAddressList*>(p);
33 return new ServerAddressList(*address_list);
34 }
35
BalancerAddressesArgDestroy(void * p)36 void BalancerAddressesArgDestroy(void* p) {
37 ServerAddressList* address_list = static_cast<ServerAddressList*>(p);
38 delete address_list;
39 }
40
BalancerAddressesArgCmp(void * p,void * q)41 int BalancerAddressesArgCmp(void* p, void* q) {
42 ServerAddressList* address_list1 = static_cast<ServerAddressList*>(p);
43 ServerAddressList* address_list2 = static_cast<ServerAddressList*>(q);
44 if (address_list1 == nullptr || address_list2 == nullptr) {
45 return GPR_ICMP(address_list1, address_list2);
46 }
47 if (address_list1->size() > address_list2->size()) return 1;
48 if (address_list1->size() < address_list2->size()) return -1;
49 for (size_t i = 0; i < address_list1->size(); ++i) {
50 int retval = (*address_list1)[i].Cmp((*address_list2)[i]);
51 if (retval != 0) return retval;
52 }
53 return 0;
54 }
55
56 const grpc_arg_pointer_vtable kBalancerAddressesArgVtable = {
57 BalancerAddressesArgCopy, BalancerAddressesArgDestroy,
58 BalancerAddressesArgCmp};
59
60 } // namespace
61
CreateGrpclbBalancerAddressesArg(const ServerAddressList * address_list)62 grpc_arg CreateGrpclbBalancerAddressesArg(
63 const ServerAddressList* address_list) {
64 return grpc_channel_arg_pointer_create(
65 const_cast<char*>(GRPC_ARG_GRPCLB_BALANCER_ADDRESSES),
66 const_cast<ServerAddressList*>(address_list),
67 &kBalancerAddressesArgVtable);
68 }
69
FindGrpclbBalancerAddressesInChannelArgs(const grpc_channel_args & args)70 const ServerAddressList* FindGrpclbBalancerAddressesInChannelArgs(
71 const grpc_channel_args& args) {
72 return grpc_channel_args_find_pointer<const ServerAddressList>(
73 &args, const_cast<char*>(GRPC_ARG_GRPCLB_BALANCER_ADDRESSES));
74 }
75
76 } // namespace grpc_core
77