Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include "src/core/client_config/lb_policy_registry.h"
35 :
36 : #include <string.h>
37 :
38 : #define MAX_POLICIES 10
39 :
40 : static grpc_lb_policy_factory *g_all_of_the_lb_policies[MAX_POLICIES];
41 : static int g_number_of_lb_policies = 0;
42 :
43 : static grpc_lb_policy_factory *g_default_lb_policy_factory;
44 :
45 2501 : void grpc_lb_policy_registry_init(grpc_lb_policy_factory *default_factory) {
46 2501 : g_number_of_lb_policies = 0;
47 2501 : g_default_lb_policy_factory = default_factory;
48 2501 : }
49 :
50 0 : void grpc_lb_policy_registry_shutdown(void) {
51 : int i;
52 0 : for (i = 0; i < g_number_of_lb_policies; i++) {
53 0 : grpc_lb_policy_factory_unref(g_all_of_the_lb_policies[i]);
54 : }
55 0 : }
56 :
57 5002 : void grpc_register_lb_policy(grpc_lb_policy_factory *factory) {
58 : int i;
59 7503 : for (i = 0; i < g_number_of_lb_policies; i++) {
60 2501 : GPR_ASSERT(0 != strcmp(factory->vtable->name,
61 : g_all_of_the_lb_policies[i]->vtable->name));
62 : }
63 5002 : GPR_ASSERT(g_number_of_lb_policies != MAX_POLICIES);
64 5002 : grpc_lb_policy_factory_ref(factory);
65 5002 : g_all_of_the_lb_policies[g_number_of_lb_policies++] = factory;
66 5002 : }
67 :
68 1445 : static grpc_lb_policy_factory *lookup_factory(const char *name) {
69 : int i;
70 :
71 1445 : if (name == NULL) return NULL;
72 :
73 1450 : for (i = 0; i < g_number_of_lb_policies; i++) {
74 1450 : if (0 == strcmp(name, g_all_of_the_lb_policies[i]->vtable->name)) {
75 1445 : return g_all_of_the_lb_policies[i];
76 : }
77 : }
78 :
79 0 : return NULL;
80 : }
81 :
82 1445 : grpc_lb_policy *grpc_lb_policy_create(const char *name,
83 : grpc_lb_policy_args *args) {
84 1445 : grpc_lb_policy_factory *factory = lookup_factory(name);
85 1445 : grpc_lb_policy *lb_policy =
86 : grpc_lb_policy_factory_create_lb_policy(factory, args);
87 1445 : return lb_policy;
88 : }
|