#include <linux/if_ether.h>
#include <msp/core/except.h>
#include <msp/debug/profilingscope.h>
+#include <msp/gl/blend.h>
+#include <msp/gl/framebuffer.h>
#include <msp/gl/immediate.h>
#include <msp/gl/matrix.h>
#include <msp/gl/misc.h>
#include "host.h"
#include "netvis.h"
#include "packet.h"
+#include "port.h"
#include "resolver.h"
using namespace std;
wnd->signal_key_press.connect(sigc::mem_fun(this, &NetVis::key_press));
wnd->show();
- GL::enable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ GL::enable(GL::BLEND);
+ GL::blend_func(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA);
font = new GL::Font;
DataFile::load(*font, "dejavu-10.font");
{
Debug::ProfilingScope s(profiler, "render");
- glClear(GL_COLOR_BUFFER_BIT);
+ GL::clear(GL::COLOR_BUFFER_BIT);
GL::matrix_mode(GL::PROJECTION);
GL::load_identity();
i->second->render();
if(draw_labels)
{
- glColor4f(1.0, 1.0, 1.0, 1.0);
for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
i->second->render_label();
GL::Texture::unbind();
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);
GL::push_matrix();
GL::translate(-484, 361, 0);
GL::scale_uniform(10);
- glColor4f(1.0, 1.0, 1.0, 1.0);
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)
{
- font->draw_string(format("%d", i->first));
+ GL::Immediate imm2((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
+ imm.color(1.0f, 1.0f, 1.0f);
+ font->draw_string(i->second.get_name(), imm2);
GL::translate(0, -1.2, 0);
}
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)
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);
}