renaming defines
[barrelfish] / usr / kaluga / common.c
1 #include <barrelfish/barrelfish.h>
2 #include <octopus/octopus.h>
3
4 #include "kaluga.h"
5
6 errval_t trigger_existing_and_watch(const char* query,
7         trigger_handler_fn event_handler, void* state,
8         octopus_trigger_id_t* tid)
9 {
10     errval_t error_code;
11     char** names = NULL;
12     char* output = NULL;
13     char* record = NULL; // freed by cpu_change_event
14     size_t len = 0;
15     octopus_trigger_t t = oct_mktrigger(0, octopus_BINDING_EVENT,
16             TRIGGER_ALWAYS, event_handler, state);
17
18     // Get current cores registered in system
19     struct octopus_thc_client_binding_t* rpc = oct_get_thc_client();
20     errval_t err = rpc->call_seq.get_names(rpc, query,
21             t, &output, tid, &error_code);
22     if (err_is_fail(err)) {
23         goto out;
24     }
25     err = error_code;
26
27     switch(err_no(err)) {
28     case SYS_ERR_OK:
29         err = oct_parse_names(output, &names, &len);
30         if (err_is_fail(err)) {
31             goto out;
32         }
33
34         for (size_t i=0; i < len; i++) {
35             KALUGA_DEBUG("get record for name:%s\n", names[i]);
36             err = oct_get(&record, names[i]);
37
38             switch (err_no(err)) {
39             case SYS_ERR_OK:
40                 event_handler(OCT_ON_SET, record, state);
41                 break;
42
43             case OCT_ERR_NO_RECORD:
44                 assert(record == NULL);
45                 break;
46
47             default:
48                 DEBUG_ERR(err, "Unable to retrieve core record for %s", names[i]);
49                 assert(record == NULL);
50                 break;
51             }
52         }
53         break;
54     case OCT_ERR_NO_RECORD:
55         err = SYS_ERR_OK; // Overwrite (trigger is set)
56         break;
57
58     default:
59         // Do nothing (wait for trigger)
60         break;
61     }
62
63 out:
64     oct_free_names(names, len);
65     free(output);
66
67     return err;
68 }