]> git.tdb.fi Git - netvis.git/blobdiff - source/netvis.cpp
Adapt to MSP library changes
[netvis.git] / source / netvis.cpp
index 3aff9cd48ef210a3f543a8e2adc2e5f258773ed9..52f586456f4a51b117b40e25ea482aa2980679f2 100644 (file)
@@ -13,7 +13,7 @@ Distributed unter the GPL
 #include <netinet/ip.h>
 #include <netinet/tcp.h>
 #include <netinet/udp.h>
-#include <msp/core/except.h>
+#include <msp/core/getopt.h>
 #include <msp/debug/profilingscope.h>
 #include <msp/gl/blend.h>
 #include <msp/gl/framebuffer.h>
@@ -23,7 +23,7 @@ Distributed unter the GPL
 #include <msp/gl/projection.h>
 #include <msp/gl/texture2d.h>
 #include <msp/gl/transform.h>
-#include <msp/strings/formatter.h>
+#include <msp/strings/format.h>
 #include <msp/time/units.h>
 #include <msp/time/utils.h>
 #include "history.h"
@@ -46,16 +46,16 @@ NetVis::NetVis(int argc, char **argv):
        frames(0)
 {
        if(argc<2)
-               throw UsageError("No interface given");
+               throw usage_error("No interface given");
        iface = argv[1];
 
        char err[1024];
        pcap = pcap_open_live(iface.c_str(), 128, true, 0, err);
        if(!pcap)
-               throw Exception(err);
+               throw runtime_error(err);
 
        if(pcap_setnonblock(pcap, true, err)==-1)
-               throw Exception(err);
+               throw runtime_error(err);
 
        pcap_lookupnet(iface.c_str(), &localnet, &localnet_mask, err);
        localnet = ntohl(localnet);
@@ -68,8 +68,7 @@ NetVis::NetVis(int argc, char **argv):
        wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &NetVis::exit), 0));
        wnd->show();
 
-       GL::enable(GL::BLEND);
-       GL::blend_func(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA);
+       GL::Blend::alpha().bind();
 
        font = new GL::Font;
        DataFile::load(*font, "dejavu-10.font");
@@ -81,6 +80,7 @@ NetVis::NetVis(int argc, char **argv):
 
 NetVis::~NetVis()
 {
+       delete history;
        delete resolver;
 
        delete font;
@@ -126,7 +126,7 @@ void NetVis::tick()
                i->second->tick(dt);
                min_activity = min(min_activity, i->second->get_activity());
        }
-       float del_limit = pow(10, 6-0.1*(max_hosts-hosts.size()-disabled_hosts.size()));
+       float del_limit = pow(10, 6-0.1*static_cast<int>(max_hosts-hosts.size()-disabled_hosts.size()));
        for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end();)
        {
                i->second->tick(dt);
@@ -141,6 +141,7 @@ void NetVis::tick()
                }
                else if(i->second->get_activity()<del_limit)
                {
+                       resolver->cancel(i->second);
                        delete i->second;
                        disabled_hosts.erase(i++);
                }
@@ -172,6 +173,19 @@ void NetVis::tick()
                }
        }
 
+       for(map<unsigned, Port *>::iterator i=ports.begin(); i!=ports.end();)
+       {
+               i->second->tick(dt);
+
+               if(!i->second->is_registered() && i->second->get_activity()<0.1)
+               {
+                       delete i->second;
+                       ports.erase(i++);
+               }
+               else
+                       ++i;
+       }
+
        for(list<Packet *>::iterator i=packets.begin(); i!=packets.end();)
        {
                (*i)->tick(dt);
@@ -192,13 +206,10 @@ void NetVis::tick()
 
 void NetVis::render()
 {
-       GL::clear(GL::COLOR_BUFFER_BIT);
+       GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT);
 
-       GL::matrix_mode(GL::PROJECTION);
-       GL::load_identity();
-       GL::ortho_centered(1024, 768);
-       GL::matrix_mode(GL::MODELVIEW);
-       GL::load_identity();
+       GL::MatrixStack::projection() = GL::Matrix::ortho_centered(1024, 768);
+       GL::MatrixStack::modelview() = GL::Matrix();
 
        for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
                i->second->render();
@@ -210,29 +221,30 @@ void NetVis::render()
                imm.end();
        }
 
