Adding test
authorPierre Gabin FODOP GUMETE <gabinfodop@gmail.com>
Thu, 31 Jan 2019 15:22:35 +0000 (16:22 +0100)
committerPierre Gabin FODOP GUMETE <gabinfodop@gmail.com>
Thu, 31 Jan 2019 15:22:35 +0000 (16:22 +0100)
Change-Id: Ib43e061fb9186c747f76cc7446f6b2e4a80cdefb

src/afb-wsj1-test.cpp [new file with mode: 0644]
src/afb-wsj1.c
src/afb-wsj1.hpp

diff --git a/src/afb-wsj1-test.cpp b/src/afb-wsj1-test.cpp
new file mode 100644 (file)
index 0000000..059c845
--- /dev/null
@@ -0,0 +1,16 @@
+#include "afb-wsj1.hpp"
+
+void onreply(const char *value){
+       std::cout<< *value<<std::endl;
+       system("PAUSE");
+}
+
+int main(){
+    std::string uri,api,verb, req;
+    char * request = (char*)req.c_str();
+    afb::wsj1 sj1;
+    sj1.connect(uri);
+    sj1.call(api.c_str(),verb.c_str(),request,onreply);
+
+    return EXIT_SUCCESS;
+}
\ No newline at end of file
index 6816617..7fb8516 100644 (file)
@@ -588,4 +588,3 @@ int afb_wsj1_reply_s(struct afb_wsj1_msg *msg, const char *object, const char *t
        return wsj1_send_isot(msg->wsj1, iserror ? RETERR : RETOK, msg->id, object, token);
 }
 
-z
\ No newline at end of file
index 17f74d3..fc99092 100644 (file)
@@ -34,11 +34,11 @@ extern "C" {
 #include <afb-ws-client.h>
 }
 
-#define WEBSOCKET_CODE_INTERNAL_ERROR    1011
-
+#include <iostream>
 #include <string>
 #include <functional>
 #include <thread>
+#include <mutex>
 
 #include <systemd/sd-event.h>
 
@@ -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<void(const char*)> onreply);
+#if 0
        void call(const char *api, const char *verb, json_object *request, std::function<void(json_object*)> &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<void(const char*)> &onreply);
        void call(const std::string &api, const std::string &verb, const std::string &request, std::function<void(std::string&)> &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<void*>(this));
+       wsj1_ = afb_ws_client_connect_wsj1(eloop_(), uri, &wsj1_itf, reinterpret_cast<void*>(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<void(const char*)> &onreply) {
+void wsj1::call(const char *api, const char *verb, char *request, std::function<void(const char*)> onreply) {
        if (!wsj1_)
-               throw std::runtime_error("not-connected");      
-       afb_wsj1_call_s(wsj1_,api,verb,request,onreply,reinterpret_cast<void*>(this));
+               throw std::runtime_error("not-connected");
+       std::function<void(const char*)> *onrep = new std::function<void(const char*)>(onreply);
+       afb_wsj1_call_s(wsj1_,api,verb,request,on_reply_,reinterpret_cast<void*>(onrep));
 }
 
+void wsj1::on_reply_(void *closure, struct afb_wsj1_msg *msg) {
+       std::function<void(const char*)> *onreply = reinterpret_cast<std::function<void(const char*)>*>(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<std::mutex> 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