*
*
*/
+#define WITH_C99 /* For ul_gsa.h iterators */
+#include <semaphore.h>
+#include <getopt.h>
#include <forb.h>
+#include <forb/config.h>
#include <fcb.h>
+#include <fcb_contact_info.h>
#include <error.h>
#include <errno.h>
#include <stdio.h>
#include <ul_gavlcust.h>
+#include <ul_gsacust.h>
#include <string.h>
#include <ul_log.h>
+#include <ul_logreg.h>
#include <forb/server_id.h>
#include <fres_contract.h>
#include "fcb_config.h"
#include <fosa_clocks_and_timers.h>
-#ifdef CONFIG_FCB_INET
+#include "contract_log.h"
+#if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
#include <forb/proto_inet.h>
#endif
-UL_LOG_CUST(ulogd_fcb);
-ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "fcb"};
+#define COMPILE_TIME_ASSERT(cond, msg) \
+ typedef char msg[(cond) ? 1 : -1]
+
+UL_LOG_CUST(ulogd_fcb);
+ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "main"};
+UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(init_ulogd_fcb, ulogd_fcb);
+
+bool opt_daemon = false;
+char *opt_pidfile = NULL;
fosa_abs_time_t start_time;
+/** List of contracts to be newly reserved or changed by resource manager */
+struct reservation_list {
+ ul_list_head_t fcb_contracts;
+ unsigned length;
+};
/**
* Resource identification
struct resource {
gavl_node_t node;
struct res_key key;
+ char *name;
fres_resource_manager mng; /**< Object reference of the resource manager */
gavl_cust_root_field_t allocators; /**< Registered allocators for this resource (from multiple applications/nodes) */
+ ul_list_head_t sc_contracts; /**< Negotiated contracts with spare capacity for this resource */
+ struct reservation_list rl; /**< Temporary list of contracts to be reserved on this resource */
};
/**
fres_resource_allocator ra;
};
+struct fcb_transaction;
+
struct fcb_contract {
fres_contract_id_t id;
+ struct res_alloc *ra; /**< Allocator for this contract TODO: Remove contract if allocator is destroyed */
+ gavl_node_t node_fcb;
+ ul_list_node_t node_sc;
+ ul_list_node_t node_reservation;
+ struct fcb_transaction *transaction;
+ struct {
+ int initial;
+ int try;
+ } sc_variant;
+ fosa_abs_time_t end_of_stability_period;
+ struct fres_contract *user_contract; /* The contract sent by user. Set after sucessfull neotiation. */
+ struct fres_contract *requested_contract; /* User request for the contract. Set by prepare_reservation_requests() in the beginning of negotiation. */
+ struct fres_contract *to_be_reserved_contract;
+ struct fres_contract *schedulable_contract;
+};
+
+struct fcb_transaction {
+ char *name;
gavl_node_t node;
- struct fres_contract *user_contract;
+ gsa_array_field_t contracts;
};
/**
* Contract broker data
*/
struct fcb {
+ fres_contract_id_t contract_counter;
gavl_cust_root_field_t resources; /**< Registered resources */
gavl_cust_root_field_t contracts; /**< Contracts negotiated by this FCB */
+ gavl_cust_root_field_t transactions; /**< Transactions negotiated by this FCB */
};
struct fcb_contract *fcb_contract_new(fres_contract_id_t *id)
if (fcb_contract->user_contract) {
fres_contract_destroy(fcb_contract->user_contract);
}
+ if (fcb_contract->to_be_reserved_contract) {
+ fres_contract_destroy(fcb_contract->to_be_reserved_contract);
+ }
+ if (fcb_contract->requested_contract) {
+ fres_contract_destroy(fcb_contract->requested_contract);
+ }
+ if (fcb_contract->schedulable_contract) {
+ fres_contract_destroy(fcb_contract->requested_contract);
+ }
free(fcb_contract);
}
+static inline int
+unsorted(const void *a, const void *b)
+{ abort(); }
+
+/* Unordered dynamic array of contracts in transaction */
+GSA_CUST_DEC(tran_contract /* cust_prefix */,
+ struct fcb_transaction /* cust_array_t */,
+ struct fcb_contract/* cust_item_t */,
+ fres_contract_id_t /* cust_key_t */,
+ contracts /* cust_array_field */,
+ id /* cust_item_key */,
+ unsorted /* cust_cmp_fnc */);
+
+GSA_CUST_IMP(tran_contract /* cust_prefix */,
+ struct fcb_transaction /* cust_array_t */,
+ struct fcb_contract/* cust_item_t */,
+ fres_contract_id_t /* cust_key_t */,
+ contracts /* cust_array_field */,
+ id /* cust_item_key */,
+ unsorted /* cust_cmp_fnc */,
+ true /* cust_ins_fl */);
+
+struct fcb_transaction *fcb_transaction_new(char *name)
+{
+ struct fcb_transaction *fcb_transaction;
+
+ fcb_transaction = malloc(sizeof(*fcb_transaction));
+ if (!fcb_transaction) {
+ return NULL;
+ }
+ memset(fcb_transaction, 0, sizeof(*fcb_transaction));
+ fcb_transaction->name = strdup(name);
+
+ tran_contract_init_array_field(fcb_transaction);
+
+ return fcb_transaction;
+}
+
+void fcb_transaction_destroy(struct fcb_transaction *fcb_transaction)
+{
+ free(fcb_transaction->name);
+ free(fcb_transaction);
+}
+
static inline int res_key_cmp(const struct res_key *a,
const struct res_key *b)
{
app /* cust_item_key */, \
forb_server_id_cmp /* cust_cmp_fnc */);
+/* Container for contracts with spare capacity negotiated for a given resource */
+UL_LIST_CUST_DEC(sc_contracts /* cust_prefix */, \
+ struct resource /* cust_head_t */, \
+ struct fcb_contract /* cust_item_t */, \
+ sc_contracts /* cust_head_field */, \
+ node_sc /* cust_node_field */);
+
+UL_LIST_CUST_DEC(reservation_list /* cust_prefix */, \
+ struct reservation_list /* cust_head_t */, \
+ struct fcb_contract /* cust_item_t */, \
+ fcb_contracts /* cust_head_field */, \
+ node_reservation /* cust_node_field */);
+
/* Container for negotiated contracts */
GAVL_CUST_NODE_INT_DEC(fcb_contract /* cust_prefix */, \
struct fcb /* cust_root_t */, \
struct fcb_contract /* cust_item_t */, \
fres_contract_id_t /* cust_key_t */, \
contracts /* cust_root_node */, \
- node /* cust_item_node */, \
+ node_fcb /* cust_item_node */, \
id /* cust_item_key */, \
fres_contract_id_cmp /* cust_cmp_fnc */);
struct fcb_contract /* cust_item_t */, \
fres_contract_id_t /* cust_key_t */, \
contracts /* cust_root_node */, \
- node /* cust_item_node */, \
+ node_fcb /* cust_item_node */, \
id /* cust_item_key */, \
fres_contract_id_cmp /* cust_cmp_fnc */);
#else
#include "fcb_contract_gavl.inc"
#endif
+static inline int
+fcb_transaction_cmp(char * const *a, char * const *b)
+{ return strcmp(*a, *b); }
+
+GAVL_CUST_NODE_INT_DEC(fcb_transaction /* cust_prefix */, \
+ struct fcb /* cust_root_t */, \
+ struct fcb_transaction /* cust_item_t */, \
+ char* /* cust_key_t */, \
+ transactions /* cust_root_node */, \
+ node /* cust_item_node */, \
+ name /* cust_item_key */, \
+ fcb_transaction_cmp /* cust_cmp_fnc */);
+
+GAVL_CUST_NODE_INT_IMP(fcb_transaction /* cust_prefix */, \
+ struct fcb /* cust_root_t */, \
+ struct fcb_transaction /* cust_item_t */, \
+ char* /* cust_key_t */, \
+ transactions /* cust_root_node */, \
+ node /* cust_item_node */, \
+ name /* cust_item_key */, \
+ fcb_transaction_cmp /* cust_cmp_fnc */);
+
+struct res_array {
+ gsa_array_field_t array;
+};
+
+GSA_CUST_DEC(res_array /* cust_prefix */,
+ struct res_array /* cust_array_t */,
+ struct resource /* cust_item_t */,
+ struct res_key /* cust_key_t */,
+ array /* cust_array_field */,
+ key /* cust_item_key */,
+ res_key_cmp /* cust_cmp_fnc */);
+
+GSA_CUST_IMP(res_array /* cust_prefix */,
+ struct res_array /* cust_array_t */,
+ struct resource /* cust_item_t */,
+ struct res_key /* cust_key_t */,
+ array /* cust_array_field */,
+ key /* cust_item_key */,
+ res_key_cmp /* cust_cmp_fnc */,
+ true /* cust_ins_fl */);
+
+
#define o2fcb(o) (struct fcb*)forb_instance_data(o)
-struct res_key *
-get_res_key(const struct fcb *fcb, const struct fres_contract *contract, struct res_key *key)
+static struct res_key*
+get_res_key(const struct fres_contract *contract, struct res_key *key)
{
fres_block_resource *block_res;
block_res = fres_contract_get_resource(contract);
- if (!block_res && !fres_contract_id_is_empty(&contract->id)) {
- /* If the contract doesn't have resource information,
- * this might be cancelation or renegotiation request,
- * so look at our database for formerly submited
- * contract. */
- struct fcb_contract *fcb_contract;
- fcb_contract = fcb_contract_find(fcb, &contract->id);
- if (fcb_contract) {
- block_res = fres_contract_get_resource(fcb_contract->user_contract);
- }
- }
if (!block_res) {
ul_logerr("No resource specified\n");
return NULL;
return key;
}
+static struct res_key*
+get_fc_res_key(const struct fcb_contract *fc, struct res_key *key)
+{
+ if (fc->user_contract)
+ get_res_key(fc->user_contract, key);
+ else
+ get_res_key(fc->requested_contract, key);
+ return key;
+}
+
/**
- * Checks whether all contracts refers to a single resource.
+ * Fills in an array of fcb_contracts according to requests submited
+ * by an application through negotiate_contracts() or
+ * negotiate_transaction(). For every contract in @a contracts, there
+ * is allocated one fcb_contract in @a fcb_contracts array.
*
- * @param fcb
- * @param contracts Array of contract pointers.
- * @param num Number of contracts.
+ * @param fcb
+ * @param fcb_contracts Where to store pointers to fcb_contracts
+ * @param contracts Contracts submited for negotiation
+ * @param num Number of contracts in contracts (which is the same as the number in fcb_contracts).
*
- * @return If all contracts refer to a signle resource, pointer to the
- * coresponding resource structure is returned. Otherwise, NULL is
- * returned.
+ * @return Zero on success, non-zero error code on error.
*/
-struct resource *
-check_single_resource(struct fcb *fcb, struct fres_contract *contracts[], int num)
+static int
+prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
+ struct fres_contract *contracts[], int num)
{
- struct resource *resource = NULL;
unsigned i;
+ struct fcb_contract *fc;
+
+ for (i=0; i<num; i++) {
+ struct fres_contract *c = contracts[i];
+
+ if (fres_contract_id_is_empty(&c->id)) {
+ /* Normal negotiation request */
+ COMPILE_TIME_ASSERT(sizeof(c->id) == sizeof(fcb->contract_counter),
+ wrong_size_of_contract_id);
+ c->id = ++fcb->contract_counter;
+ fc = fcb_contract_new(&c->id);
+ if (!fc)
+ return errno;
+ fcb_contracts[i] = fc;
+ log_contract("Negotiation request", i, c);
+ } else {
+ fc = fcb_contract_find(fcb, &c->id);
+ if (!fc) {
+ char str[60];
+ fres_contract_id_to_string(str, &c->id, sizeof(str));
+ ul_logerr("Attempt to change unknown contract %s\n", str);
+ return FRES_ERR_NOTHING_TO_RENEGOTIATE;
+ } else {
+ fcb_contracts[i] = fc;
+ if (fres_contract_get_num_blocks(c) == 0) {
+ /* Cancelation */
+ log_contract("Cancelation request", i, fc->user_contract);
+ } else {
+ /* Renegotiation */
+ log_contract("Renegotiation request", i, fc->user_contract);
+ }
+ }
+ }
+ fc->requested_contract = c;
+ }
+ return 0;
+}
+
+/**
+ * Ensure that all contracts belong to the same resource and find
+ * resource allocators for the contracts.
+ *
+ * @param fcb
+ * @param fcb_contracts Array of fcb_contracts prepared by prepare_fcb_contracts()
+ * @param num Number of contracts in @a fcb_contracts
+ * @param resource Resource for which negotiation is being done.
+ * @param app Application which requests negotiation
+ */
+static int
+check_and_setup_resource(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
+ struct resource **resource,
+ forb_server_id *app,
+ int num)
+{
+ unsigned i;
+ struct res_alloc *ra;
struct res_key key, key2 = {-1,-1};
for (i=0; i<num; i++) {
- if (!get_res_key(fcb, contracts[i], &key)) {
- return NULL;
+ struct fcb_contract *fc = fcb_contracts[i];
+
+ if (!get_fc_res_key(fc, &key))
+ return FRSH_ERR_RESOURCE_ID_INVALID;
+
+ /* Check that all contracts are for the same resource */
+ if (i==0) {
+ key2 = key;
+ *resource = fcb_resource_find(fcb, &key);
+ if (!*resource) {
+ ul_logerr("No resource manager for %d.%d registered\n",
+ key.type, key.id);
+ return FRES_ERR_NO_RESOURCE_MANAGER;
+ }
+ /* Find allocator for this request */
+ ra = fcb_alloc_find(*resource, app);
+ if (!ra) {
+ char str[60];
+ forb_server_id_to_string(str, app, sizeof(str));
+ ul_logerr("No resource allocator found for %d.%d and %s\n",
+ (*resource)->key.type, (*resource)->key.id, str);
+ return FRES_ERR_NO_RESOURCE_ALLOCATOR;
+ }
}
- if (i==0) key2 = key;
else if (key.type != key2.type ||
key.id != key2.id) {
- return NULL;
+ ul_logerr("Contract #%d differs in resource!\n", i);
+ return FRSH_ERR_RESOURCE_ID_INVALID;
+ }
+ fc->ra = ra;
+ }
+ return 0;
+}
+
+/**
+ * Prepares a list of contracts to pass to resource manager for (re)reserving.
+ *
+ * @todo Needs to be changed for compatibility with transactions.
+ *
+ * @param resource Resource for which to rebalance capacity and negotiate new contracts
+ * @param fcb_contract New requests to negotiate
+ * @param num The number of elements in @a fcb_contract
+ * @param[out] rl List with changed contracts to be commited
+ */
+static void
+prepare_reservation_list(struct resource *resource,
+ struct fcb_contract *fcb_contract[], int num)
+{
+ int i;
+ fosa_abs_time_t now;
+ struct fcb_contract *fc;
+
+ reservation_list_init_head(&resource->rl);
+ resource->rl.length = 0;
+ for (i=0; i<num; i++) {
+ assert(fcb_contract[i]->requested_contract != NULL);
+ reservation_list_insert(&resource->rl, fcb_contract[i]);
+ resource->rl.length++;
+ }
+ fosa_clock_get_time(CLOCK_REALTIME, &now);
+ ul_list_for_each(sc_contracts, resource, fc) {
+ if (fosa_abs_time_smaller(fc->end_of_stability_period, now) &&
+ fc->requested_contract == NULL) /* Do not insert contract inserted above */
+ {
+ reservation_list_insert(&resource->rl, fc);
+ resource->rl.length++;
}
}
+}
+
+static int
+reserve_resource(struct resource *resource)
+{
+ int ret, i;
+ fres_contract_ptr_seq contracts;
+ struct fres_contract *c;
+ struct fcb_contract *fc;
+ CORBA_Environment ev;
- resource = fcb_resource_find(fcb, &key);
- if (!resource) {
- ul_logerr("No resource manager for %d.%d registered\n",
- key.type, key.id);
+ if (!forb_sequence_alloc_buf(&contracts, resource->rl.length)) {
+ ret = errno;
+ goto err;
+ }
+ forb_sequence_length(&contracts) = resource->rl.length;
+
+ i=0;
+ /* Prepare FORB sequence */
+ ul_list_for_each(reservation_list, &resource->rl, fc) {
+ c = NULL;
+ if (fc->to_be_reserved_contract)
+ /* Contract without spare capacity */
+ c = fc->to_be_reserved_contract;
+ else if (fc->requested_contract)
+ c = fc->requested_contract;
+ assert(c);
+ forb_sequence_elem(&contracts, i++) = c;
}
- return resource;
+
+ /* Reserve contract */
+ ret = fres_resource_manager_reserve_contracts(resource->mng, &contracts, &ev);
+ if (forb_exception_occurred(&ev)) {
+ ret = fres_forbex2err(&ev);
+ ul_logerr("FORB exception when reserving contracts\n");
+ goto err_free;
+ }
+ if (ret < 0) {
+ ul_logerr("Contract reservation error %d\n", ret);
+ ret = FRES_ERR_ADMISSION_TEST;
+ goto err_free;
+ } else if (ret == 0)
+ ret = FRSH_NO_ERROR;
+ else if (ret == 1)
+ ret = FRSH_ERR_CONTRACT_REJECTED;
+ else
+ assert(false);
+err_free:
+ forb_sequence_free_buf(&contracts, forb_no_destructor);
+err:
+ return ret;
}
/**
*
*
- * @param fcb
- * @param contracts
- * @param num
+ * @param resource Resource for which to rebalance capacity and negotiate new contracts
+ * @param rl List with changed contracts to be commited
*
- * @return Zero on success, non-zero error code on error.
+ * @return Zero on success, non-zero error code on error
*/
static int
-prepare_reservation_contracts(struct fcb *fcb, struct fres_contract *contracts[], int num)
+rebalance_spare_capacity_and_reserve(struct resource *resource)
{
- unsigned i;
- struct fcb_contract *fcb_contract;
+ int ret;
+ struct reservation_list *rl = &resource->rl;
+ struct fcb_contract *fc;
+ fres_block_spare_capacity *s;
+
+ /* Initialize optimization */
+ ul_list_for_each(reservation_list, rl, fc) {
+ fc->to_be_reserved_contract =
+ fc->requested_contract ? fres_contract_duplicate(fc->requested_contract) :
+ fc->user_contract ? fres_contract_duplicate(fc->user_contract) :
+ NULL;
+ assert(fc->to_be_reserved_contract != NULL);
+
+ s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
+ if (s && s->granularity == FRSH_GR_DISCRETE) {
+ fc->sc_variant.initial = s->variants._length - 1;
+ fc->sc_variant.try = fc->sc_variant.initial;
+ }
+ }
+
+ bool all_combinations_tried;
+ int criterion, best_criterion = -1;
+ struct fcb_contract *fcb_changed;
+ /* Exhaustive search. Try all combinations of spare capacity
+ * variants and find the one with best creterion. */
+ do {
+ all_combinations_tried = true;
+ fcb_changed = NULL;
+ criterion = 0;
+ /* Prepare spare capacity variant */
+ ul_list_for_each(reservation_list, rl, fc) {
+ s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
+ /* TODO: Simulate continuous granularity by discretization */
+ if (s && s->granularity == FRSH_GR_DISCRETE) {
+ fres_container_copy(fc->to_be_reserved_contract->container,
+ forb_sequence_elem(&s->variants, fc->sc_variant.try));
+ criterion += fc->sc_variant.try;
+
+ if (fcb_changed == NULL) {
+ /* Chnage tried variant for the next round */
+ fc->sc_variant.try = fc->sc_variant.try > 0 ?
+ fc->sc_variant.try - 1 :
+ s->variants._length - 1;
+ /* Do not change other
+ * contracts unless we have
+ * tried all variants */
+ if (fc->sc_variant.try != fc->sc_variant.initial) {
+ fcb_changed = fc;
+ }
+ }
+ if (fc->sc_variant.try != fc->sc_variant.initial)
+ all_combinations_tried = false;
+ }
+ }
+
+ if (criterion > best_criterion) {
+ ret = reserve_resource(resource);
+ switch (ret) {
+ case FRSH_NO_ERROR:
+ best_criterion = criterion;
+ break;
+ case FRSH_ERR_CONTRACT_REJECTED:
+ break;
+ default:
+ goto err;
+ }
+ }
+ } while (!all_combinations_tried);
+
+ if (best_criterion == -1) {
+ ret = FRSH_ERR_CONTRACT_REJECTED;
+ } else {
+ /* At least some variant succeeded */
+ ret = 0;
+ sc_contracts_init_head(resource);
+ ul_list_for_each(reservation_list, rl, fc) {
+ s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
+ if (s && s->granularity == FRSH_GR_DISCRETE) {
+ sc_contracts_insert(resource, fc);
+ }
+ }
+ }
+err:
+ ul_list_for_each(reservation_list, rl, fc) {
+ fres_contract_destroy(fc->to_be_reserved_contract);
+ fc->to_be_reserved_contract = NULL;
+ }
+ return ret;
+}
+
+/**
+ * Create/change VReses according to @a schedulable_contracts.
+ *
+ * There might be more allocators for schedulable contracts, so merge
+ * adjacent vreses with the same allocator together and create/change
+ * them in one step. Also the order of schedulable contracts might be
+ * different from the initial order od ids in commit_contracts()
+ * therefore we have to search for every contract.
+ */
+static int
+change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
+{
+ struct res_alloc *last_ra = NULL;
+ fres_contract_ptr_seq vreses;
+ int i, ret = 0;
+ CORBA_Environment ev;
- for (i=0; i<num; i++) {
- struct fres_contract *c = contracts[i];
+ if (!forb_sequence_alloc_buf(&vreses, schedulable_contracts->_length)) {
+ return errno;
+ }
+ vreses._length = 0;
+
+ for (i=0; i<schedulable_contracts->_length; i++) {
+ struct fcb_contract *fc;
+ struct fres_contract *sc = schedulable_contracts->_buffer[i];
+
+ log_contract("Changing VRES", i, sc);
+
+ fc = fcb_contract_find(fcb, &sc->id);
+ assert(fc != NULL);
+
+ if (true /* TODO: if the schedulable contract is changed */) {
+ if (last_ra != fc->ra) {
+ if (vreses._length) {
+ ret = fres_resource_allocator_change_vreses(last_ra->ra,
+ &vreses, &ev);
+ if (forb_exception_occurred(&ev)) {
+ ret = fres_forbex2err(&ev);
+ goto err;
+ }
+ if (ret) goto err;
+ }
+ vreses._length = 0;
+ }
+ vreses._buffer[vreses._length] = sc;
+ vreses._length++;
+ last_ra = fc->ra;
- if (fres_contract_id_is_empty(&c->id)) {
- forb_uuid_generate((forb_uuid_t *)&c->id);
- continue;
}
- if (fres_contract_get_num_blocks(c) == 0) {
- /* Nothing to do for deletion requesst */
- continue;
+ }
+ if (last_ra) {
+ ret = fres_resource_allocator_change_vreses(last_ra->ra,
+ &vreses, &ev);
+ if (forb_exception_occurred(&ev))
+ ret = fres_forbex2err(&ev);
+ }
+err:
+ forb_sequence_free_buf(&vreses, forb_no_destructor);
+ return ret;
+}
+
+void
+free_fcb_contracts(struct fcb_contract *fcb_contracts[], int num)
+{
+ int i;
+ struct fcb_contract *fc;
+ for (i=0; i<num; i++) {
+ fc = fcb_contracts[i];
+ if (fc && !fc->user_contract) {
+ fc->requested_contract = NULL; /* Destroyed by FORB */
+ fcb_contract_destroy(fc);
}
+ }
+ free(fcb_contracts);
+}
+
+int
+commit_resource(struct resource *resource,
+ fres_contract_ptr_seq **schedulable_contracts)
+{
+ int ret;
+ fres_contract_id_seq commit_ids;
+ int i;
+ struct fcb_contract *fc;
+ CORBA_Environment ev;
+
+ if (!forb_sequence_alloc_buf(&commit_ids, resource->rl.length)) {
+ ret = errno;
+ goto err;
+ }
- /* Renegotiation */
- fcb_contract = fcb_contract_find(fcb, &c->id);
- if (fcb_contract) {
- /* Copy missing blocks from fcb_contract to contract */
- fres_contract_merge(c, fcb_contract->user_contract);
+ commit_ids._length = resource->rl.length;
+ i=0;
+ ul_list_for_each(reservation_list, &resource->rl, fc) {
+ forb_sequence_elem(&commit_ids, i) = fc->id;
+ i++;
+ }
+
+ fres_resource_manager_commit_contracts(resource->mng, &commit_ids,
+ schedulable_contracts, &ev);
+ if (forb_exception_occurred(&ev)) {
+ ret = fres_forbex2err(&ev);
+ goto err_free;
+ }
+ return 0;
+err_free:
+ forb_sequence_free_buf(&commit_ids, forb_no_destructor);
+err:
+ return ret;
+}
+
+int cancel_reservations(struct resource *resource)
+{
+ int ret;
+ fres_contract_id_seq commit_ids;
+ int i;
+ struct fcb_contract *fc;
+ CORBA_Environment ev;
+
+ if (!forb_sequence_alloc_buf(&commit_ids, resource->rl.length)) {
+ ret = errno;
+ goto err;
+ }
+
+ commit_ids._length = resource->rl.length;
+ i=0;
+ ul_list_for_each(reservation_list, &resource->rl, fc) {
+ forb_sequence_elem(&commit_ids, i) = fc->id;
+ i++;
+ }
+
+ fres_resource_manager_cancel_reservations(resource->mng, &commit_ids, &ev);
+ if (forb_exception_occurred(&ev)) {
+ ret = fres_forbex2err(&ev);
+ goto err_free;
+ }
+
+ return 0;
+err_free:
+ forb_sequence_free_buf(&commit_ids, forb_no_destructor);
+err:
+ return ret;
+}
+
+/* Add new contracts to our fcb database for later
+ * reference. Canceled contracts are removed below. */
+int
+fcb_remember_contracts(struct fcb *fcb,
+ struct fcb_contract **fcb_contracts, int num)
+{
+ struct fcb_contract *fc;
+ int i;
+
+ for (i=0; i<num; i++) {
+ fc = fcb_contracts[i];
+
+ if (fc->user_contract) {
+ if (fres_contract_get_num_blocks(fc->requested_contract) > 0) {
+ /* Renegotiation */
+ fres_contract_destroy(fc->user_contract);
+ fc->user_contract = fres_contract_duplicate(fc->requested_contract);
+ /* Note: requested_contract is also
+ * pointed by contracts parameter and
+ * will be freed by FORB. */
+ fc->requested_contract = NULL;
+ }
} else {
- char str[60];
- fres_contract_id_to_string(str, &c->id, sizeof(str));
- ul_logerr("Attempt to renegotiate unknown contract %s\n", str);
- return FRES_ERR_NOTHING_TO_RENEGOTIATE;
+ /* Insert new contracts */
+ fcb_contract_insert(fcb, fcb_contracts[i]);
+ fc->user_contract = fres_contract_duplicate(fc->requested_contract);
+ fc->requested_contract = NULL;
+ /* See the note above. */
}
+ }
+ return 0;
+}
- }
+int
+fcb_remember_schedulable_contracts(struct fcb *fcb,
+ fres_contract_ptr_seq *schedulable_contracts)
+{
+ struct fcb_contract *fc;
+ int i;
+ for (i=0; i<schedulable_contracts->_length; i++) {
+ struct fres_contract *sc = schedulable_contracts->_buffer[i];
+ fc = fcb_contract_find(fcb, &sc->id);
+ assert(fc != NULL);
+ fres_contract_destroy(fc->schedulable_contract);
+ fc->schedulable_contract = fres_contract_duplicate(sc);
+ assert(fc->schedulable_contract);
+ }
+ return 0;
+}
+
+static int
+reject_transactions(struct fcb_contract *fcb_contracts[], int num)
+{
+ int i;
+ for (i = 0; i < num; i++) {
+ if (fcb_contracts[i]->transaction)
+ return FRES_ERR_VRES_PART_OF_TRANSACTION;
+ }
return 0;
}
CORBA_Environment *ev)
{
struct fcb *fcb = o2fcb(obj);
- struct resource *resource;
- struct res_alloc *ra;
- int ret;
+ struct resource *resource = NULL;
+ int ret = 0;
forb_server_id app;
fres_contract_ptr_seq *schedulable_contracts;
struct fcb_contract **fcb_contracts;
unsigned i;
- fres_contract_id_seq* ids;
+ int num = contracts->_length;
- resource = check_single_resource(fcb, contracts->_buffer, contracts->_length);
- if (!resource) {
- ret = FRSH_ERR_RESOURCE_ID_INVALID;
+ /* Prepare output sequence for the case we return eariler with
+ * an error */
+ forb_sequence_alloc(*ids_out, 0);
+ if (!*ids_out) {
+ ev->major = FORB_EX_NO_MEMORY;
goto err;
}
-
- ret = prepare_reservation_contracts(fcb, contracts->_buffer, contracts->_length);
- if (ret)
- goto err;
+ CORBA_sequence_set_release(*ids_out, CORBA_TRUE);
forb_get_req_source(obj, &app);
- ra = fcb_alloc_find(resource, &app);
- if (!ra) {
- char str[60];
- forb_server_id_to_string(str, &app, sizeof(str));
- ul_logerr("No resource allocator found for %d.%d and %s\n",
- resource->key.type, resource->key.id, str);
- ret = FRES_ERR_NO_RESOURCE_ALLOCATOR;
- goto err;
- }
-
- /* Allocate all the needed memory before doing reservation. If
- * there is no enough memory, it has no sense to call resource
- * manager. */
- ids = malloc(sizeof(*ids));
- if (!ids) {
- ev->major = FORB_EX_NO_MEMORY;
- goto err;
- }
- memset(ids, 0, sizeof(*ids));
- CORBA_sequence_set_release(ids, CORBA_TRUE);
- *ids_out = ids;
- ids->_buffer = malloc(contracts->_length*sizeof(ids->_buffer[0]));
- if (!ids->_buffer) {
- ret = errno;
- goto err;
- }
- ids->_length = ids->_maximum = contracts->_length;
- for (i=0; i<contracts->_length; i++) {
- ids->_buffer[i] = contracts->_buffer[i]->id;
- }
- fcb_contracts = malloc(sizeof(fcb_contracts[0])*contracts->_length);
+ fcb_contracts = malloc(sizeof(fcb_contracts[0])*num);
if (!fcb_contracts) {
ret = errno;
goto err;
}
- memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*contracts->_length);
+ memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*num);
- for (i=0; i<contracts->_length; i++) {
- struct fres_contract *c = contracts->_buffer[i];
- if (fres_contract_get_num_blocks(c) > 0) {
- fcb_contracts[i] = fcb_contract_new(&c->id);
- if (!fcb_contracts[i]) {
- ret = errno ? errno : -1;
- goto err_free_fcb_contracts;
- }
- fcb_contracts[i]->user_contract = fres_contract_duplicate(c);
- if (!fcb_contracts[i]->user_contract) {
- ret = errno ? errno : -1;
- goto err_free_fcb_contracts;
- }
- }
- }
+ ret = prepare_fcb_contracts(fcb, fcb_contracts,
+ contracts->_buffer, num);
+ if (ret)
+ goto err_free_fcb_contracts;
- /* TODO: Optimize the following by introducing
- * reserve_and_commit FRM method. */
-
- /* Reserve contract */
- ret = fres_resource_manager_reserve_contracts(resource->mng, contracts, ev);
- if (forb_exception_occurred(ev) || ret < 0) {
+ ret = reject_transactions(fcb_contracts, num);
+ if (ret)
goto err_free_fcb_contracts;
- }
- if (ret == 1) {
- ul_logmsg("Contract was not accepted\n");
+ ret = check_and_setup_resource(fcb, fcb_contracts, &resource,
+ &app, num);
+ if (ret)
goto err_free_fcb_contracts;
- }
- /* Commit contract */
- fres_resource_manager_commit_contracts(resource->mng, ids,
- &schedulable_contracts, ev);
- if (forb_exception_occurred(ev)) {
- ret = FRES_ERR_FORB_EXCEPTION;
- goto err_cancel_reservation;
- }
+ prepare_reservation_list(resource, fcb_contracts, num);
- /* Create VRes */
- ret = fres_resource_allocator_change_vreses(ra->ra, schedulable_contracts, ev);
- if (CORBA_sequence_get_release(schedulable_contracts)) {
- int i;
- for (i=0; i<schedulable_contracts->_length; i++) {
- fres_contract_destroy(schedulable_contracts->_buffer[i]);
+ /* Reserve contracts */
+ ret = rebalance_spare_capacity_and_reserve(resource);
+ if (ret) {
+ if (ret == FRSH_ERR_CONTRACT_REJECTED) {
+ ul_logmsg("Contract(s) was/were rejected\n");
+ } else {
+ char msg[100];
+ fres_strerror(ret, msg, sizeof(msg));
+ ul_logerr("Reservation error: %s\n", msg);
}
- forb_free(schedulable_contracts->_buffer);
+ goto err_free_fcb_contracts;
}
- forb_free(schedulable_contracts);
- if (forb_exception_occurred(ev)) {
- ret = FRES_ERR_FORB_EXCEPTION;
+
+ /* Commit contracts */
+ ret = commit_resource(resource, &schedulable_contracts);
+ if (ret)
goto err_cancel_reservation;
- }
- if (ret != 0) {
- ul_logmsg("VRes was not created\n");
+
+ fcb_remember_contracts(fcb, fcb_contracts, num);
+ fcb_remember_schedulable_contracts(fcb, schedulable_contracts);
+
+ ret = change_vreses(fcb, schedulable_contracts);
+ if (ret)
goto err_cancel_reservation;
+
+ forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
+ if (ret != 0) {
+ ul_logerr("VRes was not created\n");
+ goto err_cancel_contracts;
}
- /* Update database of negotiated contracts stored for later reference */
- for (i=0; i<contracts->_length; i++) {
- struct fcb_contract *fcb_contract;
- fcb_contract = fcb_contract_find(fcb, &contracts->_buffer[i]->id);
- /* Delete canceled or renegotiated user contract */
- if (fcb_contract) {
- fcb_contract_delete(fcb, fcb_contract);
- fcb_contract_destroy(fcb_contract);
- }
- if (fcb_contracts[i]) {
- /* Insert new contracts */
- fcb_contract_insert(fcb, fcb_contracts[i]);
+
+ /* Return IDs and delete canceled contracts from out database */
+ if (!forb_sequence_alloc_buf(*ids_out, num)) {
+ ev->major = FORB_EX_NO_MEMORY;
+ goto err_cancel_contracts;
+ }
+ (*ids_out)->_length = num;
+ for (i=0; i<num; i++) {
+ struct fcb_contract *fc = fcb_contracts[i];
+ forb_sequence_elem(*ids_out, i) = fc->id;
+
+ if (fc->requested_contract &&
+ fres_contract_get_num_blocks(fc->requested_contract) == 0) {
+ fcb_contract_delete(fcb, fc);
+ /* Note: requested_contract is also pointed by
+ * contracts parameter and will be freed by FORB. */
+ fc->requested_contract = NULL;
+ fcb_contract_destroy(fc);
}
}
-
return 0;
+err_cancel_contracts:
+ /* TODO */
+ goto err_free_fcb_contracts;
err_cancel_reservation:
- fres_resource_manager_cancel_reservations(resource->mng, ids, ev);
+ cancel_reservations(resource);
err_free_fcb_contracts:
- for (i=0; i<contracts->_length; i++)
- fcb_contract_destroy(fcb_contracts[i]);
- free(fcb_contracts);
+ free_fcb_contracts(fcb_contracts, num);
err:
return ret;
}
+void redistribute_spare_capacity(fres_contract_broker obj,
+ const frsh_resource_type_t restype,
+ const frsh_resource_id_t resid,
+ CORBA_Environment *ev)
+{
+ struct fcb *fcb = o2fcb(obj);
+ struct res_key key = {restype, resid };
+ struct resource *resource;
+
+ resource = fcb_resource_find(fcb, &key);
+
+ prepare_reservation_list(resource, NULL, 0);
+
+/* forb_sequence_alloc(ids, rl.length); */
+/* if (!ids || !ids->_buffer) { */
+/* ev->major = FORB_EX_NO_MEMORY; */
+/* goto err_free_fcb_contracts; */
+/* } */
+/* CORBA_sequence_set_release(ids, CORBA_TRUE); */
+/* *ids_out = ids; /\* ids is freed by FORB *\/ */
+
+
+ rebalance_spare_capacity_and_reserve(resource);
+ /* Commit */
+}
+
CORBA_long register_resource(fres_contract_broker obj,
const frsh_resource_type_t restype,
const frsh_resource_id_t resid,
}
}
res->mng = forb_object_duplicate(desc->manager);
+ res->name = desc->name;
fcb_alloc_init_root_field(res);
- ul_logmsg("Registering manager for resource %d.%d\n",
- restype, resid);
+ sc_contracts_init_head(res);
+ ul_logmsg("Registering manager for resource \"%s\" (%d.%d)\n",
+ res->name, restype, resid);
fcb_resource_insert(fcb, res);
return 0;
free_err:
struct res_key resource;
forb_server_id server_id;
char server_id_str[40];
+ int ret;
forb_get_server_id(ra_obj, &server_id);
forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
if (!res) {
ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
restype, resid);
+ ret = FRES_ERR_NO_RESOURCE_MANAGER;
goto err;
}
ra = fcb_alloc_find(res, &server_id);
ul_logerr("Allocator from already registered (%s)\n",
str);
forb_free(str);
+ ret = FRES_ERR_ALLOCATOR_ALREADY_REGISTERED;
goto err;
}
ra = malloc(sizeof(*ra));
if (!ra) {
+ ret = ENOMEM;
goto err;
}
ra->app = server_id;
fcb_alloc_insert(res, ra);
return 0;
err:
- return -1;
+ return ret;
}
void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
seq->_buffer[n].restype = res->key.type;
seq->_buffer[n].resid = res->key.id;
seq->_buffer[n].desc.manager = res->mng;
+ seq->_buffer[n].desc.name = res->name;
n++;
}
*resources = seq;
}
-#ifdef CONFIG_FCB_INET
+static bool
+transaction_has_spare_capacity(const fres_transaction_t* transaction)
+{
+ struct fres_contract **c;
+ forb_sequence_foreach(&transaction->contracts, c)
+ if (fres_contract_get_spare_capacity(*c))
+ return true;
+ return false;
+}
+
+static int
+transaction_get_resources(struct fcb *fcb, struct fcb_contract *fc[],
+ int num, struct res_array *res_array)
+{
+ int i;
+ struct resource *resource;
+ struct res_key key;
+ int ret;
+
+ for (i = 0; i < num; i++) {
+ get_fc_res_key(fc[i], &key);
+ resource = fcb_resource_find(fcb, &key);
+ ret = res_array_insert(res_array, resource);
+ if (ret == -1) {
+ ret = FRSH_ERR_INTERNAL_ERROR;
+ goto err;
+ }
+ }
+ ret = 0;
+err:
+ return ret;
+}
+
+CORBA_long
+negotiate_transaction(fres_contract_broker _obj,
+ const fres_transaction_t* transaction,
+ CORBA_Environment *ev)
+{
+ struct fcb *fcb = o2fcb(_obj);
+ struct fcb_contract **fcb_contracts, *fc;
+ const fres_contract_ptr_seq* user_contracts = &transaction->contracts;
+ int i, ret, num = user_contracts->_length;
+ struct res_array res_array;
+ struct res_key key;
+ struct resource *resource;
+ fres_contract_ptr_seq *schedulable_contracts;
+ struct fcb_transaction *ft;
+
+ ul_logmsg("Negotiating transaction '%s' with %d contracts\n", transaction->name, num);
+ if (transaction_has_spare_capacity(transaction)) {
+ ret = FRES_ERR_SPARE_CAPACITY_NOT_SUPPORTED;
+ goto err;
+ }
+
+ fcb_contracts = malloc(sizeof(*fcb_contracts)*num);
+ if (!fcb_contracts) {
+ ret = errno;
+ goto err;
+ }
+ memset(fcb_contracts, 0, sizeof(*fcb_contracts)*num);
+
+ ret = prepare_fcb_contracts(fcb, fcb_contracts,
+ user_contracts->_buffer, num);
+ if (ret)
+ goto err_free_fcb_contracts;
+
+ res_array_init_array_field(&res_array);
+ ret = transaction_get_resources(fcb, fcb_contracts, num, &res_array);
+
+ gsa_cust_for_each(res_array, &res_array, resource) {
+ reservation_list_init_head(&resource->rl);
+ resource->rl.length = 0;
+ for (i = 0; i < num; i++) {
+ fc = fcb_contracts[i];
+ get_fc_res_key(fc, &key);
+ if (res_key_cmp(&key, &resource->key) == 0) {
+ reservation_list_insert(&resource->rl, fc);
+ resource->rl.length++;
+ }
+ }
+ }
+ gsa_cust_for_each(res_array, &res_array, resource) {
+ ret = reserve_resource(resource);
+ if (ret) {
+ ul_logerr("Reservation failed\n");
+ goto err_cancel_reservation;
+ }
+ }
+ fcb_remember_contracts(fcb, fcb_contracts, num);
+ gsa_cust_for_each(res_array, &res_array, resource) {
+ ret = commit_resource(resource, &schedulable_contracts);
+ if (ret) {
+ ul_logerr("Commit failed\n");
+ goto err_cancel_reservation;
+ }
+ fcb_remember_schedulable_contracts(fcb, schedulable_contracts);
+ forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
+ }
+ /* TODO: After we distinguish between APP and a real
+ * allocator, allocate VRESes (and perform mode change) here.
+ * Only if the resource allocator is not known at this point,
+ * wait with allocation the same way as it is done now. For
+ * this we will need more information about allocators i.e.
+ * its type (centralized, distributed, in app, ...). Too
+ * compilcated ;-( */
+
+ ft = fcb_transaction_new(transaction->name);
+ if (!ft) {
+ ret = errno;
+ goto err_cancel_reservation;
+ }
+ enum { NONE, CANCELATION, NEGOTIATION } op = NONE;
+ for (i = 0; i < num; i++) {
+ fc = fcb_contracts[i];
+ if (fc->requested_contract &&
+ fres_contract_get_num_blocks(fc->requested_contract) == 0) {
+ assert(i == 0 || op == CANCELATION);
+ op = CANCELATION;
+ /* TODO: Review and test transaction cancelation!!! */
+ fcb_contract_delete(fcb, fc);
+ /* Note: requested_contract is also pointed by
+ * contracts parameter and will be freed by FORB. */
+ fc->requested_contract = NULL;
+ fcb_contract_destroy(fc);
+ } else {
+ assert(i == 0 || op == NEGOTIATION);
+ op = NEGOTIATION;
+ tran_contract_insert_at(ft, fc, i);
+ fc->transaction = ft;
+ }
+ }
+ switch (op) {
+ case NEGOTIATION:
+ fcb_transaction_insert(fcb, ft);
+ break;
+ case CANCELATION:
+ fcb_transaction_delete(fcb, ft);
+ fcb_transaction_destroy(ft);
+ break;
+ default:
+ assert(false);
+ }
+ res_array_delete_all(&res_array);
+ return 0;
+err_cancel_reservation:
+ gsa_cust_for_each(res_array, &res_array, resource) {
+ cancel_reservations(resource);
+ }
+ res_array_delete_all(&res_array);
+err_free_fcb_contracts:
+ free_fcb_contracts(fcb_contracts, num);
+err:
+ return ret;
+}
+
+CORBA_long
+wait_transaction(fres_contract_broker _obj,
+ const CORBA_char * name,
+ fres_transaction_t** transaction,
+ CORBA_Environment *ev)
+{
+ return FRSH_ERR_NOT_IMPLEMENTED;
+}
+
+CORBA_long
+allocate_transaction_vres(fres_contract_broker _obj,
+ const CORBA_char * name,
+ const CORBA_long index,
+ fres_contract_id_t *id,
+ CORBA_Environment *ev)
+{
+ struct fcb *fcb = o2fcb(_obj);
+ struct fcb_transaction *ft;
+ struct fcb_contract *fc;
+ int ret;
+ struct res_alloc *ra;
+ char * const *n = (char * const *)&name;
+ struct res_key key;
+ struct resource *resource;
+ fres_contract_ptr_seq vreses;
+ CORBA_Environment ev2;
+ forb_server_id app;
+#define GOTO(label, error) do { ret = (error); goto label; } while(0)
+
+ ul_logmsg("Allocate transaction VRES '%s'[%d]\n", name, index);
+
+ ft = fcb_transaction_find(fcb, n);
+ if (!ft) GOTO(err, FRES_ERR_TRANSACTION_NOT_FOUND);
+
+ fc = tran_contract_indx2item(ft, index);
+ if (!fc) GOTO(err, FRSH_ERR_TOO_LARGE);
+
+ /* TODO: If we allow migration of task between resources (due
+ * to spare capacity reallocation), the following check will
+ * need reworking/enhancing. */
+ if (fc->ra) GOTO(err, FRES_ERR_VRES_ALREADY_ALLOCATED);
+
+ if (!get_fc_res_key(fc, &key))
+ GOTO(err, FRSH_ERR_RESOURCE_ID_INVALID);
+ resource = fcb_resource_find(fcb, &key);
+ if (!resource)
+ GOTO(err, FRES_ERR_NO_RESOURCE_MANAGER);
+
+ forb_get_req_source(_obj, &app);
+ /* Find allocator for the requested VRES */
+ ra = fcb_alloc_find(resource, &app);
+ if (!ra) {
+ char str[60];
+ forb_server_id_to_string(str, &app, sizeof(str));
+ ul_logerr("No resource allocator found for %d.%d and %s\n",
+ resource->key.type, resource->key.id, str);
+ GOTO(err, FRES_ERR_NO_RESOURCE_ALLOCATOR);
+ }
+ fc->ra = ra;
+
+ if (!forb_sequence_alloc_buf(&vreses, 1))
+ GOTO(err, errno);
+
+ assert(fc->schedulable_contract);
+ forb_sequence_elem(&vreses, 0) = fc->schedulable_contract;
+ forb_sequence_length(&vreses) = 1;
+ ret = fres_resource_allocator_change_vreses(ra->ra, &vreses, &ev2);
+ ul_logerr("Err = %d\n", ret);
+ if (forb_exception_occurred(&ev2))
+ GOTO(err_free, fres_forbex2err(&ev2));
+ if (ret)
+ goto err_free;
+ *id = fc->id;
+
+ ret = 0;
+err_free:
+ forb_sequence_free_buf(&vreses, forb_no_destructor);
+err:
+ return ret;
+}
+
+
+#if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
static int register_inet_port(forb_orb orb)
{
forb_port_t *port = forb_malloc(sizeof(*port));
return -1;
memset(port, 0, sizeof(*port));
listen_on.s_addr = INADDR_ANY;
- ret = forb_inet_port_init(&port->desc, listen_on);
+ ret = forb_inet_port_init(&port->desc, listen_on, 0);
if (ret)
return ret;
ret = forb_register_port(orb, port);
.negotiate_contracts = negotiate_contracts,
.register_resource = register_resource,
.register_allocator = register_allocator,
+ .redistribute_spare_capacity = redistribute_spare_capacity,
.get_resources = get_resources,
+ .negotiate_transaction = negotiate_transaction,
+ .wait_transaction = wait_transaction,
+ .allocate_transaction_vres = allocate_transaction_vres,
};
void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
#if 0
ul_logmsg("peer discovered: %s (orb_id '%s')\n", server_id_str, orb_id);
#endif
+
+ if (orb_id == NULL)
+ return;
+
if (strcmp(orb_id, "org.frescor.fcb") == 0) {
fosa_abs_time_t now;
fosa_rel_time_t delay;
{
}
+static struct option long_opts[] = {
+ { "daemon", optional_argument, NULL, 'd' },
+ { "loglevel", required_argument, NULL, 'l' },
+ { 0, 0, 0, 0}
+};
+
+static void
+usage(void)
+{
+ printf("usage: fcb [ options ]\n");
+ printf(" -d, --daemon [pid-file] go to background after FORB initialization\n");
+ printf(" -l, --loglevel <number>|<domain>=<number>,...\n");
+}
+
+int print_log_domain(ul_log_domain_t *domain, void *context)
+{
+ printf("%s = %d\n", domain->name, domain->level);
+ return 0;
+}
+
int main(int argc, char *argv[])
{
forb_orb orb;
.orb_id = "org.frescor.fcb",
.peer_discovery_callback = peer_discovery_callback,
.peer_dead_callback = peer_dead_callback,
+ .fixed_tcp_port = FCB_TCP_PORT,
+#ifdef CONFIG_FORB_PROTO_INET_DEFAULT
+ .fixed_server_id = FCB_SERVER_ID,
+ .redistribute_hellos = true,
+#endif
};
+ int opt;
+
+ while ((opt = getopt_long(argc, argv, "d::hl:", &long_opts[0], NULL)) != EOF) {
+ switch (opt) {
+ case 'l':
+ if (*optarg == '?') {
+ ul_logreg_for_each_domain(print_log_domain, NULL);
+ exit(0);
+ }
+ {
+ int ret;
+ ret = ul_log_domain_arg2levels(optarg);
+ if (ret)
+ error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
+ }
+ break;
+ case 'd':
+ opt_daemon = true;
+ opt_pidfile = optarg;
+ break;
+ case 'h':
+ /*default:*/
+ usage();
+ exit(opt == 'h' ? 0 : 1);
+ }
+ }
-
+ memset(&fcb_data, 0, sizeof(fcb_data));
fosa_clock_get_time(CLOCK_REALTIME, &start_time);
+ if (opt_daemon)
+ forb_daemon_prepare(opt_pidfile);
+
orb = forb_init(&argc, &argv, &attr);
if (!orb) error(1, errno, "FORB initialization failed");
-#ifdef CONFIG_FCB_INET
+#if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
ret = register_inet_port(orb);
if (ret) error(0, errno, "INET port registration failed");
#endif
fcb_resource_init_root_field(&fcb_data);
fcb_contract_init_root_field(&fcb_data);
+ fcb_transaction_init_root_field(&fcb_data);
fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
if (!fcb) error(1, errno, "forb_fres_contract_broker_new failed");
if (ret) error(1, errno, "forb_register_reference() failed");
ul_logmsg("Waiting for requests\n");
+ if (opt_daemon)
+ forb_daemon_ready();
+
ret = forb_executor_run(&executor);
if (ret) error(1, errno, "forb_executor_run failed");