-       GL::push_matrix();
-       GL::translate(-500, 360, 0);
+       GL::MatrixStack::modelview() = GL::Matrix::translation(-500, 360, 0);
        unsigned n = 0;
-       for(map<unsigned, Port *>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i, ++n)
+       for(map<unsigned, Port *>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i)
        {
-               i->second->render();
-               GL::translate(0, -12, 0);
+               float act = i->second->get_activity();
+               if((i->second->is_registered() && act>1) || act>200)
+               {
+                       i->second->render();
+                       GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -12, 0);
+                       ++n;
+               }
        }
-       GL::pop_matrix();
 
-       GL::push_matrix();
-       GL::translate(-500, -360, 0);
-       GL::scale_uniform(10);
+       GL::MatrixStack::modelview() = GL::Matrix::translation(-500, -348, 0);
+       GL::MatrixStack::modelview() *= GL::Matrix::scaling(10);
        font->draw_string(format("%d hosts", hosts.size()+disabled_hosts.size()));
-       GL::translate(0, -1.2, 0);
+       GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -1.2, 0);
+       font->draw_string(format("%d ports", ports.size()));
+       GL::MatrixStack::modelview() *= GL::Matrix::translation(0, -1.2, 0);
        font->draw_string(format("%.2f fps", fps));
        GL::Texture::unbind();
-       GL::pop_matrix();
 
-       GL::push_matrix();
-       GL::translate(170, -370, 0);
+       GL::MatrixStack::modelview() = GL::Matrix::translation(170, -370, 0);
        history->render();
-       GL::pop_matrix();
 }
 
 Host &NetVis::get_host(unsigned a)
@@ -256,7 +268,7 @@ Host &NetVis::get_host(unsigned a)
        return *host;
 }
 
-const Port &NetVis::get_port(unsigned number)
+Port &NetVis::get_port(unsigned number)
 {
        map<unsigned, Port *>::iterator i = ports.find(number);
        if(i!=ports.end())
@@ -276,17 +288,40 @@ void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const uns
                const iphdr *ip = reinterpret_cast<const iphdr *>(eth+1);
 
                unsigned size = ntohs(ip->tot_len);
-               unsigned port = 0;
+
+               Port *sport = 0;
+               Port *dport = 0;
                if(ip->protocol==IPPROTO_TCP)
                {
                        const tcphdr *tcp = reinterpret_cast<const tcphdr *>(ip+1);
-                       port = min(ntohs(tcp->source), ntohs(tcp->dest));
+                       sport = &self->get_port(ntohs(tcp->source));
+                       dport = &self->get_port(ntohs(tcp->dest));
                }
                else if(ip->protocol==IPPROTO_UDP)
                {
                        const udphdr *udp = reinterpret_cast<const udphdr *>(ip+1);
-                       port = min(ntohs(udp->source), ntohs(udp->dest));
+                       sport = &self->get_port(ntohs(udp->source));
+                       dport = &self->get_port(ntohs(udp->dest));
+               }
+
+               Port *port = 0;
+               if(sport && dport)
+               {
+                       if(sport->is_registered()!=dport->is_registered())
+                       {
+                               if(sport->is_registered())
+                                       port = sport;
+                               else
+                                       port = dport;
+                       }
+                       else if(sport->get_number()<dport->get_number())
+                               port = sport;
+                       else
+                               port = dport;
                }
+               else
+                       port = &self->get_port(0);
+
                Host &shost = self->get_host(ntohl(ip->saddr));
                Host *dhost = 0;
                if((ntohl(ip->daddr)&0xFF)!=0xFF)
@@ -295,7 +330,7 @@ void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const uns
                float throttle = shost.send_packet();
                if(throttle<1)
                {
-                       self->packets.push_back(new Packet(shost, dhost, self->get_port(port).get_color(), size));
+                       self->packets.push_back(new Packet(shost, dhost, port->get_color(), size));
                        self->packets.back()->tick(-throttle*Msp::Time::sec);
                }
 
@@ -303,6 +338,11 @@ void NetVis::capture_handler(unsigned char *user, const pcap_pkthdr *, const uns
                if(dhost)
                        dhost->add_activity(size);
 
+               if(sport)
+                       sport->add_activity(size);
+               if(dport)
+                       dport->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)
@@ -314,5 +354,3 @@ void NetVis::sighandler(int)
 {
        exit(0);
 }
-
-Application::RegApp<NetVis> NetVis::reg;