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
This commit is contained in:
csoler 2014-04-02 20:23:17 +00:00
parent 46fd60f1e4
commit afcded6c3b
9 changed files with 102 additions and 69 deletions

View File

@ -1,8 +1,8 @@
#include "MonitoredTurtle.h" #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<std::string,FileInfo>::const_iterator it( _local_files.find(hash) ) ; std::map<RsFileHash,FileInfo>::const_iterator it( _local_files.find(hash) ) ;
if(it != _local_files.end() ) if(it != _local_files.end() )
{ {
@ -13,7 +13,7 @@ bool MonitoredTurtleRouter::performLocalHashSearch(const TurtleFileHash& hash,co
return false ; return false ;
} }
void MonitoredTurtleRouter::provideFileHash(const std::string& hash) void MonitoredTurtleRouter::provideFileHash(const RsFileHash& hash)
{ {
FileInfo& info( _local_files[hash] ) ; FileInfo& info( _local_files[hash] ) ;

View File

@ -3,21 +3,21 @@
class MonitoredTurtleRouter: public p3turtle class MonitoredTurtleRouter: public p3turtle
{ {
public: public:
MonitoredTurtleRouter(p3LinkMgr *lmgr,ftServer *fts) MonitoredTurtleRouter(p3ServiceControl *sc,p3LinkMgr *lmgr,ftServer *fts)
: p3turtle(lmgr) : p3turtle(sc,lmgr)
{ {
} }
// Overload functions that I don't want to be called for real! // 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 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 // new functions to replace somme internal functionalities
void provideFileHash(const std::string& hash) ; void provideFileHash(const RsFileHash& hash) ;
private: private:
std::map<std::string,FileInfo> _local_files ; std::map<RsFileHash,FileInfo> _local_files ;
}; };

View File

@ -4,12 +4,13 @@
#include <vector> #include <vector>
#include <list> #include <list>
#include <string.h> #include <string.h>
#include <util/rsid.h> #include <retroshare/rsids.h>
#include <retroshare/rspeers.h> #include <retroshare/rspeers.h>
#include <turtle/p3turtle.h> #include <turtle/p3turtle.h>
#include <serialiser/rsserial.h> #include <serialiser/rsserial.h>
#include <pqi/p3linkmgr.h> #include <pqi/p3linkmgr.h>
#include <pqi/p3peermgr.h>
#include <ft/ftserver.h> #include <ft/ftserver.h>
#include <ft/ftcontroller.h> #include <ft/ftcontroller.h>
#include <services/p3service.h> #include <services/p3service.h>
@ -21,18 +22,14 @@ bool Network::initRandom(uint32_t nb_nodes,float connexion_probability)
_nodes.clear() ; _nodes.clear() ;
_neighbors.clear() ; _neighbors.clear() ;
std::vector<std::string> ids ; std::vector<RsPeerId> ids ;
_neighbors.resize(nb_nodes) ; _neighbors.resize(nb_nodes) ;
ids.resize(nb_nodes) ; ids.resize(nb_nodes) ;
for(uint32_t i=0;i<nb_nodes;++i) for(uint32_t i=0;i<nb_nodes;++i)
{ {
unsigned char bytes[20] ; ids[i] = RsPeerId::random() ;
for(int k=0;k<20;++k)
bytes[k] = lrand48() & 0xff ;
ids[i] = t_RsGenericIdType<20>(bytes).toStdString(false) ;
_node_ids[ids[i]] = i ; _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::cerr << "Added new node with id " << ids[i] << std::endl;
std::list<std::string> friends ; std::list<RsPeerId> friends ;
for(std::set<uint32_t>::const_iterator it(_neighbors[i].begin());it!=_neighbors[i].end();++it) for(std::set<uint32_t>::const_iterator it(_neighbors[i].begin());it!=_neighbors[i].end();++it)
friends.push_back( ids[*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<std::string,uint32_t>::const_iterator it = _node_ids.find(id) ; std::map<RsPeerId,uint32_t>::const_iterator it = _node_ids.find(id) ;
if(it == _node_ids.end()) 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) ; return node(it->second) ;
} }
@ -106,20 +103,54 @@ PeerNode& Network::node_by_id(const std::string& id)
class FakeLinkMgr: public p3LinkMgrIMPL class FakeLinkMgr: public p3LinkMgrIMPL
{ {
public: public:
FakeLinkMgr(const std::string& own_id,const std::list<std::string>& friends) FakeLinkMgr(const RsPeerId& own_id,const std::list<RsPeerId>& friends)
: p3LinkMgrIMPL(NULL,NULL),_own_id(own_id),_friends(friends) : p3LinkMgrIMPL(NULL,NULL),_own_id(own_id),_friends(friends)
{ {
} }
virtual const std::string getOwnId() { return _own_id ; } virtual const RsPeerId& getOwnId() { return _own_id ; }
virtual void getOnlineList(std::list<std::string>& lst) { lst = _friends ; } virtual void getOnlineList(std::list<RsPeerId>& lst) { lst = _friends ; }
virtual uint32_t getLinkType(const std::string&) { return RS_NET_CONN_TCP_ALL | RS_NET_CONN_SPEED_NORMAL; } 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: private:
std::string _own_id ; RsPeerId _own_id ;
std::list<std::string> _friends ; std::list<RsPeerId> _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<RsRawItem*> _item_queue ;
};
class FakePeerMgr: public p3PeerMgrIMPL
{
public:
FakePeerMgr(const RsPeerId& own,const std::list<RsPeerId>& ids)
: p3PeerMgrIMPL(own,RsPgpId(),"no name","location name")
{
for(std::list<RsPeerId>::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<RsPeerId> _ids ;
}; };
const RsTurtle *PeerNode::turtle_service() const const RsTurtle *PeerNode::turtle_service() const
@ -127,17 +158,22 @@ const RsTurtle *PeerNode::turtle_service() const
return _turtle ; return _turtle ;
} }
PeerNode::PeerNode(const std::string& id,const std::list<std::string>& friends) PeerNode::PeerNode(const RsPeerId& id,const std::list<RsPeerId>& friends)
: _id(id) : _id(id)
{ {
// add a service server. // add a service server.
_service_server = new p3ServiceServer ;
p3LinkMgr *link_mgr = new FakeLinkMgr(id, friends) ; 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. // add a turtle router.
// //
@ -156,20 +192,20 @@ void PeerNode::tick()
void PeerNode::incoming(RsRawItem *item) void PeerNode::incoming(RsRawItem *item)
{ {
_service_server->incoming(item) ; _service_server->recvItem(item) ;
} }
RsRawItem *PeerNode::outgoing() RsRawItem *PeerNode::outgoing()
{ {
return _service_server->outgoing() ; return dynamic_cast<FakePublisher*>(_publisher)->outgoing() ;
} }
void PeerNode::provideFileHash(const std::string& hash) void PeerNode::provideFileHash(const RsFileHash& hash)
{ {
_provided_hashes.insert(hash) ; _provided_hashes.insert(hash) ;
_turtle->provideFileHash(hash) ; _turtle->provideFileHash(hash) ;
} }
void PeerNode::manageFileHash(const std::string& hash) void PeerNode::manageFileHash(const RsFileHash& hash)
{ {
_managed_hashes.insert(hash) ; _managed_hashes.insert(hash) ;
_turtle->monitorTunnels(hash,_ftserver) ; _turtle->monitorTunnels(hash,_ftserver) ;

View File

@ -22,13 +22,13 @@ class PeerNode
std::map<std::string,std::string> local_dst ; std::map<std::string,std::string> local_dst ;
}; };
PeerNode(const std::string& id,const std::list<std::string>& friends) ; PeerNode(const RsPeerId& id,const std::list<RsPeerId>& friends) ;
~PeerNode() ; ~PeerNode() ;
RsRawItem *outgoing() ; RsRawItem *outgoing() ;
void incoming(RsRawItem *) ; void incoming(RsRawItem *) ;
const std::string& id() const { return _id ;} const RsPeerId& id() const { return _id ;}
void tick() ; void tick() ;
@ -36,8 +36,8 @@ class PeerNode
// Turtle-related methods // Turtle-related methods
// //
void manageFileHash(const std::string& hash) ; void manageFileHash(const RsFileHash& hash) ;
void provideFileHash(const std::string& hash) ; void provideFileHash(const RsFileHash& hash) ;
const std::set<TurtleFileHash>& providedHashes() const { return _provided_hashes; } const std::set<TurtleFileHash>& providedHashes() const { return _provided_hashes; }
const std::set<TurtleFileHash>& managedHashes() const { return _managed_hashes; } const std::set<TurtleFileHash>& managedHashes() const { return _managed_hashes; }
@ -48,7 +48,8 @@ class PeerNode
p3ServiceServer *_service_server ; p3ServiceServer *_service_server ;
MonitoredTurtleRouter *_turtle ; MonitoredTurtleRouter *_turtle ;
ftServer *_ftserver ; ftServer *_ftserver ;
std::string _id ; pqiPublisher *_publisher ;
RsPeerId _id ;
std::set<TurtleFileHash> _provided_hashes ; std::set<TurtleFileHash> _provided_hashes ;
std::set<TurtleFileHash> _managed_hashes ; std::set<TurtleFileHash> _managed_hashes ;
@ -85,9 +86,9 @@ class Network: public Graph<PeerNode>
// //
void tick() ; void tick() ;
PeerNode& node_by_id(const std::string& node_id) ; PeerNode& node_by_id(const RsPeerId& node_id) ;
private: private:
std::map<std::string,uint32_t> _node_ids ; std::map<RsPeerId,uint32_t> _node_ids ;
}; };

View File

@ -2,7 +2,7 @@
#include <QMenu> #include <QMenu>
#include <QAction> #include <QAction>
#include <util/rsid.h> #include <retroshare/rsids.h>
#include "Network.h" #include "Network.h"
#include "NetworkViewer.h" #include "NetworkViewer.h"
@ -72,7 +72,7 @@ void NetworkViewer::draw()
for(std::set<uint32_t>::const_iterator it(neighs.begin());it!=neighs.end();++it) for(std::set<uint32_t>::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) ; glColor3f(0.9f,0.4f,0.2f) ;
tunnel_nodes.insert(i) ; tunnel_nodes.insert(i) ;
@ -137,18 +137,18 @@ void NetworkViewer::draw()
int offset = 0 ; int offset = 0 ;
int text_height = 15 ; 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 ; offset += text_height ;
for(std::set<TurtleFileHash>::const_iterator it(node.providedHashes().begin());it!=node.providedHashes().end();++it) for(std::set<TurtleFileHash>::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 ; offset += text_height ;
} }
for(std::set<TurtleFileHash>::const_iterator it(node.managedHashes().begin());it!=node.managedHashes().end();++it) for(std::set<TurtleFileHash>::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 ; offset += text_height ;
} }
} }
@ -538,7 +538,7 @@ void NetworkViewer::contextMenu(QPoint p)
for(std::set<TurtleFileHash>::const_iterator it(managed_hashes.begin());it!=managed_hashes.end();++it) for(std::set<TurtleFileHash>::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())); connect(provide_hash_action, SIGNAL(triggered()), this, SLOT(actionProvideHash()));
Mnu2->addAction(provide_hash_action); Mnu2->addAction(provide_hash_action);
} }
@ -549,7 +549,7 @@ void NetworkViewer::contextMenu(QPoint p)
for(std::set<TurtleFileHash>::const_iterator it(provided_hashes.begin());it!=provided_hashes.end();++it) for(std::set<TurtleFileHash>::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())); connect(manage_hash_action, SIGNAL(triggered()), this, SLOT(actionManageHash()));
Mnu2->addAction(manage_hash_action); Mnu2->addAction(manage_hash_action);
} }
@ -562,11 +562,11 @@ void NetworkViewer::actionManageHash()
if(_current_acted_node < 0) if(_current_acted_node < 0)
return ; return ;
std::string hash ; RsFileHash hash ;
if(qobject_cast<QAction*>(sender())->text().length() == 20) //data().toString().toStdString(); if(qobject_cast<QAction*>(sender())->text().length() == 20) //data().toString().toStdString();
{ {
hash = qobject_cast<QAction*>(sender())->text().toStdString() ; hash = RsFileHash(qobject_cast<QAction*>(sender())->text().toStdString()) ;
std::cerr << "Managing existing hash " << hash << std::endl; std::cerr << "Managing existing hash " << hash << std::endl;
} }
@ -574,11 +574,7 @@ void NetworkViewer::actionManageHash()
{ {
std::cerr << "Managing random hash..." << std::endl; std::cerr << "Managing random hash..." << std::endl;
unsigned char hash_bytes[20] ; hash = RsFileHash::random() ;
for(int i=0;i<20;++i)
hash_bytes[i] = lrand48() & 0xff ;
hash = t_RsGenericIdType<20>(hash_bytes).toStdString(false) ;
} }
@ -598,7 +594,7 @@ void NetworkViewer::actionProvideHash()
return ; return ;
std::cerr << "Providing hash " << hash.toStdString() << std::endl; 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() ; updateGL() ;
} }

