1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
/*
* Copyright (c) 2017 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "websocket-server.h"
#include "communication-protocol.h"
///////////////////
//Explanation: A query can select/update 2 objects:
// - Coordinates of a node
// - MCS value
//
// AllowedObjectName select the generic object to update/select. Not all the action are allowed on a generic object.
// Furthermore not al the parameters of an object can be selected/updated.
// So an object has a list of attributes that is possible to update/select. Also There are filter: a node can be updated just if
// matches a certain filter. A filter is a tuple [key operand value]. Not all the operand are allowed on a key.
//////////////////
// TODO To improve. Right now just basic controls and actions
std::set<std::string> ProtocolDetails::AllowedObjectName = {"stats"};
std::set<std::string> ProtocolDetails::AllowedActions = { "select", "subscribe"};
std::set<std::string> ProtocolDetails::AllowedFields = {"quality", "rate", "all"};
std::function<void(const std::error_code&)> CommunicationProtocol::timerCallback;
CommunicationProtocol::CommunicationProtocol(ProtocolVersion version)
: version(version)
{
}
bool
CommunicationProtocol::checkFields(const std::string &field)
{
if (ProtocolDetails::AllowedFields.find(field) == ProtocolDetails::AllowedFields.end()) {
return false;
}
return true;
}
bool
CommunicationProtocol::checkAction(const std::string &action)
{
if (ProtocolDetails::AllowedActions.find(action) != ProtocolDetails::AllowedActions.end()) {
return true;
} else {
return false;
}
}
bool
CommunicationProtocol::checkObjectName(const std::string &objectName)
{
if (ProtocolDetails::AllowedObjectName.find(objectName) != ProtocolDetails::AllowedObjectName.end()) {
return true;
} else {
return false;
}
}
bool
CommunicationProtocol::checkFilter(const std::vector<std::string> &filter)
{
// TODO So far just the filter interface id == something is supported.
if (filter.size() < 3) {
std::cerr << "The format of the filter is not correct." << std::endl;
return false;
}
return true;
}
bool
CommunicationProtocol::checkParameters(const std::string ¶meter)
{
if (ProtocolDetails::AllowedFields.find(parameter) == ProtocolDetails::AllowedFields.end()) {
std::cerr << "The parameter specified [" << parameter << "] cannot be selected (or does not exist)." << std::endl;
return false;
}
return true;
}
std::string
CommunicationProtocol::evaluateFilters(const std::list<std::vector<std::string>> &filters)
{
// Switch between operand values
for (auto filter : filters) {
if (filter[0] == "id" and filter[1] == "==") {
return filter[2];
} else {
std::cerr << "So far just one filter is supported [==]" << std::endl;
}
}
return std::string("");
}
void
CommunicationProtocol::processQuery(Server *s, websocketpp::connection_hdl hdl, message_ptr msg,
Query query)
{
std::string interface_id;
std::cout << query.toJsonString() << std::endl;
const std::string& action = query.getAction();
if (!checkAction(action)) {
std::cout << "Error in the action" << std::endl;
return ;
}
const std::string& objectName = query.getObjectName();
if (!checkObjectName(objectName)) {
std::cout << "Error in the object name" << std::endl;
return ;
}
// See the filter for understanding which node update
const std::list<std::vector<std::string>> &filters = query.getFilter();
for (auto filter : filters) {
if (!checkFilter(filter)) {
std::cout << "Error in the filters." << std::endl;
}
}
if (action == *ProtocolDetails::AllowedActions.find("select")) {
Query reply = makeReplyQuery(query);
reply.setLast(true);
try {
s->send(hdl, reply.toJsonString(), msg->get_opcode());
} catch (...) {
std::cerr << "Impossible to connect to the remote endpoint. Closing connection..." << std::endl;
}
} else if (action == *ProtocolDetails::AllowedActions.find("subscribe")) {
subscribeTimer = std::shared_ptr<asio::steady_timer>(new asio::steady_timer(s->get_io_service(),
std::chrono::milliseconds(1000)));
timerCallback = [this, s, hdl, msg, query]
(const std::error_code &ec) {
if (!ec) {
Query reply = this->makeReplyQuery(query);
if (reply.isEmpty()) {
std::cerr << "Malformed reply. Closing connection.." << std::endl;
return;
}
std::cout << "SENDING: " << reply.toJsonString() << std::endl;
try {
s->send(hdl, reply.toJsonString(), msg->get_opcode());
} catch (...) {
std::cerr << "Impossible to connect to the remote endpoint. Closing connection..." << std::endl;
return;
}
subscribeTimer->expires_from_now(std::chrono::milliseconds(1000));
subscribeTimer->async_wait(timerCallback);
}
};
subscribeTimer->async_wait(timerCallback);
}
}
Query
CommunicationProtocol::makeReplyQuery(const Query &request)
{
const std::list<std::string> &fields = request.getFields();
const std::string &objectName = request.getObjectName();
const std::list<std::vector<std::string>> &filters = request.getFilter();
std::string station = evaluateFilters(filters);
if (objectName == "stats") {
std::list<std::vector<std::string>> fltr;
std::list<std::string> flds = {};
std::map<std::string, std::string> prms;
for (auto field : fields) {
if (field == "bufferLevel" || field == "*") {
prms["bufferLevel"] = QString::number(graphDataSource->getBufferLevel()).toStdString();
}
if (field == "bitRate" || field == "*")
{
prms["bitRate"] = QString::number(graphDataSource->getFps()).toStdString();
}
if (field == "quality" || field == "*")
{
uint32_t quality = graphDataSource->getQuality();
if(quality == 2)
prms["quality"] = "LD";
else if(quality == 6)
prms["quality"] = "SD";
else if(quality == 9)
prms["quality"] = "HD";
else if(quality == 12)
prms["quality"] = "FHD";
else if(quality == 15)
prms["quality"] = "QHD";
else if (quality == 18)
prms["quality"] = "UHD";
else
prms["quality"] = "Unknown";
}
}
return Query(*ProtocolDetails::AllowedActions.find("select"),
*ProtocolDetails::AllowedObjectName.find("stats"),
fltr, prms, flds, false);
}
return Query();
}
void
CommunicationProtocol::setGraphDataSource(GraphDataSource *graphDataSource)
{
this->graphDataSource = graphDataSource;
}
|