]> git.tdb.fi Git - netvis.git/blobdiff - source/netvis.cpp
Tick new and reappearing hosts a few times to put them in sensible locations
[netvis.git] / source / netvis.cpp
index bcdba3f28c6314ff1ebf72a9891181e797dba08d..3aff9cd48ef210a3f543a8e2adc2e5f258773ed9 100644 (file)
@@ -9,10 +9,10 @@ Distributed unter the GPL
 #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>
@@ -26,6 +26,7 @@ Distributed unter the GPL
 #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"
@@ -42,8 +43,6 @@ NetVis::NetVis(int argc, char **argv):
        font(0),
        max_hosts(1000),
        max_visible_hosts(30),
-       draw_labels(true),
-       blend(true),
        frames(0)
 {
        if(argc<2)
@@ -67,7 +66,6 @@ NetVis::NetVis(int argc, char **argv):
        wnd = new Graphics::SimpleGLWindow(1024, 768);
        wnd->set_title("NetVis");
        wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &NetVis::exit), 0));
-       wnd->signal_key_press.connect(sigc::mem_fun(this, &NetVis::key_press));
        wnd->show();
 
        GL::enable(GL::BLEND);
@@ -76,6 +74,8 @@ NetVis::NetVis(int argc, char **argv):
        font = new GL::Font;
        DataFile::load(*font, "dejavu-10.font");
 
+       history = new History(*this, 301, 100);
+
        catch_signal(SIGINT);
 }
 
@@ -117,6 +117,8 @@ void NetVis::tick()
        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)
@@ -133,6 +135,8 @@ void NetVis::tick()
                {
                        i->second->set_active(true);
                        hosts.insert(*i);
+                       for(unsigned j=0; j<100; ++j)
+                               i->second->tick(100*Time::msec);
                        disabled_hosts.erase(i++);
                }
                else if(i->second->get_activity()<del_limit)
@@ -224,6 +228,11 @@ void NetVis::render()
        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)
@@ -241,6 +250,8 @@ Host &NetVis::get_host(unsigned a)
                host->set_local(true);
        resolver->push(host);
        host->set_position(Vector2(rand()*400.0/RAND_MAX-200.0, rand()*400.0/RAND_MAX-200.0));
+       for(unsigned j=0; j<100; ++j)
+               host->tick(100*Time::msec);
        hosts[a] = host;
        return *host;
 }
@@ -255,17 +266,6 @@ const Port &NetVis::get_port(unsigned number)
        return *port;
 }
 
-void NetVis::key_press(unsigned key, unsigned, wchar_t)
-{
-       if(key==46)
-               draw_labels = !draw_labels;
-       else if(key==56)
-       {
-               blend = !blend;
-               GL::set(GL_BLEND, blend);
-       }
-}
-
 void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const unsigned char *data)
 {
        NetVis *self = reinterpret_cast<NetVis *>(user);
@@ -302,6 +302,11 @@ void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const uns
                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);
        }
 }