]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - fres/resalloc/fra_registry.c
Handle properly errors when manager is not registered
[frescor/frsh.git] / fres / resalloc / fra_registry.c
1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                 */
4 /*                                                                        */
5 /*   Universidad de Cantabria,              SPAIN                         */
6 /*   University of York,                    UK                            */
7 /*   Scuola Superiore Sant'Anna,            ITALY                         */
8 /*   Kaiserslautern University,             GERMANY                       */
9 /*   Univ. Politécnica  Valencia,           SPAIN                        */
10 /*   Czech Technical University in Prague,  CZECH REPUBLIC                */
11 /*   ENEA                                   SWEDEN                        */
12 /*   Thales Communication S.A.              FRANCE                        */
13 /*   Visual Tools S.A.                      SPAIN                         */
14 /*   Rapita Systems Ltd                     UK                            */
15 /*   Evidence                               ITALY                         */
16 /*                                                                        */
17 /*   See http://www.frescor.org for a link to partners' websites          */
18 /*                                                                        */
19 /*          FRESCOR project (FP6/2005/IST/5-034026) is funded             */
20 /*       in part by the European Union Sixth Framework Programme          */
21 /*       The European Union is not liable of any use that may be          */
22 /*       made of this code.                                               */
23 /*                                                                        */
24 /*                                                                        */
25 /*  This file is part of FRSH (FRescor ScHeduler)                         */
26 /*                                                                        */
27 /* FRSH is free software; you can redistribute it and/or modify it        */
28 /* under terms of the GNU General Public License as published by the      */
29 /* Free Software Foundation; either version 2, or (at your option) any    */
30 /* later version.  FRSH is distributed in the hope that it will be        */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
33 /* General Public License for more details. You should have received a    */
34 /* copy of the GNU General Public License along with FRSH; see file       */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
36 /* Cambridge, MA 02139, USA.                                              */
37 /*                                                                        */
38 /* As a special exception, including FRSH header files in a file,         */
39 /* instantiating FRSH generics or templates, or linking other files       */
40 /* with FRSH objects to produce an executable application, does not       */
41 /* by itself cause the resulting executable application to be covered     */
42 /* by the GNU General Public License. This exception does not             */
43 /* however invalidate any other reasons why the executable file might be  */
44 /* covered by the GNU Public License.                                     */
45 /**************************************************************************/
46
47 /**
48  * @file   fra_registry.c
49  * @author Michal Sojka <sojkam1@fel.cvut.cz>
50  * @date   Thu Oct 23 15:26:19 2008
51  * 
52  * @brief  Registry of FRES Resource Allocator.
53  * 
54  * This registry is used by fra_register() and its main purpose is to
55  * allow "late" registration of allocators to FCB. Late registration
56  * means, that the allocator is registered with FCB only when the
57  * application accesses the resource.
58  *
59  * This functionality is useful at least during testing and debugging,
60  * when it is not necessary to run all the resource managers. Only the
61  * managers for resources used by the application are required.
62  */
63 #include <forb.h>
64 #include <fra.h>
65 #include <fcb.h>
66 #include <ul_log.h>
67 #include <ul_gavlcust.h>
68 #include "fra_generic.h"
69 #include <stdbool.h>
70
71 UL_LOG_CUST(ulogd_fra_registry);
72 ul_log_domain_t ulogd_fra_registry = {UL_LOGL_MSG, "fra_registry"};
73
74 #define save_errno(cmd) do { int _e = errno; cmd; errno = _e; } while(0)
75
76 struct registered_allocator {
77         gavl_node_t node;
78         struct fres_allocator *allocator;
79         bool registered_with_fcb;
80 };
81
82 #define FRA_MAX_ALLOCATORS 32
83 static struct registered_allocator allocators[FRA_MAX_ALLOCATORS];
84 static unsigned free_allocator_idx = 0;
85
86 static struct registry {
87         forb_orb                orb;
88         fres_contract_broker    fcb;
89         forb_executor_t         *executor;
90
91         gavl_cust_root_field_t  allocators;
92 } registry;
93
94
95 static inline int fres_allocator_cmp(struct fres_allocator * const *a,
96                                      struct fres_allocator * const *b)
97 {
98         if ((*a)->res_type < (*b)->res_type) {
99                 return -1;
100         } else if ((*a)->res_type > (*b)->res_type) {
101                 return +1;
102         } else if ((*a)->res_id < (*b)->res_id) {
103                 return -1;
104         } else if ((*a)->res_id > (*b)->res_id) {
105                 return +1;
106         } else {
107                 return 0;
108         }
109 }
110
111 GAVL_CUST_NODE_INT_DEC(fra_registry       /* cust_prefix */,
112                        struct registry    /* cust_root_t */,
113                        struct registered_allocator /* cust_item_t */,
114                        struct fres_allocator* /* cust_key_t */,
115                        allocators         /* cust_root_node */,
116                        node               /* cust_item_node */,
117                        allocator          /* cust_item_key */,
118                        fres_allocator_cmp /* cust_cmp_fnc */);
119
120 GAVL_CUST_NODE_INT_IMP(fra_registry       /* cust_prefix */,
121                        struct registry    /* cust_root_t */,
122                        struct registered_allocator /* cust_item_t */,
123                        struct fres_allocator* /* cust_key_t */,
124                        allocators         /* cust_root_node */,
125                        node               /* cust_item_node */,
126                        allocator          /* cust_item_key */,
127                        fres_allocator_cmp /* cust_cmp_fnc */);
128
129
130 /** 
131  * Initializes registry of resource allocators.
132  *
133  * @param orb
134  * @param fcb Contract broker object reference
135  * @param executor Executor for all allocator objects
136  */
137 void fra_registry_init(forb_orb            orb,
138                        fres_contract_broker fcb,
139                        forb_executor_t     *executor)
140 {
141         registry.orb = orb;
142         registry.fcb = fcb;
143         registry.executor = executor;
144
145         fra_registry_init_root_field(&registry);
146 }
147
148 /** 
149  * Registeres the allocator in registry.
150  * 
151  * @param allocator 
152  * 
153  * @return Zero on success, -1 on error.
154  */
155 int fra_register(struct fres_allocator *allocator)
156 {
157         struct registered_allocator *ra;
158         int ret;
159         
160         ra = fra_registry_find(&registry, &allocator);
161         if (!ra && free_allocator_idx < FRA_MAX_ALLOCATORS) {
162                 ra = &allocators[free_allocator_idx];
163                 free_allocator_idx++;
164                 ra->allocator = allocator;
165                 ra->registered_with_fcb = false;
166                 fra_registry_insert(&registry, ra);
167                 ret = 0;
168         } else {
169                 ret = -1;
170         }
171         return ret;
172 }
173
174 /** 
175  * Creates FORB object reference for the allocator and registeres it
176  * with contract broker.
177  * 
178  * @param allocator 
179  * 
180  * @return Zero on success, non-zero error code on error.
181  */
182 static int register_fra_to_fcb(struct fres_allocator *allocator)
183 {
184         int ret;
185         struct forb_env env;
186         fres_resource_allocator fra;
187
188         fra = fra_new(registry.orb, registry.executor, allocator);
189
190         if (allocator->activate_callback) {
191                 ret = allocator->activate_callback(registry.orb);
192                 if (ret) goto err_release;
193         }
194         /* Register resource allocator */
195         ret = fres_contract_broker_register_allocator(registry.fcb,
196                                                       allocator->res_type,
197                                                       allocator->res_id,
198                                                       fra, &env);
199         if (forb_exception_occurred(&env)) {
200                 ret = FRES_ERR_FORB_EXCEPTION;
201                 goto err_release;
202         }
203         if (ret)
204                 goto err_release;
205         return 0;
206 err_release:
207         save_errno(forb_object_release(fra));
208         return ret;
209 }
210
211 /** 
212  * Creates the allocator object and registeres it with contract
213  * broker. The allocator must be previously registered by
214  * fra_register(). If the allocator is already registered, this
215  * function does nothing.
216  * 
217  * @param res_type 
218  * @param res_id
219  * 
220  * @return Zero on success, non-zero error code on error.
221  */
222
223 int fra_activate(frsh_resource_type_t res_type,
224                  frsh_resource_id_t   res_id)
225 {
226         struct fres_allocator key, *pkey=&key;
227         struct registered_allocator *ra;
228         int ret;
229         
230         key.res_type = res_type;
231         key.res_id = res_id;
232         ra = fra_registry_find(&registry, &pkey);
233         if (ra) {
234                 if (ra->registered_with_fcb) {
235                         ret = 0;
236                 } else {
237                         ret = register_fra_to_fcb(ra->allocator);
238                         if (ret == 0) {
239                                 ra->registered_with_fcb = true;
240                         }
241                 }
242         } else {
243                 ret = FRSH_ERR_RESOURCE_ID_INVALID;
244         }
245
246         return ret;
247 }
248
249 /** 
250  * Returns allocator for a given resource, previously registered by
251  * fra_register().
252  * 
253  * @param res_type 
254  * @param res_id 
255  * 
256  * @return Pointer to the resource allocator or NULL if no allocator
257  * is registered for the resource.
258  */
259 struct fres_allocator *fra_get(frsh_resource_type_t res_type,
260                                frsh_resource_id_t   res_id)
261 {
262         struct registered_allocator *ra;
263         struct fres_allocator key, *pkey=&key;
264
265         pkey->res_type = res_type;
266         pkey->res_id = res_id;
267         
268         ra = fra_registry_find(&registry, &pkey);
269         if (ra) {
270                 return ra->allocator;
271         } else {
272                 return NULL;
273         }
274 }