]> git.tdb.fi Git - netvis.git/blobdiff - source/host.cpp
Handle addresses in a more generic way
[netvis.git] / source / host.cpp
index afa61b71e775a4eaa5ab49c8c87fc799ce5f71b0..93c537ea104fae71d27977f7486feca149821ebd 100644 (file)
@@ -19,17 +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),
        throttle(0)
 {
-       in_addr ina;
-       ina.s_addr = htonl(addr);
-       name = inet_ntoa(ina);
-       short_name = name;
 }
 
 void Host::set_name(const string &n)
@@ -99,11 +97,11 @@ void Host::tick(const Msp::Time::TimeDelta &td)
        if(!active)
                return;
 
-       const map<unsigned, Host *> &hosts = netvis.get_hosts();
+       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<unsigned, Host *>::const_iterator i=hosts.begin(); i!=hosts.end(); ++i)
+       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);