• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Channel arg key for a string indicating an address's balancer name.
27 #define GRPC_ARG_ADDRESS_BALANCER_NAME "grpc.address_balancer_name"
28 
29 namespace grpc_core {
30 
31 namespace {
32 
BalancerAddressesArgCopy(void * p)33 void* BalancerAddressesArgCopy(void* p) {
34   ServerAddressList* address_list = static_cast<ServerAddressList*>(p);
35   return new ServerAddressList(*address_list);
36 }
37 
BalancerAddressesArgDestroy(void * p)38 void BalancerAddressesArgDestroy(void* p) {
39   ServerAddressList* address_list = static_cast<ServerAddressList*>(p);
40   delete address_list;
41 }
42 
BalancerAddressesArgCmp(void * p,void * q)43 int BalancerAddressesArgCmp(void* p, void* q) {
44   ServerAddressList* address_list1 = static_cast<ServerAddressList*>(p);
45   ServerAddressList* address_list2 = static_cast<ServerAddressList*>(q);
46   if (address_list1 == nullptr || address_list2 == nullptr) {
47     return GPR_ICMP(address_list1, address_list2);
48   }
49   if (address_list1->size() > address_list2->size()) return 1;
50   if (address_list1->size() < address_list2->size()) return -1;
51   for (size_t i = 0; i < address_list1->size(); ++i) {
52     int retval = (*address_list1)[i].Cmp((*address_list2)[i]);
53     if (retval != 0) return retval;
54   }
55   return 0;
56 }
57 
58 const grpc_arg_pointer_vtable kBalancerAddressesArgVtable = {
59     BalancerAddressesArgCopy, BalancerAddressesArgDestroy,
60     BalancerAddressesArgCmp};
61 
62 }  // namespace
63 
CreateGrpclbBalancerAddressesArg(const ServerAddressList * address_list)64 grpc_arg CreateGrpclbBalancerAddressesArg(
65     const ServerAddressList* address_list) {
66   return grpc_channel_arg_pointer_create(
67       const_cast<char*>(GRPC_ARG_GRPCLB_BALANCER_ADDRESSES),
68       const_cast<ServerAddressList*>(address_list),
69       &kBalancerAddressesArgVtable);
70 }
71 
FindGrpclbBalancerAddressesInChannelArgs(const grpc_channel_args & args)72 const ServerAddressList* FindGrpclbBalancerAddressesInChannelArgs(
73     const grpc_channel_args& args) {
74   return grpc_channel_args_find_pointer<const ServerAddressList>(
75       &args, const_cast<char*>(GRPC_ARG_GRPCLB_BALANCER_ADDRESSES));
76 }
77 
CreateGrpclbBalancerNameArg(const char * balancer_name)78 grpc_arg CreateGrpclbBalancerNameArg(const char* balancer_name) {
79   return grpc_channel_arg_string_create(
80       const_cast<char*>(GRPC_ARG_ADDRESS_BALANCER_NAME),
81       const_cast<char*>(balancer_name));
82 }
83 
FindGrpclbBalancerNameInChannelArgs(const grpc_channel_args & args)84 const char* FindGrpclbBalancerNameInChannelArgs(const grpc_channel_args& args) {
85   return grpc_channel_args_find_string(
86       &args, const_cast<char*>(GRPC_ARG_ADDRESS_BALANCER_NAME));
87 }
88 
89 }  // namespace grpc_core
90