2020-02-20 07:12:36 -05:00
|
|
|
/*
|
2021-06-13 10:32:18 -04:00
|
|
|
* Copyright (C) 2020 Luciano Iam <oss@lucianoiam.com>
|
2020-02-20 07:12:36 -05:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2020-02-24 16:03:01 -05:00
|
|
|
#ifndef NDEBUG
|
2020-02-20 07:12:36 -05:00
|
|
|
#include <iostream>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <boost/property_tree/json_parser.hpp>
|
|
|
|
#include <boost/property_tree/ptree.hpp>
|
2020-02-23 10:03:59 -05:00
|
|
|
#include <sstream>
|
2020-02-20 07:12:36 -05:00
|
|
|
|
|
|
|
#include "message.h"
|
2020-04-22 12:17:13 -04:00
|
|
|
#include "json.h"
|
2020-02-20 07:12:36 -05:00
|
|
|
|
|
|
|
// JSON does not support Infinity or NaN
|
2020-02-23 10:03:59 -05:00
|
|
|
#define XSTR(s) STR (s)
|
2020-02-20 07:12:36 -05:00
|
|
|
#define STR(s) #s
|
|
|
|
#define JSON_INF 1.0e+128
|
2020-02-23 10:03:59 -05:00
|
|
|
#define JSON_INF_STR XSTR (JSON_INF)
|
2020-02-20 07:12:36 -05:00
|
|
|
|
2020-08-30 15:15:07 -04:00
|
|
|
using namespace ArdourSurface;
|
|
|
|
|
2020-02-20 07:12:36 -05:00
|
|
|
namespace pt = boost::property_tree;
|
|
|
|
|
|
|
|
NodeStateMessage::NodeStateMessage (const NodeState& state)
|
|
|
|
: _valid (true)
|
|
|
|
, _state (state)
|
|
|
|
{
|
2020-02-23 10:03:59 -05:00
|
|
|
_write = state.n_val () > 0;
|
2020-02-20 07:12:36 -05:00
|
|
|
}
|
|
|
|
|
2020-02-23 10:03:59 -05:00
|
|
|
NodeStateMessage::NodeStateMessage (void* buf, size_t len)
|
2020-02-20 07:12:36 -05:00
|
|
|
: _valid (false)
|
|
|
|
, _write (false)
|
|
|
|
{
|
2020-02-23 10:03:59 -05:00
|
|
|
try {
|
|
|
|
std::string s (static_cast<char*> (buf), len);
|
|
|
|
|
|
|
|
std::istringstream is (s);
|
|
|
|
pt::ptree root;
|
|
|
|
pt::read_json (is, root);
|
|
|
|
|
|
|
|
_state = NodeState (root.get<std::string> ("node"));
|
|
|
|
|
2024-09-15 05:04:30 -04:00
|
|
|
pt::ptree addr = pt::ptree ();
|
|
|
|
addr = root.get_child ("addr", addr);
|
2020-02-23 10:03:59 -05:00
|
|
|
|
|
|
|
for (pt::ptree::iterator it = addr.begin (); it != addr.end (); ++it) {
|
|
|
|
// throws if datatype not uint32_t
|
2024-08-28 18:36:41 -04:00
|
|
|
_state.add_addr (static_cast<uint32_t> (stoul (it->second.data ())));
|
2020-02-23 10:03:59 -05:00
|
|
|
}
|
|
|
|
|
2024-09-15 05:04:30 -04:00
|
|
|
pt::ptree val = pt::ptree ();
|
|
|
|
val = root.get_child ("val", val);
|
2020-02-23 10:03:59 -05:00
|
|
|
|
|
|
|
for (pt::ptree::iterator it = val.begin (); it != val.end (); ++it) {
|
|
|
|
std::string val = it->second.data ();
|
|
|
|
|
|
|
|
try {
|
2024-08-28 18:36:41 -04:00
|
|
|
_state.add_val (stoi (val));
|
|
|
|
} catch (...) {
|
2020-02-23 10:03:59 -05:00
|
|
|
try {
|
2024-08-28 18:36:41 -04:00
|
|
|
double d = stod (val);
|
2020-02-23 10:03:59 -05:00
|
|
|
if (d >= JSON_INF) {
|
|
|
|
d = std::numeric_limits<double>::infinity ();
|
|
|
|
} else if (d <= -JSON_INF) {
|
|
|
|
d = -std::numeric_limits<double>::infinity ();
|
|
|
|
}
|
|
|
|
_state.add_val (d);
|
2024-08-28 18:36:41 -04:00
|
|
|
} catch (...) {
|
2020-02-23 10:03:59 -05:00
|
|
|
if (val == "false") {
|
|
|
|
_state.add_val (false);
|
|
|
|
} else if (val == "true") {
|
|
|
|
_state.add_val (true);
|
|
|
|
} else {
|
|
|
|
_state.add_val (val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_state.n_val () > 0) {
|
|
|
|
_write = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_valid = true;
|
|
|
|
|
|
|
|
} catch (const std::exception& exc) {
|
2020-02-24 16:03:01 -05:00
|
|
|
#ifndef NDEBUG
|
2020-02-23 10:03:59 -05:00
|
|
|
std::cerr << "cannot parse message - " << exc.what () << std::endl;
|
2020-02-20 07:12:36 -05:00
|
|
|
#endif
|
2020-02-23 10:03:59 -05:00
|
|
|
}
|
2020-02-20 07:12:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2020-02-23 10:03:59 -05:00
|
|
|
NodeStateMessage::serialize (void* buf, size_t len) const
|
2020-02-20 07:12:36 -05:00
|
|
|
{
|
2020-02-23 10:03:59 -05:00
|
|
|
// boost json writes all values as strings, we do not want that
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
ss << "{\"node\":\"" << _state.node () << "\"";
|
|
|
|
|
|
|
|
int n_addr = _state.n_addr ();
|
|
|
|
|
|
|
|
if (n_addr > 0) {
|
|
|
|
ss << ",\"addr\":[";
|
|
|
|
|
|
|
|
for (int i = 0; i < n_addr; i++) {
|
|
|
|
if (i > 0) {
|
|
|
|
ss << ',';
|
|
|
|
}
|
|
|
|
|
|
|
|
ss << _state.nth_addr (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
ss << "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
int n_val = _state.n_val ();
|
|
|
|
|
|
|
|
if (n_val > 0) {
|
|
|
|
ss << ",\"val\":[";
|
|
|
|
|
|
|
|
for (int i = 0; i < n_val; i++) {
|
|
|
|
if (i > 0) {
|
|
|
|
ss << ',';
|
|
|
|
}
|
|
|
|
|
|
|
|
TypedValue val = _state.nth_val (i);
|
|
|
|
|
|
|
|
switch (val.type ()) {
|
|
|
|
case TypedValue::Empty:
|
|
|
|
ss << "null";
|
|
|
|
break;
|
|
|
|
case TypedValue::Bool:
|
|
|
|
ss << (static_cast<bool> (val) ? "true" : "false");
|
|
|
|
break;
|
|
|
|
case TypedValue::Int:
|
|
|
|
ss << static_cast<int> (val);
|
|
|
|
break;
|
|
|
|
case TypedValue::Double: {
|
|
|
|
double d = static_cast<double> (val);
|
|
|
|
if (d == std::numeric_limits<double>::infinity ()) {
|
|
|
|
ss << JSON_INF_STR;
|
|
|
|
} else if (d == -std::numeric_limits<double>::infinity ()) {
|
|
|
|
ss << "-" JSON_INF_STR;
|
|
|
|
} else {
|
|
|
|
ss << d;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TypedValue::String:
|
2020-08-30 15:15:07 -04:00
|
|
|
ss << '"' << WebSocketsJSON::escape (static_cast<std::string> (val)) << '"';
|
2020-02-23 10:03:59 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ss << "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
ss << '}';
|
|
|
|
|
|
|
|
std::string s = ss.str ();
|
|
|
|
const char* cs = s.c_str ();
|
|
|
|
size_t cs_sz = strlen (cs);
|
|
|
|
|
|
|
|
if (len < cs_sz) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy (buf, cs, cs_sz);
|
|
|
|
|
|
|
|
return cs_sz;
|
2020-02-20 07:12:36 -05:00
|
|
|
}
|