From: Pierre Gabin FODOP GUMETE Date: Thu, 31 Jan 2019 15:22:35 +0000 (+0100) Subject: Adding test X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fapp-framework-binder.git;a=commitdiff_plain;h=9ed1400b1df1d645603f075529ff8e8c8154d145 Adding test Change-Id: Ib43e061fb9186c747f76cc7446f6b2e4a80cdefb --- diff --git a/src/afb-wsj1-test.cpp b/src/afb-wsj1-test.cpp new file mode 100644 index 00000000..059c845b --- /dev/null +++ b/src/afb-wsj1-test.cpp @@ -0,0 +1,16 @@ +#include "afb-wsj1.hpp" + +void onreply(const char *value){ + std::cout<< *value<wsj1, iserror ? RETERR : RETOK, msg->id, object, token); } -z \ No newline at end of file diff --git a/src/afb-wsj1.hpp b/src/afb-wsj1.hpp index 17f74d37..fc990928 100644 --- a/src/afb-wsj1.hpp +++ b/src/afb-wsj1.hpp @@ -34,11 +34,11 @@ extern "C" { #include } -#define WEBSOCKET_CODE_INTERNAL_ERROR 1011 - +#include #include #include #include +#include #include @@ -55,15 +55,17 @@ public: void connect(const std::string &uri) { connect(uri.c_str()); } void connect(const char *uri); void disconnect(); + void call(const char *api, const char *verb, char *request, std::function onreply); +#if 0 void call(const char *api, const char *verb, json_object *request, std::function &onreply) { call(api,verb,json_object_to_json_string_ext(request,JSON_C_TO_STRING_PLAIN),[](const char *x){onreply(std::string(x));}); }; - void call(const char *api, const char *verb, char *request, std::function &onreply); void call(const std::string &api, const std::string &verb, const std::string &request, std::function &onreply) - { + { call(api.c_str(), verb.c_str(), request.c_str(), [=](const char *x){onreply(std::string(x));}); } +#endif public: class msg { friend class wsj1; @@ -82,9 +84,10 @@ private: void on_hangup_(struct afb_wsj1 *wsj1) {} void on_call_(const char *api, const char *verb, struct afb_wsj1_msg *msg) {} void on_event_(const char *event, struct afb_wsj1_msg *msg); - static void on_reply_(void *closure, struct afb_wsj1_msg *msg); private: - static sd_event *eloop(); + static sd_event *eloop_(); +private: + static void on_reply_(void *closure, struct afb_wsj1_msg *msg); }; struct afb_wsj1_itf wsj1::wsj1_itf = { @@ -96,7 +99,7 @@ struct afb_wsj1_itf wsj1::wsj1_itf = { void wsj1::connect(const char *uri) { if (wsj1_) throw std::runtime_error("already-connected"); - wsj1_ = afb_ws_client_connect_wsj1(eloop(), uri, &wsj1_itf, reinterpret_cast(this)); + wsj1_ = afb_ws_client_connect_wsj1(eloop_(), uri, &wsj1_itf, reinterpret_cast(this)); if (!wsj1_) throw std::runtime_error("connection-failed"); } @@ -116,10 +119,35 @@ void wsj1::on_event_(const char *event, struct afb_wsj1_msg *msg) { } -void wsj1::call(const char *api, const char *verb, char *request, std::function &onreply) { +void wsj1::call(const char *api, const char *verb, char *request, std::function onreply) { if (!wsj1_) - throw std::runtime_error("not-connected"); - afb_wsj1_call_s(wsj1_,api,verb,request,onreply,reinterpret_cast(this)); + throw std::runtime_error("not-connected"); + std::function *onrep = new std::function(onreply); + afb_wsj1_call_s(wsj1_,api,verb,request,on_reply_,reinterpret_cast(onrep)); } +void wsj1::on_reply_(void *closure, struct afb_wsj1_msg *msg) { + std::function *onreply = reinterpret_cast*>(closure); + (*onreply)(afb_wsj1_msg_object_s(msg)); + afb_wsj1_msg_unref(msg); + delete onreply; +} + +sd_event *wsj1::eloop_() { + static sd_event *el = nullptr; + static std::mutex lock; + std::lock_guard guard(lock); + if (!el) { + if (sd_event_new(&el) == 0) { + std::thread t(sd_event_loop, el); + t.detach(); + } + } +} + + + } +// faire un test qui verifi le fonctionnement de la classe, +// il faut faire bien attention avec la fonction call qui doit se terminer avan tle programme, un get char ou un cin +// ecrire lma fonction on reply qui affichhe le resultat de la requete à l'ecran \ No newline at end of file