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
58 #include <forb/config.h>
63 #include <ul_gavlcust.h>
66 #include <ul_logreg.h>
67 #include <forb/server_id.h>
68 #include <fres_contract.h>
69 #include "fcb_config.h"
70 #include <fosa_clocks_and_timers.h>
71 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
72 #include <forb/proto_inet.h>
75 UL_LOG_CUST(ulogd_fcb);
76 ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "fcb"};
78 fosa_abs_time_t start_time;
82 * Resource identification
85 frsh_resource_type_t type;
86 frsh_resource_id_t id;
96 fres_resource_manager mng; /**< Object reference of the resource manager */
97 gavl_cust_root_field_t allocators; /**< Registered allocators for this resource (from multiple applications/nodes) */
98 ul_list_head_t sc_contracts; /**< Negotiated contracts with spare capacity for this resource */
102 * Resource allocator registered in different nodes/applications
107 fres_resource_allocator ra;
110 struct fcb_contract {
111 fres_contract_id_t id;
112 struct res_alloc *ra; /**< Allocator for this contract TODO: Remove contract if allocator is destroyed */
113 gavl_node_t node_fcb;
114 ul_list_node_t node_sc;
115 ul_list_node_t node_reservation;
120 fosa_abs_time_t end_of_stability_period;
121 struct fres_contract *user_contract; /* The contract sent by user. Set after sucessfull neotiation. */
122 struct fres_contract *requested_contract; /* User request for the contract. Set by prepare_reservation_requests() in the beginning of negotiation. */
123 struct fres_contract *to_be_reserved_contract;
124 struct fres_contract *schedulable_contract;
128 * Contract broker data
131 gavl_cust_root_field_t resources; /**< Registered resources */
132 gavl_cust_root_field_t contracts; /**< Contracts negotiated by this FCB */
135 /** List of contracts to be reserved during spare capacity rebalancing */
136 struct reservation_list {
137 ul_list_head_t fcb_contracts;
141 struct fcb_contract *fcb_contract_new(fres_contract_id_t *id)
143 struct fcb_contract *fcb_contract;
145 fcb_contract = malloc(sizeof(*fcb_contract));
149 memset(fcb_contract, 0, sizeof(*fcb_contract));
150 fcb_contract->id = *id;
155 void fcb_contract_destroy(struct fcb_contract *fcb_contract)
157 if (fcb_contract->user_contract) {
158 fres_contract_destroy(fcb_contract->user_contract);
160 if (fcb_contract->to_be_reserved_contract) {
161 fres_contract_destroy(fcb_contract->to_be_reserved_contract);
163 if (fcb_contract->requested_contract) {
164 fres_contract_destroy(fcb_contract->requested_contract);
166 if (fcb_contract->schedulable_contract) {
167 fres_contract_destroy(fcb_contract->requested_contract);
172 static inline int res_key_cmp(const struct res_key *a,
173 const struct res_key *b)
175 if (a->type < b->type) {
177 } else if (a->type > b->type) {
179 } else if (a->id < b->id) {
181 } else if (a->id > b->id) {
188 /* Container for registered resources */
189 GAVL_CUST_NODE_INT_DEC(fcb_resource /* cust_prefix */, \
190 struct fcb /* cust_root_t */, \
191 struct resource/* cust_item_t */, \
192 struct res_key /* cust_key_t */, \
193 resources /* cust_root_node */, \
194 node /* cust_item_node */, \
195 key /* cust_item_key */, \
196 res_key_cmp /* cust_cmp_fnc */);
198 GAVL_CUST_NODE_INT_IMP(fcb_resource /* cust_prefix */, \
199 struct fcb /* cust_root_t */, \
200 struct resource/* cust_item_t */, \
201 struct res_key /* cust_key_t */, \
202 resources /* cust_root_node */, \
203 node /* cust_item_node */, \
204 key /* cust_item_key */, \
205 res_key_cmp /* cust_cmp_fnc */);
207 /* Container for allocators registered for a given resource */
208 GAVL_CUST_NODE_INT_DEC(fcb_alloc /* cust_prefix */, \
209 struct resource /* cust_root_t */, \
210 struct res_alloc /* cust_item_t */, \
211 forb_server_id /* cust_key_t */, \
212 allocators /* cust_root_node */, \
213 node /* cust_item_node */, \
214 app /* cust_item_key */, \
215 fres_contract_id_cmp /* cust_cmp_fnc */);
217 GAVL_CUST_NODE_INT_IMP(fcb_alloc /* cust_prefix */, \
218 struct resource /* cust_root_t */, \
219 struct res_alloc /* cust_item_t */, \
220 forb_server_id /* cust_key_t */, \
221 allocators /* cust_root_node */, \
222 node /* cust_item_node */, \
223 app /* cust_item_key */, \
224 forb_server_id_cmp /* cust_cmp_fnc */);
226 /* Container for contracts with spare capacity negotiated for a given resource */
227 UL_LIST_CUST_DEC(sc_contracts /* cust_prefix */, \
228 struct resource /* cust_head_t */, \
229 struct fcb_contract /* cust_item_t */, \
230 sc_contracts /* cust_head_field */, \
231 node_sc /* cust_node_field */);
233 UL_LIST_CUST_DEC(reservation_list /* cust_prefix */, \
234 struct reservation_list /* cust_head_t */, \
235 struct fcb_contract /* cust_item_t */, \
236 fcb_contracts /* cust_head_field */, \
237 node_reservation /* cust_node_field */);
239 /* Container for negotiated contracts */
240 GAVL_CUST_NODE_INT_DEC(fcb_contract /* cust_prefix */, \
241 struct fcb /* cust_root_t */, \
242 struct fcb_contract /* cust_item_t */, \
243 fres_contract_id_t /* cust_key_t */, \
244 contracts /* cust_root_node */, \
245 node_fcb /* cust_item_node */, \
246 id /* cust_item_key */, \
247 fres_contract_id_cmp /* cust_cmp_fnc */);
250 GAVL_CUST_NODE_INT_IMP(fcb_contract /* cust_prefix */, \
251 struct fcb /* cust_root_t */, \
252 struct fcb_contract /* cust_item_t */, \
253 fres_contract_id_t /* cust_key_t */, \
254 contracts /* cust_root_node */, \
255 node_fcb /* cust_item_node */, \
256 id /* cust_item_key */, \
257 fres_contract_id_cmp /* cust_cmp_fnc */);
259 #include "fcb_contract_gavl.inc"
263 #define o2fcb(o) (struct fcb*)forb_instance_data(o)
266 get_res_key(const struct fres_contract *contract, struct res_key *key)
268 fres_block_resource *block_res;
270 block_res = fres_contract_get_resource(contract);
272 ul_logerr("No resource specified\n");
275 key->type = block_res->resource_type;
276 key->id = block_res->resource_id;
282 * Fills in an array of fcb_contracts according to requests submited
283 * to negotiate_contracts(). For every contract in @a contracts, there
284 * is one fcb_contract in @a fcb_contracts array.
287 * @param fcb_contracts Where to store pointers to fcb_contracts
288 * @param resource Resource for which negotiation is being done.
289 * @param app Application which requests negotiation
290 * @param contracts Contracts submited to negotiate_contracts()
291 * @param num Number of contracts in contracts (which is the same as the number in fcb_contracts).
293 * @return Zero on success, non-zero error code on error.
296 prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
297 struct resource **resource,
299 struct fres_contract *contracts[], int num)
302 struct fcb_contract *fc;
303 struct res_alloc *ra;
304 struct res_key key, key2 = {-1,-1};
306 for (i=0; i<num; i++) {
307 struct fres_contract *c = contracts[i];
309 if (fres_contract_id_is_empty(&c->id)) {
310 /* Normal negotiation request */
311 forb_uuid_generate((forb_uuid_t *)&c->id);
312 fc = fcb_contract_new(&c->id);
315 fcb_contracts[i] = fc;
316 if (!get_res_key(c, &key)) {
317 return FRSH_ERR_RESOURCE_ID_INVALID;
320 fc = fcb_contract_find(fcb, &c->id);
323 fres_contract_id_to_string(str, &c->id, sizeof(str));
324 ul_logerr("Attempt to change unknown contract %s\n", str);
325 return FRES_ERR_NOTHING_TO_RENEGOTIATE;
327 fcb_contracts[i] = fc;
328 if (fres_contract_get_num_blocks(c) == 0) {
330 get_res_key(fc->user_contract, &key);
333 if (!get_res_key(c, &key)) {
334 return FRSH_ERR_RESOURCE_ID_INVALID;
339 fc->requested_contract = c;
341 /* Check that all contracts are for the same resource */
344 *resource = fcb_resource_find(fcb, &key);
346 ul_logerr("No resource manager for %d.%d registered\n",
348 return FRSH_ERR_RESOURCE_ID_INVALID;
351 else if (key.type != key2.type ||
353 ul_logerr("Contract #%d differs in resource!\n", i);
354 return FRSH_ERR_RESOURCE_ID_INVALID;
357 /* Find allocator for this request */
358 ra = fcb_alloc_find(*resource, app);
361 forb_server_id_to_string(str, app, sizeof(str));
362 ul_logerr("No resource allocator found for %d.%d and %s\n",
363 (*resource)->key.type, (*resource)->key.id, str);
364 return FRES_ERR_NO_RESOURCE_ALLOCATOR;
372 * @param resource Resource for which to rebalance capacity and negotiate new contracts
373 * @param fcb_contract New requests to negotiate
374 * @param num The number of elements in @a fcb_contract
375 * @param[out] rl List with changed contracts to be commited
378 prepare_reservation_list(struct resource *resource,
379 struct fcb_contract *fcb_contract[], int num,
380 struct reservation_list *rl)
384 struct fcb_contract *fc;
386 reservation_list_init_head(rl);
388 for (i=0; i<num; i++) {
389 assert(fcb_contract[i]->requested_contract != NULL);
390 reservation_list_insert(rl, fcb_contract[i]);
393 fosa_clock_get_time(CLOCK_REALTIME, &now);
394 ul_list_for_each(sc_contracts, resource, fc) {
395 if (fosa_abs_time_smaller(fc->end_of_stability_period, now) &&
396 fc->requested_contract == NULL) /* Do not insert contract inserted above */
398 reservation_list_insert(rl, fc);
407 * @param resource Resource for which to rebalance capacity and negotiate new contracts
408 * @param rl List with changed contracts to be commited
410 * @return Zero on success, non-zero error code on error
413 rebalance_spare_capacity_and_reserve(struct resource *resource,
414 struct reservation_list *rl)
418 struct fcb_contract *fc;
419 fres_block_spare_capacity *s;
421 fres_contract_ptr_seq contracts;
422 if (!forb_sequence_alloc_buf(contracts, rl->length)) {
425 contracts._length = rl->length;
427 /* Initialize optimization */
428 ul_list_for_each(reservation_list, rl, fc) {
429 fc->to_be_reserved_contract =
430 fc->requested_contract ? fres_contract_duplicate(fc->requested_contract) :
431 fc->user_contract ? fres_contract_duplicate(fc->user_contract) :
433 assert(fc->to_be_reserved_contract != NULL);
435 forb_sequence_elem(contracts, i) = fc->to_be_reserved_contract;
438 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
439 if (s && s->granularity == FRSH_GR_DISCRETE) {
440 fc->sc_variant.initial = s->variants._length - 1;
441 fc->sc_variant.try = fc->sc_variant.initial;
445 bool all_combinations_tried;
446 int criterion, best_criterion = -1;
447 struct fcb_contract *fcb_changed;
448 /* Exhaustive search. Try all combinations of spare capacity
449 * variants and find the one with best creterion. */
451 all_combinations_tried = true;
454 /* Prepare spare capacity variant */
455 ul_list_for_each(reservation_list, rl, fc) {
456 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
457 /* TODO: Simulate continuous granularity by discretization */
458 if (s && s->granularity == FRSH_GR_DISCRETE) {
459 fres_container_copy(fc->to_be_reserved_contract->container,
460 forb_sequence_elem(s->variants, fc->sc_variant.try));
461 criterion += fc->sc_variant.try;
463 if (fcb_changed == NULL) {
464 /* Chnage tried variant for the next round */
465 fc->sc_variant.try = fc->sc_variant.try > 0 ?
466 fc->sc_variant.try - 1 :
467 s->variants._length - 1;
468 /* Do not change other
469 * contracts unless we have
470 * tried all variants */
471 if (fc->sc_variant.try != fc->sc_variant.initial) {
475 if (fc->sc_variant.try != fc->sc_variant.initial)
476 all_combinations_tried = false;
480 if (criterion > best_criterion) {
481 CORBA_Environment ev;
482 /* Reserve contract */
483 ret = fres_resource_manager_reserve_contracts(resource->mng, &contracts, &ev);
484 if (forb_exception_occurred(&ev)) {
485 ret = fres_forbex2err(&ev);
486 ul_logerr("FORB exception when reserving contracts\n");
490 ul_logerr("Contract reservation error %d\n", ret);
491 ret = FRES_ERR_ADMISSION_TEST;
494 if (ret == 0) { /* negotiation succeeded */
495 best_criterion = criterion;
498 } while (!all_combinations_tried);
500 if (best_criterion == -1) {
501 ret = FRSH_ERR_CONTRACT_REJECTED;
503 /* At least some variant succeeded */
505 sc_contracts_init_head(resource);
506 ul_list_for_each(reservation_list, rl, fc) {
507 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
508 if (s && s->granularity == FRSH_GR_DISCRETE) {
509 sc_contracts_insert(resource, fc);
514 ul_list_for_each(reservation_list, rl, fc) {
515 fres_contract_destroy(fc->to_be_reserved_contract);
516 fc->to_be_reserved_contract = NULL;
518 forb_sequence_free_buf(contracts, forb_no_destructor);
523 * Create/change VReses according to @a schedulable_contracts.
525 * There might be more allocators for schedulable contracts, so merge
526 * adjacent vreses with the same allocator together and create/change
527 * them in one step. Also the order of schedulable contracts might be
528 * different from the initial order od ids in commit_contracts()
529 * therefore we have to search for every contract.
532 change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
534 struct res_alloc *last_ra = NULL;
535 fres_contract_ptr_seq vreses;
537 CORBA_Environment ev;
539 if (!forb_sequence_alloc_buf(vreses, schedulable_contracts->_length)) {
544 for (i=0; i<schedulable_contracts->_length; i++) {
545 struct fcb_contract *fc;
546 struct fres_contract *sc = schedulable_contracts->_buffer[i];
548 fc = fcb_contract_find(fcb, &sc->id);
551 if (true /* TODO: if the schedulable contract is changed */) {
552 if (last_ra != fc->ra) {
553 if (vreses._length) {
554 ret = fres_resource_allocator_change_vreses(last_ra->ra,
556 if (forb_exception_occurred(&ev)) {
557 ret = fres_forbex2err(&ev);
564 vreses._buffer[vreses._length] = sc;
566 fres_contract_destroy(fc->schedulable_contract);
567 fc->schedulable_contract = fres_contract_duplicate(sc);
572 ret = fres_resource_allocator_change_vreses(last_ra->ra,
574 if (forb_exception_occurred(&ev))
575 ret = fres_forbex2err(&ev);
577 forb_sequence_free_buf(vreses, forb_no_destructor);
582 negotiate_contracts(fres_contract_broker obj,
583 const fres_contract_ptr_seq* contracts,
584 fres_contract_id_seq** ids_out,
585 CORBA_Environment *ev)
587 struct fcb *fcb = o2fcb(obj);
588 struct resource *resource = NULL;
591 fres_contract_ptr_seq *schedulable_contracts;
592 struct fcb_contract **fcb_contracts, *fc;
594 fres_contract_id_seq commit_ids;
596 /* Prepare output sequence for the case we return eariler with
598 forb_sequence_alloc(*ids_out, 0);
600 ev->major = FORB_EX_NO_MEMORY;
603 CORBA_sequence_set_release(*ids_out, CORBA_TRUE);
605 forb_get_req_source(obj, &app);
607 fcb_contracts = malloc(sizeof(fcb_contracts[0])*contracts->_length);
608 if (!fcb_contracts) {
612 memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*contracts->_length);
614 ret = prepare_fcb_contracts(fcb, fcb_contracts, &resource,
615 &app, contracts->_buffer, contracts->_length);
617 goto err_free_fcb_contracts;
619 struct reservation_list rl;
620 prepare_reservation_list(resource,
621 fcb_contracts, contracts->_length,
624 /* Allocate all the needed memory before doing reservation. If
625 * there is no enough memory, it has no sense to call resource
627 if (!forb_sequence_alloc_buf(commit_ids, rl.length)) {
629 goto err_free_fcb_contracts;
632 /* Reserve contracts */
633 ret = rebalance_spare_capacity_and_reserve(resource, &rl);
635 if (ret == FRSH_ERR_CONTRACT_REJECTED) {
636 ul_logmsg("Contract(s) was/were rejected\n");
639 fres_strerror(ret, msg, sizeof(msg));
640 ul_logerr("Reservation error: %s\n", msg);
642 goto err_free_fcb_contracts;
645 /* Commit contracts */
646 commit_ids._length = rl.length;
648 ul_list_for_each(reservation_list, &rl, fc) {
649 forb_sequence_elem(commit_ids, i) = fc->id;
653 fres_resource_manager_commit_contracts(resource->mng, &commit_ids,
654 &schedulable_contracts, ev);
655 if (forb_exception_occurred(ev)) {
656 ret = fres_forbex2err(ev);
657 goto err_cancel_reservation;
660 /* Add new contracts to our fcb database for later
661 * reference. Canceled contracts are removed below. */
662 for (i=0; i<contracts->_length; i++) {
663 fc = fcb_contracts[i];
665 if (fc->user_contract) {
666 if (fres_contract_get_num_blocks(fc->requested_contract) > 0) {
668 fres_contract_destroy(fc->user_contract);
669 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
670 /* Note: requested_contract is also
671 * pointed by contracts parameter and
672 * will be freed by FORB. */
673 fc->requested_contract = NULL;
676 /* Insert new contracts */
677 fcb_contract_insert(fcb, fcb_contracts[i]);
678 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
679 fc->requested_contract = NULL;
680 /* See the note above. */
684 ret = change_vreses(fcb, schedulable_contracts);
686 goto err_cancel_reservation;
688 forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
690 ul_logerr("VRes was not created\n");
691 goto err_cancel_contracts;
695 /* Return IDs and delete canceled contracts from out database */
696 if (!forb_sequence_alloc_buf(**ids_out, contracts->_length)) {
697 ev->major = FORB_EX_NO_MEMORY;
698 goto err_cancel_contracts;
700 (*ids_out)->_length = contracts->_length;
701 for (i=0; i<contracts->_length; i++) {
702 struct fcb_contract *fc = fcb_contracts[i];
703 forb_sequence_elem(**ids_out, i) = fc->id;
705 if (fc->requested_contract &&
706 fres_contract_get_num_blocks(fc->requested_contract) == 0) {
707 fcb_contract_delete(fcb, fc);
708 /* Note: requested_contract is also pointed by
709 * contracts parameter and will be freed by FORB. */
710 fc->requested_contract = NULL;
711 fcb_contract_destroy(fc);
716 err_cancel_contracts:
718 goto err_free_fcb_contracts;
719 err_cancel_reservation:
720 fres_resource_manager_cancel_reservations(resource->mng, &commit_ids, ev);
721 err_free_fcb_contracts:
722 for (i=0; i<contracts->_length; i++) {
723 fc = fcb_contracts[i];
724 if (fc && !fc->user_contract) {
725 fcb_contracts[i]->requested_contract = NULL; /* Destroyed by FORB */
726 fcb_contract_destroy(fcb_contracts[i]);
734 void redistribute_spare_capacity(fres_contract_broker obj,
735 const frsh_resource_type_t restype,
736 const frsh_resource_id_t resid,
737 CORBA_Environment *ev)
739 struct fcb *fcb = o2fcb(obj);
740 struct res_key key = {restype, resid };
741 struct resource *resource;
742 struct reservation_list rl;
744 resource = fcb_resource_find(fcb, &key);
746 prepare_reservation_list(resource, NULL, 0, &rl);
748 /* forb_sequence_alloc(ids, rl.length); */
749 /* if (!ids || !ids->_buffer) { */
750 /* ev->major = FORB_EX_NO_MEMORY; */
751 /* goto err_free_fcb_contracts; */
753 /* CORBA_sequence_set_release(ids, CORBA_TRUE); */
754 /* *ids_out = ids; /\* ids is freed by FORB *\/ */
757 rebalance_spare_capacity_and_reserve(resource, &rl);
761 CORBA_long register_resource(fres_contract_broker obj,
762 const frsh_resource_type_t restype,
763 const frsh_resource_id_t resid,
764 const fres_resource_desc *desc,
765 CORBA_Environment *ev)
767 struct fcb *fcb = o2fcb(obj);
768 struct resource *res, *res2;
770 res = malloc(sizeof(*res));
772 memset(res, 0, sizeof(*res));
773 res->key.type = restype;
775 res2 = fcb_resource_find(fcb, &res->key);
777 if (forb_object_is_stale(res2->mng)) {
778 ul_logmsg("Removing stale manager for resource %d.%d\n",
780 forb_object_release(res2->mng);
781 fcb_resource_delete(fcb, res2);
782 /* TODO: Delete also all allocators associated
783 * with this stale resource manager. */
787 ul_logerr("Resource manager %d.%d already registered\n",
792 res->mng = forb_object_duplicate(desc->manager);
793 res->name = desc->name;
795 fcb_alloc_init_root_field(res);
796 sc_contracts_init_head(res);
797 ul_logmsg("Registering manager for resource \"%s\" (%d.%d)\n",
798 res->name, restype, resid);
799 fcb_resource_insert(fcb, res);
808 CORBA_long register_allocator(fres_contract_broker obj,
809 const frsh_resource_type_t restype,
810 const frsh_resource_id_t resid,
811 const fres_resource_allocator ra_obj,
812 CORBA_Environment *ev)
814 struct fcb *fcb = o2fcb(obj);
815 struct resource *res;
816 struct res_alloc *ra;
817 struct res_key resource;
818 forb_server_id server_id;
819 char server_id_str[40];
821 forb_get_server_id(ra_obj, &server_id);
822 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
823 ul_logmsg("Registering allocator for resource %d.%d in app %s\n",
824 restype, resid, server_id_str);
826 resource.type = restype;
828 res = fcb_resource_find(fcb, &resource);
830 ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
834 ra = fcb_alloc_find(res, &server_id);
836 char *str = forb_object_to_string(ra_obj);
837 ul_logerr("Allocator from already registered (%s)\n",
842 ra = malloc(sizeof(*ra));
847 ra->ra = forb_object_duplicate(ra_obj);
848 fcb_alloc_insert(res, ra);
851 return FRSH_ERR_RESOURCE_ID_INVALID;
854 void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
856 struct fcb *fcb = o2fcb(obj);
857 fres_resource_seq *seq;
858 struct resource *res;
861 seq = malloc(sizeof(*seq));
863 ev->major = FORB_EX_NO_MEMORY;
866 memset(seq, 0, sizeof(*seq));
869 gavl_cust_for_each(fcb_resource, fcb, res) {
873 seq->_buffer = CORBA_sequence_fres_resource_allocbuf(n);
878 gavl_cust_for_each(fcb_resource, fcb, res) {
879 seq->_buffer[n].restype = res->key.type;
880 seq->_buffer[n].resid = res->key.id;
881 seq->_buffer[n].desc.manager = res->mng;
882 seq->_buffer[n].desc.name = res->name;
889 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
890 static int register_inet_port(forb_orb orb)
892 forb_port_t *port = forb_malloc(sizeof(*port));
894 struct in_addr listen_on;
898 memset(port, 0, sizeof(*port));
899 listen_on.s_addr = INADDR_ANY;
900 ret = forb_inet_port_init(&port->desc, listen_on);
903 ret = forb_register_port(orb, port);
908 struct forb_fres_contract_broker_impl impl = {
909 .negotiate_contracts = negotiate_contracts,
910 .register_resource = register_resource,
911 .register_allocator = register_allocator,
912 .redistribute_spare_capacity = redistribute_spare_capacity,
913 .get_resources = get_resources,
916 void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
918 forb_server_id server_id;
919 char server_id_str[sizeof(forb_server_id)*2+1];
921 forb_get_server_id(peer_orb, &server_id);
922 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
924 ul_logmsg("peer discovered: %s (orb_id '%s')\n", server_id_str, orb_id);
930 if (strcmp(orb_id, "org.frescor.fcb") == 0) {
932 fosa_rel_time_t delay;
933 fosa_clock_get_time(CLOCK_REALTIME, &now);
934 delay = fosa_abs_time_extract_interval(start_time, now);
935 ul_logmsg("node joined: %s (time %ld ms)\n", server_id_str,
936 fosa_rel_time_to_msec(delay));
940 void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
944 static struct option long_opts[] = {
945 { "loglevel", 1, 0, 'l' },
952 printf("usage: fcb [ options ]\n");
953 printf(" -l, --loglevel <number>|<domain>=<number>,...\n");
956 int main(int argc, char *argv[])
960 fres_contract_broker fcb;
961 forb_executor_t executor;
963 forb_init_attr_t attr = {
964 .orb_id = "org.frescor.fcb",
965 .peer_discovery_callback = peer_discovery_callback,
966 .peer_dead_callback = peer_dead_callback,
970 ul_logreg_domain(&ulogd_fcb);
972 while ((opt = getopt_long(argc, argv, "l:", &long_opts[0], NULL)) != EOF) {
975 ul_log_domain_arg2levels(optarg);
980 exit(opt == 'h' ? 0 : 1);
984 fosa_clock_get_time(CLOCK_REALTIME, &start_time);
986 orb = forb_init(&argc, &argv, &attr);
987 if (!orb) error(1, errno, "FORB initialization failed");
989 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
990 ret = register_inet_port(orb);
991 if (ret) error(0, errno, "INET port registration failed");
994 fcb_resource_init_root_field(&fcb_data);
995 fcb_contract_init_root_field(&fcb_data);
997 fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
998 if (!fcb) error(1, errno, "forb_fres_contract_broker_new failed");
1000 /* Prepare executor before we register the fcb reference,
1001 * so that no reuqests are lost */
1002 ret = forb_executor_init(&executor);
1003 if (ret) error(1, errno, "forb_executor_init failed");
1005 ret = forb_executor_register_object(&executor, fcb);
1006 if (ret) error(1, errno, "forb_executor_register_object failed");
1008 ret = forb_register_reference(fcb, fres_contract_broker_reg_name);
1009 if (ret) error(1, errno, "forb_register_reference() failed");
1011 ul_logmsg("Waiting for requests\n");
1012 ret = forb_executor_run(&executor);
1013 if (ret) error(1, errno, "forb_executor_run failed");