--- /dev/null
+#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;
+}
--- /dev/null
+#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
addr(a),
local(false),
active(true),
- activity(0),
throttle(0)
{
in_addr ina;
void Host::add_activity(unsigned bytes)
{
- activity += bytes*0.06935;
+ activity.add_bytes(bytes);
}
float Host::send_packet()
{
float dt = td/Msp::Time::sec;
- activity *= pow(0.933f, dt);
+ activity.tick(td);
throttle -= dt;
if(throttle<0)
throttle = 0;
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);
#include <map>
#include <string>
#include <msp/time/timedelta.h>
+#include "activity.h"
#include "vector2.h"
class NetVis;
bool local;
Vector2 pos;
bool active;
- float activity;
+ Activity activity;
float throttle;
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 &);