]> rtime.felk.cvut.cz Git - frescor/frsh.git/blobdiff - resources/dummy/frm_dummy.c
forb: executable resources and fcb changed into dynamic libs
[frescor/frsh.git] / resources / dummy / frm_dummy.c
index 6db333444bb0e1c5e7ce76602bc3938727f40db8..1314d03ff5c46701b27b3cd0344a072c0993a2ea 100644 (file)
 #include <forb.h>
 #include <error.h>
 #include <errno.h>
+#include <getopt.h>
 #include <fres_sa_scenario.h>
 #include <stdbool.h>
 #include <ul_log.h>
+#include <ul_logreg.h>
 #include "res_dummy.h"
 #include <stdio.h>
 #include "dummy_config.h"
 
+bool opt_accept_all = false;
+bool opt_daemon = false;
+char *opt_pidfile = NULL;
+int opt_res_id = DUMMY_RESOURCE_ID;
+
 struct dummy_data {
        int some_data;
 };
@@ -80,6 +87,12 @@ int admission_test(struct fres_sa_scenario *scenario, void *priv, bool *schedula
 #ifdef CONFIG_RESOURCE_DUMMY_VERBOSE   
        printf("Admission test:\n");
 #endif
+
+       if (opt_accept_all) {
+               *schedulable = true;
+               return 0;
+       }
+
        data->some_data++;
 
        fres_sa_scenario_for_each_no_cancel_contract(scenario, c) {
@@ -125,8 +138,10 @@ int admission_test(struct fres_sa_scenario *scenario, void *priv, bool *schedula
                        1000*fosa_rel_time_to_msec(basic->budget) /
                        fosa_rel_time_to_msec(basic->period);
        }
-       scenario->utilization = utilization/10;
        *schedulable = utilization < 1000;
+       if (*schedulable) {
+               scenario->utilization = utilization/10;
+       }
        
 #ifdef CONFIG_RESOURCE_DUMMY_VERBOSE   
        printf("utilization=%ld.%03ld => %s\n", utilization/1000, utilization%1000,
@@ -138,21 +153,62 @@ int admission_test(struct fres_sa_scenario *scenario, void *priv, bool *schedula
 
 struct dummy_data dummy_data;
 
-static const struct fres_res_manager frm = {
+static struct fres_res_manager frm = {
        .res_type = DUMMY_RESOURCE_TYPE,
        .res_id = DUMMY_RESOURCE_ID,
        .admission_test = admission_test,
-       .priv = &dummy_data
+       .priv = &dummy_data,
+       .name = "Dummy resource",
+};
+
+static struct option long_opts[] = {
+    { "loglevel", 1, 0, 'l' },
+//     { "id",           required_argument, 0, 'i' },
+    { "accept-all", 0, 0, 'a' },
+    { "help", 0, 0, 'h' },
+    { 0, 0, 0, 0}
 };
 
+static void
+usage(void)
+{
+       printf("usage: frm_dummy [ options ]\n");
+       printf("  -a, --accept-all   Accepts all contracts\n");
+       printf("  -d, --daemon [pid-file]   go to background after FORB initialization\n");
+/*     printf("  -i, --id <resource id>    Use a different id that the default (%d)\n", DUMMY_RESOURCE_ID);*/
+       printf("  -h, --help         Display this help\n");
+       printf("  -l, --loglevel <number>|<domain>=<number>,...\n");
+}
 
-int main(int argc, char *argv[])
+int forb_main(forb_orb orb, int argc, char *argv[])
 {
-       forb_orb orb;
        int ret;
        forb_init_attr_t attr = { .orb_id = "org.frescor.frm.dummy" };
-       orb = forb_init(&argc, &argv, &attr);
-       if (!orb) error(1, errno, "forb_init");
+       int  opt;
+
+       while ((opt = getopt_long(argc, argv, "ai:l:h", &long_opts[0], NULL)) != EOF) {
+               switch (opt) {
+                       case 'a':
+                               opt_accept_all = true;
+                       case 'd':
+                               opt_daemon = true;
+                               opt_pidfile = optarg;
+                               break;
+//                     case 'i':
+//                             opt_res_id = atoi(optarg);
+//                             break;
+                       case 'l':
+                               ul_log_domain_arg2levels(optarg);
+                               break;
+                       case 'h':
+                       /*default:*/
+                               usage();
+                               exit(opt == 'h' ? 0 : 1);
+               }
+       }
+
+       if (opt_daemon)
+               forb_daemon_prepare(opt_pidfile);
 
        /* Register fres_block_dummy_sched to contract handling
         * functions */
@@ -162,6 +218,7 @@ int main(int argc, char *argv[])
        ulogd_frm_generic.level = UL_LOGL_ERR;
 #endif
 
+       frm.res_id = opt_res_id;
        ret = frm_register_and_run(orb, &frm);
 
        if (ret != 0) {