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>
66 #include "fcb_config.h"
67 #include <fosa_clocks_and_timers.h>
68 #ifdef CONFIG_FCB_INET
69 #include <forb/proto_inet.h>
72 UL_LOG_CUST(ulogd_fcb);
73 ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "fcb"};
75 fosa_abs_time_t start_time;
79 * Resource identification
82 frsh_resource_type_t type;
83 frsh_resource_id_t id;
92 fres_resource_manager mng; /**< Object reference of the resource manager */
93 gavl_cust_root_field_t allocators; /**< Registered allocators for this resource (from multiple applications/nodes) */
94 ul_list_head_t sc_contracts; /**< Negotiated contracts with spare capacity for this resource */
98 * Resource allocator registered in different nodes/applications
103 fres_resource_allocator ra;
106 struct fcb_contract {
107 fres_contract_id_t id;
108 struct res_alloc *ra; /**< Allocator for this contract TODO: Remove contract if allocator is destroyed */
109 gavl_node_t node_fcb;
110 ul_list_node_t node_sc;
111 ul_list_node_t node_reservation;
116 fosa_abs_time_t end_of_stability_period;
117 struct fres_contract *user_contract; /* The contract sent by user. Set after sucessfull neotiation. */
118 struct fres_contract *requested_contract; /* User request for the contract. Set by prepare_reservation_requests() in the beginning of negotiation. */
119 struct fres_contract *to_be_reserved_contract;
120 struct fres_contract *schedulable_contract;
124 * Contract broker data
127 gavl_cust_root_field_t resources; /**< Registered resources */
128 gavl_cust_root_field_t contracts; /**< Contracts negotiated by this FCB */
131 /** List of contracts to be reserved during spare capacity rebalancing */
132 struct reservation_list {
133 ul_list_head_t fcb_contracts;
137 struct fcb_contract *fcb_contract_new(fres_contract_id_t *id)
139 struct fcb_contract *fcb_contract;
141 fcb_contract = malloc(sizeof(*fcb_contract));
145 memset(fcb_contract, 0, sizeof(*fcb_contract));
146 fcb_contract->id = *id;
151 void fcb_contract_destroy(struct fcb_contract *fcb_contract)
153 if (fcb_contract->user_contract) {
154 fres_contract_destroy(fcb_contract->user_contract);
156 if (fcb_contract->to_be_reserved_contract) {
157 fres_contract_destroy(fcb_contract->to_be_reserved_contract);
159 if (fcb_contract->requested_contract) {
160 fres_contract_destroy(fcb_contract->requested_contract);
162 if (fcb_contract->schedulable_contract) {
163 fres_contract_destroy(fcb_contract->requested_contract);
168 static inline int res_key_cmp(const struct res_key *a,
169 const struct res_key *b)
171 if (a->type < b->type) {
173 } else if (a->type > b->type) {
175 } else if (a->id < b->id) {
177 } else if (a->id > b->id) {
184 /* Container for registered resources */
185 GAVL_CUST_NODE_INT_DEC(fcb_resource /* cust_prefix */, \
186 struct fcb /* cust_root_t */, \
187 struct resource/* cust_item_t */, \
188 struct res_key /* cust_key_t */, \
189 resources /* cust_root_node */, \
190 node /* cust_item_node */, \
191 key /* cust_item_key */, \
192 res_key_cmp /* cust_cmp_fnc */);
194 GAVL_CUST_NODE_INT_IMP(fcb_resource /* cust_prefix */, \
195 struct fcb /* cust_root_t */, \
196 struct resource/* cust_item_t */, \
197 struct res_key /* cust_key_t */, \
198 resources /* cust_root_node */, \
199 node /* cust_item_node */, \
200 key /* cust_item_key */, \
201 res_key_cmp /* cust_cmp_fnc */);
203 /* Container for allocators registered for a given resource */
204 GAVL_CUST_NODE_INT_DEC(fcb_alloc /* cust_prefix */, \
205 struct resource /* cust_root_t */, \
206 struct res_alloc /* cust_item_t */, \
207 forb_server_id /* cust_key_t */, \
208 allocators /* cust_root_node */, \
209 node /* cust_item_node */, \
210 app /* cust_item_key */, \
211 fres_contract_id_cmp /* cust_cmp_fnc */);
213 GAVL_CUST_NODE_INT_IMP(fcb_alloc /* cust_prefix */, \
214 struct resource /* cust_root_t */, \
215 struct res_alloc /* cust_item_t */, \
216 forb_server_id /* cust_key_t */, \
217 allocators /* cust_root_node */, \
218 node /* cust_item_node */, \
219 app /* cust_item_key */, \
220 forb_server_id_cmp /* cust_cmp_fnc */);
222 /* Container for contracts with spare capacity negotiated for a given resource */
223 UL_LIST_CUST_DEC(sc_contracts /* cust_prefix */, \
224 struct resource /* cust_head_t */, \
225 struct fcb_contract /* cust_item_t */, \
226 sc_contracts /* cust_head_field */, \
227 node_sc /* cust_node_field */);
229 UL_LIST_CUST_DEC(reservation_list /* cust_prefix */, \
230 struct reservation_list /* cust_head_t */, \
231 struct fcb_contract /* cust_item_t */, \
232 fcb_contracts /* cust_head_field */, \
233 node_reservation /* cust_node_field */);
235 /* Container for negotiated contracts */
236 GAVL_CUST_NODE_INT_DEC(fcb_contract /* cust_prefix */, \
237 struct fcb /* cust_root_t */, \
238 struct fcb_contract /* cust_item_t */, \
239 fres_contract_id_t /* cust_key_t */, \
240 contracts /* cust_root_node */, \
241 node_fcb /* cust_item_node */, \
242 id /* cust_item_key */, \
243 fres_contract_id_cmp /* cust_cmp_fnc */);
246 GAVL_CUST_NODE_INT_IMP(fcb_contract /* cust_prefix */, \
247 struct fcb /* cust_root_t */, \
248 struct fcb_contract /* cust_item_t */, \
249 fres_contract_id_t /* cust_key_t */, \
250 contracts /* cust_root_node */, \
251 node_fcb /* cust_item_node */, \
252 id /* cust_item_key */, \
253 fres_contract_id_cmp /* cust_cmp_fnc */);
255 #include "fcb_contract_gavl.inc"
259 #define o2fcb(o) (struct fcb*)forb_instance_data(o)
262 get_res_key(const struct fres_contract *contract, struct res_key *key)
264 fres_block_resource *block_res;
266 block_res = fres_contract_get_resource(contract);
268 ul_logerr("No resource specified\n");
271 key->type = block_res->resource_type;
272 key->id = block_res->resource_id;
278 * Fills in an array of fcb_contracts according to requests submited
279 * to negotiate_contracts(). For every contract in @a contracts, there
280 * is one fcb_contract in @a fcb_contracts array.
283 * @param fcb_contracts Where to store pointers to fcb_contracts
284 * @param resource Resource for which negotiation is being done.
285 * @param app Application which requests negotiation
286 * @param contracts Contracts submited to negotiate_contracts()
287 * @param num Number of contracts in contracts (which is the same as the number in fcb_contracts).
289 * @return Zero on success, non-zero error code on error.
292 prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
293 struct resource **resource,
295 struct fres_contract *contracts[], int num)
298 struct fcb_contract *fc;
299 struct res_alloc *ra;
300 struct res_key key, key2 = {-1,-1};
302 for (i=0; i<num; i++) {
303 struct fres_contract *c = contracts[i];
305 if (fres_contract_id_is_empty(&c->id)) {
306 /* Normal negotiation request */
307 forb_uuid_generate((forb_uuid_t *)&c->id);
308 fc = fcb_contract_new(&c->id);
311 fcb_contracts[i] = fc;
312 if (!get_res_key(c, &key)) {
313 return FRSH_ERR_RESOURCE_ID_INVALID;
316 fc = fcb_contract_find(fcb, &c->id);
319 fres_contract_id_to_string(str, &c->id, sizeof(str));
320 ul_logerr("Attempt to change unknown contract %s\n", str);
321 return FRES_ERR_NOTHING_TO_RENEGOTIATE;
323 fcb_contracts[i] = fc;
324 if (fres_contract_get_num_blocks(c) == 0) {
326 get_res_key(fc->user_contract, &key);
329 if (!get_res_key(c, &key)) {
330 return FRSH_ERR_RESOURCE_ID_INVALID;
335 fc->requested_contract = c;
337 /* Check that all contracts are for the same resource */
340 *resource = fcb_resource_find(fcb, &key);
342 ul_logerr("No resource manager for %d.%d registered\n",
344 return FRSH_ERR_RESOURCE_ID_INVALID;
347 else if (key.type != key2.type ||
349 ul_logerr("Contract #%d differs in resource!\n", i);
350 return FRSH_ERR_RESOURCE_ID_INVALID;
353 /* Find allocator for this request */
354 ra = fcb_alloc_find(*resource, app);
357 forb_server_id_to_string(str, app, sizeof(str));
358 ul_logerr("No resource allocator found for %d.%d and %s\n",
359 (*resource)->key.type, (*resource)->key.id, str);
360 return FRES_ERR_NO_RESOURCE_ALLOCATOR;
367 static void fres_contract_ptr_destroy(struct fres_contract **p)
369 fres_contract_destroy(*p);
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 forb_sequence_alloc_buf(contracts, rl->length);
424 contracts._length = rl->length;
426 /* Initialize optimization */
427 ul_list_for_each(reservation_list, rl, fc) {
428 fc->to_be_reserved_contract =
429 fc->requested_contract ? fres_contract_duplicate(fc->requested_contract) :
430 fc->user_contract ? fres_contract_duplicate(fc->user_contract) :
432 assert(fc->to_be_reserved_contract != NULL);
434 forb_sequence_elem(contracts, i) = fc->to_be_reserved_contract;
437 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
438 if (s && s->granularity == FRSH_GR_DISCRETE) {
439 fc->sc_variant.initial = s->variants._length - 1;
440 fc->sc_variant.try = fc->sc_variant.initial;
444 bool all_combinations_tried;
445 int criterion, best_criterion = -1;
446 struct fcb_contract *fcb_changed;
447 /* Exhaustive search. Try all combinations of spare capacity
448 * variants and find the one with best creterion. */
450 all_combinations_tried = true;
453 /* Prepare spare capacity variant */
454 ul_list_for_each(reservation_list, rl, fc) {
455 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
456 /* TODO: Simulate continuous granularity by discretization */
457 if (s && s->granularity == FRSH_GR_DISCRETE) {
458 fres_container_copy(fc->to_be_reserved_contract->container,
459 forb_sequence_elem(s->variants, fc->sc_variant.try));
460 criterion += fc->sc_variant.try;
462 if (fcb_changed == NULL) {
463 /* Chnage tried variant for the next round */
464 fc->sc_variant.try = fc->sc_variant.try > 0 ?
465 fc->sc_variant.try - 1 :
466 s->variants._length - 1;
467 /* Do not change other
468 * contracts unless we have
469 * tried all variants */
470 if (fc->sc_variant.try != fc->sc_variant.initial) {
474 if (fc->sc_variant.try != fc->sc_variant.initial)
475 all_combinations_tried = false;
479 if (criterion > best_criterion) {
480 CORBA_Environment ev;
481 /* Reserve contract */
482 ret = fres_resource_manager_reserve_contracts(resource->mng, &contracts, &ev);
483 if (forb_exception_occurred(&ev)) {
484 ret = FRES_ERR_FORB_EXCEPTION;
485 ul_logerr("FORB exception when reserving contracts\n");
489 ul_logerr("Contract reservation error %d\n", ret);
490 ret = FRES_ERR_ADMISSION_TEST;
493 if (ret == 0) { /* negotiation succeeded */
494 best_criterion = criterion;
497 } while (!all_combinations_tried);
499 if (best_criterion == -1) {
500 ret = FRSH_ERR_CONTRACT_REJECTED;
502 /* At least some variant succeeded */
504 sc_contracts_init_head(resource);
505 ul_list_for_each(reservation_list, rl, fc) {
506 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
507 if (s && s->granularity == FRSH_GR_DISCRETE) {
508 sc_contracts_insert(resource, fc);
513 ul_list_for_each(reservation_list, rl, fc) {
514 fres_contract_destroy(fc->to_be_reserved_contract);
515 fc->to_be_reserved_contract = NULL;
517 forb_sequence_free_buf(contracts, forb_no_destructor);
522 * Create/change VReses according to @a schedulable_contracts.
524 * There might be more allocators for schedulable contracts, so merge
525 * adjacent vreses with the same allocator together and create/change
526 * them in one step. Also the order of schedulable contracts might be
527 * different from the initial order od ids in commit_contracts()
528 * therefore we have to search for every contract.
531 change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
533 struct res_alloc *last_ra = NULL;
534 fres_contract_ptr_seq vreses;
536 CORBA_Environment ev;
538 forb_sequence_alloc_buf(vreses, schedulable_contracts->_length);
541 for (i=0; i<schedulable_contracts->_length; i++) {
542 struct fcb_contract *fc;
543 struct fres_contract *sc = schedulable_contracts->_buffer[i];
545 fc = fcb_contract_find(fcb, &sc->id);
548 if (true /* TODO: if the schedulable contract is changed */) {
549 if (last_ra != fc->ra) {
550 if (vreses._length) {
551 ret = fres_resource_allocator_change_vreses(last_ra->ra,
553 if (forb_exception_occurred(&ev)) {
554 ret = FRES_ERR_FORB_EXCEPTION;
561 vreses._buffer[vreses._length] = sc;
563 fres_contract_destroy(fc->schedulable_contract);
564 fc->schedulable_contract = fres_contract_duplicate(sc);
569 ret = fres_resource_allocator_change_vreses(last_ra->ra,
571 if (forb_exception_occurred(&ev))
572 ret = FRES_ERR_FORB_EXCEPTION;
574 forb_sequence_free_buf(vreses, forb_no_destructor);
579 negotiate_contracts(fres_contract_broker obj,
580 const fres_contract_ptr_seq* contracts,
581 fres_contract_id_seq** ids_out,
582 CORBA_Environment *ev)
584 struct fcb *fcb = o2fcb(obj);
585 struct resource *resource;
588 fres_contract_ptr_seq *schedulable_contracts;
589 struct fcb_contract **fcb_contracts, *fc;
591 fres_contract_id_seq commit_ids;
593 /* Prepare output sequence for the case we return eariler with
595 forb_sequence_alloc(*ids_out, 0);
597 ev->major = FORB_EX_NO_MEMORY;
600 CORBA_sequence_set_release(*ids_out, CORBA_TRUE);
602 forb_get_req_source(obj, &app);
604 fcb_contracts = malloc(sizeof(fcb_contracts[0])*contracts->_length);
605 if (!fcb_contracts) {
609 memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*contracts->_length);
611 ret = prepare_fcb_contracts(fcb, fcb_contracts, &resource,
612 &app, contracts->_buffer, contracts->_length);
614 goto err_free_fcb_contracts;
616 struct reservation_list rl;
617 prepare_reservation_list(resource,
618 fcb_contracts, contracts->_length,
621 /* Allocate all the needed memory before doing reservation. If
622 * there is no enough memory, it has no sense to call resource
624 forb_sequence_alloc_buf(commit_ids, rl.length);
625 if (!commit_ids._buffer) {
627 goto err_free_fcb_contracts;
630 /* Reserve contracts */
631 ret = rebalance_spare_capacity_and_reserve(resource, &rl);
633 if (ret == FRSH_ERR_CONTRACT_REJECTED) {
634 ul_logmsg("Contract(s) was/were rejected\n");
637 fres_strerror(ret, msg, sizeof(msg));
638 ul_logerr("Reservation error: %s\n", msg);
640 goto err_free_fcb_contracts;
643 /* Commit contracts */
644 commit_ids._length = rl.length;
646 ul_list_for_each(reservation_list, &rl, fc) {
647 forb_sequence_elem(commit_ids, i) = fc->id;
651 fres_resource_manager_commit_contracts(resource->mng, &commit_ids,
652 &schedulable_contracts, ev);
653 if (forb_exception_occurred(ev)) {
654 ret = FRES_ERR_FORB_EXCEPTION;
655 goto err_cancel_reservation;
658 /* Add new contracts to our fcb database for later
659 * reference. Canceled contracts are removed below. */
660 for (i=0; i<contracts->_length; i++) {
661 fc = fcb_contracts[i];
663 if (fc->user_contract) {
664 if (fres_contract_get_num_blocks(fc->requested_contract) > 0) {
666 fres_contract_destroy(fc->user_contract);
667 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
668 /* Note: requested_contract is also
669 * pointed by contracts parameter and
670 * will be freed by FORB. */
671 fc->requested_contract = NULL;
674 /* Insert new contracts */
675 fcb_contract_insert(fcb, fcb_contracts[i]);
676 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
677 fc->requested_contract = NULL;
678 /* See the note above. */
682 ret = change_vreses(fcb, schedulable_contracts);
684 goto err_cancel_reservation;
686 forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
688 ul_logerr("VRes was not created\n");
689 goto err_cancel_contracts;
693 /* Return IDs and delete canceled contracts from out database */
694 forb_sequence_alloc_buf(**ids_out, contracts->_length);
695 if (!(*ids_out)->_buffer) {
696 ev->major = FORB_EX_NO_MEMORY;
697 goto err_cancel_contracts;
699 (*ids_out)->_length = contracts->_length;
700 for (i=0; i<contracts->_length; i++) {
701 struct fcb_contract *fc = fcb_contracts[i];
702 forb_sequence_elem(**ids_out, i) = fc->id;
704 if (fc->requested_contract &&
705 fres_contract_get_num_blocks(fc->requested_contract) == 0) {
706 fcb_contract_delete(fcb, fc);
707 /* Note: requested_contract is also pointed by
708 * contracts parameter and will be freed by FORB. */
709 fc->requested_contract = NULL;
710 fcb_contract_destroy(fc);
715 err_cancel_contracts:
717 goto err_free_fcb_contracts;
718 err_cancel_reservation:
719 fres_resource_manager_cancel_reservations(resource->mng, &commit_ids, ev);
720 err_free_fcb_contracts:
721 for (i=0; i<contracts->_length; i++) {
722 fc = fcb_contracts[i];
723 if (fc && !fc->user_contract) {
724 fcb_contracts[i]->requested_contract = NULL; /* Destroyed by FORB */
725 fcb_contract_destroy(fcb_contracts[i]);
733 void redistribute_spare_capacity(fres_contract_broker obj,
734 const frsh_resource_type_t restype,
735 const frsh_resource_id_t resid,
736 CORBA_Environment *ev)
738 struct fcb *fcb = o2fcb(obj);
739 struct res_key key = {restype, resid };
740 struct resource *resource;
741 struct reservation_list rl;
743 resource = fcb_resource_find(fcb, &key);
745 prepare_reservation_list(resource, NULL, 0, &rl);
747 /* forb_sequence_alloc(ids, rl.length); */
748 /* if (!ids || !ids->_buffer) { */
749 /* ev->major = FORB_EX_NO_MEMORY; */
750 /* goto err_free_fcb_contracts; */
752 /* CORBA_sequence_set_release(ids, CORBA_TRUE); */
753 /* *ids_out = ids; /\* ids is freed by FORB *\/ */
756 rebalance_spare_capacity_and_reserve(resource, &rl);
760 CORBA_long register_resource(fres_contract_broker obj,
761 const frsh_resource_type_t restype,
762 const frsh_resource_id_t resid,
763 const fres_resource_desc *desc,
764 CORBA_Environment *ev)
766 struct fcb *fcb = o2fcb(obj);
767 struct resource *res, *res2;
769 res = malloc(sizeof(*res));
771 memset(res, 0, sizeof(*res));
772 res->key.type = restype;
774 res2 = fcb_resource_find(fcb, &res->key);
776 if (forb_object_is_stale(res2->mng)) {
777 ul_logmsg("Removing stale manager for resource %d.%d\n",
779 forb_object_release(res2->mng);
780 fcb_resource_delete(fcb, res2);
781 /* TODO: Delete also all allocators associated
782 * with this stale resource manager. */
786 ul_logerr("Resource manager %d.%d already registered\n",
791 res->mng = forb_object_duplicate(desc->manager);
793 fcb_alloc_init_root_field(res);
794 sc_contracts_init_head(res);
795 ul_logmsg("Registering manager for resource %d.%d\n",
797 fcb_resource_insert(fcb, res);
806 CORBA_long register_allocator(fres_contract_broker obj,
807 const frsh_resource_type_t restype,
808 const frsh_resource_id_t resid,
809 const fres_resource_allocator ra_obj,
810 CORBA_Environment *ev)
812 struct fcb *fcb = o2fcb(obj);
813 struct resource *res;
814 struct res_alloc *ra;
815 struct res_key resource;
816 forb_server_id server_id;
817 char server_id_str[40];
819 forb_get_server_id(ra_obj, &server_id);
820 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
821 ul_logmsg("Registering allocator for resource %d.%d in app %s\n",
822 restype, resid, server_id_str);
824 resource.type = restype;
826 res = fcb_resource_find(fcb, &resource);
828 ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
832 ra = fcb_alloc_find(res, &server_id);
834 char *str = forb_object_to_string(ra_obj);
835 ul_logerr("Allocator from already registered (%s)\n",
840 ra = malloc(sizeof(*ra));
845 ra->ra = forb_object_duplicate(ra_obj);
846 fcb_alloc_insert(res, ra);
849 return FRSH_ERR_RESOURCE_ID_INVALID;
852 void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
854 struct fcb *fcb = o2fcb(obj);
855 fres_resource_seq *seq;
856 struct resource *res;
859 seq = malloc(sizeof(*seq));
861 ev->major = FORB_EX_NO_MEMORY;
864 memset(seq, 0, sizeof(*seq));
867 gavl_cust_for_each(fcb_resource, fcb, res) {
871 seq->_buffer = CORBA_sequence_fres_resource_allocbuf(n);
876 gavl_cust_for_each(fcb_resource, fcb, res) {
877 seq->_buffer[n].restype = res->key.type;
878 seq->_buffer[n].resid = res->key.id;
879 seq->_buffer[n].desc.manager = res->mng;
886 #ifdef CONFIG_FCB_INET
887 static int register_inet_port(forb_orb orb)
889 forb_port_t *port = forb_malloc(sizeof(*port));
891 struct in_addr listen_on;
895 memset(port, 0, sizeof(*port));
896 listen_on.s_addr = INADDR_ANY;
897 ret = forb_inet_port_init(&port->desc, listen_on);
900 ret = forb_register_port(orb, port);
905 struct forb_fres_contract_broker_impl impl = {
906 .negotiate_contracts = negotiate_contracts,
907 .register_resource = register_resource,
908 .register_allocator = register_allocator,
909 .redistribute_spare_capacity = redistribute_spare_capacity,
910 .get_resources = get_resources,
913 void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
915 forb_server_id server_id;
916 char server_id_str[sizeof(forb_server_id)*2+1];
918 forb_get_server_id(peer_orb, &server_id);
919 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
921 ul_logmsg("peer discovered: %s (orb_id '%s')\n", server_id_str, orb_id);
927 if (strcmp(orb_id, "org.frescor.fcb") == 0) {
929 fosa_rel_time_t delay;
930 fosa_clock_get_time(CLOCK_REALTIME, &now);
931 delay = fosa_abs_time_extract_interval(start_time, now);
932 ul_logmsg("node joined: %s (time %ld ms)\n", server_id_str,
933 fosa_rel_time_to_msec(delay));
937 void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
941 int main(int argc, char *argv[])
945 fres_contract_broker fcb;
946 forb_executor_t executor;
948 forb_init_attr_t attr = {
949 .orb_id = "org.frescor.fcb",
950 .peer_discovery_callback = peer_discovery_callback,
951 .peer_dead_callback = peer_dead_callback,
955 fosa_clock_get_time(CLOCK_REALTIME, &start_time);
957 orb = forb_init(&argc, &argv, &attr);
958 if (!orb) error(1, errno, "FORB initialization failed");
960 #ifdef CONFIG_FCB_INET
961 ret = register_inet_port(orb);
962 if (ret) error(0, errno, "INET port registration failed");
965 fcb_resource_init_root_field(&fcb_data);
966 fcb_contract_init_root_field(&fcb_data);
968 fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
969 if (!fcb) error(1, errno, "forb_fres_contract_broker_new failed");
971 /* Prepare executor before we register the fcb reference,
972 * so that no reuqests are lost */
973 ret = forb_executor_init(&executor);
974 if (ret) error(1, errno, "forb_executor_init failed");
976 ret = forb_executor_register_object(&executor, fcb);
977 if (ret) error(1, errno, "forb_executor_register_object failed");
979 ret = forb_register_reference(fcb, fres_contract_broker_reg_name);
980 if (ret) error(1, errno, "forb_register_reference() failed");
982 ul_logmsg("Waiting for requests\n");
983 ret = forb_executor_run(&executor);
984 if (ret) error(1, errno, "forb_executor_run failed");