]> git.tdb.fi Git - netvis.git/blobdiff - source/host.cpp
Handle ICMP packets
[netvis.git] / source / host.cpp
index 4314d75f63714c01c5a7e3f5f1ac42f9df06c9ae..93c537ea104fae71d27977f7486feca149821ebd 100644 (file)
@@ -19,18 +19,15 @@ Distributed unter the GPL
 using namespace std;
 using namespace Msp;
 
-Host::Host(NetVis &nv, unsigned a):
+Host::Host(NetVis &nv, const Address &a):
        netvis(nv),
        addr(a),
+       name(addr.str()),
+       short_name(name),
        local(false),
        active(true),
-       activity(0),
        throttle(0)
 {
-       in_addr ina;
-       ina.s_addr = htonl(addr);
-       name = inet_ntoa(ina);
-       short_name = name;
 }
 
 void Host::set_name(const string &n)
@@ -39,15 +36,15 @@ void Host::set_name(const string &n)
 
        if(local)
        {
-               unsigned dot = name.find('.');
+               string::size_type dot = name.find('.');
                short_name = name.substr(0, dot);
        }
        else
        {
-               unsigned dot = name.size();
+               string::size_type dot = name.size();
                for(unsigned i=0; (dot>0 && dot!=string::npos); ++i)
                {
-                       unsigned prev = name.rfind('.', dot-1);
+                       string::size_type prev = name.rfind('.', dot-1);
                        if(prev+15<name.size() && i>1)
                                break;
                        dot = prev;
@@ -77,7 +74,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 +89,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;
@@ -100,10 +97,11 @@ void Host::tick(const Msp::Time::TimeDelta &td)
        if(!active)
                return;
 
-       const map<unsigned, Host *> &hosts = netvis.get_hosts();
-       float fx = -pos.x*0.1;
-       float fy = -pos.y*0.1;
-       for(map<unsigned, Host *>::const_iterator i=hosts.begin(); i!=hosts.end(); ++i)
+       const map<Address, Host *> &hosts = netvis.get_hosts();
+       float center_force = (local ? 0.5 : 0.1);
+       float fx = -pos.x*center_force;
+       float fy = -pos.y*center_force;
+       for(map<Address, Host *>::const_iterator i=hosts.begin(); i!=hosts.end(); ++i)
        {
                if(i->second!=this)
                {
@@ -113,10 +111,7 @@ void Host::tick(const Msp::Time::TimeDelta &td)
                        float d2 = dx*dx+dy*dy;
                        float d = sqrt(d2);
 
-                       unsigned other_addr = i->second->get_address();
-                       unsigned matching_bits = 0;
-                       for(unsigned j=32; (j-- && !((addr^other_addr)>>j));)
-                               ++matching_bits;
+                       unsigned matching_bits = addr.common_prefix_length(i->second->get_address());
 
                        float optimal_dist = 100+(24-min(matching_bits, 24U))*12;
                        float f = 5000.0*(1.0/optimal_dist-1.0/d);
@@ -137,12 +132,12 @@ void Host::render() const
        if(!active)
                return;
 
-       GL::PushMatrix push_;
-       GL::translate(static_cast<int>(pos.x), static_cast<int>(pos.y), 0);
+       GL::MatrixStack::Push push_(GL::MatrixStack::modelview());
+       GL::MatrixStack::modelview() *= GL::Matrix::translation(static_cast<int>(pos.x), static_cast<int>(pos.y), 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);
@@ -150,12 +145,9 @@ void Host::render() const
        imm.end();
 
        const GL::Font &font = netvis.get_font();
-       GL::translate(-static_cast<int>(font.get_string_width(short_name)*5), 6, 0);
-       GL::scale_uniform(10);
+       GL::MatrixStack::modelview() *= GL::Matrix::translation(-static_cast<int>(font.get_string_width(short_name)*5), 6, 0);
+       GL::MatrixStack::modelview() *= GL::Matrix::scaling(10);
 
-       imm.color(1.0f, 1.0f, 1.0f);
-       font.draw_string(short_name, imm);
+       font.draw_string(short_name);
        GL::Texture::unbind();
-
-       GL::pop_matrix();
 }