]> git.tdb.fi Git - netvis.git/blob - source/history.cpp
Add a traffic history graph
[netvis.git] / source / history.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 <msp/gl/immediate.h>
9 #include <msp/gl/matrix.h>
10 #include <msp/gl/meshbuilder.h>
11 #include <msp/gl/misc.h>
12 #include <msp/strings/formatter.h>
13 #include <msp/time/units.h>
14 #include "history.h"
15 #include "netvis.h"
16
17 using namespace std;
18 using namespace Msp;
19
20 History::History(NetVis &n, unsigned w, unsigned h):
21         netvis(n),
22         width(w),
23         height(h),
24         buffer(w),
25         scale(1024),
26         mesh((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2))
27 {
28         texture.storage(GL::RGBA, height, width, 0);
29         texture.set_min_filter(GL::LINEAR);
30         GL::MeshBuilder bld(mesh);
31         bld.color(1.0f, 1.0f, 1.0f);
32         bld.begin(GL::QUADS);
33         bld.texcoord(0, 1);
34         bld.vertex(0, 0);
35         bld.texcoord(0, 0);
36         bld.vertex(width, 0);
37         bld.texcoord(1, 0);
38         bld.vertex(width, height);
39         bld.texcoord(1, 1);
40         bld.vertex(0, height);
41         bld.end();
42 }
43
44 void History::activity(unsigned down, unsigned up)
45 {
46         accum.down += down;
47         accum.up += up;
48 }
49
50 void History::tick(const Time::TimeStamp &t)
51 {
52         if(t>next_push)
53         {
54                 buffer.push(accum);
55                 accum = Bandwidth();
56                 next_push += Time::sec;
57                 if(next_push<t)
58                         next_push = t+Time::sec;
59
60                 unsigned m = 0;
61                 for(unsigned i=0; i<width; ++i)
62                 {
63                         const Bandwidth &bw = buffer.get(i);
64                         m = max(m, max(bw.down, bw.up));
65                 }
66                 scale = 1;
67                 while(m>=scale*1000)
68                         scale *= 1024;
69                 while(m>=scale*5)
70                         scale *= 10;
71                 if(m>scale*2)
72                         scale *= 5;
73                 else if(m>scale)
74                         scale *= 2;
75
76                 create_texture();
77         }
78 }
79
80 void History::render() const
81 {
82         texture.bind();
83         mesh.draw();
84
85         char suffix = 'B';
86         unsigned value = scale;
87         if(value>=1024)
88         {
89                 value /= 1024;
90                 suffix = 'k';
91         }
92         if(value>=1024)
93         {
94                 value /= 1024;
95                 suffix = 'M';
96         }
97         GL::PushMatrix push_;
98         GL::translate(width+5, height-10, 0);
99         GL::scale_uniform(10);
100         GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
101         imm.color(1.0f, 1.0f, 1.0f);
102         netvis.get_font().draw_string(format("%d%c", value, suffix), imm);
103         GL::translate(0, (height-12)*-0.05, 0);
104         netvis.get_font().draw_string(format("%g%c", value*0.5, suffix), imm);
105         GL::translate(0, (height-12)*-0.05, 0);
106         netvis.get_font().draw_string(format("0%c", suffix), imm);
107         GL::Texture::unbind();
108 }
109
110 void History::create_texture()
111 {
112         vector<unsigned> data(width*height);
113         for(unsigned y=0; y<width; ++y)
114         {
115                 const Bandwidth &bw = buffer.get(y);
116                 unsigned down = bw.down*height/scale;
117                 unsigned up = bw.up*height/scale;
118                 unsigned up_min = up;
119                 unsigned up_max = up;
120                 if(y>0)
121                 {
122                         unsigned value = (up+buffer.get(y-1).up*height/scale)/2;
123                         up_min = min(value+1, up_min);
124                         up_max = max(value, up_max);
125                 }
126                 if(y<width-1)
127                 {
128                         unsigned value = (up+buffer.get(y+1).up*height/scale)/2;
129                         up_min = min(value+1, up_min);
130                         up_max = max(value, up_max);
131                 }
132                 unsigned *row = &data[y*100];
133                 for(unsigned x=0; x<100; ++x)
134                 {
135                         if(x>=up_min && x<=up_max)
136                                 row[x] = 0xFF8000FF;
137                         else if(x<=down)
138                                 row[x] = 0xFF80FF80;
139                         else if(x%25==24 || y%60==0)
140                                 row[x] = 0x40FFFFFF;
141                         else
142                                 row[x] = 0x00000000;
143                 }
144         }
145         texture.image(0, GL::RGBA, GL::UNSIGNED_BYTE, data.data());
146 }
147
148
149 History::Bandwidth::Bandwidth():
150         down(0),
151         up(0)
152 { }