]> git.tdb.fi Git - netvis.git/blob - source/host.cpp
Handle ICMP packets
[netvis.git] / source / host.cpp
1 /* $Id$
2
3 This file is part of NetVis
4 Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed unter the GPL
6 */
7
8 #include <cmath>
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>
16 #include "host.h"
17 #include "netvis.h"
18
19 using namespace std;
20 using namespace Msp;
21
22 Host::Host(NetVis &nv, const Address &a):
23         netvis(nv),
24         addr(a),
25         name(addr.str()),
26         short_name(name),
27         local(false),
28         active(true),
29         throttle(0)
30 {
31 }
32
33 void Host::set_name(const string &n)
34 {
35         name = n;
36
37         if(local)
38         {
39                 string::size_type dot = name.find('.');
40                 short_name = name.substr(0, dot);
41         }
42         else
43         {
44                 string::size_type dot = name.size();
45                 for(unsigned i=0; (dot>0 && dot!=string::npos); ++i)
46                 {
47                         string::size_type prev = name.rfind('.', dot-1);
48                         if(prev+15<name.size() && i>1)
49                                 break;
50                         dot = prev;
51                 }
52
53                 if(dot==string::npos)
54                         short_name = name;
55                 else
56                         short_name = "..."+name.substr(dot+1);
57         }
58 }
59
60 void Host::set_local(bool l)
61 {
62         local = l;
63 }
64
65 void Host::set_position(const Vector2 &p)
66 {
67         pos = p;
68 }
69
70 void Host::set_active(bool a)
71 {
72         active = a;
73 }
74
75 void Host::add_activity(unsigned bytes)
76 {
77         activity.add_bytes(bytes);
78 }
79
80 float Host::send_packet()
81 {
82         float ret = throttle;
83         if(throttle<1)
84                 throttle += 0.025;
85         return ret;
86 }
87
88 void Host::tick(const Msp::Time::TimeDelta &td)
89 {
90         float dt = td/Msp::Time::sec;
91
92         activity.tick(td);
93         throttle -= dt;
94         if(throttle<0)
95                 throttle = 0;
96
97         if(!active)
98                 return;
99
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)
105         {
106                 if(i->second!=this)
107                 {
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;
112                         float d = sqrt(d2);
113
114                         unsigned matching_bits = addr.common_prefix_length(i->second->get_address());
115
116                         float optimal_dist = 100+(24-min(matching_bits, 24U))*12;
117                         float f = 5000.0*(1.0/optimal_dist-1.0/d);
118
119                         fx += dx/d*f;
120                         fy += dy/d*f;
121                 }
122         }
123
124         if(fx<-4 || fx>4)
125                 pos.x += fx*dt;
126         if(fy<-4 || fy>4)
127                 pos.y += fy*dt;
128 }
129
130 void Host::render() const
131 {
132         if(!active)
133                 return;
134
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);
137
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));
141         imm.vertex(-5, -5);
142         imm.vertex(5, -5);
143         imm.vertex(5, 5);
144         imm.vertex(-5, 5);
145         imm.end();
146
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);
150
151         font.draw_string(short_name);
152         GL::Texture::unbind();
153 }