blob: 6e710e86d9527ca382f40d10f68976b712ede711 [file] [log] [blame]
David Garcia Quintas5c4543d2015-09-03 15:49:56 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
David Garcia Quintas5c4543d2015-09-03 15:49:56 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
David Garcia Quintas5c4543d2015-09-03 15:49:56 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070016 *
17 */
18
Craig Tiller9eb0fde2017-03-31 16:59:30 -070019#include "src/core/ext/filters/client_channel/lb_policy_registry.h"
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070020
21#include <string.h>
22
Mark D. Roth69f783d2016-11-10 10:10:56 -080023#include "src/core/lib/support/string.h"
24
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070025#define MAX_POLICIES 10
26
Craig Tillerbaa14a92017-11-03 09:09:36 -070027static grpc_lb_policy_factory* g_all_of_the_lb_policies[MAX_POLICIES];
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070028static int g_number_of_lb_policies = 0;
29
Craig Tiller3113ef42016-03-29 09:03:14 -070030void grpc_lb_policy_registry_init(void) { g_number_of_lb_policies = 0; }
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070031
Craig Tillera82950e2015-09-22 12:33:20 -070032void grpc_lb_policy_registry_shutdown(void) {
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070033 int i;
Craig Tillera82950e2015-09-22 12:33:20 -070034 for (i = 0; i < g_number_of_lb_policies; i++) {
35 grpc_lb_policy_factory_unref(g_all_of_the_lb_policies[i]);
36 }
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070037}
38
Craig Tillerbaa14a92017-11-03 09:09:36 -070039void grpc_register_lb_policy(grpc_lb_policy_factory* factory) {
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070040 int i;
Craig Tillera82950e2015-09-22 12:33:20 -070041 for (i = 0; i < g_number_of_lb_policies; i++) {
Mark D. Roth69f783d2016-11-10 10:10:56 -080042 GPR_ASSERT(0 != gpr_stricmp(factory->vtable->name,
43 g_all_of_the_lb_policies[i]->vtable->name));
Craig Tillera82950e2015-09-22 12:33:20 -070044 }
45 GPR_ASSERT(g_number_of_lb_policies != MAX_POLICIES);
46 grpc_lb_policy_factory_ref(factory);
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070047 g_all_of_the_lb_policies[g_number_of_lb_policies++] = factory;
48}
49
Craig Tillerbaa14a92017-11-03 09:09:36 -070050static grpc_lb_policy_factory* lookup_factory(const char* name) {
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070051 int i;
52
Craig Tiller4782d922017-11-10 09:53:21 -080053 if (name == nullptr) return nullptr;
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070054
Craig Tillera82950e2015-09-22 12:33:20 -070055 for (i = 0; i < g_number_of_lb_policies; i++) {
Mark D. Roth69f783d2016-11-10 10:10:56 -080056 if (0 == gpr_stricmp(name, g_all_of_the_lb_policies[i]->vtable->name)) {
Craig Tillera82950e2015-09-22 12:33:20 -070057 return g_all_of_the_lb_policies[i];
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070058 }
Craig Tillera82950e2015-09-22 12:33:20 -070059 }
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070060
Craig Tiller4782d922017-11-10 09:53:21 -080061 return nullptr;
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070062}
63
Craig Tillerbaa14a92017-11-03 09:09:36 -070064grpc_lb_policy* grpc_lb_policy_create(grpc_exec_ctx* exec_ctx, const char* name,
65 grpc_lb_policy_args* args) {
66 grpc_lb_policy_factory* factory = lookup_factory(name);
67 grpc_lb_policy* lb_policy =
David Garcia Quintas67c0d042016-03-25 01:37:53 -070068 grpc_lb_policy_factory_create_lb_policy(exec_ctx, factory, args);
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070069 return lb_policy;
70}