1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners: */
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 */
12 /* Thales Communication S.A. FRANCE */
13 /* Visual Tools S.A. SPAIN */
14 /* Rapita Systems Ltd UK */
17 /* See http://www.frescor.org for a link to partners' websites */
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. */
25 /* This file is part of FRSH (FRescor ScHeduler) */
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. */
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 /**************************************************************************/
49 * @author Michal Sojka <sojkam1@fel.cvut.cz>
50 * @date Mon Oct 20 18:00:18 2008
52 * @brief FRESCOR Contract Broker
61 #include <ul_gavlcust.h>
64 #include <forb/server_id.h>
65 #include <fres_contract.h>
67 UL_LOG_CUST(ulogd_fcb);
68 ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "fcb"};
72 * Resource identification
75 frsh_resource_type_t type;
76 frsh_resource_id_t id;
80 * Registered resource manager
84 struct res_key resource;
85 fres_resource_manager rm;
86 gavl_cust_root_field_t schedulers; /**< Registered schedulers for this resource (from multiple applications/nodes) */
90 * Registered resource scheduler
95 fres_resource_scheduler rs;
99 gavl_cust_root_field_t resources;
102 static inline int res_key_cmp(struct res_key *a, struct res_key *b)
104 if (a->type < b->type) {
106 } else if (a->type > b->type) {
108 } else if (a->id < b->id) {
110 } else if (a->id > b->id) {
117 GAVL_CUST_NODE_INT_DEC(fcb_resource /* cust_prefix */, \
118 struct fcb /* cust_root_t */, \
119 struct res_mng /* cust_item_t */, \
120 struct res_key /* cust_key_t */, \
121 resources /* cust_root_node */, \
122 node /* cust_item_node */, \
123 resource /* cust_item_key */, \
124 res_key_cmp /* cust_cmp_fnc */)
126 GAVL_CUST_NODE_INT_IMP(fcb_resource /* cust_prefix */, \
127 struct fcb /* cust_root_t */, \
128 struct res_mng /* cust_item_t */, \
129 struct res_key /* cust_key_t */, \
130 resources /* cust_root_node */, \
131 node /* cust_item_node */, \
132 resource /* cust_item_key */, \
133 res_key_cmp /* cust_cmp_fnc */)
135 GAVL_CUST_NODE_INT_DEC(fcb_sched /* cust_prefix */, \
136 struct res_mng /* cust_root_t */, \
137 struct res_sched /* cust_item_t */, \
138 forb_server_id /* cust_key_t */, \
139 schedulers /* cust_root_node */, \
140 node /* cust_item_node */, \
141 app /* cust_item_key */, \
142 fres_contract_id_cmp /* cust_cmp_fnc */)
144 GAVL_CUST_NODE_INT_IMP(fcb_sched /* cust_prefix */, \
145 struct res_mng /* cust_root_t */, \
146 struct res_sched /* cust_item_t */, \
147 forb_server_id /* cust_key_t */, \
148 schedulers /* cust_root_node */, \
149 node /* cust_item_node */, \
150 app /* cust_item_key */, \
151 forb_server_id_cmp /* cust_cmp_fnc */)
154 #define o2fcb(o) (struct fcb*)forb_instance_data(o)
157 negotiate_contract(fres_contract_broker obj,
158 const fres_contract_ptr contract,
159 fres_contract_id_t* id,
160 CORBA_Environment *ev)
162 struct fcb *fcb = o2fcb(obj);
163 fres_block_resource *res;
164 struct res_key res_key;
166 struct res_sched *rs;
169 fres_contract_ptr contract_seq_buf;
170 fres_contract_ptr_seq contracts;
171 fres_contract_ptr_seq *contracts_sched;
172 fres_contract_id_seq ids;
175 res = fres_contract_get_resource(contract);
177 ul_logerr("No resource specified\n");
181 res_key.type = res->resource_type;
182 res_key.id = res->resource_id;
184 rm = fcb_resource_find(fcb, &res_key);
186 ul_logerr("No resource manager for %d.%d registered\n",
187 res_key.type, res_key.id);
192 forb_uuid_generate((forb_uuid_t *)id);
195 forb_get_req_source(obj, &app);
196 rs = fcb_sched_find(rm, &app);
199 forb_server_id_to_string(str, &app, sizeof(str));
200 ul_logerr("No resource scheduler found for %s\n",
205 /* Reserve contract */
206 contracts._length = 1;
207 contract_seq_buf = contract;
208 contracts._buffer = &contract_seq_buf;
209 ret = fres_resource_manager_reserve_contracts(rm->rm, &contracts, ev);
210 if (forb_exception_occured(ev)) {
214 ul_logmsg("Contract was not accepted\n");
218 /* Commit contract */
221 fres_resource_manager_commit_contracts(rm->rm, &ids, &contracts_sched, ev);
222 if (forb_exception_occured(ev)) {
227 ret = fres_resource_scheduler_create_vres(rs->rs, contracts_sched->_buffer[0], ev);
228 if (CORBA_sequence_get_release(contracts_sched)) forb_free(contracts_sched->_buffer);
229 forb_free(contracts_sched);
230 if (forb_exception_occured(ev)) {
234 ul_logmsg("VRes was not created\n");
243 CORBA_long register_manager(fres_contract_broker obj,
244 const frsh_resource_type_t restype,
245 const frsh_resource_id_t resid,
246 const fres_resource_manager rm_obj,
247 CORBA_Environment *ev)
249 struct fcb *fcb = o2fcb(obj);
250 struct res_mng *rm, *rm2;
251 rm = malloc(sizeof(*rm));
253 memset(rm, 0, sizeof(*rm));
254 rm->resource.type = restype;
255 rm->resource.id = resid;
256 rm2 = fcb_resource_find(fcb, &rm->resource);
258 ul_logerr("Resource manager %d.%d already regsitered\n",
262 rm->rm = forb_object_duplicate(rm_obj);
263 fcb_resource_insert(fcb, rm);
272 CORBA_long register_scheduler(fres_contract_broker obj,
273 const frsh_resource_type_t restype,
274 const frsh_resource_id_t resid,
275 const fres_resource_scheduler rs_obj,
276 CORBA_Environment *ev)
278 struct fcb *fcb = o2fcb(obj);
280 struct res_sched *rs;
281 struct res_key resource;
282 forb_server_id server_id;
285 resource.type = restype;
287 rm = fcb_resource_find(fcb, &resource);
291 forb_get_server_id(rs_obj, &server_id);
292 rs = fcb_sched_find(rm, &server_id);
294 char *str = forb_object_to_string(rs_obj);
295 ul_logerr("Scheduler from already registered (%s)\n",
300 rs = malloc(sizeof(*rs));
305 rs->rs = forb_object_duplicate(rs_obj);
306 fcb_sched_insert(rm, rs);
312 struct forb_fres_contract_broker_impl impl = {
313 .negotiate_contract = negotiate_contract,
314 .register_manager = register_manager,
315 .register_scheduler = register_scheduler,
318 int main(int argc, char *argv[])
322 fres_contract_broker fcb;
324 orb = forb_init(&argc, &argv, "fcb");
325 if (!orb) error(1, errno, "FORB initialization failed");
329 fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
331 if (forb_register_reference(fcb, fres_contract_broker_reg_name) != 0) {
332 error(1, errno, "forb_register_reference() failed");
335 forb_execute_object(fcb);