View File

@ -61,9 +61,9 @@ class TRHistogram
return ; return ;
ox += 10 ; ox += 10 ;
std::map<std::string,std::vector<int> > hits ; std::map<RsPeerId,std::vector<int> > hits ;
std::map<std::string,std::vector<int> > depths ; std::map<RsPeerId,std::vector<int> > depths ;
std::map<std::string,std::vector<int> >::iterator it ; std::map<RsPeerId,std::vector<int> >::iterator it ;
int max_hits = 1; int max_hits = 1;
int max_depth = 1; int max_depth = 1;
@ -211,11 +211,11 @@ void TurtleRouterStatistics::updateDisplay()
_tst_CW->update(); _tst_CW->update();
} }
QString TurtleRouterStatistics::getPeerName(const std::string& peer_id) QString TurtleRouterStatistics::getPeerName(const RsPeerId& peer_id)
{ {
static std::map<std::string, QString> names ; static std::map<RsPeerId, QString> names ;
std::map<std::string,QString>::const_iterator it = names.find(peer_id) ; std::map<RsPeerId,QString>::const_iterator it = names.find(peer_id) ;
if( it != names.end()) if( it != names.end())
return it->second ; return it->second ;

View File

@ -37,7 +37,7 @@ class TurtleRouterStatistics: public RsAutoUpdatePage, public Ui::TurtleRouterSt
~TurtleRouterStatistics(); ~TurtleRouterStatistics();
// Cache for peer names. // 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 ; } void setTurtleRouter(const RsTurtle *turtle) { _turtle = turtle ; }

View File

@ -4,7 +4,7 @@
#include "NetworkSimulatorGUI.h" #include "NetworkSimulatorGUI.h"
#include "MonitoredRsPeers.h" #include "MonitoredRsPeers.h"
#include <QApplication> #include <QApplication>
#include <common/argstream.h> #include <util/argstream.h>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -21,8 +21,8 @@ int main(int argc, char *argv[])
float connexion_probability = 0.2 ; float connexion_probability = 0.2 ;
as >> option('i',"gui",show_gui,"show gui (vs. do the pipeline automatically)") 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('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)") >> parameter('p',"connexion probability",connexion_probability,"probability that two nodes are connected (exponential law)",false)
>> help() ; >> help() ;
as.defaultErrorHandling() ; as.defaultErrorHandling() ;

View File

@ -16,4 +16,4 @@ HEADERS = Network.h MonitoredTurtle.h NetworkViewer.h NetworkSimulatorGUI.h \
FORMS = NetworkSimulatorGUI.ui TurtleRouterStatistics.ui 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