]> rtime.felk.cvut.cz Git - frescor/frsh.git/blobdiff - fres/cbroker/fcb.c
Unify parameters of forb_sequence_*()
[frescor/frsh.git] / fres / cbroker / fcb.c
index 3912904ff1d2100f331f19eced1e12c86c6e9da7..f34d47f003ad5348d4667957410bfd65c26622ef 100644 (file)
@@ -53,6 +53,7 @@
  * 
  * 
  */
+#include <semaphore.h>
 #include <getopt.h>
 #include <forb.h>
 #include <forb/config.h>
 #endif
 
 UL_LOG_CUST(ulogd_fcb);
-ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "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;
 
 
@@ -309,8 +312,6 @@ prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
        for (i=0; i<num; i++) {
                struct fres_contract *c = contracts[i];
 
-               log_contract("Received contract", i, c);
-               
                if (fres_contract_id_is_empty(&c->id)) {
                        /* Normal negotiation request */
                        forb_uuid_generate((forb_uuid_t *)&c->id);
@@ -321,6 +322,7 @@ prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
                        if (!get_res_key(c, &key)) {
                                return FRSH_ERR_RESOURCE_ID_INVALID;
                        }
+                       log_contract("Negotiation request", i, c);
                } else {
                        fc = fcb_contract_find(fcb, &c->id);
                        if (!fc) {
@@ -333,11 +335,13 @@ prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
                                if (fres_contract_get_num_blocks(c) == 0) {
                                        /* Cancelation */
                                        get_res_key(fc->user_contract, &key);
+                                       log_contract("Cancelation request", i, fc->user_contract);
                                } else {
                                        /* Renegotiation */
                                        if (!get_res_key(c, &key)) {
                                                return FRSH_ERR_RESOURCE_ID_INVALID;
                                        }
+                                       log_contract("Renegotiation request", i, fc->user_contract);
                                }
                        }
                }
@@ -424,7 +428,7 @@ rebalance_spare_capacity_and_reserve(struct resource *resource,
        fres_block_spare_capacity *s;
 
        fres_contract_ptr_seq contracts;
-       if (!forb_sequence_alloc_buf(contracts, rl->length)) {
+       if (!forb_sequence_alloc_buf(&contracts, rl->length)) {
                return errno;
        }
        contracts._length = rl->length;
@@ -437,7 +441,7 @@ rebalance_spare_capacity_and_reserve(struct resource *resource,
                        NULL;
                assert(fc->to_be_reserved_contract != NULL);
 
-               forb_sequence_elem(contracts, i) = fc->to_be_reserved_contract;
+               forb_sequence_elem(&contracts, i) = fc->to_be_reserved_contract;
                i++;
                
                s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
@@ -462,7 +466,7 @@ rebalance_spare_capacity_and_reserve(struct resource *resource,
                        /* 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));
+                                                   forb_sequence_elem(&s->variants, fc->sc_variant.try));
                                criterion += fc->sc_variant.try;
 
                                if (fcb_changed == NULL) {
@@ -520,7 +524,7 @@ err:
                fres_contract_destroy(fc->to_be_reserved_contract);
                fc->to_be_reserved_contract = NULL;
        }
-       forb_sequence_free_buf(contracts, forb_no_destructor);
+       forb_sequence_free_buf(&contracts, forb_no_destructor);
        return ret;
 }
 
@@ -538,10 +542,10 @@ 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;
+       int i, ret = 0;
        CORBA_Environment ev;
        
-       if (!forb_sequence_alloc_buf(vreses, schedulable_contracts->_length)) {
+       if (!forb_sequence_alloc_buf(&vreses, schedulable_contracts->_length)) {
                return errno;
        }
        vreses._length = 0;
@@ -555,8 +559,7 @@ change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
                fc = fcb_contract_find(fcb, &sc->id);
                assert(fc != NULL);
 
-               if (true /* TODO: if the schedulable contract is changed */ &&
-                   fres_contract_get_type(&sc) != FRSH_CT_DUMMY) {
+               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,
@@ -577,12 +580,14 @@ change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
                
                }
        }
-       ret = fres_resource_allocator_change_vreses(last_ra->ra,
-                                                   &vreses, &ev);
-       if (forb_exception_occurred(&ev))
-               ret = fres_forbex2err(&ev);
+       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);
+       forb_sequence_free_buf(&vreses, forb_no_destructor);
        return ret;
 }
 
@@ -632,7 +637,7 @@ negotiate_contracts(fres_contract_broker obj,
        /* Allocate all the needed memory before doing reservation. If
         * there is no enough memory, it has no sense to call resource
         * manager. */
-       if (!forb_sequence_alloc_buf(commit_ids, rl.length)) {
+       if (!forb_sequence_alloc_buf(&commit_ids, rl.length)) {
                ret = errno;
                goto err_free_fcb_contracts;
        }
@@ -654,7 +659,7 @@ negotiate_contracts(fres_contract_broker obj,
        commit_ids._length = rl.length;
        i=0;
        ul_list_for_each(reservation_list, &rl, fc) {
-               forb_sequence_elem(commit_ids, i) = fc->id;
+               forb_sequence_elem(&commit_ids, i) = fc->id;
                i++;
        }
        
@@ -701,14 +706,14 @@ negotiate_contracts(fres_contract_broker obj,
 
 
        /* Return IDs and delete canceled contracts from out database */
-       if (!forb_sequence_alloc_buf(**ids_out, contracts->_length)) {
+       if (!forb_sequence_alloc_buf(*ids_out, contracts->_length)) {
                ev->major = FORB_EX_NO_MEMORY;
                goto err_cancel_contracts;
        }
        (*ids_out)->_length = contracts->_length;
        for (i=0; i<contracts->_length; i++) {
                struct fcb_contract *fc =  fcb_contracts[i];
-               forb_sequence_elem(**ids_out, i) = fc->id;
+               forb_sequence_elem(*ids_out, i) = fc->id;
 
                if (fc->requested_contract &&
                    fres_contract_get_num_blocks(fc->requested_contract) == 0) {
@@ -954,7 +959,8 @@ void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
 }
 
 static struct option long_opts[] = {
-    { "loglevel", 1, 0, 'l' },
+    { "daemon",   optional_argument, NULL, 'd' },
+    { "loglevel", required_argument, NULL, 'l' },
     { 0, 0, 0, 0}
 };
 
@@ -962,9 +968,16 @@ 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;
@@ -984,10 +997,23 @@ int main(int argc, char *argv[])
        };
        int  opt;
 
-       while ((opt = getopt_long(argc, argv, "l:", &long_opts[0], NULL)) != EOF) {
+       while ((opt = getopt_long(argc, argv, "d::l:", &long_opts[0], NULL)) != EOF) {
                switch (opt) {
                        case 'l':
-                               ul_log_domain_arg2levels(optarg);
+                               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:*/
@@ -998,6 +1024,9 @@ int main(int argc, char *argv[])
 
        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");
 
@@ -1024,6 +1053,9 @@ int main(int argc, char *argv[])
        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");