Skip to content
Snippets Groups Projects
Commit 09fce36a authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Merge branch 'master' of gitlab.utu.fi:nicolas.pope/ftl

parents c4c1adf8 b04c16f0
No related branches found
No related tags found
No related merge requests found
Pipeline #22605 passed
...@@ -76,6 +76,7 @@ class Net : public Stream { ...@@ -76,6 +76,7 @@ class Net : public Stream {
ftl::UUID peer_; ftl::UUID peer_;
int64_t last_frame_; int64_t last_frame_;
int64_t frame_no_; int64_t frame_no_;
int64_t last_ping_;
std::string uri_; std::string uri_;
bool host_; bool host_;
int tally_; int tally_;
......
...@@ -22,7 +22,7 @@ float Net::sample_count__ = 0.0f; ...@@ -22,7 +22,7 @@ float Net::sample_count__ = 0.0f;
int64_t Net::last_msg__ = 0; int64_t Net::last_msg__ = 0;
MUTEX Net::msg_mtx__; MUTEX Net::msg_mtx__;
Net::Net(nlohmann::json &config, ftl::net::Universe *net) : Stream(config), active_(false), net_(net), clock_adjust_(0) { Net::Net(nlohmann::json &config, ftl::net::Universe *net) : Stream(config), active_(false), net_(net), clock_adjust_(0), last_ping_(0) {
// TODO: Install "find_stream" binding if not installed... // TODO: Install "find_stream" binding if not installed...
if (!net_->isBound("find_stream")) { if (!net_->isBound("find_stream")) {
net_->bind("find_stream", [this](const std::string &uri) -> optional<ftl::UUID> { net_->bind("find_stream", [this](const std::string &uri) -> optional<ftl::UUID> {
...@@ -292,7 +292,7 @@ bool Net::_sendRequest(Channel c, uint8_t frameset, uint8_t frames, uint8_t coun ...@@ -292,7 +292,7 @@ bool Net::_sendRequest(Channel c, uint8_t frameset, uint8_t frames, uint8_t coun
clock_adjust_ = ftl::timer::get_time() - mastertime + (latency/2); clock_adjust_ = ftl::timer::get_time() - mastertime + (latency/2);
//if (clock_adjust_ > 0) { //if (clock_adjust_ > 0) {
// LOG(INFO) << "Clock adjustment: " << clock_adjust_; //LOG(INFO) << "LATENCY: " << latency;
//} //}
}); });
} catch (...) { } catch (...) {
...@@ -357,25 +357,29 @@ bool Net::_processRequest(ftl::net::Peer &p, const ftl::codecs::Packet &pkt) { ...@@ -357,25 +357,29 @@ bool Net::_processRequest(ftl::net::Peer &p, const ftl::codecs::Packet &pkt) {
// Sync clocks! // Sync clocks!
if (ftl::timer::isClockSlave() && p.id() == time_peer_) { if (ftl::timer::isClockSlave() && p.id() == time_peer_) {
auto start = std::chrono::high_resolution_clock::now(); auto start = std::chrono::high_resolution_clock::now();
int64_t now = ftl::timer::get_time();
if (last_ping_ < now-500) {
last_ping_ = now;
try { try {
net_->asyncCall<int64_t>(time_peer_, "__ping__", [this, start](const int64_t &mastertime) { net_->asyncCall<int64_t>(time_peer_, "__ping__", [this, start](const int64_t &mastertime) {
auto elapsed = std::chrono::high_resolution_clock::now() - start; auto elapsed = std::chrono::high_resolution_clock::now() - start;
int64_t latency = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count(); int64_t latency = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
auto clock_adjust = mastertime - (ftl::timer::get_time() + (latency/2)); auto clock_adjust = mastertime - (ftl::timer::get_time() + (latency/2));
if (clock_adjust > 0) { if (clock_adjust != 0) {
LOG(INFO) << "Clock adjustment: " << clock_adjust; LOG(INFO) << "Clock adjustment: " << clock_adjust << ", latency=" << latency/2;
//LOG(INFO) << "Latency: " << (latency / 2); //LOG(INFO) << "Latency: " << (latency / 2);
//LOG(INFO) << "Local: " << std::chrono::time_point_cast<std::chrono::milliseconds>(start).time_since_epoch().count() << ", master: " << mastertime; //LOG(INFO) << "Local: " << std::chrono::time_point_cast<std::chrono::milliseconds>(start).time_since_epoch().count() << ", master: " << mastertime;
ftl::timer::setClockAdjustment(clock_adjust); ftl::timer::setClockAdjustment(clock_adjust);
} }
}); });
} catch (...) { } catch (...) {
LOG(ERROR) << "Ping failed"; LOG(ERROR) << "Ping failed";
// Reset time peer and remove timer // Reset time peer and remove timer
time_peer_ = ftl::UUID(0); time_peer_ = ftl::UUID(0);
return false; return false;
}
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment