diff --git a/include/villas/node.hpp b/include/villas/node.hpp index 5c0842d8d..8cec6db01 100644 --- a/include/villas/node.hpp +++ b/include/villas/node.hpp @@ -106,6 +106,12 @@ protected: return -1; } + virtual + json_t * _readStatus() const + { + return nullptr; + } + public: /** Initialize node with default values */ Node(const uuid_t &id = {}, const std::string &name = ""); diff --git a/include/villas/nodes/webrtc.hpp b/include/villas/nodes/webrtc.hpp index 8f33b411f..1776d44ca 100644 --- a/include/villas/nodes/webrtc.hpp +++ b/include/villas/nodes/webrtc.hpp @@ -47,6 +47,9 @@ protected: virtual int _write(struct Sample *smps[], unsigned cnt); + virtual + json_t * _readStatus() const; + public: WebRTCNode(const uuid_t &id = {}, const std::string &name = ""); diff --git a/include/villas/nodes/webrtc/peer_connection.hpp b/include/villas/nodes/webrtc/peer_connection.hpp index 5f0c83f87..bdf374a01 100644 --- a/include/villas/nodes/webrtc/peer_connection.hpp +++ b/include/villas/nodes/webrtc/peer_connection.hpp @@ -10,6 +10,7 @@ #pragma once +#include #include #include @@ -31,6 +32,8 @@ public: void onMessage(std::function callback); void sendMessage(rtc::binary msg); + json_t * readStatus() const; + void connect(); void disconnect(); diff --git a/lib/node.cpp b/lib/node.cpp index c1a1d7429..759cd91cb 100644 --- a/lib/node.cpp +++ b/lib/node.cpp @@ -463,6 +463,10 @@ json_t * Node::toJson() const if (stats) json_object_set_new(json_node, "stats", stats->toJson()); + auto *status = _readStatus(); + if (status) + json_object_set_new(json_node, "status", status); + /* Add all additional fields of node here. * This can be used for metadata */ json_object_update(json_node, config); diff --git a/lib/nodes/webrtc/peer_connection.cpp b/lib/nodes/webrtc/peer_connection.cpp index 0b445c2fb..aa98e0d0b 100644 --- a/lib/nodes/webrtc/peer_connection.cpp +++ b/lib/nodes/webrtc/peer_connection.cpp @@ -10,9 +10,11 @@ #include #include + #include #include #include + #include #include #include @@ -79,6 +81,31 @@ bool PeerConnection::waitForDataChannel(std::chrono::seconds timeout) return startupCondition.wait_until(lock, deadline, [this](){ return this->stopStartup; }); } +json_t * PeerConnection::readStatus() +{ + auto *json = json_pack("{ s: I, s: I }", + "bytes_received", conn->bytesReceived(), + "bytes_sent", conn->bytesSent() + ); + + auto rtt = conn->rtt(); + if (rtt.has_value()) { + auto *json_rtt = json_real(rtt.value().count() / 1e3); + json_object_set_new(json, "rtt", json_rtt); + } + + rtc::Candidate local, remote; + if (conn->getSelectedCandidatePair(&local, &remote)) { + auto *json_cp = json_pack("{ s: s, s: s }", + "local", std::string(local).c_str(), + "remote", std::string(remote).c_str() + ); + json_object_set_new(json, "candidate_pair", json_cp); + } + + return json; +} + void PeerConnection::notifyStartup() { stopStartup = true;