aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Collignon <loic.collignon@iot.bzh>2019-02-27 17:44:37 +0100
committerLoïc Collignon <loic.collignon@iot.bzh>2019-02-27 17:54:26 +0100
commit8c588d04fcb72c2a9b298f73f7085a1bed46fa40 (patch)
treea591065da4cb9f12239c88a3cb1abbb8a34e3258
parent9271575a7dd2b783eda8bcc7f06479ee20abbd99 (diff)
Added a tutorial and fix few errorssandbox/ctxnop/binding-object
Added the tuto-5 that show how to benefit from the new 'binding-object' header. Fix few errors that can cause segfault when dealing with events and use the right afb::req object for verbs. Change-Id: I0563dd72a2843b2b54c2e40398ba129aac05ff0c Signed-off-by: Loïc Collignon <loic.collignon@iot.bzh>
-rw-r--r--bindings/tutorial/CMakeLists.txt1
-rw-r--r--bindings/tutorial/tuto-5.cpp93
-rw-r--r--include/afb/c++/binding-object.hpp46
-rw-r--r--include/afb/c++/binding-wrap.hpp3
4 files changed, 118 insertions, 25 deletions
diff --git a/bindings/tutorial/CMakeLists.txt b/bindings/tutorial/CMakeLists.txt
index 07b7e3f1..f05fa541 100644
--- a/bindings/tutorial/CMakeLists.txt
+++ b/bindings/tutorial/CMakeLists.txt
@@ -33,5 +33,6 @@ tuto(1 c)
tuto(2 c)
tuto(3 cpp)
tuto(4 c)
+tuto(5 cpp)
tuto(app1 c)
diff --git a/bindings/tutorial/tuto-5.cpp b/bindings/tutorial/tuto-5.cpp
new file mode 100644
index 00000000..458ae7b5
--- /dev/null
+++ b/bindings/tutorial/tuto-5.cpp
@@ -0,0 +1,93 @@
+#include <afb/c++/binding>
+#include <json-c/json.h>
+#include <string.h>
+
+class tuto5
+ : public afb::base_api_t<tuto5>
+{
+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<char*>(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<char*>(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<tuto5>(h, "tuto-5", "fifth tutorial: C++");
+ return 0;
+}
diff --git a/include/afb/c++/binding-object.hpp b/include/afb/c++/binding-object.hpp
index 307189b9..1744535f 100644
--- a/include/afb/c++/binding-object.hpp
+++ b/include/afb/c++/binding-object.hpp
@@ -57,12 +57,12 @@ namespace afb
/**
* @brief TApi's method pointer.
*/
- using TVerbCallback = void(TApi::*)(afb_req_t);
+ using TVerbCallback = void(TApi::*)(req);
/***
* @brief TApi's const method pointer.
*/
- using TVerbCallbackConst = void(TApi::*)(afb_req_t) const;
+ using TVerbCallbackConst = void(TApi::*)(req) const;
/**
* @brief Pre-init callback for an api created using @c afb::api::new_api.
@@ -91,7 +91,7 @@ namespace afb
return -2;
}
- api->handle_ = handle;
+ api->api_ = handle;
return api->preinit(handle);
}
@@ -131,30 +131,30 @@ namespace afb
/**
* @brief Verb callback for a verb added using @c afb::api::add_verb.
* @tparam callback TApi's method to call
- * @param[in] req Request to handle.
+ * @param[in] r Request to handle.
*/
template <TVerbCallback callback>
- static void verb(afb_req_t req)
+ static void verb(afb_req_t r)
{
- assert(req != nullptr);
+ assert(r != nullptr);
- afb_api_t handle = afb_req_get_api(req);
+ afb_api_t handle = afb_req_get_api(r);
if (handle)
{
void* userdata = afb_api_get_userdata(handle);
if (userdata)
{
TApi* api = reinterpret_cast<TApi*>(userdata);
- (api->*callback)(req);
+ (api->*callback)(afb::req(r));
}
else
{
- afb_req_fail(req, "Failed to get the API object!", nullptr);
+ afb_req_fail(r, "Failed to get the API object!", nullptr);
}
}
else
{
- afb_req_fail(req, "Failed to get the corresponding API from the query!", nullptr);
+ afb_req_fail(r, "Failed to get the corresponding API from the query!", nullptr);
}
}
@@ -164,27 +164,27 @@ namespace afb
* @param[in] req Request to handle.
*/
template <TVerbCallbackConst callback>
- static void verb(afb_req_t req)
+ static void verb(afb_req_t r)
{
- assert(req != nullptr);
+ assert(r != nullptr);
- afb_api_t handle = afb_req_get_api(req);
+ afb_api_t handle = afb_req_get_api(r);
if (handle)
{
void* userdata = afb_api_get_userdata(handle);
if (userdata)
{
TApi* api = reinterpret_cast<TApi*>(userdata);
- (api->*callback)(req);
+ (api->*callback)(afb::req(r));
}
else
{
- afb_req_fail(req, "Failed to get the API object!", nullptr);
+ afb_req_fail(r, "Failed to get the API object!", nullptr);
}
}
else
{
- afb_req_fail(req, "Failed to get the corresponding API from the query!", nullptr);
+ afb_req_fail(r, "Failed to get the corresponding API from the query!", nullptr);
}
}
};
@@ -212,8 +212,6 @@ namespace afb
base_api_t& operator=(const base_api_t&) = delete;
protected:
- afb_api_t handle_;
-
/**
* @brief Default constructor.
*/
@@ -238,7 +236,7 @@ namespace afb
int add_verb(const std::string& verb, const std::string& info, void* vcbdata = nullptr, const struct afb_auth* auth = nullptr, uint32_t session = AFB_SESSION_NONE_X2, int glob = 0)
{
return afb_api_add_verb(
- handle_,
+ api_,
verb.c_str(),
info == "" ? nullptr : info.c_str(),
TTraits::template verb<Callback>,
@@ -263,7 +261,7 @@ namespace afb
int add_verb(const std::string& verb, const std::string& info, void* vcbdata = nullptr, const struct afb_auth* auth = nullptr, uint32_t session = AFB_SESSION_NONE_X2, int glob = 0)
{
return afb_api_add_verb(
- handle_,
+ api_,
verb.c_str(),
info == "" ? nullptr : info.c_str(),
TTraits::template verb<Callback>,
@@ -284,21 +282,21 @@ namespace afb
* @brief Get the API's handle.
* @return The API's handle.
*/
- afb_api_t handle() const { return handle_; }
+ afb_api_t handle() const { return api_; }
/**
* @brief Implicit conversion to C handle.
* @return The API's handle.
*/
- operator afb_api_t() const { return handle_; }
+ operator afb_api_t() const { return api_; }
/**
* @brief Destructor.
*/
virtual ~base_api_t()
{
- if (handle_ && afb_api_delete_api(handle_))
- AFB_API_ERROR(handle_, "Failed to delete API.");
+ if (api_ && afb_api_delete_api(api_))
+ AFB_API_ERROR(api_, "Failed to delete API.");
}
/**
diff --git a/include/afb/c++/binding-wrap.hpp b/include/afb/c++/binding-wrap.hpp
index 04f4add1..ae0e5eeb 100644
--- a/include/afb/c++/binding-wrap.hpp
+++ b/include/afb/c++/binding-wrap.hpp
@@ -90,6 +90,7 @@ bool callsync(const char *api, const char *verb, struct json_object *args, struc
/* apis */
class api
{
+protected:
afb_api_t api_;
public:
using call_cb = void (*)(void *closure, struct json_object *object, const char *error, const char *info, afb_api_t api);
@@ -406,7 +407,7 @@ inline bool event::is_valid() const { return afb_event_is_valid(event_); }
inline int event::broadcast(json_object *object) const { return afb_event_broadcast(event_, object); }
inline int event::push(json_object *object) const { return afb_event_push(event_, object); }
-inline void event::unref() { afb_event_unref(event_); event_ = nullptr; }
+inline void event::unref() { if (event_) { afb_event_unref(event_); } event_ = nullptr; }
inline void event::addref() { afb_event_addref(event_); }
inline const char *event::name() const { return afb_event_name(event_); }