3 This file is part of NetVis
4 Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed unter the GPL
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
11 #include <msp/gl/immediate.h>
12 #include <msp/gl/matrix.h>
13 #include <msp/gl/texture.h>
14 #include <msp/gl/transform.h>
15 #include <msp/time/units.h>
22 Host::Host(NetVis &nv, const Address &a):
33 void Host::set_name(const string &n)
39 string::size_type dot = name.find('.');
40 short_name = name.substr(0, dot);
44 string::size_type dot = name.size();
45 for(unsigned i=0; (dot>0 && dot!=string::npos); ++i)
47 string::size_type prev = name.rfind('.', dot-1);
48 if(prev+15<name.size() && i>1)
56 short_name = "..."+name.substr(dot+1);
60 void Host::set_local(bool l)
65 void Host::set_position(const Vector2 &p)
70 void Host::set_active(bool a)
75 void Host::add_activity(unsigned bytes)
77 activity.add_bytes(bytes);
80 float Host::send_packet()
88 void Host::tick(const Msp::Time::TimeDelta &td)
90 float dt = td/Msp::Time::sec;
100 const map<Address, Host *> &hosts = netvis.get_hosts();
101 float center_force = (local ? 0.5 : 0.1);
102 float fx = -pos.x*center_force;
103 float fy = -pos.y*center_force;
104 for(map<Address, Host *>::const_iterator i=hosts.begin(); i!=hosts.end(); ++i)
108 const Vector2 &other_pos = i->second->get_position();
109 float dx = other_pos.x-pos.x;
110 float dy = other_pos.y-pos.y;
111 float d2 = dx*dx+dy*dy;
114 unsigned matching_bits = addr.common_prefix_length(i->second->get_address());
116 float optimal_dist = 100+(24-min(matching_bits, 24U))*12;
117 float f = 5000.0*(1.0/optimal_dist-1.0/d);
130 void Host::render() const
135 GL::MatrixStack::Push push_(GL::MatrixStack::modelview());
136 GL::MatrixStack::modelview() *= GL::Matrix::translation(static_cast<int>(pos.x), static_cast<int>(pos.y), 0);
138 GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
139 imm.begin(GL::QUADS);
140 imm.color(1.0f, 1.0f, 1.0f, max(min(static_cast<float>(activity.get_average()/10000), 1.0f), 0.2f));
147 const GL::Font &font = netvis.get_font();
148 GL::MatrixStack::modelview() *= GL::Matrix::translation(-static_cast<int>(font.get_string_width(short_name)*5), 6, 0);
149 GL::MatrixStack::modelview() *= GL::Matrix::scaling(10);
151 font.draw_string(short_name);
152 GL::Texture::unbind();