OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
http_protocol.hpp
1#pragma once
2#include <ossia/network/base/protocol.hpp>
3#include <ossia/network/generic/wrapped_parameter.hpp>
4
5#include <ossia-qt/js_utilities.hpp>
6
7#include <QByteArray>
8#include <QJSValue>
9#include <QList>
10#include <QObject>
11
12#include <verdigris>
13
14class QQmlEngine;
15class QQmlComponent;
16class QNetworkAccessManager;
17class QNetworkReply;
18
19namespace ossia::net
20{
21
22struct http_parameter_data_base
23{
24 http_parameter_data_base() = default;
25 http_parameter_data_base(const http_parameter_data_base&) = default;
26 http_parameter_data_base(http_parameter_data_base&&) = default;
27 http_parameter_data_base& operator=(const http_parameter_data_base&) = default;
28 http_parameter_data_base& operator=(http_parameter_data_base&&) = default;
29 http_parameter_data_base(const QJSValue& val)
30 {
31 if(auto r = val.property("request"); r.isString() || r.isCallable())
32 {
33 request = r;
34 }
35
36 if(requestIsValid())
37 {
38 auto a = val.property("answer");
39 if(a.isCallable())
40 {
41 answer = a;
42 }
43 }
44
45 if(auto data = val.property("requestData"); data.isString() || data.isCallable())
46 {
47 requestData = data;
48 }
49 if(auto post = val.property("method"); post.toString().toLower() == "post")
50 {
51 is_post = true;
52 }
53 if(auto r = val.property("onRead"); r.isCallable())
54 {
55 onRead = r;
56 }
57 if(auto r = val.property("onReadString"); r.isCallable())
58 {
59 onReadString = r;
60 }
61 }
62
63 bool requestIsValid() const noexcept
64 {
65 return !request.isUndefined() && !request.isNull();
66 }
67 QJSValue request;
68 QJSValue answer;
69 QJSValue requestData;
70 QJSValue onRead;
71 QJSValue onReadString;
72 bool is_post{};
73};
74
75struct http_parameter_data
76 : public parameter_data
77 , public http_parameter_data_base
78{
79 using base_data_type = http_parameter_data_base;
80 http_parameter_data() = default;
81 http_parameter_data(const http_parameter_data&) = default;
82 http_parameter_data(http_parameter_data&&) = default;
83 http_parameter_data& operator=(const http_parameter_data&) = default;
84 http_parameter_data& operator=(http_parameter_data&&) = default;
85
86 http_parameter_data(const std::string& name)
87 : parameter_data{name}
88 {
89 }
90
91 http_parameter_data(const QJSValue& val)
92 : parameter_data{ossia::qt::make_parameter_data(val)}
93 , http_parameter_data_base{val}
94 {
95 }
96
97 bool valid() const noexcept { return requestIsValid() || type; }
98};
99
100using http_parameter = wrapped_parameter<http_parameter_data>;
101using http_node = ossia::net::wrapped_node<http_parameter_data, http_parameter>;
102class OSSIA_EXPORT http_protocol final
103 : public QObject
105{
106 W_OBJECT(http_protocol)
107
108public:
109 explicit http_protocol(QByteArray code);
110
111 http_protocol(const http_protocol&) = delete;
112 http_protocol(http_protocol&&) = delete;
113 http_protocol& operator=(const http_protocol&) = delete;
114 http_protocol& operator=(http_protocol&&) = delete;
115
116 ~http_protocol() override;
117
118 bool update(ossia::net::node_base& node_base) override;
119
120 bool pull(ossia::net::parameter_base& parameter_base) override;
121
122 bool
123 push(const ossia::net::parameter_base& parameter_base, const ossia::value& v) override;
124 bool push_raw(const ossia::net::full_parameter_data& parameter_base) override;
125
126 bool observe(ossia::net::parameter_base& parameter_base, bool enable) override;
127
128 void set_device(ossia::net::device_base& dev) override;
129
130 static http_parameter_data read_data(const QJSValue& js) { return js; }
131
132public:
133 void sig_push(http_parameter* arg_1, const ossia::value& v)
134 E_SIGNAL(OSSIA_EXPORT, sig_push, arg_1, v)
135
136private:
137 void slot_push(http_parameter*, const ossia::value& v);
138 W_SLOT(slot_push);
139
140private:
141 QUrl requestUrl(const http_parameter* addr_p, const ossia::value& v);
142 QByteArray requestData(const http_parameter* addr_p, const ossia::value& v);
143 void apply_reply(QJSValue);
144
145 QQmlEngine* m_engine{};
146 QQmlComponent* m_component{};
147
148 QNetworkAccessManager* m_access{};
149
150 QByteArray m_code;
151 ossia::net::device_base* m_device{};
152 ossia::hash_map<QNetworkReply*, http_parameter*> m_replies;
153};
154using http_device = ossia::net::wrapped_device<http_node, http_protocol>;
155class OSSIA_EXPORT HTTP : public QObject
156{
157};
158}
159
160Q_DECLARE_METATYPE(ossia::net::http_parameter*)
161W_REGISTER_ARGTYPE(ossia::net::http_parameter*)
162W_REGISTER_ARGTYPE(const ossia::net::http_parameter*)
Root of a device tree.
Definition ossia/network/base/device.hpp:58
The node_base class.
Definition node.hpp:48
The parameter_base class.
Definition ossia/network/base/parameter.hpp:48
The protocol_base class.
Definition protocol.hpp:40
The value class.
Definition value.hpp:173
Definition git_info.h:7
Full information about a parameter.
Definition parameter_data.hpp:61