Change last seen to seconds since last announcement

Change last seen from seconds since startup to seconds since the source
was announced.
This commit is contained in:
Anders Martinsson 2023-09-19 09:11:16 +02:00
parent 767d38db29
commit 8cdfec3cd6
4 changed files with 10 additions and 2 deletions

View File

@ -651,7 +651,7 @@ where:
> **last\_seen** > **last\_seen**
> JSON number specifying the last time the source was announced. > JSON number specifying the last time the source was announced.
> This time is expressed in seconds since the daemon startup. > This time is expressed in seconds since the source was announced.
> **announce_period** > **announce_period**
> JSON number specifying the meausured period in seconds between the last source announcements. > JSON number specifying the meausured period in seconds between the last source announcements.

View File

@ -102,6 +102,7 @@ bool Browser::worker() {
if ((last_update_ - upd_source.last_seen) != 0) { if ((last_update_ - upd_source.last_seen) != 0) {
upd_source.announce_period = last_update_ - upd_source.last_seen; upd_source.announce_period = last_update_ - upd_source.last_seen;
upd_source.last_seen = last_update_; upd_source.last_seen = last_update_;
upd_source.last_seen_timepoint = steady_clock::now();
sources_.replace(it, upd_source); sources_.replace(it, upd_source);
} }
} else { } else {
@ -168,6 +169,7 @@ void Browser::on_change_rtsp_source(const std::string& name,
upd_source.origin = sdp_get_origin(s.sdp); upd_source.origin = sdp_get_origin(s.sdp);
upd_source.sdp = s.sdp; upd_source.sdp = s.sdp;
upd_source.last_seen = last_update_; upd_source.last_seen = last_update_;
upd_source.last_seen_timepoint = steady_clock::now();
sources_.get<name_tag>().replace(it, upd_source); sources_.get<name_tag>().replace(it, upd_source);
return; return;
} }

View File

@ -38,6 +38,7 @@
#include "utils.hpp" #include "utils.hpp"
using namespace boost::multi_index; using namespace boost::multi_index;
using namespace std::chrono;
struct RemoteSource { struct RemoteSource {
std::string id; std::string id;
@ -49,6 +50,7 @@ struct RemoteSource {
std::string sdp; std::string sdp;
uint32_t last_seen{0}; /* seconds from daemon startup */ uint32_t last_seen{0}; /* seconds from daemon startup */
uint32_t announce_period{0}; /* period between annoucements */ uint32_t announce_period{0}; /* period between annoucements */
time_point<steady_clock> last_seen_timepoint{steady_clock::now()};
}; };
class Browser : public MDNSClient { class Browser : public MDNSClient {

View File

@ -23,11 +23,15 @@
#include <iostream> #include <iostream>
#include <regex> #include <regex>
#include <string> #include <string>
#include <chrono>
#include "log.hpp" #include "log.hpp"
#include "utils.hpp" #include "utils.hpp"
#include "json.hpp" #include "json.hpp"
using namespace std::chrono;
using second_t = duration<double, std::ratio<1> >;
static inline std::string remove_undesired_chars(const std::string& s) { static inline std::string remove_undesired_chars(const std::string& s) {
std::regex html_regex("[^ A-Za-z0-9:~.,_/=%()\\r\\n\\t\?#-]?"); std::regex html_regex("[^ A-Za-z0-9:~.,_/=%()\\r\\n\\t\?#-]?");
return std::regex_replace(s, html_regex, ""); return std::regex_replace(s, html_regex, "");
@ -253,7 +257,7 @@ std::string remote_source_to_json(const RemoteSource& source) {
<< ",\n \"domain\": \"" << escape_json(source.domain) << "\"" << ",\n \"domain\": \"" << escape_json(source.domain) << "\""
<< ",\n \"address\": \"" << escape_json(source.address) << "\"" << ",\n \"address\": \"" << escape_json(source.address) << "\""
<< ",\n \"sdp\": \"" << escape_json(source.sdp) << "\"" << ",\n \"sdp\": \"" << escape_json(source.sdp) << "\""
<< ",\n \"last_seen\": " << unsigned(source.last_seen) << ",\n \"last_seen\": " << unsigned(duration_cast<second_t>(steady_clock::now() - source.last_seen_timepoint).count())
<< ",\n \"announce_period\": " << unsigned(source.announce_period) << ",\n \"announce_period\": " << unsigned(source.announce_period)
<< " \n }"; << " \n }";
return ss.str(); return ss.str();