X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-wsj1.hpp;h=fc990928615580aa69faa4a6e42fc756c2f78b5a;hb=9ed1400b1df1d645603f075529ff8e8c8154d145;hp=17f74d37f88efefb775cefa79a634dc46e408aca;hpb=c17659aeeee859977f811a1eb43d599925f8a032;p=src%2Fapp-framework-binder.git 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