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>
60 #include <fcb_contact_info.h>
64 #include <ul_gavlcust.h>
67 #include <ul_logreg.h>
68 #include <forb/server_id.h>
69 #include <fres_contract.h>
70 #include "fcb_config.h"
71 #include <fosa_clocks_and_timers.h>
72 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
73 #include <forb/proto_inet.h>
76 UL_LOG_CUST(ulogd_fcb);
77 ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "fcb"};
79 fosa_abs_time_t start_time;
83 * Resource identification
86 frsh_resource_type_t type;
87 frsh_resource_id_t id;
97 fres_resource_manager mng; /**< Object reference of the resource manager */
98 gavl_cust_root_field_t allocators; /**< Registered allocators for this resource (from multiple applications/nodes) */
99 ul_list_head_t sc_contracts; /**< Negotiated contracts with spare capacity for this resource */
103 * Resource allocator registered in different nodes/applications
108 fres_resource_allocator ra;
111 struct fcb_contract {
112 fres_contract_id_t id;
113 struct res_alloc *ra; /**< Allocator for this contract TODO: Remove contract if allocator is destroyed */
114 gavl_node_t node_fcb;
115 ul_list_node_t node_sc;
116 ul_list_node_t node_reservation;
121 fosa_abs_time_t end_of_stability_period;
122 struct fres_contract *user_contract; /* The contract sent by user. Set after sucessfull neotiation. */
123 struct fres_contract *requested_contract; /* User request for the contract. Set by prepare_reservation_requests() in the beginning of negotiation. */
124 struct fres_contract *to_be_reserved_contract;
125 struct fres_contract *schedulable_contract;
129 * Contract broker data
132 gavl_cust_root_field_t resources; /**< Registered resources */
133 gavl_cust_root_field_t contracts; /**< Contracts negotiated by this FCB */
136 /** List of contracts to be reserved during spare capacity rebalancing */
137 struct reservation_list {
138 ul_list_head_t fcb_contracts;
142 struct fcb_contract *fcb_contract_new(fres_contract_id_t *id)
144 struct fcb_contract *fcb_contract;
146 fcb_contract = malloc(sizeof(*fcb_contract));
150 memset(fcb_contract, 0, sizeof(*fcb_contract));
151 fcb_contract->id = *id;
156 void fcb_contract_destroy(struct fcb_contract *fcb_contract)
158 if (fcb_contract->user_contract) {
159 fres_contract_destroy(fcb_contract->user_contract);
161 if (fcb_contract->to_be_reserved_contract) {
162 fres_contract_destroy(fcb_contract->to_be_reserved_contract);
164 if (fcb_contract->requested_contract) {
165 fres_contract_destroy(fcb_contract->requested_contract);
167 if (fcb_contract->schedulable_contract) {
168 fres_contract_destroy(fcb_contract->requested_contract);
173 static inline int res_key_cmp(const struct res_key *a,
174 const struct res_key *b)
176 if (a->type < b->type) {
178 } else if (a->type > b->type) {
180 } else if (a->id < b->id) {
182 } else if (a->id > b->id) {
189 /* Container for registered resources */
190 GAVL_CUST_NODE_INT_DEC(fcb_resource /* cust_prefix */, \
191 struct fcb /* cust_root_t */, \
192 struct resource/* cust_item_t */, \
193 struct res_key /* cust_key_t */, \
194 resources /* cust_root_node */, \
195 node /* cust_item_node */, \
196 key /* cust_item_key */, \
197 res_key_cmp /* cust_cmp_fnc */);
199 GAVL_CUST_NODE_INT_IMP(fcb_resource /* cust_prefix */, \
200 struct fcb /* cust_root_t */, \
201 struct resource/* cust_item_t */, \
202 struct res_key /* cust_key_t */, \
203 resources /* cust_root_node */, \
204 node /* cust_item_node */, \
205 key /* cust_item_key */, \
206 res_key_cmp /* cust_cmp_fnc */);
208 /* Container for allocators registered for a given resource */
209 GAVL_CUST_NODE_INT_DEC(fcb_alloc /* cust_prefix */, \
210 struct resource /* cust_root_t */, \
211 struct res_alloc /* cust_item_t */, \
212 forb_server_id /* cust_key_t */, \
213 allocators /* cust_root_node */, \
214 node /* cust_item_node */, \
215 app /* cust_item_key */, \
216 fres_contract_id_cmp /* cust_cmp_fnc */);
218 GAVL_CUST_NODE_INT_IMP(fcb_alloc /* cust_prefix */, \
219 struct resource /* cust_root_t */, \
220 struct res_alloc /* cust_item_t */, \
221 forb_server_id /* cust_key_t */, \
222 allocators /* cust_root_node */, \
223 node /* cust_item_node */, \
224 app /* cust_item_key */, \
225 forb_server_id_cmp /* cust_cmp_fnc */);
227 /* Container for contracts with spare capacity negotiated for a given resource */
228 UL_LIST_CUST_DEC(sc_contracts /* cust_prefix */, \
229 struct resource /* cust_head_t */, \
230 struct fcb_contract /* cust_item_t */, \
231 sc_contracts /* cust_head_field */, \
232 node_sc /* cust_node_field */);
234 UL_LIST_CUST_DEC(reservation_list /* cust_prefix */, \
235 struct reservation_list /* cust_head_t */, \
236 struct fcb_contract /* cust_item_t */, \
237 fcb_contracts /* cust_head_field */, \
238 node_reservation /* cust_node_field */);
240 /* Container for negotiated contracts */
241 GAVL_CUST_NODE_INT_DEC(fcb_contract /* cust_prefix */, \
242 struct fcb /* cust_root_t */, \
243 struct fcb_contract /* cust_item_t */, \
244 fres_contract_id_t /* cust_key_t */, \
245 contracts /* cust_root_node */, \
246 node_fcb /* cust_item_node */, \
247 id /* cust_item_key */, \
248 fres_contract_id_cmp /* cust_cmp_fnc */);
251 GAVL_CUST_NODE_INT_IMP(fcb_contract /* cust_prefix */, \
252 struct fcb /* cust_root_t */, \
253 struct fcb_contract /* cust_item_t */, \
254 fres_contract_id_t /* cust_key_t */, \
255 contracts /* cust_root_node */, \
256 node_fcb /* cust_item_node */, \
257 id /* cust_item_key */, \
258 fres_contract_id_cmp /* cust_cmp_fnc */);
260 #include "fcb_contract_gavl.inc"
264 #define o2fcb(o) (struct fcb*)forb_instance_data(o)
267 get_res_key(const struct fres_contract *contract, struct res_key *key)
269 fres_block_resource *block_res;
271 block_res = fres_contract_get_resource(contract);
273 ul_logerr("No resource specified\n");
276 key->type = block_res->resource_type;
277 key->id = block_res->resource_id;
283 * Fills in an array of fcb_contracts according to requests submited
284 * to negotiate_contracts(). For every contract in @a contracts, there
285 * is one fcb_contract in @a fcb_contracts array.
288 * @param fcb_contracts Where to store pointers to fcb_contracts
289 * @param resource Resource for which negotiation is being done.
290 * @param app Application which requests negotiation
291 * @param contracts Contracts submited to negotiate_contracts()
292 * @param num Number of contracts in contracts (which is the same as the number in fcb_contracts).
294 * @return Zero on success, non-zero error code on error.
297 prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
298 struct resource **resource,
300 struct fres_contract *contracts[], int num)
303 struct fcb_contract *fc;
304 struct res_alloc *ra;
305 struct res_key key, key2 = {-1,-1};
307 for (i=0; i<num; i++) {
308 struct fres_contract *c = contracts[i];
310 if (fres_contract_id_is_empty(&c->id)) {
311 /* Normal negotiation request */
312 forb_uuid_generate((forb_uuid_t *)&c->id);
313 fc = fcb_contract_new(&c->id);
316 fcb_contracts[i] = fc;
317 if (!get_res_key(c, &key)) {
318 return FRSH_ERR_RESOURCE_ID_INVALID;
321 fc = fcb_contract_find(fcb, &c->id);
324 fres_contract_id_to_string(str, &c->id, sizeof(str));
325 ul_logerr("Attempt to change unknown contract %s\n", str);
326 return FRES_ERR_NOTHING_TO_RENEGOTIATE;
328 fcb_contracts[i] = fc;
329 if (fres_contract_get_num_blocks(c) == 0) {
331 get_res_key(fc->user_contract, &key);
334 if (!get_res_key(c, &key)) {
335 return FRSH_ERR_RESOURCE_ID_INVALID;
340 fc->requested_contract = c;
342 /* Check that all contracts are for the same resource */
345 *resource = fcb_resource_find(fcb, &key);
347 ul_logerr("No resource manager for %d.%d registered\n",
349 return FRES_ERR_NO_RESOURCE_MANAGER;
352 else if (key.type != key2.type ||
354 ul_logerr("Contract #%d differs in resource!\n", i);
355 return FRSH_ERR_RESOURCE_ID_INVALID;
358 /* Find allocator for this request */
359 ra = fcb_alloc_find(*resource, app);
362 forb_server_id_to_string(str, app, sizeof(str));
363 ul_logerr("No resource allocator found for %d.%d and %s\n",
364 (*resource)->key.type, (*resource)->key.id, str);
365 return FRES_ERR_NO_RESOURCE_ALLOCATOR;
373 * @param resource Resource for which to rebalance capacity and negotiate new contracts
374 * @param fcb_contract New requests to negotiate
375 * @param num The number of elements in @a fcb_contract
376 * @param[out] rl List with changed contracts to be commited
379 prepare_reservation_list(struct resource *resource,
380 struct fcb_contract *fcb_contract[], int num,
381 struct reservation_list *rl)
385 struct fcb_contract *fc;
387 reservation_list_init_head(rl);
389 for (i=0; i<num; i++) {
390 assert(fcb_contract[i]->requested_contract != NULL);
391 reservation_list_insert(rl, fcb_contract[i]);
394 fosa_clock_get_time(CLOCK_REALTIME, &now);
395 ul_list_for_each(sc_contracts, resource, fc) {
396 if (fosa_abs_time_smaller(fc->end_of_stability_period, now) &&
397 fc->requested_contract == NULL) /* Do not insert contract inserted above */
399 reservation_list_insert(rl, fc);
408 * @param resource Resource for which to rebalance capacity and negotiate new contracts
409 * @param rl List with changed contracts to be commited
411 * @return Zero on success, non-zero error code on error
414 rebalance_spare_capacity_and_reserve(struct resource *resource,
415 struct reservation_list *rl)
419 struct fcb_contract *fc;
420 fres_block_spare_capacity *s;
422 fres_contract_ptr_seq contracts;
423 if (!forb_sequence_alloc_buf(contracts, rl->length)) {
426 contracts._length = rl->length;
428 /* Initialize optimization */
429 ul_list_for_each(reservation_list, rl, fc) {
430 fc->to_be_reserved_contract =
431 fc->requested_contract ? fres_contract_duplicate(fc->requested_contract) :
432 fc->user_contract ? fres_contract_duplicate(fc->user_contract) :
434 assert(fc->to_be_reserved_contract != NULL);
436 forb_sequence_elem(contracts, i) = fc->to_be_reserved_contract;
439 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
440 if (s && s->granularity == FRSH_GR_DISCRETE) {
441 fc->sc_variant.initial = s->variants._length - 1;
442 fc->sc_variant.try = fc->sc_variant.initial;
446 bool all_combinations_tried;
447 int criterion, best_criterion = -1;
448 struct fcb_contract *fcb_changed;
449 /* Exhaustive search. Try all combinations of spare capacity
450 * variants and find the one with best creterion. */
452 all_combinations_tried = true;
455 /* Prepare spare capacity variant */
456 ul_list_for_each(reservation_list, rl, fc) {
457 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
458 /* TODO: Simulate continuous granularity by discretization */
459 if (s && s->granularity == FRSH_GR_DISCRETE) {
460 fres_container_copy(fc->to_be_reserved_contract->container,
461 forb_sequence_elem(s->variants, fc->sc_variant.try));
462 criterion += fc->sc_variant.try;
464 if (fcb_changed == NULL) {
465 /* Chnage tried variant for the next round */
466 fc->sc_variant.try = fc->sc_variant.try > 0 ?
467 fc->sc_variant.try - 1 :
468 s->variants._length - 1;
469 /* Do not change other
470 * contracts unless we have
471 * tried all variants */
472 if (fc->sc_variant.try != fc->sc_variant.initial) {
476 if (fc->sc_variant.try != fc->sc_variant.initial)
477 all_combinations_tried = false;
481 if (criterion > best_criterion) {
482 CORBA_Environment ev;
483 /* Reserve contract */
484 ret = fres_resource_manager_reserve_contracts(resource->mng, &contracts, &ev);
485 if (forb_exception_occurred(&ev)) {
486 ret = fres_forbex2err(&ev);
487 ul_logerr("FORB exception when reserving contracts\n");
491 ul_logerr("Contract reservation error %d\n", ret);
492 ret = FRES_ERR_ADMISSION_TEST;
495 if (ret == 0) { /* negotiation succeeded */
496 best_criterion = criterion;
499 } while (!all_combinations_tried);
501 if (best_criterion == -1) {
502 ret = FRSH_ERR_CONTRACT_REJECTED;
504 /* At least some variant succeeded */
506 sc_contracts_init_head(resource);
507 ul_list_for_each(reservation_list, rl, fc) {
508 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
509 if (s && s->granularity == FRSH_GR_DISCRETE) {
510 sc_contracts_insert(resource, fc);
515 ul_list_for_each(reservation_list, rl, fc) {
516 fres_contract_destroy(fc->to_be_reserved_contract);
517 fc->to_be_reserved_contract = NULL;
519 forb_sequence_free_buf(contracts, forb_no_destructor);
524 * Create/change VReses according to @a schedulable_contracts.
526 * There might be more allocators for schedulable contracts, so merge
527 * adjacent vreses with the same allocator together and create/change
528 * them in one step. Also the order of schedulable contracts might be
529 * different from the initial order od ids in commit_contracts()
530 * therefore we have to search for every contract.
533 change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
535 struct res_alloc *last_ra = NULL;
536 fres_contract_ptr_seq vreses;
538 CORBA_Environment ev;
540 if (!forb_sequence_alloc_buf(vreses, schedulable_contracts->_length)) {
545 for (i=0; i<schedulable_contracts->_length; i++) {
546 struct fcb_contract *fc;
547 struct fres_contract *sc = schedulable_contracts->_buffer[i];
549 fc = fcb_contract_find(fcb, &sc->id);
552 if (true /* TODO: if the schedulable contract is changed */) {
553 if (last_ra != fc->ra) {
554 if (vreses._length) {
555 ret = fres_resource_allocator_change_vreses(last_ra->ra,
557 if (forb_exception_occurred(&ev)) {
558 ret = fres_forbex2err(&ev);
565 vreses._buffer[vreses._length] = sc;
567 fres_contract_destroy(fc->schedulable_contract);
568 fc->schedulable_contract = fres_contract_duplicate(sc);
573 ret = fres_resource_allocator_change_vreses(last_ra->ra,
575 if (forb_exception_occurred(&ev))
576 ret = fres_forbex2err(&ev);
578 forb_sequence_free_buf(vreses, forb_no_destructor);
583 negotiate_contracts(fres_contract_broker obj,
584 const fres_contract_ptr_seq* contracts,
585 fres_contract_id_seq** ids_out,
586 CORBA_Environment *ev)
588 struct fcb *fcb = o2fcb(obj);
589 struct resource *resource = NULL;
592 fres_contract_ptr_seq *schedulable_contracts;
593 struct fcb_contract **fcb_contracts, *fc;
595 fres_contract_id_seq commit_ids;
597 /* Prepare output sequence for the case we return eariler with
599 forb_sequence_alloc(*ids_out, 0);
601 ev->major = FORB_EX_NO_MEMORY;
604 CORBA_sequence_set_release(*ids_out, CORBA_TRUE);
606 forb_get_req_source(obj, &app);
608 fcb_contracts = malloc(sizeof(fcb_contracts[0])*contracts->_length);
609 if (!fcb_contracts) {
613 memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*contracts->_length);
615 ret = prepare_fcb_contracts(fcb, fcb_contracts, &resource,
616 &app, contracts->_buffer, contracts->_length);
618 goto err_free_fcb_contracts;
620 struct reservation_list rl;
621 prepare_reservation_list(resource,
622 fcb_contracts, contracts->_length,
625 /* Allocate all the needed memory before doing reservation. If
626 * there is no enough memory, it has no sense to call resource
628 if (!forb_sequence_alloc_buf(commit_ids, rl.length)) {
630 goto err_free_fcb_contracts;
633 /* Reserve contracts */
634 ret = rebalance_spare_capacity_and_reserve(resource, &rl);
636 if (ret == FRSH_ERR_CONTRACT_REJECTED) {
637 ul_logmsg("Contract(s) was/were rejected\n");
640 fres_strerror(ret, msg, sizeof(msg));
641 ul_logerr("Reservation error: %s\n", msg);
643 goto err_free_fcb_contracts;
646 /* Commit contracts */
647 commit_ids._length = rl.length;
649 ul_list_for_each(reservation_list, &rl, fc) {
650 forb_sequence_elem(commit_ids, i) = fc->id;
654 fres_resource_manager_commit_contracts(resource->mng, &commit_ids,
655 &schedulable_contracts, ev);
656 if (forb_exception_occurred(ev)) {
657 ret = fres_forbex2err(ev);
658 goto err_cancel_reservation;
661 /* Add new contracts to our fcb database for later
662 * reference. Canceled contracts are removed below. */
663 for (i=0; i<contracts->_length; i++) {
664 fc = fcb_contracts[i];
666 if (fc->user_contract) {
667 if (fres_contract_get_num_blocks(fc->requested_contract) > 0) {
669 fres_contract_destroy(fc->user_contract);
670 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
671 /* Note: requested_contract is also
672 * pointed by contracts parameter and
673 * will be freed by FORB. */
674 fc->requested_contract = NULL;
677 /* Insert new contracts */
678 fcb_contract_insert(fcb, fcb_contracts[i]);
679 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
680 fc->requested_contract = NULL;
681 /* See the note above. */
685 ret = change_vreses(fcb, schedulable_contracts);
687 goto err_cancel_reservation;
689 forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
691 ul_logerr("VRes was not created\n");
692 goto err_cancel_contracts;
696 /* Return IDs and delete canceled contracts from out database */
697 if (!forb_sequence_alloc_buf(**ids_out, contracts->_length)) {
698 ev->major = FORB_EX_NO_MEMORY;
699 goto err_cancel_contracts;
701 (*ids_out)->_length = contracts->_length;
702 for (i=0; i<contracts->_length; i++) {
703 struct fcb_contract *fc = fcb_contracts[i];
704 forb_sequence_elem(**ids_out, i) = fc->id;
706 if (fc->requested_contract &&
707 fres_contract_get_num_blocks(fc->requested_contract) == 0) {
708 fcb_contract_delete(fcb, fc);
709 /* Note: requested_contract is also pointed by
710 * contracts parameter and will be freed by FORB. */
711 fc->requested_contract = NULL;
712 fcb_contract_destroy(fc);
717 err_cancel_contracts:
719 goto err_free_fcb_contracts;
720 err_cancel_reservation:
721 fres_resource_manager_cancel_reservations(resource->mng, &commit_ids, ev);
722 err_free_fcb_contracts:
723 for (i=0; i<contracts->_length; i++) {
724 fc = fcb_contracts[i];
725 if (fc && !fc->user_contract) {
726 fcb_contracts[i]->requested_contract = NULL; /* Destroyed by FORB */
727 fcb_contract_destroy(fcb_contracts[i]);
735 void redistribute_spare_capacity(fres_contract_broker obj,
736 const frsh_resource_type_t restype,
737 const frsh_resource_id_t resid,
738 CORBA_Environment *ev)
740 struct fcb *fcb = o2fcb(obj);
741 struct res_key key = {restype, resid };
742 struct resource *resource;
743 struct reservation_list rl;
745 resource = fcb_resource_find(fcb, &key);
747 prepare_reservation_list(resource, NULL, 0, &rl);
749 /* forb_sequence_alloc(ids, rl.length); */
750 /* if (!ids || !ids->_buffer) { */
751 /* ev->major = FORB_EX_NO_MEMORY; */
752 /* goto err_free_fcb_contracts; */
754 /* CORBA_sequence_set_release(ids, CORBA_TRUE); */
755 /* *ids_out = ids; /\* ids is freed by FORB *\/ */
758 rebalance_spare_capacity_and_reserve(resource, &rl);
762 CORBA_long register_resource(fres_contract_broker obj,
763 const frsh_resource_type_t restype,
764 const frsh_resource_id_t resid,
765 const fres_resource_desc *desc,
766 CORBA_Environment *ev)
768 struct fcb *fcb = o2fcb(obj);
769 struct resource *res, *res2;
771 res = malloc(sizeof(*res));
773 memset(res, 0, sizeof(*res));
774 res->key.type = restype;
776 res2 = fcb_resource_find(fcb, &res->key);
778 if (forb_object_is_stale(res2->mng)) {
779 ul_logmsg("Removing stale manager for resource %d.%d\n",
781 forb_object_release(res2->mng);
782 fcb_resource_delete(fcb, res2);
783 /* TODO: Delete also all allocators associated
784 * with this stale resource manager. */
788 ul_logerr("Resource manager %d.%d already registered\n",
793 res->mng = forb_object_duplicate(desc->manager);
794 res->name = desc->name;
796 fcb_alloc_init_root_field(res);
797 sc_contracts_init_head(res);
798 ul_logmsg("Registering manager for resource \"%s\" (%d.%d)\n",
799 res->name, restype, resid);
800 fcb_resource_insert(fcb, res);
809 CORBA_long register_allocator(fres_contract_broker obj,
810 const frsh_resource_type_t restype,
811 const frsh_resource_id_t resid,
812 const fres_resource_allocator ra_obj,
813 CORBA_Environment *ev)
815 struct fcb *fcb = o2fcb(obj);
816 struct resource *res;
817 struct res_alloc *ra;
818 struct res_key resource;
819 forb_server_id server_id;
820 char server_id_str[40];
823 forb_get_server_id(ra_obj, &server_id);
824 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
825 ul_logmsg("Registering allocator for resource %d.%d in app %s\n",
826 restype, resid, server_id_str);
828 resource.type = restype;
830 res = fcb_resource_find(fcb, &resource);
832 ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
834 ret = FRES_ERR_NO_RESOURCE_MANAGER;
837 ra = fcb_alloc_find(res, &server_id);
839 char *str = forb_object_to_string(ra_obj);
840 ul_logerr("Allocator from already registered (%s)\n",
843 ret = FRES_ERR_ALLOCATOR_ALREADY_REGISTERED;
846 ra = malloc(sizeof(*ra));
852 ra->ra = forb_object_duplicate(ra_obj);
853 fcb_alloc_insert(res, ra);
859 void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
861 struct fcb *fcb = o2fcb(obj);
862 fres_resource_seq *seq;
863 struct resource *res;
866 seq = malloc(sizeof(*seq));
868 ev->major = FORB_EX_NO_MEMORY;
871 memset(seq, 0, sizeof(*seq));
874 gavl_cust_for_each(fcb_resource, fcb, res) {
878 seq->_buffer = CORBA_sequence_fres_resource_allocbuf(n);
883 gavl_cust_for_each(fcb_resource, fcb, res) {
884 seq->_buffer[n].restype = res->key.type;
885 seq->_buffer[n].resid = res->key.id;
886 seq->_buffer[n].desc.manager = res->mng;
887 seq->_buffer[n].desc.name = res->name;
894 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
895 static int register_inet_port(forb_orb orb)
897 forb_port_t *port = forb_malloc(sizeof(*port));
899 struct in_addr listen_on;
903 memset(port, 0, sizeof(*port));
904 listen_on.s_addr = INADDR_ANY;
905 ret = forb_inet_port_init(&port->desc, listen_on, 0);
908 ret = forb_register_port(orb, port);
913 struct forb_fres_contract_broker_impl impl = {
914 .negotiate_contracts = negotiate_contracts,
915 .register_resource = register_resource,
916 .register_allocator = register_allocator,
917 .redistribute_spare_capacity = redistribute_spare_capacity,
918 .get_resources = get_resources,
921 void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
923 forb_server_id server_id;
924 char server_id_str[sizeof(forb_server_id)*2+1];
926 forb_get_server_id(peer_orb, &server_id);
927 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
929 ul_logmsg("peer discovered: %s (orb_id '%s')\n", server_id_str, orb_id);
935 if (strcmp(orb_id, "org.frescor.fcb") == 0) {
937 fosa_rel_time_t delay;
938 fosa_clock_get_time(CLOCK_REALTIME, &now);
939 delay = fosa_abs_time_extract_interval(start_time, now);
940 ul_logmsg("node joined: %s (time %ld ms)\n", server_id_str,
941 fosa_rel_time_to_msec(delay));
945 void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
949 static struct option long_opts[] = {
950 { "loglevel", 1, 0, 'l' },
957 printf("usage: fcb [ options ]\n");
958 printf(" -l, --loglevel <number>|<domain>=<number>,...\n");
961 int main(int argc, char *argv[])
965 fres_contract_broker fcb;
966 forb_executor_t executor;
968 forb_init_attr_t attr = {
969 .orb_id = "org.frescor.fcb",
970 .peer_discovery_callback = peer_discovery_callback,
971 .peer_dead_callback = peer_dead_callback,
972 .fixed_tcp_port = FCB_TCP_PORT,
973 #ifdef CONFIG_FORB_PROTO_INET_DEFAULT
974 .fixed_server_id = FCB_SERVER_ID,
975 .redistribute_hellos = true,
980 ul_logreg_domain(&ulogd_fcb);
982 while ((opt = getopt_long(argc, argv, "l:", &long_opts[0], NULL)) != EOF) {
985 ul_log_domain_arg2levels(optarg);
990 exit(opt == 'h' ? 0 : 1);
994 fosa_clock_get_time(CLOCK_REALTIME, &start_time);
996 orb = forb_init(&argc, &argv, &attr);
997 if (!orb) error(1, errno, "FORB initialization failed");
999 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1000 ret = register_inet_port(orb);
1001 if (ret) error(0, errno, "INET port registration failed");
1004 fcb_resource_init_root_field(&fcb_data);
1005 fcb_contract_init_root_field(&fcb_data);
1007 fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
1008 if (!fcb) error(1, errno, "forb_fres_contract_broker_new failed");
1010 /* Prepare executor before we register the fcb reference,
1011 * so that no reuqests are lost */
1012 ret = forb_executor_init(&executor);
1013 if (ret) error(1, errno, "forb_executor_init failed");
1015 ret = forb_executor_register_object(&executor, fcb);
1016 if (ret) error(1, errno, "forb_executor_register_object failed");
1018 ret = forb_register_reference(fcb, fres_contract_broker_reg_name);
1019 if (ret) error(1, errno, "forb_register_reference() failed");
1021 ul_logmsg("Waiting for requests\n");
1022 ret = forb_executor_run(&executor);
1023 if (ret) error(1, errno, "forb_executor_run failed");