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):
31 ina.s_addr = htonl(addr);
32 name = inet_ntoa(ina);
36 void Host::set_name(const string &n)
42 unsigned dot = name.find('.');
43 short_name = name.substr(0, dot);
47 unsigned dot = name.size();
48 for(unsigned i=0; (dot>0 && dot!=string::npos); ++i)
50 unsigned prev = name.rfind('.', dot-1);
51 if(prev+15<name.size() && i>1)
59 short_name = "..."+name.substr(dot+1);
63 void Host::set_local(bool l)
68 void Host::set_position(const Vector2 &p)
73 void Host::set_active(bool a)
78 void Host::add_activity(unsigned bytes)
80 activity += bytes*0.06935;
83 float Host::send_packet()
91 void Host::tick(const Msp::Time::TimeDelta &td)
93 float dt = td/Msp::Time::sec;
95 activity *= pow(0.933f, dt);
103 const map<unsigned, Host *> &hosts = netvis.get_hosts();
104 float fx = -pos.x*0.1;
105 float fy = -pos.y*0.1;
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::PushMatrix push_;
141 GL::translate(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/10000), 1.0f), 0.2f));
152 const GL::Font &font = netvis.get_font();
153 GL::translate(-static_cast<int>(font.get_string_width(short_name)*5), 6, 0);
154 GL::scale_uniform(10);
156 imm.color(1.0f, 1.0f, 1.0f);
157 font.draw_string(short_name, imm);
158 GL::Texture::unbind();