| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- /**
- * @file ondemand.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * @section DESCRIPTION
- *
- * On-demand process manager.
- *
- * Synopsis:
- * ondemand(string template_name, list args)
- *
- * Description:
- * Manages an on-demand template process using a process template named
- * template_name.
- * On deinitialization, if the process is running, reqests its termination
- * and waits for it to terminate.
- *
- * Synopsis:
- * ondemand::demand()
- *
- * Description:
- * Demands the availability of an on-demand template process.
- * This statement is in UP state if and only if the template process of the
- * corresponding ondemand object is completely up.
- *
- * Variables:
- * Exposes variables and objects from the template process corresponding to
- * the ondemand object.
- */
- #include <stdlib.h>
- #include <string.h>
- #include <misc/offset.h>
- #include <misc/debug.h>
- #include <structure/LinkedList1.h>
- #include <ncd/NCDModule.h>
- #include <generated/blog_channel_ncd_ondemand.h>
- #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
- struct ondemand {
- NCDModuleInst *i;
- const char *template_name;
- NCDValRef args;
- LinkedList1 demands_list;
- int dying;
- int have_process;
- NCDModuleProcess process;
- int process_terminating;
- int process_up;
- };
- struct demand {
- NCDModuleInst *i;
- struct ondemand *od;
- LinkedList1Node demands_list_node;
- };
- static int ondemand_start_process (struct ondemand *o);
- static void ondemand_terminate_process (struct ondemand *o);
- static void ondemand_process_handler (struct ondemand *o, int event);
- static void ondemand_free (struct ondemand *o);
- static void demand_free (struct demand *o);
- static int ondemand_start_process (struct ondemand *o)
- {
- ASSERT(!o->dying)
- ASSERT(!o->have_process)
-
- // start process
- if (!NCDModuleProcess_Init(&o->process, o->i, o->template_name, o->args, o, (NCDModuleProcess_handler_event)ondemand_process_handler)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
- goto fail0;
- }
-
- // set have process
- o->have_process = 1;
-
- // set process not terminating
- o->process_terminating = 0;
-
- // set process not up
- o->process_up = 0;
-
- return 1;
-
- fail0:
- return 0;
- }
- static void ondemand_terminate_process (struct ondemand *o)
- {
- ASSERT(o->have_process)
- ASSERT(!o->process_terminating)
-
- // request termination
- NCDModuleProcess_Terminate(&o->process);
-
- // set process terminating
- o->process_terminating = 1;
-
- if (o->process_up) {
- // set process down
- o->process_up = 0;
-
- // signal demands down
- for (LinkedList1Node *n = LinkedList1_GetFirst(&o->demands_list); n; n = LinkedList1Node_Next(n)) {
- struct demand *demand = UPPER_OBJECT(n, struct demand, demands_list_node);
- ASSERT(demand->od == o)
- NCDModuleInst_Backend_Down(demand->i);
- }
- }
- }
- static void ondemand_process_handler (struct ondemand *o, int event)
- {
- ASSERT(o->have_process)
-
- switch (event) {
- case NCDMODULEPROCESS_EVENT_UP: {
- ASSERT(!o->process_terminating)
- ASSERT(!o->process_up)
-
- // set process up
- o->process_up = 1;
-
- // signal demands up
- for (LinkedList1Node *n = LinkedList1_GetFirst(&o->demands_list); n; n = LinkedList1Node_Next(n)) {
- struct demand *demand = UPPER_OBJECT(n, struct demand, demands_list_node);
- ASSERT(demand->od == o)
- NCDModuleInst_Backend_Up(demand->i);
- }
- } break;
-
- case NCDMODULEPROCESS_EVENT_DOWN: {
- ASSERT(!o->process_terminating)
- ASSERT(o->process_up)
-
- // continue process
- NCDModuleProcess_Continue(&o->process);
-
- // set process down
- o->process_up = 0;
-
- // signal demands down
- for (LinkedList1Node *n = LinkedList1_GetFirst(&o->demands_list); n; n = LinkedList1Node_Next(n)) {
- struct demand *demand = UPPER_OBJECT(n, struct demand, demands_list_node);
- ASSERT(demand->od == o)
- NCDModuleInst_Backend_Down(demand->i);
- }
- } break;
-
- case NCDMODULEPROCESS_EVENT_TERMINATED: {
- ASSERT(o->process_terminating)
- ASSERT(!o->process_up)
-
- // free process
- NCDModuleProcess_Free(&o->process);
-
- // set have no process
- o->have_process = 0;
-
- // if dying, die finally
- if (o->dying) {
- ondemand_free(o);
- return;
- }
-
- // if demands arrivied, restart process
- if (!LinkedList1_IsEmpty(&o->demands_list)) {
- if (!ondemand_start_process(o)) {
- // error demands
- while (!LinkedList1_IsEmpty(&o->demands_list)) {
- struct demand *demand = UPPER_OBJECT(LinkedList1_GetFirst(&o->demands_list), struct demand, demands_list_node);
- ASSERT(demand->od == o)
- NCDModuleInst_Backend_SetError(demand->i);
- demand_free(demand);
- }
- }
- }
- } break;
- }
- }
- static void ondemand_func_new (void *vo, NCDModuleInst *i)
- {
- struct ondemand *o = vo;
- o->i = i;
-
- // read arguments
- NCDValRef arg_template_name;
- NCDValRef arg_args;
- if (!NCDVal_ListRead(i->args, 2, &arg_template_name, &arg_args)) {
- ModuleLog(i, BLOG_ERROR, "wrong arity");
- goto fail0;
- }
- if (!NCDVal_IsStringNoNulls(arg_template_name) || !NCDVal_IsList(arg_args)) {
- ModuleLog(i, BLOG_ERROR, "wrong type");
- goto fail0;
- }
- o->template_name = NCDVal_StringValue(arg_template_name);
- o->args = arg_args;
-
- // init demands list
- LinkedList1_Init(&o->demands_list);
-
- // set not dying
- o->dying = 0;
-
- // set have no process
- o->have_process = 0;
-
- // signal up
- NCDModuleInst_Backend_Up(i);
- return;
-
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void ondemand_free (struct ondemand *o)
- {
- ASSERT(!o->have_process)
-
- // die demands
- while (!LinkedList1_IsEmpty(&o->demands_list)) {
- struct demand *demand = UPPER_OBJECT(LinkedList1_GetFirst(&o->demands_list), struct demand, demands_list_node);
- ASSERT(demand->od == o)
- demand_free(demand);
- }
-
- NCDModuleInst_Backend_Dead(o->i);
- }
- static void ondemand_func_die (void *vo)
- {
- struct ondemand *o = vo;
- ASSERT(!o->dying)
-
- // if not have process, die right away
- if (!o->have_process) {
- ondemand_free(o);
- return;
- }
-
- // set dying
- o->dying = 1;
-
- // request process termination if not already
- if (!o->process_terminating) {
- ondemand_terminate_process(o);
- }
- }
- static void demand_func_new (void *vo, NCDModuleInst *i)
- {
- struct demand *o = vo;
- o->i = i;
-
- // read arguments
- if (!NCDVal_ListRead(i->args, 0)) {
- ModuleLog(i, BLOG_ERROR, "wrong arity");
- goto fail0;
- }
-
- // set ondemand
- o->od = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
-
- // add to ondemand's demands list
- LinkedList1_Append(&o->od->demands_list, &o->demands_list_node);
-
- // start process if needed
- if (!o->od->have_process) {
- ASSERT(!o->od->dying)
-
- if (!ondemand_start_process(o->od)) {
- goto fail1;
- }
- }
-
- // if process is up, signal up
- if (o->od->process_up) {
- NCDModuleInst_Backend_Up(i);
- }
-
- return;
-
- fail1:
- LinkedList1_Remove(&o->od->demands_list, &o->demands_list_node);
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void demand_free (struct demand *o)
- {
- // remove from ondemand's demands list
- LinkedList1_Remove(&o->od->demands_list, &o->demands_list_node);
-
- // request process termination if no longer needed
- if (o->od->have_process && !o->od->process_terminating && LinkedList1_IsEmpty(&o->od->demands_list)) {
- ondemand_terminate_process(o->od);
- }
-
- NCDModuleInst_Backend_Dead(o->i);
- }
- static void demand_func_die (void *vo)
- {
- struct demand *o = vo;
-
- demand_free(o);
- }
- static int demand_func_getobj (void *vo, const char *objname, NCDObject *out_object)
- {
- struct demand *o = vo;
- ASSERT(o->od->have_process)
- ASSERT(o->od->process_up)
-
- return NCDModuleProcess_GetObj(&o->od->process, objname, out_object);
- }
- static const struct NCDModule modules[] = {
- {
- .type = "ondemand",
- .func_new2 = ondemand_func_new,
- .func_die = ondemand_func_die,
- .alloc_size = sizeof(struct ondemand)
- }, {
- .type = "ondemand::demand",
- .func_new2 = demand_func_new,
- .func_die = demand_func_die,
- .func_getobj = demand_func_getobj,
- .alloc_size = sizeof(struct demand)
- }, {
- .type = NULL
- }
- };
- const struct NCDModuleGroup ncdmodule_ondemand = {
- .modules = modules
- };
|