From 0f3063b70fc10311323fec6d4493a5316e51d439 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Tue, 26 Jun 2018 17:53:17 +0200 Subject: Be able to dispatch required api at the wanted time Separate the require api step from ConfigExec and if called from anywhere but CtlConfigExec function, take care that not already initialized or that the previous has failed, so this is a new try. Change-Id: Ic98ef09487f7f58c1b1cb6c417eba261a5a81b13 Signed-off-by: Romain Forlot --- ctl-lib/ctl-config.c | 37 +++++++++++++++++++++++++++---------- ctl-lib/ctl-config.h | 1 + 2 files changed, 28 insertions(+), 10 deletions(-) (limited to 'ctl-lib') diff --git a/ctl-lib/ctl-config.c b/ctl-lib/ctl-config.c index 8ef9a85..fcd0e02 100644 --- a/ctl-lib/ctl-config.c +++ b/ctl-lib/ctl-config.c @@ -104,28 +104,45 @@ char* CtlConfigSearch(AFB_ApiT apiHandle, const char *dirList, const char *prefi return NULL; } -static void DispatchRequireOneApi(AFB_ApiT apiHandle, json_object * bindindJ) { +static int DispatchRequireOneApi(AFB_ApiT apiHandle, json_object * bindindJ) { const char* requireBinding = json_object_get_string(bindindJ); int err = AFB_RequireApi(apiHandle, requireBinding, 1); if (err) { AFB_ApiWarning(apiHandle, "CTL-LOAD-CONFIG:REQUIRE Fail to get=%s", requireBinding); } -} - -int CtlConfigExec(AFB_ApiT apiHandle, CtlConfigT *ctlConfig) { - // best effort to initialise everything before starting - if (ctlConfig->requireJ) { + return err; +} - if (json_object_get_type(ctlConfig->requireJ) == json_type_array) { - for (int idx = 0; idx < json_object_array_length(ctlConfig->requireJ); idx++) { - DispatchRequireOneApi(apiHandle, json_object_array_get_idx(ctlConfig->requireJ, idx)); +/** + * @brief Best effort to initialise everything before starting + * Call afb_require_api at the first call or if there was an error because + * the CtlConfigExec could be called anywhere and not in binding init. + * So you could call this function at init time. + * + * @param apiHandle : a afb_daemon api handle, see AFB_ApiT in afb_definitions.h + * @param requireJ : json_object array of api name required. + */ +void DispatchRequireApi(AFB_ApiT apiHandle, json_object * requireJ) { + static int init = 0, err = 0; + int idx; + + if ( (! init || err) && requireJ) { + if (json_object_get_type(requireJ) == json_type_array) { + for (idx = 0; idx < json_object_array_length(requireJ); idx++) { + err += DispatchRequireOneApi(apiHandle, json_object_array_get_idx(requireJ, idx)); } } else { - DispatchRequireOneApi(apiHandle, ctlConfig->requireJ); + err += DispatchRequireOneApi(apiHandle, requireJ); } } + init = 1; +} + +int CtlConfigExec(AFB_ApiT apiHandle, CtlConfigT *ctlConfig) { + + DispatchRequireApi(apiHandle, ctlConfig->requireJ); #ifdef CONTROL_SUPPORT_LUA // load static LUA utilities LuaConfigExec(apiHandle); diff --git a/ctl-lib/ctl-config.h b/ctl-lib/ctl-config.h index 68d0110..a174b27 100644 --- a/ctl-lib/ctl-config.h +++ b/ctl-lib/ctl-config.h @@ -99,6 +99,7 @@ int CtlConfigMagicNew(); json_object* CtlConfigScan(const char *dirList, const char *prefix) ; char* ConfigSearch(AFB_ApiT apiHandle, json_object *responseJ); char* CtlConfigSearch(AFB_ApiT apiHandle, const char *dirList, const char *prefix) ; +void DispatchRequiredApi(AFB_ApiT apiHandle, json_object * requireJ); int CtlConfigExec(AFB_ApiT apiHandle, CtlConfigT *ctlConfig) ; CtlConfigT *CtlLoadMetaData(AFB_ApiT apiHandle,const char* filepath) ; int CtlLoadSections(AFB_ApiT apiHandle, CtlConfigT *ctlHandle, CtlSectionT *sections); -- cgit 1.2.3-korg