aboutsummaryrefslogtreecommitdiffstats
path: root/ctl-lib
diff options
context:
space:
mode:
Diffstat (limited to 'ctl-lib')
-rw-r--r--ctl-lib/ctl-config.c37
-rw-r--r--ctl-lib/ctl-config.h1
2 files changed, 28 insertions, 10 deletions
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);