root/cherokee/trunk/cherokee/balancer.c

Revision 2073, 4.8 kB (checked in by alo, 1 week ago)

--

Line 
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* Cherokee
4  *
5  * Authors:
6  *      Alvaro Lopez Ortega <alvaro@alobbs.com>
7  *
8  * Copyright (C) 2001-2008 Alvaro Lopez Ortega
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of version 2 of the GNU General Public
12  * License as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  */
24
25 #include "common-internal.h"
26 #include "balancer.h"
27 #include "plugin_loader.h"
28 #include "server-protected.h"
29 #include "source_interpreter.h"
30
31 #define DEFAULT_SOURCES_ALLOCATION 5
32
33
34 ret_t
35 cherokee_balancer_init_base (cherokee_balancer_t *balancer, cherokee_plugin_info_t *info)
36 {
37         /* Init the base class
38          */
39         cherokee_module_init_base (MODULE(balancer), NULL, info);
40
41         /* Virtual methods
42          */
43         balancer->dispatch     = NULL;
44         balancer->configure    = NULL;
45        
46         /* Sources
47          */
48         balancer->sources_len  = 0;
49         balancer->sources_size = 0;
50         balancer->sources      = NULL;
51
52         return ret_ok;
53 }
54
55
56 ret_t
57 cherokee_balancer_mrproper (cherokee_balancer_t *balancer)
58 {
59         /* There is nothing to free here
60          */
61         return ret_ok;
62 }
63
64
65 ret_t
66 cherokee_balancer_configure_base (cherokee_balancer_t    *balancer,
67                                   cherokee_server_t      *srv,
68                                   cherokee_config_node_t *conf)
69 {
70         ret_t                   ret;
71         cherokee_list_t        *i;
72         cherokee_source_t      *src;
73         cherokee_config_node_t *subconf  = NULL;
74         cherokee_config_node_t *subconf2 = NULL;
75
76         /* Look for the type of the source objects
77          */
78         ret = cherokee_config_node_get (conf, "source", &subconf);
79         if (ret != ret_ok) {
80                 PRINT_ERROR_S ("ERROR: Balancer: An entry 'source' is needed\n");
81                 return ret_error;
82         }
83
84         cherokee_config_node_foreach (i, subconf) {
85                 subconf2 = CONFIG_NODE(i);
86
87                 ret = cherokee_avl_get (&srv->sources, &subconf2->val, (void **)&src);
88                 if (ret != ret_ok) {
89                         PRINT_ERROR ("Could not find source '%s'\n", subconf2->val.buf);
90                         return ret_error;
91                 }
92
93                 cherokee_balancer_add_source (balancer, src);
94         }
95
96         return ret_ok;
97 }
98
99
100 static ret_t
101 alloc_more_sources (cherokee_balancer_t *balancer)
102 {
103         size_t size;
104
105         if (balancer->sources == NULL) {
106                 size = DEFAULT_SOURCES_ALLOCATION * sizeof(cherokee_source_t *);
107                 balancer->sources = (cherokee_source_t **) malloc (size);
108         } else {
109                 size = (balancer->sources_size + DEFAULT_SOURCES_ALLOCATION ) * sizeof(cherokee_source_t *);
110                 balancer->sources = (cherokee_source_t **) realloc (balancer->sources, size);
111         }
112        
113         if (balancer->sources == NULL)
114                 return ret_nomem;
115        
116         memset (balancer->sources + balancer->sources_len, 0, DEFAULT_SOURCES_ALLOCATION);
117
118         balancer->sources_size += DEFAULT_SOURCES_ALLOCATION;
119         return ret_ok;
120 }
121
122
123 ret_t
124 cherokee_balancer_add_source (cherokee_balancer_t *balancer, cherokee_source_t *source)
125 {
126         ret_t ret;
127        
128         if (balancer->sources_len >= balancer->sources_size) {
129                 ret = alloc_more_sources (balancer);
130                 if (ret != ret_ok) return ret;
131         }
132
133         balancer->sources[balancer->sources_len] = source;
134         balancer->sources_len++;
135
136         return ret_ok;
137 }
138
139
140 ret_t
141 cherokee_balancer_dispatch (cherokee_balancer_t *balancer, cherokee_connection_t *conn, cherokee_source_t **source)
142 {
143         if (balancer->dispatch == NULL)
144                 return ret_error;
145
146         return balancer->dispatch (balancer, conn, source);
147 }
148
149
150 ret_t
151 cherokee_balancer_free (cherokee_balancer_t *bal)
152 {
153         ret_t              ret;
154         cherokee_module_t *module = MODULE(bal);
155
156         /* Sanity check
157          */
158         if (module->free == NULL) {
159                 return ret_error;
160         }
161        
162         /* Call the virtual method implementation
163          */
164         ret = MODULE(bal)->free (bal);
165         if (unlikely (ret < ret_ok)) return ret;
166        
167         /* Free the rest
168          */
169         cherokee_balancer_mrproper(bal);
170
171         free (bal);
172         return ret_ok;
173 }
174
175
176 ret_t
177 cherokee_balancer_instance (cherokee_buffer_t       *name,
178                             cherokee_config_node_t  *conf,
179                             cherokee_server_t       *srv,
180                             cherokee_balancer_t    **balancer)
181 {
182         ret_t                      ret;
183         balancer_new_func_t        new_func;
184         balancer_configure_func_t  config_func;
185         cherokee_plugin_info_t    *info = NULL;
186        
187         if (cherokee_buffer_is_empty (name)) {
188                 PRINT_ERROR_S ("Balancer defined without a value\n");
189                 return ret_error;
190         }
191
192         ret = cherokee_plugin_loader_get (&srv->loader, name->buf, &info);
193         if (ret != ret_ok) return ret;
194        
195         new_func = (balancer_new_func_t) info->instance;
196         ret = new_func (balancer);
197         if (ret != ret_ok) return ret;
198
199
200         config_func = (balancer_configure_func_t) info->configure;
201         ret = config_func (*balancer, srv, conf);
202         if (ret != ret_ok) return ret;
203
204         return ret_ok;
205 }
Note: See TracBrowser for help on using the browser.