]> git.tdb.fi Git - netvis.git/commitdiff
Move activity tracking to a separate class
authorMikko Rasa <tdb@tdb.fi>
Sun, 15 Jun 2014 08:14:25 +0000 (11:14 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 15 Jun 2014 08:14:25 +0000 (11:14 +0300)
source/activity.cpp [new file with mode: 0644]
source/activity.h [new file with mode: 0644]
source/host.cpp
source/host.h

diff --git a/source/activity.cpp b/source/activity.cpp
new file mode 100644 (file)
index 0000000..05a4892
--- /dev/null
@@ -0,0 +1,30 @@
+#include <cmath>
+#include <msp/time/units.h>
+#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 (file)
index 0000000..c816a6d
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef ACTIVITY_H_
+#define ACTIVITY_H_
+
+#include <msp/time/timedelta.h>
+
+class Activity
+{
+private:
+       float average;
+
+public:
+       Activity();
+
+       void add_bytes(unsigned);
+       float get_average() const { return average; }
+
+       void tick(const Msp::Time::TimeDelta &);
+};
+
+#endif
index 1e92ed77da2acc848dd0ba1daf7e0926fb4f64df..c18f117ba11d3dc360328ad0eb539c3d4e8939ed 100644 (file)
@@ -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<float>(activity/10000), 1.0f), 0.2f));
+       imm.color(1.0f, 1.0f, 1.0f, max(min(static_cast<float>(activity.get_average()/10000), 1.0f), 0.2f));
        imm.vertex(-5, -5);
        imm.vertex(5, -5);
        imm.vertex(5, 5);
index b600dea04e3d9d315c865fbaabb3df7d69fc5e5b..ec2741d6b7127183381cb6f858b054b8d0fd5dc2 100644 (file)
@@ -11,6 +11,7 @@ Distributed unter the GPL
 #include <map>
 #include <string>
 #include <msp/time/timedelta.h>
+#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 &);