]> git.tdb.fi Git - netvis.git/commitdiff
Resolve port names
authorMikko Rasa <tdb@tdb.fi>
Sat, 24 Oct 2009 09:55:50 +0000 (09:55 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sat, 24 Oct 2009 09:55:50 +0000 (09:55 +0000)
Better algorithm for generating colors

source/netvis.cpp
source/netvis.h
source/port.cpp [new file with mode: 0644]
source/port.h [new file with mode: 0644]

index 7f0ac9e6b5de33f0d9fec3ca86bd5721ebeab22f..cece749e9be6a2ab6003e5d02447673a5ad2d983 100644 (file)
@@ -29,6 +29,7 @@ Distributed unter the GPL
 #include "host.h"
 #include "netvis.h"
 #include "packet.h"
+#include "port.h"
 #include "resolver.h"
 
 using namespace std;
@@ -211,9 +212,9 @@ void NetVis::tick()
                GL::push_matrix();
                GL::translate(-500, 360, 0);
                unsigned n = 0;
-               for(map<unsigned, GL::Color>::iterator i=port_colors.begin(); (i!=port_colors.end() && n<20); ++i, ++n)
+               for(map<unsigned, Port>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i, ++n)
                {
-                       GL::Color &color = i->second;
+                       const GL::Color &color = i->second.get_color();
 
                        imm.begin(GL::QUADS);
                        imm.color(color.r, color.g, color.b, color.a);
@@ -237,11 +238,11 @@ void NetVis::tick()
                        GL::translate(-484, 361, 0);
                        GL::scale_uniform(10);
                        n = 0;
-                       for(map<unsigned, GL::Color>::iterator i=port_colors.begin(); (i!=port_colors.end() && n<20); ++i, ++n)
+                       for(map<unsigned, Port>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i, ++n)
                        {
                                GL::Immediate imm2((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
                                imm.color(1.0f, 1.0f, 1.0f);
-                               font->draw_string(format("%d", i->first), imm2);
+                               font->draw_string(i->second.get_name(), imm2);
 
                                GL::translate(0, -1.2, 0);
                        }
@@ -284,23 +285,54 @@ Host &NetVis::get_host(unsigned a)
        return *host;
 }
 
-GL::Color &NetVis::get_port_color(unsigned port)
+GL::Color NetVis::generate_color(bool privileged) const
 {
-       map<unsigned, GL::Color>::iterator i = port_colors.find(port);
-       if(i!=port_colors.end())
+       GL::Color color;
+       color.r = rand()*1.0/RAND_MAX;
+       color.g = rand()*1.0/RAND_MAX;
+       color.b = rand()*1.0/RAND_MAX;
+       float high = max(max(color.r, color.g), color.b);
+       color = color*(1.0/high);
+       if(privileged)
+       {
+               float low = min(min(color.r, color.g), color.b);
+               color = (color+-low)*(1/(1-low));
+       }
+       else
+               color = color*0.6+0.4;
+       return color;
+}
+
+const Port &NetVis::get_port(unsigned number)
+{
+       map<unsigned, Port>::iterator i = ports.find(number);
+       if(i!=ports.end())
                return i->second;
 
-       GL::Color color;
-       while(1)
+       GL::Color best_color;
+       float best_score = 0;
+       for(unsigned j=0; (j<100 && best_score<1); ++j)
        {
-               color.r = rand()*1.0/RAND_MAX;
-               color.g = rand()*1.0/RAND_MAX;
-               color.b = rand()*1.0/RAND_MAX;
-               if(color.r>0.5 || color.g>0.5 || color.b>0.7)
-                       break;
+               GL::Color color = generate_color(number<1024);
+
+               float score = 2;
+               for(i=ports.begin(); i!=ports.end(); ++i)
+               {
+                       const GL::Color &other = i->second.get_color();
+                       float dr = color.r-other.r;
+                       float dg = color.g-other.g;
+                       float db = color.b-other.b;
+                       score = min(score, dr*dr+dg*dg+db*db);
+               }
+               if(score>best_score)
+               {
+                       best_score = score;
+                       best_color = color;
+               }
        }
-       color.a = 0.4f;
-       return port_colors[port] = color;
+       best_color.a = 0.4f;
+       i = ports.insert(map<unsigned, Port>::value_type(number, Port(number, best_color))).first;
+       return i->second;
 }
 
 void NetVis::key_press(unsigned key, unsigned, wchar_t)
@@ -343,7 +375,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_color(port), size));
+                       self->packets.push_back(new Packet(shost, dhost, self->get_port(port).get_color(), size));
                        self->packets.back()->tick(-throttle*Msp::Time::sec);
                }
 
index 47f5de5bd31e7b05ea80248fe1ae32f32ffa5669..fd1b73cddc57440a38e7a3fc3ad48203fd1d9a5c 100644 (file)
@@ -22,6 +22,7 @@ Distributed unter the GPL
 
 class Host;
 class Packet;
+class Port;
 class Resolver;
 
 class NetVis: public Msp::Application
@@ -41,7 +42,7 @@ private:
        std::map<unsigned, Host *> hosts;
        std::map<unsigned, Host *> disabled_hosts;
        std::list<Packet *> packets;
-       std::map<unsigned, Msp::GL::Color> port_colors;
+       std::map<unsigned, Port> ports;
        bool draw_labels;
        bool blend;
 
@@ -61,7 +62,8 @@ public:
 private:
        virtual void tick();
        Host &get_host(unsigned);
-       Msp::GL::Color &get_port_color(unsigned);
+       Msp::GL::Color generate_color(bool) const;
+       const Port &get_port(unsigned);
        void key_press(unsigned, unsigned, wchar_t);
 
        static void capture_handler(unsigned char *, const pcap_pkthdr *, const unsigned char *);
diff --git a/source/port.cpp b/source/port.cpp
new file mode 100644 (file)
index 0000000..11ccfe0
--- /dev/null
@@ -0,0 +1,27 @@
+/* $Id$
+
+This file is part of NetVis
+Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
+Distributed unter the GPL
+*/
+
+#include <netinet/in.h>
+#include <netdb.h>
+#include <msp/strings/lexicalcast.h>
+#include "port.h"
+
+Port::Port(unsigned n, const Msp::GL::Color &c):
+       number(n),
+       color(c)
+{
+       char buf[128];
+       sockaddr_in addr;
+       addr.sin_family = AF_INET;
+       addr.sin_port = ntohs(number);
+       addr.sin_addr.s_addr = 0;
+       int err = getnameinfo(reinterpret_cast<sockaddr *>(&addr), sizeof(sockaddr_in), 0, 0, buf, sizeof(buf), 0);
+       if(err==0)
+               name = buf;
+       else
+               name = Msp::lexical_cast(number);
+}
diff --git a/source/port.h b/source/port.h
new file mode 100644 (file)
index 0000000..ebc9c22
--- /dev/null
@@ -0,0 +1,29 @@
+/* $Id$
+
+This file is part of NetVis
+Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
+Distributed unter the GPL
+*/
+
+#ifndef PORT_H_
+#define PORT_H_
+
+#include <string>
+#include <msp/gl/color.h>
+
+class Port
+{
+private:
+       unsigned number;
+       std::string name;
+       Msp::GL::Color color;
+
+public:
+       Port(unsigned, const Msp::GL::Color &);
+
+       unsigned get_number() const { return number; }
+       const std::string &get_name() const { return name; }
+       const Msp::GL::Color &get_color() const { return color; }
+};
+
+#endif