From afcded6c3baf710d6df778550d31cb9de68d16db Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 2 Apr 2014 20:23:17 +0000 Subject: [PATCH] updated network simulator to new class layout and ids git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@7224 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- .../network_simulator/MonitoredTurtle.cpp | 6 +- .../tests/network_simulator/MonitoredTurtle.h | 12 +-- .../src/tests/network_simulator/Network.cpp | 90 +++++++++++++------ .../src/tests/network_simulator/Network.h | 15 ++-- .../tests/network_simulator/NetworkViewer.cpp | 26 +++--- .../TurtleRouterStatistics.cpp | 12 +-- .../TurtleRouterStatistics.h | 2 +- .../src/tests/network_simulator/main.cpp | 6 +- .../src/tests/network_simulator/network.pro | 2 +- 9 files changed, 102 insertions(+), 69 deletions(-) diff --git a/libretroshare/src/tests/network_simulator/MonitoredTurtle.cpp b/libretroshare/src/tests/network_simulator/MonitoredTurtle.cpp index 72b933d3c..4707626c1 100644 --- a/libretroshare/src/tests/network_simulator/MonitoredTurtle.cpp +++ b/libretroshare/src/tests/network_simulator/MonitoredTurtle.cpp @@ -1,8 +1,8 @@ #include "MonitoredTurtle.h" -bool MonitoredTurtleRouter::performLocalHashSearch(const TurtleFileHash& hash,const std::string& peer_id,FileInfo& info) +bool MonitoredTurtleRouter::performLocalHashSearch(const TurtleFileHash& hash,const RsPeerId& peer_id,FileInfo& info) { - std::map::const_iterator it( _local_files.find(hash) ) ; + std::map::const_iterator it( _local_files.find(hash) ) ; if(it != _local_files.end() ) { @@ -13,7 +13,7 @@ bool MonitoredTurtleRouter::performLocalHashSearch(const TurtleFileHash& hash,co return false ; } -void MonitoredTurtleRouter::provideFileHash(const std::string& hash) +void MonitoredTurtleRouter::provideFileHash(const RsFileHash& hash) { FileInfo& info( _local_files[hash] ) ; diff --git a/libretroshare/src/tests/network_simulator/MonitoredTurtle.h b/libretroshare/src/tests/network_simulator/MonitoredTurtle.h index f7895d5f3..b7f9187f7 100644 --- a/libretroshare/src/tests/network_simulator/MonitoredTurtle.h +++ b/libretroshare/src/tests/network_simulator/MonitoredTurtle.h @@ -3,21 +3,21 @@ class MonitoredTurtleRouter: public p3turtle { public: - MonitoredTurtleRouter(p3LinkMgr *lmgr,ftServer *fts) - : p3turtle(lmgr) + MonitoredTurtleRouter(p3ServiceControl *sc,p3LinkMgr *lmgr,ftServer *fts) + : p3turtle(sc,lmgr) { } // Overload functions that I don't want to be called for real! - virtual bool loadConfiguration(std::string& loadHash) { return true ;} + virtual bool loadConfiguration(RsFileHash& loadHash) { return true ;} virtual bool saveConfiguration() { return true ;} - virtual bool performLocalHashSearch(const TurtleFileHash& hash,const std::string& peer_id,FileInfo& info) ; + virtual bool performLocalHashSearch(const TurtleFileHash& hash,const RsPeerId& peer_id,FileInfo& info) ; // new functions to replace somme internal functionalities - void provideFileHash(const std::string& hash) ; + void provideFileHash(const RsFileHash& hash) ; private: - std::map _local_files ; + std::map _local_files ; }; diff --git a/libretroshare/src/tests/network_simulator/Network.cpp b/libretroshare/src/tests/network_simulator/Network.cpp index 2dd1a928a..a6853fc26 100644 --- a/libretroshare/src/tests/network_simulator/Network.cpp +++ b/libretroshare/src/tests/network_simulator/Network.cpp @@ -4,12 +4,13 @@ #include #include #include -#include +#include #include #include #include #include +#include #include #include #include @@ -21,18 +22,14 @@ bool Network::initRandom(uint32_t nb_nodes,float connexion_probability) _nodes.clear() ; _neighbors.clear() ; - std::vector ids ; + std::vector ids ; _neighbors.resize(nb_nodes) ; ids.resize(nb_nodes) ; for(uint32_t i=0;i(bytes).toStdString(false) ; + ids[i] = RsPeerId::random() ; _node_ids[ids[i]] = i ; } @@ -57,7 +54,7 @@ bool Network::initRandom(uint32_t nb_nodes,float connexion_probability) { std::cerr << "Added new node with id " << ids[i] << std::endl; - std::list friends ; + std::list friends ; for(std::set::const_iterator it(_neighbors[i].begin());it!=_neighbors[i].end();++it) friends.push_back( ids[*it] ) ; @@ -93,12 +90,12 @@ void Network::tick() } } -PeerNode& Network::node_by_id(const std::string& id) +PeerNode& Network::node_by_id(const RsPeerId& id) { - std::map::const_iterator it = _node_ids.find(id) ; + std::map::const_iterator it = _node_ids.find(id) ; if(it == _node_ids.end()) - throw std::runtime_error("Error. Bad id passed to node_by_id ("+id+")") ; + throw std::runtime_error("Error. Bad id passed to node_by_id ("+id.toStdString()+")") ; return node(it->second) ; } @@ -106,20 +103,54 @@ PeerNode& Network::node_by_id(const std::string& id) class FakeLinkMgr: public p3LinkMgrIMPL { public: - FakeLinkMgr(const std::string& own_id,const std::list& friends) + FakeLinkMgr(const RsPeerId& own_id,const std::list& friends) : p3LinkMgrIMPL(NULL,NULL),_own_id(own_id),_friends(friends) { } - virtual const std::string getOwnId() { return _own_id ; } - virtual void getOnlineList(std::list& lst) { lst = _friends ; } - virtual uint32_t getLinkType(const std::string&) { return RS_NET_CONN_TCP_ALL | RS_NET_CONN_SPEED_NORMAL; } + virtual const RsPeerId& getOwnId() { return _own_id ; } + virtual void getOnlineList(std::list& lst) { lst = _friends ; } + virtual uint32_t getLinkType(const RsPeerId&) { return RS_NET_CONN_TCP_ALL | RS_NET_CONN_SPEED_NORMAL; } - virtual bool getPeerName(const std::string &ssl_id, std::string &name) { name = ssl_id ; return true ;} + virtual bool getPeerName(const RsPeerId &ssl_id, std::string &name) { name = ssl_id.toStdString() ; return true ;} private: - std::string _own_id ; - std::list _friends ; + RsPeerId _own_id ; + std::list _friends ; +}; + +class FakePublisher: public pqiPublisher +{ + public: + virtual bool sendItem(RsRawItem *item) + { + _item_queue.push_back(item) ; + } + + RsRawItem *outgoing() + { + RsRawItem *item = _item_queue.front() ; + _item_queue.pop_front() ; + return item ; + } + + private: + std::list _item_queue ; +}; + +class FakePeerMgr: public p3PeerMgrIMPL +{ + public: + FakePeerMgr(const RsPeerId& own,const std::list& ids) + : p3PeerMgrIMPL(own,RsPgpId(),"no name","location name") + { + for(std::list::const_iterator it(ids.begin());it!=ids.end();++it) + _ids.insert(*it) ; + } + + virtual bool idFriend(const RsPeerId& ssl_id) { return _ids.find(ssl_id) != _ids.end() ; } + + std::set _ids ; }; const RsTurtle *PeerNode::turtle_service() const @@ -127,17 +158,22 @@ const RsTurtle *PeerNode::turtle_service() const return _turtle ; } -PeerNode::PeerNode(const std::string& id,const std::list& friends) +PeerNode::PeerNode(const RsPeerId& id,const std::list& friends) : _id(id) { // add a service server. - _service_server = new p3ServiceServer ; - p3LinkMgr *link_mgr = new FakeLinkMgr(id, friends) ; - ftServer *ft_server = new ftServer(NULL,link_mgr) ; + p3PeerMgr *peer_mgr = new FakePeerMgr(id, friends) ; - _service_server->addService(_turtle = new MonitoredTurtleRouter(link_mgr,ft_server)) ; + _publisher = new FakePublisher ; + p3ServiceControl *ctrl = new p3ServiceControl(link_mgr) ; + + _service_server = new p3ServiceServer(_publisher,ctrl); + + ftServer *ft_server = new ftServer(peer_mgr,ctrl) ; + + _service_server->addService(_turtle = new MonitoredTurtleRouter(ctrl,link_mgr,ft_server),true) ; // add a turtle router. // @@ -156,20 +192,20 @@ void PeerNode::tick() void PeerNode::incoming(RsRawItem *item) { - _service_server->incoming(item) ; + _service_server->recvItem(item) ; } RsRawItem *PeerNode::outgoing() { - return _service_server->outgoing() ; + return dynamic_cast(_publisher)->outgoing() ; } -void PeerNode::provideFileHash(const std::string& hash) +void PeerNode::provideFileHash(const RsFileHash& hash) { _provided_hashes.insert(hash) ; _turtle->provideFileHash(hash) ; } -void PeerNode::manageFileHash(const std::string& hash) +void PeerNode::manageFileHash(const RsFileHash& hash) { _managed_hashes.insert(hash) ; _turtle->monitorTunnels(hash,_ftserver) ; diff --git a/libretroshare/src/tests/network_simulator/Network.h b/libretroshare/src/tests/network_simulator/Network.h index e3805038f..c799eb536 100644 --- a/libretroshare/src/tests/network_simulator/Network.h +++ b/libretroshare/src/tests/network_simulator/Network.h @@ -22,13 +22,13 @@ class PeerNode std::map local_dst ; }; - PeerNode(const std::string& id,const std::list& friends) ; + PeerNode(const RsPeerId& id,const std::list& friends) ; ~PeerNode() ; RsRawItem *outgoing() ; void incoming(RsRawItem *) ; - const std::string& id() const { return _id ;} + const RsPeerId& id() const { return _id ;} void tick() ; @@ -36,8 +36,8 @@ class PeerNode // Turtle-related methods // - void manageFileHash(const std::string& hash) ; - void provideFileHash(const std::string& hash) ; + void manageFileHash(const RsFileHash& hash) ; + void provideFileHash(const RsFileHash& hash) ; const std::set& providedHashes() const { return _provided_hashes; } const std::set& managedHashes() const { return _managed_hashes; } @@ -48,7 +48,8 @@ class PeerNode p3ServiceServer *_service_server ; MonitoredTurtleRouter *_turtle ; ftServer *_ftserver ; - std::string _id ; + pqiPublisher *_publisher ; + RsPeerId _id ; std::set _provided_hashes ; std::set _managed_hashes ; @@ -85,9 +86,9 @@ class Network: public Graph // void tick() ; - PeerNode& node_by_id(const std::string& node_id) ; + PeerNode& node_by_id(const RsPeerId& node_id) ; private: - std::map _node_ids ; + std::map _node_ids ; }; diff --git a/libretroshare/src/tests/network_simulator/NetworkViewer.cpp b/libretroshare/src/tests/network_simulator/NetworkViewer.cpp index be0c500f9..8352684b4 100644 --- a/libretroshare/src/tests/network_simulator/NetworkViewer.cpp +++ b/libretroshare/src/tests/network_simulator/NetworkViewer.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include "Network.h" #include "NetworkViewer.h" @@ -72,7 +72,7 @@ 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()) + if(traffic_info.local_src.find(_network.node(*it).id().toStdString())!=traffic_info.local_src.end() || traffic_info.local_dst.find(_network.node(*it).id().toStdString())!=traffic_info.local_dst.end()) { glColor3f(0.9f,0.4f,0.2f) ; tunnel_nodes.insert(i) ; @@ -137,18 +137,18 @@ void NetworkViewer::draw() 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())) ; + drawText(10+_node_coords[_current_displayed_node].x,offset + height()-_node_coords[_current_displayed_node].y, "Node id = " + QString::fromStdString(node.id().toStdString())) ; 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) ); + drawText(10+_node_coords[_current_displayed_node].x,offset + height()-_node_coords[_current_displayed_node].y, "Server for hash " + QString::fromStdString((*it).toStdString()) ); 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) ) ; + drawText(10+_node_coords[_current_displayed_node].x,offset + height()-_node_coords[_current_displayed_node].y, "Client for hash " + QString::fromStdString((*it).toStdString()) ) ; offset += text_height ; } } @@ -538,7 +538,7 @@ void NetworkViewer::contextMenu(QPoint p) for(std::set::const_iterator it(managed_hashes.begin());it!=managed_hashes.end();++it) { - QAction* provide_hash_action = new QAction(QString::fromStdString(*it), Mnu2); + QAction* provide_hash_action = new QAction(QString::fromStdString((*it).toStdString()), Mnu2); connect(provide_hash_action, SIGNAL(triggered()), this, SLOT(actionProvideHash())); Mnu2->addAction(provide_hash_action); } @@ -549,7 +549,7 @@ void NetworkViewer::contextMenu(QPoint p) for(std::set::const_iterator it(provided_hashes.begin());it!=provided_hashes.end();++it) { - QAction* manage_hash_action = new QAction(QString::fromStdString(*it), Mnu2); + QAction* manage_hash_action = new QAction(QString::fromStdString((*it).toStdString()), Mnu2); connect(manage_hash_action, SIGNAL(triggered()), this, SLOT(actionManageHash())); Mnu2->addAction(manage_hash_action); } @@ -562,11 +562,11 @@ void NetworkViewer::actionManageHash() if(_current_acted_node < 0) return ; - std::string hash ; + RsFileHash hash ; if(qobject_cast(sender())->text().length() == 20) //data().toString().toStdString(); { - hash = qobject_cast(sender())->text().toStdString() ; + hash = RsFileHash(qobject_cast(sender())->text().toStdString()) ; std::cerr << "Managing existing hash " << hash << std::endl; } @@ -574,11 +574,7 @@ void NetworkViewer::actionManageHash() { std::cerr << "Managing random hash..." << std::endl; - unsigned char hash_bytes[20] ; - for(int i=0;i<20;++i) - hash_bytes[i] = lrand48() & 0xff ; - - hash = t_RsGenericIdType<20>(hash_bytes).toStdString(false) ; + hash = RsFileHash::random() ; } @@ -598,7 +594,7 @@ void NetworkViewer::actionProvideHash() return ; std::cerr << "Providing hash " << hash.toStdString() << std::endl; - _network.node(_current_acted_node).provideFileHash(hash.toStdString()) ; + _network.node(_current_acted_node).provideFileHash(RsFileHash(hash.toStdString())) ; updateGL() ; } diff --git a/libretroshare/src/tests/network_simulator/TurtleRouterStatistics.cpp b/libretroshare/src/tests/network_simulator/TurtleRouterStatistics.cpp index 1b04a0dc2..299a37156 100644 --- a/libretroshare/src/tests/network_simulator/TurtleRouterStatistics.cpp +++ b/libretroshare/src/tests/network_simulator/TurtleRouterStatistics.cpp @@ -61,9 +61,9 @@ class TRHistogram return ; ox += 10 ; - std::map > hits ; - std::map > depths ; - std::map >::iterator it ; + std::map > hits ; + std::map > depths ; + std::map >::iterator it ; int max_hits = 1; int max_depth = 1; @@ -211,11 +211,11 @@ void TurtleRouterStatistics::updateDisplay() _tst_CW->update(); } -QString TurtleRouterStatistics::getPeerName(const std::string& peer_id) +QString TurtleRouterStatistics::getPeerName(const RsPeerId& peer_id) { - static std::map names ; + static std::map names ; - std::map::const_iterator it = names.find(peer_id) ; + std::map::const_iterator it = names.find(peer_id) ; if( it != names.end()) return it->second ; diff --git a/libretroshare/src/tests/network_simulator/TurtleRouterStatistics.h b/libretroshare/src/tests/network_simulator/TurtleRouterStatistics.h index a6c3be5af..ebff4bf69 100644 --- a/libretroshare/src/tests/network_simulator/TurtleRouterStatistics.h +++ b/libretroshare/src/tests/network_simulator/TurtleRouterStatistics.h @@ -37,7 +37,7 @@ class TurtleRouterStatistics: public RsAutoUpdatePage, public Ui::TurtleRouterSt ~TurtleRouterStatistics(); // Cache for peer names. - static QString getPeerName(const std::string& peer_id) ; + static QString getPeerName(const RsPeerId& peer_id) ; void setTurtleRouter(const RsTurtle *turtle) { _turtle = turtle ; } diff --git a/libretroshare/src/tests/network_simulator/main.cpp b/libretroshare/src/tests/network_simulator/main.cpp index 1b9cf2e0e..e7702ffbe 100644 --- a/libretroshare/src/tests/network_simulator/main.cpp +++ b/libretroshare/src/tests/network_simulator/main.cpp @@ -4,7 +4,7 @@ #include "NetworkSimulatorGUI.h" #include "MonitoredRsPeers.h" #include -#include +#include int main(int argc, char *argv[]) { @@ -21,8 +21,8 @@ int main(int argc, char *argv[]) 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)") + >> parameter('n',"nodes",nb_nodes,"number of nodes in the network",false) + >> parameter('p',"connexion probability",connexion_probability,"probability that two nodes are connected (exponential law)",false) >> help() ; as.defaultErrorHandling() ; diff --git a/libretroshare/src/tests/network_simulator/network.pro b/libretroshare/src/tests/network_simulator/network.pro index 805a756a2..b294968ec 100644 --- a/libretroshare/src/tests/network_simulator/network.pro +++ b/libretroshare/src/tests/network_simulator/network.pro @@ -16,4 +16,4 @@ HEADERS = Network.h MonitoredTurtle.h NetworkViewer.h NetworkSimulatorGUI.h \ FORMS = NetworkSimulatorGUI.ui TurtleRouterStatistics.ui -LIBS *= ../../lib/libretroshare.a ../../../../libbitdht/src/lib/libbitdht.a ../../../../openpgpsdk/src/lib/libops.a -lgnome-keyring -lupnp -lssl -lcrypto -lbz2 -lixml +LIBS *= ../../lib/libretroshare.a ../../../../libbitdht/src/lib/libbitdht.a ../../../../../lib/sqlcipher/.libs/libsqlcipher.a ../../../../openpgpsdk/src/lib/libops.a -lgnome-keyring -lupnp -lssl -lcrypto -lbz2 -lixml