From cc29ebb178d4695573f10a829534cb660e14e79d Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 15 Jun 2014 11:14:25 +0300 Subject: [PATCH] Move activity tracking to a separate class --- source/activity.cpp | 30 ++++++++++++++++++++++++++++++ source/activity.h | 20 ++++++++++++++++++++ source/host.cpp | 7 +++---- source/host.h | 5 +++-- 4 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 source/activity.cpp create mode 100644 source/activity.h diff --git a/source/activity.cpp b/source/activity.cpp new file mode 100644 index 0000000..05a4892 --- /dev/null +++ b/source/activity.cpp @@ -0,0 +1,30 @@ +#include +#include +#include "activity.h" + +using namespace Msp; + +Activity::Activity(): + average(0) +{ } + +void Activity::add_bytes(unsigned n) +{ + // Scale for correct integral + average += n*0.06935; +} + +void Activity::tick(const Time::TimeDelta &dt) +{ + static Time::TimeDelta last_dt; + static float decay = 0; + + if(dt!=last_dt) + { + // Half-life in 10 seconds + decay = pow(0.933f, dt/Time::sec); + last_dt = dt; + } + + average *= decay; +} diff --git a/source/activity.h b/source/activity.h new file mode 100644 index 0000000..c816a6d --- /dev/null +++ b/source/activity.h @@ -0,0 +1,20 @@ +#ifndef ACTIVITY_H_ +#define ACTIVITY_H_ + +#include + +class Activity +{ +private: + float average; + +public: + Activity(); + + void add_bytes(unsigned); + float get_average() const { return average; } + + void tick(const Msp::Time::TimeDelta &); +}; + +#endif diff --git a/source/host.cpp b/source/host.cpp index 1e92ed7..c18f117 100644 --- a/source/host.cpp +++ b/source/host.cpp @@ -24,7 +24,6 @@ Host::Host(NetVis &nv, unsigned a): addr(a), local(false), active(true), - activity(0), throttle(0) { in_addr ina; @@ -77,7 +76,7 @@ void Host::set_active(bool a) void Host::add_activity(unsigned bytes) { - activity += bytes*0.06935; + activity.add_bytes(bytes); } float Host::send_packet() @@ -92,7 +91,7 @@ void Host::tick(const Msp::Time::TimeDelta &td) { float dt = td/Msp::Time::sec; - activity *= pow(0.933f, dt); + activity.tick(td); throttle -= dt; if(throttle<0) throttle = 0; @@ -142,7 +141,7 @@ void Host::render() const GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2)); imm.begin(GL::QUADS); - imm.color(1.0f, 1.0f, 1.0f, max(min(static_cast(activity/10000), 1.0f), 0.2f)); + imm.color(1.0f, 1.0f, 1.0f, max(min(static_cast(activity.get_average()/10000), 1.0f), 0.2f)); imm.vertex(-5, -5); imm.vertex(5, -5); imm.vertex(5, 5); diff --git a/source/host.h b/source/host.h index b600dea..ec2741d 100644 --- a/source/host.h +++ b/source/host.h @@ -11,6 +11,7 @@ Distributed unter the GPL #include #include #include +#include "activity.h" #include "vector2.h" class NetVis; @@ -25,7 +26,7 @@ private: bool local; Vector2 pos; bool active; - float activity; + Activity activity; float throttle; public: @@ -41,7 +42,7 @@ public: void set_active(bool); bool get_active() const { return active; } void add_activity(unsigned); - float get_activity() const { return activity; } + float get_activity() const { return activity.get_average(); } float send_packet(); void tick(const Msp::Time::TimeDelta &); -- 2.45.2