From 06ba0cb170e61e21f9175bf44dee49b5393552e3 Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 6 Oct 2012 13:58:26 +0000 Subject: [PATCH] improved display of tunnels in network simulator git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@5643 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- .../src/tests/network_simulator/Network.cpp | 2 +- .../tests/network_simulator/NetworkViewer.cpp | 189 ++++++++++++------ .../tests/network_simulator/NetworkViewer.h | 7 +- .../src/tests/network_simulator/main.cpp | 6 +- 4 files changed, 141 insertions(+), 63 deletions(-) diff --git a/libretroshare/src/tests/network_simulator/Network.cpp b/libretroshare/src/tests/network_simulator/Network.cpp index 5da2c09ca..6b04be31d 100644 --- a/libretroshare/src/tests/network_simulator/Network.cpp +++ b/libretroshare/src/tests/network_simulator/Network.cpp @@ -40,7 +40,7 @@ bool Network::initRandom(uint32_t nb_nodes,float connexion_probability) // for(uint32_t i=0;i tunnel_nodes ; + glEnable(GL_LINE_SMOOTH) ; glBegin(GL_LINES) ; @@ -65,7 +71,11 @@ void NetworkViewer::draw() for(std::set::const_iterator it(neighs.begin());it!=neighs.end();++it) { if(traffic_info.local_src.find(_network.node(*it).id())!=traffic_info.local_src.end() || traffic_info.local_dst.find(_network.node(*it).id())!=traffic_info.local_dst.end()) + { glColor3f(0.9f,0.4f,0.2f) ; + tunnel_nodes.insert(i) ; + tunnel_nodes.insert(*it) ; + } else glColor3f(0.4f,0.4f,0.4f) ; @@ -82,21 +92,64 @@ void NetworkViewer::draw() // Draw all nodes. // glEnable(GL_POINT_SMOOTH) ; + glPointSize(20.0f) ; + glBegin(GL_POINTS) ; + + if(_current_selected_node > -1) + { + glColor4f(1.0f,0.2f,0.1f,0.7f) ; + glVertex2f(_node_coords[_current_selected_node].x, _node_coords[_current_selected_node].y) ; + } + glEnd() ; + glPointSize(10.0f) ; glBegin(GL_POINTS) ; for(uint32_t i=0;i<_network.n_nodes();++i) { - if((int)i == _current_selected_node) - glColor3f(1.0f,0.2f,0.1f) ; - else - glColor3f(0.8f,0.8f,0.8f) ; + float r = 0.8 ; + float g = 0.8 ; + float b = 0.8 ; + + if(!_network.node(i).providedHashes().empty()) + r *= 2.0, g /= 2.0, b /= 2.0f ; + + if(!_network.node(i).managedHashes().empty()) + g *= 2.0, b /= 2.0, r /= 2.0f ; + + if(tunnel_nodes.find(i) != tunnel_nodes.end() && r==0.8f && g==0.8f && b==0.8f) + r = 0.9f, g=0.4f,b=0.2f ; + + glColor3f(r,g,b) ; glVertex2f(_node_coords[i].x, _node_coords[i].y) ; } glEnd() ; + // Draw info about current node under mouse. + // + if(_current_displayed_node > -1) + { + const PeerNode& node(_network.node(_current_displayed_node)) ; + int offset = 0 ; + int text_height = 15 ; + + drawText(10+_node_coords[_current_displayed_node].x,offset + height()-_node_coords[_current_displayed_node].y, "Node id = " + QString::fromStdString(node.id())) ; + offset += text_height ; + + for(std::set::const_iterator it(node.providedHashes().begin());it!=node.providedHashes().end();++it) + { + drawText(10+_node_coords[_current_displayed_node].x,offset + height()-_node_coords[_current_displayed_node].y, "Server for hash " + QString::fromStdString(*it) ); + offset += text_height ; + } + + for(std::set::const_iterator it(node.managedHashes().begin());it!=node.managedHashes().end();++it) + { + drawText(10+_node_coords[_current_displayed_node].x,offset + height()-_node_coords[_current_displayed_node].y, "Client for hash " + QString::fromStdString(*it) ) ; + offset += text_height ; + } + } glMatrixMode(GL_MODELVIEW) ; glPopMatrix() ; @@ -222,70 +275,77 @@ void NetworkViewer::timerEvent(QTimerEvent *event) if(!isVisible()) return ; - static const int S = 256 ; - static double *forceMap = new double[2*S*S] ; - - memset(forceMap,0,2*S*S*sizeof(double)) ; - - for(uint32_t i=0;i<_network.n_nodes();++i) + if(_nodes_need_recomputing) { - float x = S*_node_coords[i].x/width() ; - float y = S*_node_coords[i].y/height() ; + std::cerr << "Updating forces..."<< std::endl; + static const int S = 256 ; + static double *forceMap = new double[2*S*S] ; - int i=(int)floor(x) ; - int j=(int)floor(y) ; + memset(forceMap,0,2*S*S*sizeof(double)) ; - float di = x-i ; - float dj = y-j ; - - if( i>=0 && i=0 && j new_coords(_node_coords) ; + float di = x-i ; + float dj = y-j ; - for(uint32_t i=0;i<_network.n_nodes();++i) - if(i != _current_selected_node || !_dragging) - { - float x = _node_coords[i].x ; - float y = _node_coords[i].y ; - - calculateForces(i,forceMap,S,S,x,y,speedf,new_coords[i].x,new_coords[i].y); + if( i>=0 && i=0 && j 0.2 || fabsf(_node_coords[i].y - new_coords[i].y) > 0.2) - itemsMoved = true; + // compute convolution with 1/omega kernel. + // + convolveWithGaussian(forceMap,S,20) ; - //std::cerr << "Old i = " << _node_coords[i].x << ", new = " << new_coords[i].x << std::endl; - _node_coords[i] = new_coords[i] ; + static float speedf=1.0f; + + std::vector new_coords(_node_coords) ; + + for(uint32_t i=0;i<_network.n_nodes();++i) + if(i != _current_selected_node || !_dragging) + { + float x = _node_coords[i].x ; + float y = _node_coords[i].y ; + + calculateForces(i,forceMap,S,S,x,y,speedf,new_coords[i].x,new_coords[i].y); + } + + bool itemsMoved = false; + for(uint32_t i=0;i<_node_coords.size();++i) + { + if( fabsf(_node_coords[i].x - new_coords[i].x) > 1.0 || fabsf(_node_coords[i].y - new_coords[i].y) > 1.0) + itemsMoved = true; + + //std::cerr << "Old i = " << _node_coords[i].x << ", new = " << new_coords[i].x << std::endl; + _node_coords[i] = new_coords[i] ; + } + + if (!itemsMoved) { + // killTimer(timerId); + //#ifdef DEBUG_ELASTIC + std::cerr << "Killing timr" << std::endl ; + _nodes_need_recomputing = false ; + //#endif + timerId = 0; + } + else + { + updateGL() ; + usleep(2000) ; + } } - - if (!itemsMoved) { - killTimer(timerId); -//#ifdef DEBUG_ELASTIC - std::cerr << "Killing timr" << std::endl ; -//#endif - timerId = 0; - } else - { updateGL() ; - usleep(2000) ; - } } @@ -296,11 +356,22 @@ void NetworkViewer::mouseMoveEvent(QMouseEvent *e) _node_coords[_current_selected_node].x = e->x() ; _node_coords[_current_selected_node].y = height() - e->y() ; - if(timerId == 0) - timerId = startTimer(1000/25) ; + _nodes_need_recomputing = true ; updateGL() ; } + + float x = e->x() ; + float y = height()-e->y() ; + + _current_displayed_node = -1 ; + + for(uint32_t i=0;i<_node_coords.size();++i) + if( pow(_node_coords[i].x-x,2)+pow(_node_coords[i].y-y,2) < 5*5) + { + _current_displayed_node = i; + break ; + } } void NetworkViewer::mouseReleaseEvent(QMouseEvent *e) diff --git a/libretroshare/src/tests/network_simulator/NetworkViewer.h b/libretroshare/src/tests/network_simulator/NetworkViewer.h index b97ac4355..e8c054d7b 100644 --- a/libretroshare/src/tests/network_simulator/NetworkViewer.h +++ b/libretroshare/src/tests/network_simulator/NetworkViewer.h @@ -60,16 +60,19 @@ class NetworkViewer: public QGLViewer std::vector _node_coords ; std::vector _node_speeds ; - static const float MASS_FACTOR = 7 ; - static const float FRICTION_FACTOR = 9.8 ; + static const float MASS_FACTOR = 10 ; + static const float FRICTION_FACTOR = 15.8 ; static const float REPULSION_FACTOR = 8 ; static const float NODE_DISTANCE = 30.0 ; int timerId ; int _current_selected_node ; + int _current_displayed_node ; bool _dragging ; + bool _nodes_need_recomputing ; QAction *action_ManageHash ; QAction *action_ProvideHash ; }; + diff --git a/libretroshare/src/tests/network_simulator/main.cpp b/libretroshare/src/tests/network_simulator/main.cpp index de5881b5e..514bc0855 100644 --- a/libretroshare/src/tests/network_simulator/main.cpp +++ b/libretroshare/src/tests/network_simulator/main.cpp @@ -17,8 +17,12 @@ int main(int argc, char *argv[]) #endif argstream as(argc,argv) ; bool show_gui = false; + int nb_nodes = 20 ; + float connexion_probability = 0.2 ; as >> option('i',"gui",show_gui,"show gui (vs. do the pipeline automatically)") + >> parameter('n',"nodes",nb_nodes,"number of nodes in the network") + >> parameter('p',"connexion probability",connexion_probability,"probability that two nodes are connected (exponential law)") >> help() ; as.defaultErrorHandling() ; @@ -27,7 +31,7 @@ int main(int argc, char *argv[]) Network network ; - network.initRandom(20,0.2) ; + network.initRandom(nb_nodes,connexion_probability) ; rsPeers = new MonitoredRsPeers(network) ;