--- /dev/null
+/* $Id$
+
+This file is part of NetVis
+Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
+Distributed unter the GPL
+*/
+
+#include <msp/gl/immediate.h>
+#include <msp/gl/matrix.h>
+#include <msp/gl/meshbuilder.h>
+#include <msp/gl/misc.h>
+#include <msp/strings/formatter.h>
+#include <msp/time/units.h>
+#include "history.h"
+#include "netvis.h"
+
+using namespace std;
+using namespace Msp;
+
+History::History(NetVis &n, unsigned w, unsigned h):
+ netvis(n),
+ width(w),
+ height(h),
+ buffer(w),
+ scale(1024),
+ mesh((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2))
+{
+ texture.storage(GL::RGBA, height, width, 0);
+ texture.set_min_filter(GL::LINEAR);
+ GL::MeshBuilder bld(mesh);
+ bld.color(1.0f, 1.0f, 1.0f);
+ bld.begin(GL::QUADS);
+ bld.texcoord(0, 1);
+ bld.vertex(0, 0);
+ bld.texcoord(0, 0);
+ bld.vertex(width, 0);
+ bld.texcoord(1, 0);
+ bld.vertex(width, height);
+ bld.texcoord(1, 1);
+ bld.vertex(0, height);
+ bld.end();
+}
+
+void History::activity(unsigned down, unsigned up)
+{
+ accum.down += down;
+ accum.up += up;
+}
+
+void History::tick(const Time::TimeStamp &t)
+{
+ if(t>next_push)
+ {
+ buffer.push(accum);
+ accum = Bandwidth();
+ next_push += Time::sec;
+ if(next_push<t)
+ next_push = t+Time::sec;
+
+ unsigned m = 0;
+ for(unsigned i=0; i<width; ++i)
+ {
+ const Bandwidth &bw = buffer.get(i);
+ m = max(m, max(bw.down, bw.up));
+ }
+ scale = 1;
+ while(m>=scale*1000)
+ scale *= 1024;
+ while(m>=scale*5)
+ scale *= 10;
+ if(m>scale*2)
+ scale *= 5;
+ else if(m>scale)
+ scale *= 2;
+
+ create_texture();
+ }
+}
+
+void History::render() const
+{
+ texture.bind();
+ mesh.draw();
+
+ char suffix = 'B';
+ unsigned value = scale;
+ if(value>=1024)
+ {
+ value /= 1024;
+ suffix = 'k';
+ }
+ if(value>=1024)
+ {
+ value /= 1024;
+ suffix = 'M';
+ }
+ GL::PushMatrix push_;
+ GL::translate(width+5, height-10, 0);
+ GL::scale_uniform(10);
+ GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
+ imm.color(1.0f, 1.0f, 1.0f);
+ netvis.get_font().draw_string(format("%d%c", value, suffix), imm);
+ GL::translate(0, (height-12)*-0.05, 0);
+ netvis.get_font().draw_string(format("%g%c", value*0.5, suffix), imm);
+ GL::translate(0, (height-12)*-0.05, 0);
+ netvis.get_font().draw_string(format("0%c", suffix), imm);
+ GL::Texture::unbind();
+}
+
+void History::create_texture()
+{
+ vector<unsigned> data(width*height);
+ for(unsigned y=0; y<width; ++y)
+ {
+ const Bandwidth &bw = buffer.get(y);
+ unsigned down = bw.down*height/scale;
+ unsigned up = bw.up*height/scale;
+ unsigned up_min = up;
+ unsigned up_max = up;
+ if(y>0)
+ {
+ unsigned value = (up+buffer.get(y-1).up*height/scale)/2;
+ up_min = min(value+1, up_min);
+ up_max = max(value, up_max);
+ }
+ if(y<width-1)
+ {
+ unsigned value = (up+buffer.get(y+1).up*height/scale)/2;
+ up_min = min(value+1, up_min);
+ up_max = max(value, up_max);
+ }
+ unsigned *row = &data[y*100];
+ for(unsigned x=0; x<100; ++x)
+ {
+ if(x>=up_min && x<=up_max)
+ row[x] = 0xFF8000FF;
+ else if(x<=down)
+ row[x] = 0xFF80FF80;
+ else if(x%25==24 || y%60==0)
+ row[x] = 0x40FFFFFF;
+ else
+ row[x] = 0x00000000;
+ }
+ }
+ texture.image(0, GL::RGBA, GL::UNSIGNED_BYTE, data.data());
+}
+
+
+History::Bandwidth::Bandwidth():
+ down(0),
+ up(0)
+{ }
--- /dev/null
+/* $Id$
+
+This file is part of NetVis
+Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
+Distributed unter the GPL
+*/
+
+#ifndef HISTORY_H_
+#define HISTORY_H_
+
+#include <msp/gl/mesh.h>
+#include <msp/gl/texture2d.h>
+#include <msp/time/timestamp.h>
+#include "ringbuffer.h"
+
+class NetVis;
+
+class History
+{
+private:
+ struct Bandwidth
+ {
+ unsigned down;
+ unsigned up;
+
+ Bandwidth();
+ };
+
+ NetVis &netvis;
+ unsigned width;
+ unsigned height;
+ Bandwidth accum;
+ RingBuffer<Bandwidth> buffer;
+ Msp::Time::TimeStamp next_push;
+ unsigned scale;
+ Msp::GL::Texture2D texture;
+ Msp::GL::Mesh mesh;
+
+public:
+ History(NetVis &, unsigned, unsigned);
+ void activity(unsigned, unsigned);
+ void tick(const Msp::Time::TimeStamp &);
+ void render() const;
+private:
+ void create_texture();
+};
+
+#endif
#include <cstdlib>
#include <cmath>
#include <signal.h>
+#include <netinet/ether.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
-#include <linux/if_ether.h>
#include <msp/core/except.h>
#include <msp/debug/profilingscope.h>
#include <msp/gl/blend.h>
#include <msp/strings/formatter.h>
#include <msp/time/units.h>
#include <msp/time/utils.h>
+#include "history.h"
#include "host.h"
#include "netvis.h"
#include "packet.h"
font = new GL::Font;
DataFile::load(*font, "dejavu-10.font");
+ history = new History(*this, 301, 100);
+
catch_signal(SIGINT);
}
while(pcap_dispatch(pcap, -1, &capture_handler, reinterpret_cast<unsigned char *>(this))>0) ;
resolver->tick();
+ history->tick(tick_t);
+
float min_activity = numeric_limits<float>::max();
for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
font->draw_string(format("%.2f fps", fps));
GL::Texture::unbind();
GL::pop_matrix();
+
+ GL::push_matrix();
+ GL::translate(170, -370, 0);
+ history->render();
+ GL::pop_matrix();
}
Host &NetVis::get_host(unsigned a)
shost.add_activity(size);
if(dhost)
dhost->add_activity(size);
+
+ if((ntohl(ip->saddr)&self->localnet_mask)==self->localnet)
+ self->history->activity(0, size);
+ else if((ntohl(ip->daddr)&self->localnet_mask)==self->localnet)
+ self->history->activity(size, 0);
}
}
#include <msp/gl/font.h>
#include <msp/time/timestamp.h>
+class History;
class Host;
class Packet;
class Port;
std::map<unsigned, Host *> disabled_hosts;
std::list<Packet *> packets;
std::map<unsigned, Port *> ports;
+ History *history;
Msp::Time::TimeStamp tick_t;
Msp::Time::TimeStamp fps_t;
Host &get_host(unsigned);
Msp::GL::Color generate_color(bool) const;
const Port &get_port(unsigned);
+ void create_history_texture();
static void capture_handler(unsigned char *, const pcap_pkthdr *, const unsigned char *);
else
name = Msp::lexical_cast(number);
+ const map<unsigned, Port *> &ports = netvis.get_ports();
+ unsigned tries = 100;
+ if(ports.size()>100 && number>=1024)
+ tries = 10000/ports.size()+1;
float best_score = 0;
- for(unsigned i=0; (i<100 && best_score<1); ++i)
+ for(unsigned i=0; (i<tries && best_score<1); ++i)
{
GL::Color c;
c.r = rand()*1.0/RAND_MAX;
c = c*0.6+0.4;
float score = 2;
- const map<unsigned, Port *> &ports = netvis.get_ports();
for(map<unsigned, Port *>::const_iterator j=ports.begin(); j!=ports.end(); ++j)
{
+ if(number<1024 && j->first>1024)
+ break;
const GL::Color &other = j->second->get_color();
float dr = c.r-other.r;
float dg = c.g-other.g;
{
while(!done)
{
- while(1)
+ while(!done)
{
Host *host;
{
}
}
- Time::sleep(Time::sec);
+ Time::sleep(100*Time::msec);
}
}
--- /dev/null
+/* $Id$
+
+This file is part of NetVis
+Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
+Distributed unter the GPL
+*/
+
+#ifndef RINGBUFFER_H_
+#define RINGBUFFER_H_
+
+#include <vector>
+
+template<typename T>
+class RingBuffer
+{
+private:
+ unsigned sz;
+ std::vector<T> data;
+ unsigned pos;
+
+public:
+ RingBuffer(unsigned s): sz(s), data(sz), pos(0) { }
+ void push(const T &v) { data[pos=(pos+1)%sz] = v; }
+ const T &get(unsigned i) const { return data[(pos+sz-i)%sz]; }
+ unsigned size() const { return sz; }
+};
+
+#endif