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, unsigned a):
30 ina.s_addr = htonl(addr);
31 name = inet_ntoa(ina);
35 void Host::set_name(const string &n)
41 string::size_type dot = name.find('.');
42 short_name = name.substr(0, dot);
46 string::size_type dot = name.size();
47 for(unsigned i=0; (dot>0 && dot!=string::npos); ++i)
49 string::size_type prev = name.rfind('.', dot-1);
50 if(prev+15<name.size() && i>1)
58 short_name = "..."+name.substr(dot+1);
62 void Host::set_local(bool l)
67 void Host::set_position(const Vector2 &p)
72 void Host::set_active(bool a)
77 void Host::add_activity(unsigned bytes)
79 activity.add_bytes(bytes);
82 float Host::send_packet()
90 void Host::tick(const Msp::Time::TimeDelta &td)
92 float dt = td/Msp::Time::sec;
102 const map<unsigned, Host *> &hosts = netvis.get_hosts();
103 float center_force = (local ? 0.5 : 0.1);
104 float fx = -pos.x*center_force;
105 float fy = -pos.y*center_force;
106 for(map<unsigned, Host *>::const_iterator i=hosts.begin(); i!=hosts.end(); ++i)
110 const Vector2 &other_pos = i->second->get_position();
111 float dx = other_pos.x-pos.x;
112 float dy = other_pos.y-pos.y;
113 float d2 = dx*dx+dy*dy;
116 unsigned other_addr = i->second->get_address();
117 unsigned matching_bits = 0;
118 for(unsigned j=32; (j-- && !((addr^other_addr)>>j));)
121 float optimal_dist = 100+(24-min(matching_bits, 24U))*12;
122 float f = 5000.0*(1.0/optimal_dist-1.0/d);
135 void Host::render() const
140 GL::MatrixStack::Push push_(GL::MatrixStack::modelview());
141 GL::MatrixStack::modelview() *= GL::Matrix::translation(static_cast<int>(pos.x), static_cast<int>(pos.y), 0);
143 GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
144 imm.begin(GL::QUADS);
145 imm.color(1.0f, 1.0f, 1.0f, max(min(static_cast<float>(activity.get_average()/10000), 1.0f), 0.2f));
152 const GL::Font &font = netvis.get_font();
153 GL::MatrixStack::modelview() *= GL::Matrix::translation(-static_cast<int>(font.get_string_width(short_name)*5), 6, 0);
154 GL::MatrixStack::modelview() *= GL::Matrix::scaling(10);
156 font.draw_string(short_name);
157 GL::Texture::unbind();