From 6b2b09db8ee3cf88b09b59696d50b4fa2de79a72 Mon Sep 17 00:00:00 2001 From: José Bollo Date: Thu, 11 Apr 2019 13:09:56 +0200 Subject: Reordering of sample and tutorial bindings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The main idea here is to install the sample and tutorial bindings in specific directory. This will at the end help to package parts of the binder. It also includes a simplification of CMakeLists files. Also fix an error in packaging rpm. Bug-AGL: SPEC-2165 Change-Id: I494cc753796848cde849de1c3596893c78fa228f Signed-off-by: José Bollo --- bindings/CMakeLists.txt | 2 +- bindings/intrinsics/CMakeLists.txt | 2 +- bindings/samples/CMakeLists.txt | 88 ++++++-------------------------- bindings/tutorial/CMakeLists.txt | 38 -------------- bindings/tutorial/export.map | 1 - bindings/tutorial/tuto-1.c | 19 ------- bindings/tutorial/tuto-2.c | 100 ------------------------------------- bindings/tutorial/tuto-3.cpp | 95 ----------------------------------- bindings/tutorial/tuto-4.c | 29 ----------- bindings/tutorial/tuto-5.cpp | 93 ---------------------------------- bindings/tutorial/tuto-app1.c | 30 ----------- bindings/tutorials/CMakeLists.txt | 37 ++++++++++++++ bindings/tutorials/export.map | 1 + bindings/tutorials/tuto-1.c | 19 +++++++ bindings/tutorials/tuto-2.c | 100 +++++++++++++++++++++++++++++++++++++ bindings/tutorials/tuto-3.cpp | 95 +++++++++++++++++++++++++++++++++++ bindings/tutorials/tuto-4.c | 29 +++++++++++ bindings/tutorials/tuto-5.cpp | 93 ++++++++++++++++++++++++++++++++++ bindings/tutorials/tuto-app1.c | 30 +++++++++++ 19 files changed, 420 insertions(+), 481 deletions(-) delete mode 100644 bindings/tutorial/CMakeLists.txt delete mode 100644 bindings/tutorial/export.map delete mode 100644 bindings/tutorial/tuto-1.c delete mode 100644 bindings/tutorial/tuto-2.c delete mode 100644 bindings/tutorial/tuto-3.cpp delete mode 100644 bindings/tutorial/tuto-4.c delete mode 100644 bindings/tutorial/tuto-5.cpp delete mode 100644 bindings/tutorial/tuto-app1.c create mode 100644 bindings/tutorials/CMakeLists.txt create mode 100644 bindings/tutorials/export.map create mode 100644 bindings/tutorials/tuto-1.c create mode 100644 bindings/tutorials/tuto-2.c create mode 100644 bindings/tutorials/tuto-3.cpp create mode 100644 bindings/tutorials/tuto-4.c create mode 100644 bindings/tutorials/tuto-5.cpp create mode 100644 bindings/tutorials/tuto-app1.c (limited to 'bindings') diff --git a/bindings/CMakeLists.txt b/bindings/CMakeLists.txt index 35f22897..c4fbdea0 100644 --- a/bindings/CMakeLists.txt +++ b/bindings/CMakeLists.txt @@ -18,5 +18,5 @@ ADD_SUBDIRECTORY(intrinsics) ADD_SUBDIRECTORY(samples) -ADD_SUBDIRECTORY(tutorial) +ADD_SUBDIRECTORY(tutorials) diff --git a/bindings/intrinsics/CMakeLists.txt b/bindings/intrinsics/CMakeLists.txt index 70da19e9..ee981217 100644 --- a/bindings/intrinsics/CMakeLists.txt +++ b/bindings/intrinsics/CMakeLists.txt @@ -27,7 +27,7 @@ SET_TARGET_PROPERTIES(afb-dbus-binding PROPERTIES PREFIX "" LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" ) -TARGET_LINK_LIBRARIES(afb-dbus-binding ${link_libraries}) +TARGET_LINK_LIBRARIES(afb-dbus-binding ${json-c_LDFLAGS} ${libsystemd_LDFLAGS}) INSTALL(TARGETS afb-dbus-binding LIBRARY DESTINATION ${binding_install_dir}) diff --git a/bindings/samples/CMakeLists.txt b/bindings/samples/CMakeLists.txt index 34b364f5..51442684 100644 --- a/bindings/samples/CMakeLists.txt +++ b/bindings/samples/CMakeLists.txt @@ -16,78 +16,18 @@ # limitations under the License. ########################################################################### - -INCLUDE_DIRECTORIES(${include_dirs}) - -################################################## -# AuthLogin -################################################## -ADD_LIBRARY(authLogin MODULE AuthLogin.c) -SET_TARGET_PROPERTIES(authLogin PROPERTIES - PREFIX "" - LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" -) -TARGET_LINK_LIBRARIES(authLogin ${link_libraries}) -INSTALL(TARGETS authLogin - LIBRARY DESTINATION ${binding_install_dir}) - -################################################## -# DemoContext -################################################## -ADD_LIBRARY(demoContext MODULE DemoContext.c) -SET_TARGET_PROPERTIES(demoContext PROPERTIES - PREFIX "" - LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" -) -TARGET_LINK_LIBRARIES(demoContext ${link_libraries}) -INSTALL(TARGETS demoContext - LIBRARY DESTINATION ${binding_install_dir}) - -################################################## -# DemoPost -################################################## -ADD_LIBRARY(demoPost MODULE DemoPost.c) -SET_TARGET_PROPERTIES(demoPost PROPERTIES - PREFIX "" - LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" -) -TARGET_LINK_LIBRARIES(demoPost ${link_libraries}) -INSTALL(TARGETS demoPost - LIBRARY DESTINATION ${binding_install_dir}) - -################################################## -# tic-tac-toe -################################################## -ADD_LIBRARY(tic-tac-toe MODULE tic-tac-toe.c) -SET_TARGET_PROPERTIES(tic-tac-toe PROPERTIES - PREFIX "" - LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" -) -TARGET_LINK_LIBRARIES(tic-tac-toe ${link_libraries}) -INSTALL(TARGETS tic-tac-toe - LIBRARY DESTINATION ${binding_install_dir}) - -################################################## -# hello2 -################################################## -ADD_LIBRARY(hello2 MODULE hello2.c) -SET_TARGET_PROPERTIES(hello2 PROPERTIES - PREFIX "" - LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" -) -TARGET_LINK_LIBRARIES(hello2 ${link_libraries}) -INSTALL(TARGETS hello2 - LIBRARY DESTINATION ${binding_install_dir}) - -################################################## -# hello3 -################################################## -ADD_LIBRARY(hello3 MODULE hello3.c) -SET_TARGET_PROPERTIES(hello3 PROPERTIES - PREFIX "" - LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" -) -TARGET_LINK_LIBRARIES(hello3 ${link_libraries}) -INSTALL(TARGETS hello3 - LIBRARY DESTINATION ${binding_install_dir}) +macro(sample name source) + add_library(${name} MODULE ${source}) + set_target_properties(${name} PROPERTIES PREFIX "" LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map") +# target_link_libraries(${name} ${link_libraries}) + target_link_libraries(${name} ${json-c_LDFLAGS} ${CMAKE_THREAD_LIBS_INIT}) + install(TARGETS ${name} LIBRARY DESTINATION ${install_datadir}/bindings/samples) +endmacro(sample) + +sample(authLogin AuthLogin.c) +sample(demoContext DemoContext.c) +sample(demoPost DemoPost.c) +sample(tic-tac-toe tic-tac-toe.c) +sample(hello2 hello2.c) +sample(hello3 hello3.c) diff --git a/bindings/tutorial/CMakeLists.txt b/bindings/tutorial/CMakeLists.txt deleted file mode 100644 index f05fa541..00000000 --- a/bindings/tutorial/CMakeLists.txt +++ /dev/null @@ -1,38 +0,0 @@ -########################################################################### -# Copyright (C) 2015-2019 "IoT.bzh" -# -# author: José Bollo -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -########################################################################### - - -#INCLUDE_DIRECTORIES(${include_dirs}) - -MACRO(tuto num ext) - ADD_LIBRARY(tuto-${num} MODULE tuto-${num}.${ext}) - SET_TARGET_PROPERTIES(tuto-${num} PROPERTIES - PREFIX "" - LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" - ) - TARGET_LINK_LIBRARIES(tuto-${num} ${link_libraries}) -ENDMACRO(tuto) - - -tuto(1 c) -tuto(2 c) -tuto(3 cpp) -tuto(4 c) -tuto(5 cpp) - -tuto(app1 c) diff --git a/bindings/tutorial/export.map b/bindings/tutorial/export.map deleted file mode 100644 index ee2f4133..00000000 --- a/bindings/tutorial/export.map +++ /dev/null @@ -1 +0,0 @@ -{ global: afbBindingV*; local: *; }; diff --git a/bindings/tutorial/tuto-1.c b/bindings/tutorial/tuto-1.c deleted file mode 100644 index 1f58be6c..00000000 --- a/bindings/tutorial/tuto-1.c +++ /dev/null @@ -1,19 +0,0 @@ -#define AFB_BINDING_VERSION 3 -#include - -void hello(afb_req_t req) -{ - AFB_REQ_DEBUG(req, "hello world"); - afb_req_reply(req, NULL, NULL, "hello world"); -} - -const afb_verb_t verbs[] = { - { .verb="hello", .callback=hello }, - { .verb=NULL } -}; - -const afb_binding_t afbBindingExport = { - .api = "tuto-1", - .verbs = verbs -}; - diff --git a/bindings/tutorial/tuto-2.c b/bindings/tutorial/tuto-2.c deleted file mode 100644 index 10797081..00000000 --- a/bindings/tutorial/tuto-2.c +++ /dev/null @@ -1,100 +0,0 @@ -#include -#include - -#define AFB_BINDING_VERSION 3 -#include - -afb_event_t event_login, event_logout; - -void login(afb_req_t req) -{ - json_object *args, *user, *passwd; - char *usr; - - args = afb_req_json(req); - if (!json_object_object_get_ex(args, "user", &user) - || !json_object_object_get_ex(args, "password", &passwd)) { - AFB_REQ_ERROR(req, "login, bad request: %s", json_object_get_string(args)); - afb_req_reply(req, NULL, "bad-request", NULL); - } else if (afb_req_context_get(req)) { - AFB_REQ_ERROR(req, "login, bad state, logout first"); - afb_req_reply(req, NULL, "bad-state", NULL); - } else if (strcmp(json_object_get_string(passwd), "please")) { - AFB_REQ_ERROR(req, "login, unauthorized: %s", json_object_get_string(args)); - afb_req_reply(req, NULL, "unauthorized", NULL); - } else { - usr = strdup(json_object_get_string(user)); - AFB_REQ_NOTICE(req, "login user: %s", usr); - afb_req_session_set_LOA(req, 1); - afb_req_context_set(req, usr, free); - afb_req_reply(req, NULL, NULL, NULL); - afb_event_push(event_login, json_object_new_string(usr)); - } -} - -void action(afb_req_t req) -{ - json_object *args, *val; - char *usr; - - args = afb_req_json(req); - usr = afb_req_context_get(req); - AFB_REQ_NOTICE(req, "action for user %s: %s", usr, json_object_get_string(args)); - if (json_object_object_get_ex(args, "subscribe", &val)) { - if (json_object_get_boolean(val)) { - AFB_REQ_NOTICE(req, "user %s subscribes to events", usr); - afb_req_subscribe(req, event_login); - afb_req_subscribe(req, event_logout); - } else { - AFB_REQ_NOTICE(req, "user %s unsubscribes to events", usr); - afb_req_unsubscribe(req, event_login); - afb_req_unsubscribe(req, event_logout); - } - } - afb_req_reply(req, json_object_get(args), NULL, NULL); -} - -void logout(afb_req_t req) -{ - char *usr; - - usr = afb_req_context_get(req); - AFB_REQ_NOTICE(req, "login user %s out", usr); - afb_event_push(event_logout, json_object_new_string(usr)); - afb_req_session_set_LOA(req, 0); - afb_req_context_clear(req); - afb_req_reply(req, NULL, NULL, NULL); -} - -int preinit(afb_api_t api) -{ - AFB_API_NOTICE(api, "preinit"); - return 0; -} - -int init(afb_api_t api) -{ - AFB_API_NOTICE(api, "init"); - event_login = afb_api_make_event(api, "login"); - event_logout = afb_api_make_event(api, "logout"); - if (afb_event_is_valid(event_login) && afb_event_is_valid(event_logout)) - return 0; - AFB_API_ERROR(api, "Can't create events"); - return -1; -} - -const afb_verb_t verbs[] = { - { .verb="login", .callback=login }, - { .verb="action", .callback=action, .session=AFB_SESSION_LOA_1 }, - { .verb="logout", .callback=logout, .session=AFB_SESSION_LOA_1 }, - { .verb=NULL } -}; - -const afb_binding_t afbBindingExport = { - .api = "tuto-2", - .specification = NULL, - .verbs = verbs, - .preinit = preinit, - .init = init, - .noconcurrency = 0 -}; \ No newline at end of file diff --git a/bindings/tutorial/tuto-3.cpp b/bindings/tutorial/tuto-3.cpp deleted file mode 100644 index 8ad69ef3..00000000 --- a/bindings/tutorial/tuto-3.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#include -#include - -#define AFB_BINDING_VERSION 3 -#include - -afb::event event_login, event_logout; - -void login(afb::req req) -{ - json_object *args, *user, *passwd; - char *usr; - - args = req.json(); - if (!json_object_object_get_ex(args, "user", &user) - || !json_object_object_get_ex(args, "password", &passwd)) { - AFB_REQ_ERROR(req, "login, bad request: %s", json_object_get_string(args)); - req.fail("bad-request"); - } else if (afb_req_context_get(req)) { - AFB_REQ_ERROR(req, "login, bad state, logout first"); - req.fail("bad-state"); - } else if (strcmp(json_object_get_string(passwd), "please")) { - AFB_REQ_ERROR(req, "login, unauthorized: %s", json_object_get_string(args)); - req.fail("unauthorized"); - } else { - usr = strdup(json_object_get_string(user)); - AFB_REQ_NOTICE(req, "login user: %s", usr); - req.session_set_LOA(1); -// req.context(1, nullptr, free, usr); - req.success(); - event_login.push(json_object_new_string(usr)); - } -} - -void action(afb::req req) -{ - json_object *args, *val; - char *usr; - - args = req.json(); -// usr = (char*)req.context_get(); -usr = nullptr; - AFB_REQ_NOTICE(req, "action for user %s: %s", usr, json_object_get_string(args)); - if (json_object_object_get_ex(args, "subscribe", &val)) { - if (json_object_get_boolean(val)) { - AFB_REQ_NOTICE(req, "user %s subscribes to events", usr); - req.subscribe(event_login); - req.subscribe(event_logout); - } else { - AFB_REQ_NOTICE(req, "user %s unsubscribes to events", usr); - req.unsubscribe(event_login); - req.unsubscribe(event_logout); - } - } - req.success(json_object_get(args)); -} - -void logout(afb::req req) -{ - char *usr; - -// usr = (char*)req.context_get(); -usr = nullptr; - AFB_REQ_NOTICE(req, "login user %s out", usr); - event_logout.push(json_object_new_string(usr)); - req.session_set_LOA(0); -// req.context_clear(); - req.success(); -} - -int init( -#if AFB_BINDING_VERSION >= 3 - afb_api_t api -#endif -) -{ - AFB_NOTICE("init"); - event_login = afb_daemon_make_event("login"); - event_logout = afb_daemon_make_event("logout"); - if (afb_event_is_valid(event_login) && afb_event_is_valid(event_logout)) - return 0; - AFB_ERROR("Can't create events"); - return -1; -} - -const afb_verb_t verbs[] = { - afb::verb("login", login, "log in the system"), - afb::verb("action", action, "perform an action", AFB_SESSION_LOA_1), - afb::verb("logout", logout, "log out the system", AFB_SESSION_LOA_1), - afb::verbend() -}; - -const afb_binding_t afbBindingExport = afb::binding("tuto-3", verbs, "third tutorial: C++", init); - - diff --git a/bindings/tutorial/tuto-4.c b/bindings/tutorial/tuto-4.c deleted file mode 100644 index cb909fd0..00000000 --- a/bindings/tutorial/tuto-4.c +++ /dev/null @@ -1,29 +0,0 @@ -#define AFB_BINDING_VERSION 3 -#include - -void hello(afb_req_t req) -{ - AFB_REQ_DEBUG(req, "hello world"); - afb_req_reply(req, NULL, NULL, "hello world"); -} - -const afb_verb_t verbs[] = { - { .verb="hello", .callback=hello }, - { .verb=NULL } -}; - - -static int init(afb_api_t api) -{ - int rc = afb_api_require_api(api, "hello", 1); - if (!rc) - rc = afb_api_call_sync(api, "hello", "ping", NULL, NULL, NULL, NULL); - return rc; -} - -const afb_binding_t afbBindingExport = { - .api = "tuto-4", - .verbs = verbs, - .init = init -}; - diff --git a/bindings/tutorial/tuto-5.cpp b/bindings/tutorial/tuto-5.cpp deleted file mode 100644 index 458ae7b5..00000000 --- a/bindings/tutorial/tuto-5.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include -#include -#include - -class tuto5 - : public afb::base_api_t -{ -private: - afb::event event_login; - afb::event event_logout; - -public: - void login(afb::req req) - { - json_object *user, *passwd; - - json_object* args = req.json(); - if (!json_object_object_get_ex(args, "user", &user) - || !json_object_object_get_ex(args, "password", &passwd)) { - AFB_REQ_ERROR(req, "login, bad request: %s", json_object_get_string(args)); - req.fail("bad-request"); - } else if (afb_req_context_get(req)) { - AFB_REQ_ERROR(req, "login, bad state, logout first"); - req.fail("bad-state"); - } else if (std::string(json_object_get_string(passwd)) != std::string("please")) { - AFB_REQ_ERROR(req, "login, unauthorized: %s", json_object_get_string(args)); - req.fail("unauthorized"); - } else { - char* username = strdup(json_object_get_string(user)); - AFB_REQ_NOTICE(req, "login user: %s", username); - req.session_set_LOA(1); - afb_req_context_set(req, username, free); - req.success(); - event_login.push(json_object_new_string(username)); - } - } - - void action(afb::req req) const - { - json_object* val; - json_object* args = req.json(); - char* username = reinterpret_cast(afb_req_context_get(req)); - AFB_REQ_NOTICE(req, "action for user %s: %s", username, json_object_get_string(args)); - if (json_object_object_get_ex(args, "subscribe", &val)) { - if (json_object_get_boolean(val)) { - AFB_REQ_NOTICE(req, "user %s subscribes to events", username); - req.subscribe(event_login); - req.subscribe(event_logout); - } else { - AFB_REQ_NOTICE(req, "user %s unsubscribes to events", username); - req.unsubscribe(event_login); - req.unsubscribe(event_logout); - } - } - req.success(json_object_get(args)); - } - - void logout(afb::req req) - { - char* username = reinterpret_cast(afb_req_context_get(req)); - AFB_REQ_NOTICE(req, "login user %s out", username); - event_logout.push(json_object_new_string(username)); - req.session_set_LOA(0); - afb_req_context_clear(req); - req.success(); - } - - int preinit(afb_api_t h) override - { - return !( - (add_verb<&tuto5::login>("login", "log in the system") == 0) && - (add_verb<&tuto5::action>("action", "perform an action", nullptr, nullptr, AFB_SESSION_LOA_1) == 0) && - (add_verb<&tuto5::logout>("logout", "log out the system", nullptr, nullptr, AFB_SESSION_LOA_1) == 0) - ); - } - - int init() override - { - AFB_API_NOTICE(api_, "init"); - event_login = make_event("login"); - event_logout = make_event("logout"); - if (event_login.is_valid() && event_logout.is_valid()) - return 0; - AFB_API_ERROR(api_, "Can't create events"); - return -1; - } -}; - -int afbBindingEntry(afb_api_t h) -{ - afb::new_api(h, "tuto-5", "fifth tutorial: C++"); - return 0; -} diff --git a/bindings/tutorial/tuto-app1.c b/bindings/tutorial/tuto-app1.c deleted file mode 100644 index 93747cdd..00000000 --- a/bindings/tutorial/tuto-app1.c +++ /dev/null @@ -1,30 +0,0 @@ -#include - -#define AFB_BINDING_VERSION 3 -#include - -static int appmain(void *arg) -{ - const char *name = arg; - char buffer[50]; - - AFB_API_NOTICE(afbBindingV3root, "Entering Application main"); - printf("Hello, I'm %s!\n", name); - printf("What's your name? "); - scanf("%s", buffer); - printf("Hi %s! Nice to meet you. OOOOPS I'm late bye bye\n", buffer); - return 0; -} - -static void application(int signum, void *arg) -{ - if (signum) - exit(127); - exit(appmain(arg)); -} - -int afbBindingV3entry(struct afb_api_x3 *rootapi) -{ - return afb_api_queue_job(rootapi, application, "BOB", NULL, 0); -} - diff --git a/bindings/tutorials/CMakeLists.txt b/bindings/tutorials/CMakeLists.txt new file mode 100644 index 00000000..e071d7a9 --- /dev/null +++ b/bindings/tutorials/CMakeLists.txt @@ -0,0 +1,37 @@ +########################################################################### +# Copyright (C) 2015-2019 "IoT.bzh" +# +# author: José Bollo +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +########################################################################### + + +MACRO(tuto num ext) + ADD_LIBRARY(tuto-${num} MODULE tuto-${num}.${ext}) + SET_TARGET_PROPERTIES(tuto-${num} PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" + ) + TARGET_LINK_LIBRARIES(tuto-${num} ${json-c_LDFLAGS}) + install(TARGETS tuto-${num} LIBRARY DESTINATION ${install_datadir}/bindings/tutorials) +ENDMACRO(tuto) + + +tuto(1 c) +tuto(2 c) +tuto(3 cpp) +tuto(4 c) +tuto(5 cpp) + +tuto(app1 c) diff --git a/bindings/tutorials/export.map b/bindings/tutorials/export.map new file mode 100644 index 00000000..ee2f4133 --- /dev/null +++ b/bindings/tutorials/export.map @@ -0,0 +1 @@ +{ global: afbBindingV*; local: *; }; diff --git a/bindings/tutorials/tuto-1.c b/bindings/tutorials/tuto-1.c new file mode 100644 index 00000000..1f58be6c --- /dev/null +++ b/bindings/tutorials/tuto-1.c @@ -0,0 +1,19 @@ +#define AFB_BINDING_VERSION 3 +#include + +void hello(afb_req_t req) +{ + AFB_REQ_DEBUG(req, "hello world"); + afb_req_reply(req, NULL, NULL, "hello world"); +} + +const afb_verb_t verbs[] = { + { .verb="hello", .callback=hello }, + { .verb=NULL } +}; + +const afb_binding_t afbBindingExport = { + .api = "tuto-1", + .verbs = verbs +}; + diff --git a/bindings/tutorials/tuto-2.c b/bindings/tutorials/tuto-2.c new file mode 100644 index 00000000..10797081 --- /dev/null +++ b/bindings/tutorials/tuto-2.c @@ -0,0 +1,100 @@ +#include +#include + +#define AFB_BINDING_VERSION 3 +#include + +afb_event_t event_login, event_logout; + +void login(afb_req_t req) +{ + json_object *args, *user, *passwd; + char *usr; + + args = afb_req_json(req); + if (!json_object_object_get_ex(args, "user", &user) + || !json_object_object_get_ex(args, "password", &passwd)) { + AFB_REQ_ERROR(req, "login, bad request: %s", json_object_get_string(args)); + afb_req_reply(req, NULL, "bad-request", NULL); + } else if (afb_req_context_get(req)) { + AFB_REQ_ERROR(req, "login, bad state, logout first"); + afb_req_reply(req, NULL, "bad-state", NULL); + } else if (strcmp(json_object_get_string(passwd), "please")) { + AFB_REQ_ERROR(req, "login, unauthorized: %s", json_object_get_string(args)); + afb_req_reply(req, NULL, "unauthorized", NULL); + } else { + usr = strdup(json_object_get_string(user)); + AFB_REQ_NOTICE(req, "login user: %s", usr); + afb_req_session_set_LOA(req, 1); + afb_req_context_set(req, usr, free); + afb_req_reply(req, NULL, NULL, NULL); + afb_event_push(event_login, json_object_new_string(usr)); + } +} + +void action(afb_req_t req) +{ + json_object *args, *val; + char *usr; + + args = afb_req_json(req); + usr = afb_req_context_get(req); + AFB_REQ_NOTICE(req, "action for user %s: %s", usr, json_object_get_string(args)); + if (json_object_object_get_ex(args, "subscribe", &val)) { + if (json_object_get_boolean(val)) { + AFB_REQ_NOTICE(req, "user %s subscribes to events", usr); + afb_req_subscribe(req, event_login); + afb_req_subscribe(req, event_logout); + } else { + AFB_REQ_NOTICE(req, "user %s unsubscribes to events", usr); + afb_req_unsubscribe(req, event_login); + afb_req_unsubscribe(req, event_logout); + } + } + afb_req_reply(req, json_object_get(args), NULL, NULL); +} + +void logout(afb_req_t req) +{ + char *usr; + + usr = afb_req_context_get(req); + AFB_REQ_NOTICE(req, "login user %s out", usr); + afb_event_push(event_logout, json_object_new_string(usr)); + afb_req_session_set_LOA(req, 0); + afb_req_context_clear(req); + afb_req_reply(req, NULL, NULL, NULL); +} + +int preinit(afb_api_t api) +{ + AFB_API_NOTICE(api, "preinit"); + return 0; +} + +int init(afb_api_t api) +{ + AFB_API_NOTICE(api, "init"); + event_login = afb_api_make_event(api, "login"); + event_logout = afb_api_make_event(api, "logout"); + if (afb_event_is_valid(event_login) && afb_event_is_valid(event_logout)) + return 0; + AFB_API_ERROR(api, "Can't create events"); + return -1; +} + +const afb_verb_t verbs[] = { + { .verb="login", .callback=login }, + { .verb="action", .callback=action, .session=AFB_SESSION_LOA_1 }, + { .verb="logout", .callback=logout, .session=AFB_SESSION_LOA_1 }, + { .verb=NULL } +}; + +const afb_binding_t afbBindingExport = { + .api = "tuto-2", + .specification = NULL, + .verbs = verbs, + .preinit = preinit, + .init = init, + .noconcurrency = 0 +}; \ No newline at end of file diff --git a/bindings/tutorials/tuto-3.cpp b/bindings/tutorials/tuto-3.cpp new file mode 100644 index 00000000..8ad69ef3 --- /dev/null +++ b/bindings/tutorials/tuto-3.cpp @@ -0,0 +1,95 @@ +#include +#include + +#define AFB_BINDING_VERSION 3 +#include + +afb::event event_login, event_logout; + +void login(afb::req req) +{ + json_object *args, *user, *passwd; + char *usr; + + args = req.json(); + if (!json_object_object_get_ex(args, "user", &user) + || !json_object_object_get_ex(args, "password", &passwd)) { + AFB_REQ_ERROR(req, "login, bad request: %s", json_object_get_string(args)); + req.fail("bad-request"); + } else if (afb_req_context_get(req)) { + AFB_REQ_ERROR(req, "login, bad state, logout first"); + req.fail("bad-state"); + } else if (strcmp(json_object_get_string(passwd), "please")) { + AFB_REQ_ERROR(req, "login, unauthorized: %s", json_object_get_string(args)); + req.fail("unauthorized"); + } else { + usr = strdup(json_object_get_string(user)); + AFB_REQ_NOTICE(req, "login user: %s", usr); + req.session_set_LOA(1); +// req.context(1, nullptr, free, usr); + req.success(); + event_login.push(json_object_new_string(usr)); + } +} + +void action(afb::req req) +{ + json_object *args, *val; + char *usr; + + args = req.json(); +// usr = (char*)req.context_get(); +usr = nullptr; + AFB_REQ_NOTICE(req, "action for user %s: %s", usr, json_object_get_string(args)); + if (json_object_object_get_ex(args, "subscribe", &val)) { + if (json_object_get_boolean(val)) { + AFB_REQ_NOTICE(req, "user %s subscribes to events", usr); + req.subscribe(event_login); + req.subscribe(event_logout); + } else { + AFB_REQ_NOTICE(req, "user %s unsubscribes to events", usr); + req.unsubscribe(event_login); + req.unsubscribe(event_logout); + } + } + req.success(json_object_get(args)); +} + +void logout(afb::req req) +{ + char *usr; + +// usr = (char*)req.context_get(); +usr = nullptr; + AFB_REQ_NOTICE(req, "login user %s out", usr); + event_logout.push(json_object_new_string(usr)); + req.session_set_LOA(0); +// req.context_clear(); + req.success(); +} + +int init( +#if AFB_BINDING_VERSION >= 3 + afb_api_t api +#endif +) +{ + AFB_NOTICE("init"); + event_login = afb_daemon_make_event("login"); + event_logout = afb_daemon_make_event("logout"); + if (afb_event_is_valid(event_login) && afb_event_is_valid(event_logout)) + return 0; + AFB_ERROR("Can't create events"); + return -1; +} + +const afb_verb_t verbs[] = { + afb::verb("login", login, "log in the system"), + afb::verb("action", action, "perform an action", AFB_SESSION_LOA_1), + afb::verb("logout", logout, "log out the system", AFB_SESSION_LOA_1), + afb::verbend() +}; + +const afb_binding_t afbBindingExport = afb::binding("tuto-3", verbs, "third tutorial: C++", init); + + diff --git a/bindings/tutorials/tuto-4.c b/bindings/tutorials/tuto-4.c new file mode 100644 index 00000000..cb909fd0 --- /dev/null +++ b/bindings/tutorials/tuto-4.c @@ -0,0 +1,29 @@ +#define AFB_BINDING_VERSION 3 +#include + +void hello(afb_req_t req) +{ + AFB_REQ_DEBUG(req, "hello world"); + afb_req_reply(req, NULL, NULL, "hello world"); +} + +const afb_verb_t verbs[] = { + { .verb="hello", .callback=hello }, + { .verb=NULL } +}; + + +static int init(afb_api_t api) +{ + int rc = afb_api_require_api(api, "hello", 1); + if (!rc) + rc = afb_api_call_sync(api, "hello", "ping", NULL, NULL, NULL, NULL); + return rc; +} + +const afb_binding_t afbBindingExport = { + .api = "tuto-4", + .verbs = verbs, + .init = init +}; + diff --git a/bindings/tutorials/tuto-5.cpp b/bindings/tutorials/tuto-5.cpp new file mode 100644 index 00000000..458ae7b5 --- /dev/null +++ b/bindings/tutorials/tuto-5.cpp @@ -0,0 +1,93 @@ +#include +#include +#include + +class tuto5 + : public afb::base_api_t +{ +private: + afb::event event_login; + afb::event event_logout; + +public: + void login(afb::req req) + { + json_object *user, *passwd; + + json_object* args = req.json(); + if (!json_object_object_get_ex(args, "user", &user) + || !json_object_object_get_ex(args, "password", &passwd)) { + AFB_REQ_ERROR(req, "login, bad request: %s", json_object_get_string(args)); + req.fail("bad-request"); + } else if (afb_req_context_get(req)) { + AFB_REQ_ERROR(req, "login, bad state, logout first"); + req.fail("bad-state"); + } else if (std::string(json_object_get_string(passwd)) != std::string("please")) { + AFB_REQ_ERROR(req, "login, unauthorized: %s", json_object_get_string(args)); + req.fail("unauthorized"); + } else { + char* username = strdup(json_object_get_string(user)); + AFB_REQ_NOTICE(req, "login user: %s", username); + req.session_set_LOA(1); + afb_req_context_set(req, username, free); + req.success(); + event_login.push(json_object_new_string(username)); + } + } + + void action(afb::req req) const + { + json_object* val; + json_object* args = req.json(); + char* username = reinterpret_cast(afb_req_context_get(req)); + AFB_REQ_NOTICE(req, "action for user %s: %s", username, json_object_get_string(args)); + if (json_object_object_get_ex(args, "subscribe", &val)) { + if (json_object_get_boolean(val)) { + AFB_REQ_NOTICE(req, "user %s subscribes to events", username); + req.subscribe(event_login); + req.subscribe(event_logout); + } else { + AFB_REQ_NOTICE(req, "user %s unsubscribes to events", username); + req.unsubscribe(event_login); + req.unsubscribe(event_logout); + } + } + req.success(json_object_get(args)); + } + + void logout(afb::req req) + { + char* username = reinterpret_cast(afb_req_context_get(req)); + AFB_REQ_NOTICE(req, "login user %s out", username); + event_logout.push(json_object_new_string(username)); + req.session_set_LOA(0); + afb_req_context_clear(req); + req.success(); + } + + int preinit(afb_api_t h) override + { + return !( + (add_verb<&tuto5::login>("login", "log in the system") == 0) && + (add_verb<&tuto5::action>("action", "perform an action", nullptr, nullptr, AFB_SESSION_LOA_1) == 0) && + (add_verb<&tuto5::logout>("logout", "log out the system", nullptr, nullptr, AFB_SESSION_LOA_1) == 0) + ); + } + + int init() override + { + AFB_API_NOTICE(api_, "init"); + event_login = make_event("login"); + event_logout = make_event("logout"); + if (event_login.is_valid() && event_logout.is_valid()) + return 0; + AFB_API_ERROR(api_, "Can't create events"); + return -1; + } +}; + +int afbBindingEntry(afb_api_t h) +{ + afb::new_api(h, "tuto-5", "fifth tutorial: C++"); + return 0; +} diff --git a/bindings/tutorials/tuto-app1.c b/bindings/tutorials/tuto-app1.c new file mode 100644 index 00000000..93747cdd --- /dev/null +++ b/bindings/tutorials/tuto-app1.c @@ -0,0 +1,30 @@ +#include + +#define AFB_BINDING_VERSION 3 +#include + +static int appmain(void *arg) +{ + const char *name = arg; + char buffer[50]; + + AFB_API_NOTICE(afbBindingV3root, "Entering Application main"); + printf("Hello, I'm %s!\n", name); + printf("What's your name? "); + scanf("%s", buffer); + printf("Hi %s! Nice to meet you. OOOOPS I'm late bye bye\n", buffer); + return 0; +} + +static void application(int signum, void *arg) +{ + if (signum) + exit(127); + exit(appmain(arg)); +} + +int afbBindingV3entry(struct afb_api_x3 *rootapi) +{ + return afb_api_queue_job(rootapi, application, "BOB", NULL, 0); +} + -- cgit 1.2.3-korg