Adding new C++ Class
[src/app-framework-binder.git] / src / afb-wsj1.hpp
1 /*
2  * Copyright (C) 2015-2018 "IoT.bzh"
3  * Author Gabin Fodop
4  * Author José Bollo <jose.bollo@iot.bzh>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #pragma once
20
21 #define _GNU_SOURCE
22
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <assert.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <pthread.h>
30
31 extern "C" {
32 #include <json-c/json.h>
33 #include <afb-wsj1.h>
34 #include <afb-ws-client.h>
35 }
36
37 #define WEBSOCKET_CODE_INTERNAL_ERROR    1011
38
39 #include <string>
40 #include <functional>
41 #include <thread>
42
43 #include <systemd/sd-event.h>
44
45 namespace afb {
46
47 class wsj1 {
48 public:
49         wsj1() noexcept = default;
50         wsj1(const wsj1 &other) = delete;
51         wsj1(wsj1 &&other) noexcept { wsj1_ = other.wsj1_; other.wsj1_ = nullptr; }
52         wsj1(const std::string &uri) { connect(uri); }
53         wsj1(const char *uri) { connect(uri); }
54         ~wsj1() { if (wsj1_) disconnect(); }
55         void connect(const std::string &uri) { connect(uri.c_str()); }
56         void connect(const char *uri);
57         void disconnect();
58         void call(const char *api, const char *verb, json_object *request, std::function<void(json_object*)> &onreply)
59         {
60                 call(api,verb,json_object_to_json_string_ext(request,JSON_C_TO_STRING_PLAIN),[](const char *x){onreply(std::string(x));});
61         };
62         void call(const char *api, const char *verb, char *request, std::function<void(const char*)> &onreply);
63         void call(const std::string &api, const std::string &verb, const std::string &request, std::function<void(std::string&)> &onreply)
64         { 
65                 call(api.c_str(), verb.c_str(), request.c_str(), [=](const char *x){onreply(std::string(x));});
66         }
67 public:
68         class msg {
69                 friend class wsj1;
70                 msg(afb_wsj1_msg *msg) : msg_{msg} {}
71                 afb_wsj1_msg *msg_;
72         public:
73         };
74 private:
75         afb_wsj1 *wsj1_ = nullptr;
76 private:
77         static struct afb_wsj1_itf wsj1_itf;
78         static void itf_hangup_(void *closure, struct afb_wsj1 *wsj1);
79         static void itf_on_call_(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
80         static void itf_on_event_(void *closure, const char *event, struct afb_wsj1_msg *msg);
81 private:
82         void on_hangup_(struct afb_wsj1 *wsj1) {}
83         void on_call_(const char *api, const char *verb, struct afb_wsj1_msg *msg) {}
84         void on_event_(const char *event, struct afb_wsj1_msg *msg);
85         static void on_reply_(void *closure, struct afb_wsj1_msg *msg);
86 private:
87         static sd_event *eloop();
88 };
89
90 struct afb_wsj1_itf wsj1::wsj1_itf = {
91         wsj1::itf_hangup_,
92         wsj1::itf_on_call_,
93         wsj1::itf_on_event_
94 };
95
96 void wsj1::connect(const char *uri) {
97         if (wsj1_)
98                 throw std::runtime_error("already-connected");
99         wsj1_ = afb_ws_client_connect_wsj1(eloop(), uri, &wsj1_itf, reinterpret_cast<void*>(this));
100         if (!wsj1_)
101                 throw std::runtime_error("connection-failed");
102 }
103
104 void wsj1::disconnect() {
105         if (!wsj1_)
106                 throw std::runtime_error("not-connected");
107         afb_wsj1_unref(wsj1_);
108         wsj1_ = nullptr;
109 }
110
111 void wsj1::itf_on_event_(void *closure, const char *event, struct afb_wsj1_msg *msg) {
112         reinterpret_cast<wsj1*>(closure)->on_event_(event, msg);
113 }
114
115 void wsj1::on_event_(const char *event, struct afb_wsj1_msg *msg) {
116
117 }
118
119 void wsj1::call(const char *api, const char *verb, char *request, std::function<void(const char*)> &onreply) {
120         if (!wsj1_)
121                 throw std::runtime_error("not-connected");      
122         afb_wsj1_call_s(wsj1_,api,verb,request,onreply,reinterpret_cast<void*>(this));
123 }
124
125 }