]> git.tdb.fi Git - netvis.git/blobdiff - source/host.cpp
Resolve IP addresses to hostnames
[netvis.git] / source / host.cpp
index be24f05360d05060455228e66b6492dbb2160397..e5eafc899fd98db7bd9d851fb208b237f72b1619 100644 (file)
@@ -1,3 +1,10 @@
+/* $Id$
+
+This file is part of NetVis
+Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
+Distributed unter the GPL
+*/
+
 #include <cmath>
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -14,11 +21,48 @@ using namespace Msp;
 
 Host::Host(NetVis &nv, unsigned a):
        netvis(nv),
-       addr(a)
+       addr(a),
+       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)
+{
+       name=n;
+
+       if(local)
+       {
+               unsigned dot=name.find('.');
+               short_name=name.substr(0, dot);
+       }
+       else
+       {
+               unsigned dot=name.size();
+               for(unsigned i=0; (dot>0 && dot!=string::npos); ++i)
+               {
+                       unsigned prev=name.rfind('.', dot-1);
+                       if(prev+15<name.size() && i>1)
+                               break;
+                       dot=prev;
+               }
+
+               if(dot==string::npos)
+                       short_name=name;
+               else
+                       short_name="..."+name.substr(dot+1);
+       }
+}
+
+void Host::set_local(bool l)
+{
+       local=l;
 }
 
 void Host::set_position(const Vector2 &p)
@@ -26,19 +70,36 @@ void Host::set_position(const Vector2 &p)
        pos=p;
 }
 
-void Host::add_connection(Connection &)
+void Host::set_active(bool a)
+{
+       active=a;
+}
+
+void Host::add_activity(unsigned bytes)
 {
+       activity+=bytes*0.06935;
 }
 
-Connection *Host::get_connection(Host &)
+float Host::send_packet()
 {
-       return 0;
+       float ret=throttle;
+       if(throttle<1)
+               throttle+=0.025;
+       return ret;
 }
 
 void Host::tick(const Msp::Time::TimeDelta &td)
 {
        float dt=td/Msp::Time::sec;
 
+       activity*=pow(0.933f, dt);
+       throttle-=dt;
+       if(throttle<0)
+               throttle=0;
+
+       if(!active)
+               return;
+
        const map<unsigned, Host *> &hosts=netvis.get_hosts();
        float fx=-pos.x*0.1;
        float fy=-pos.y*0.1;
@@ -57,8 +118,8 @@ void Host::tick(const Msp::Time::TimeDelta &td)
                        for(unsigned j=32; (j-- && !((addr^other_addr)>>j));)
                                ++matching_bits;
 
-                       float nearness=24-min(matching_bits, 24U);
-                       float f=10000.0*(1.0/(60+nearness*15)-1.0/d);
+                       float optimal_dist=100+(24-min(matching_bits, 24U))*12;
+                       float f=5000.0*(1.0/optimal_dist-1.0/d);
 
                        fx+=dx/d*f;
                        fy+=dy/d*f;
@@ -73,23 +134,36 @@ void Host::tick(const Msp::Time::TimeDelta &td)
 
 void Host::render() const
 {
+       if(!active)
+               return;
+
        GL::push_matrix();
        GL::translate(static_cast<int>(pos.x), static_cast<int>(pos.y), 0);
 
        GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
        imm.begin(GL::QUADS);
-       imm.color(1.0f, 1.0f, 1.0f, 1.0f);
+       imm.color(1.0f, 1.0f, 1.0f, max(min(static_cast<float>(activity/10000), 1.0f), 0.2f));
        imm.vertex(-5, -5);
        imm.vertex(5, -5);
        imm.vertex(5, 5);
        imm.vertex(-5, 5);
        imm.end();
 
+       GL::pop_matrix();
+}
+
+void Host::render_label() const
+{
+       if(!active)
+               return;
+
        const GL::Font &font=netvis.get_font();
-       GL::translate(-static_cast<int>(font.get_string_width(name)*5), 6, 0);
+
+       GL::push_matrix();
+       GL::translate(static_cast<int>(pos.x)-static_cast<int>(font.get_string_width(short_name)*5), static_cast<int>(pos.y)+6, 0);
        GL::scale_uniform(10);
-       font.draw_string(name);
-       GL::Texture::unbind();
+
+       font.draw_string(short_name);
 
        GL::pop_matrix();
 }