From 4fbb098daa349a663015426d1210f1b59d0478c4 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 23 Oct 2014 16:46:09 -0700 Subject: Testnet work... getting there! --- testnet/SimNet.cpp | 43 ++++++++++---------------------------- testnet/SimNet.hpp | 15 ++++--------- testnet/SimNetSocketManager.cpp | 17 ++++++++------- testnet/SimNetSocketManager.hpp | 7 +++++-- testnet/TestEthernetTapFactory.cpp | 8 ------- testnet/TestEthernetTapFactory.hpp | 35 ------------------------------- 6 files changed, 29 insertions(+), 96 deletions(-) (limited to 'testnet') diff --git a/testnet/SimNet.cpp b/testnet/SimNet.cpp index a644a9bc..3349d5c2 100644 --- a/testnet/SimNet.cpp +++ b/testnet/SimNet.cpp @@ -40,50 +40,29 @@ SimNet::~SimNet() { } -SimNetSocketManager *newEndpoint() +SimNetSocketManager *SimNet::newEndpoint(const InetAddress &addr) { Mutex::Lock _l(_lock); if (_endpoints.size() >= ZT_SIMNET_MAX_TESTNET_SIZE) return (SimNetSocketManager *)0; + if (_endpoints.find(addr) != _endpoints.end()) + return (SimNetSocketManager *)0; - InetAddress fake; - uint32_t ip = _prng.next32(); - for(;;) { - ++ip; - ip &= 0x00ffffff; - ip |= 0x0a000000; // 10.x.x.x - if (((ip >> 16) & 0xff) == 0xff) ip ^= 0x00010000; - if (((ip >> 8) & 0xff) == 0xff) ip ^= 0x00000100; - if ((ip & 0xff) == 0xff) --ip; - if ((ip & 0xff) == 0x00) ++ip; - uint32_t ipn = Utils::hton(ip); - fake.set(&ipn,4,8); // 10.x.x.x/8 - if (_endpoints.find(fake) == _endpoints.end()) { - SimNetSocketManager *sm = &(_endpoints[fake]); - sm->_sn = this; - sm->_address = fake; - return sm; - } - } + SimNetSocketManager *sm = new SimNetSocketManager(); + sm->_sn = this; + sm->_address = addr; + _endpoints[addr] = sm; + return sm; } -SimNetSocketManager *get(const InetAddress &addr) +SimNetSocketManager *SimNet::get(const InetAddress &addr) { Mutex::Lock _l(_lock); - std::map< InetAddress,SimNetSocketManager >::iterator ep(_endpoints.find(addr)); + std::map< InetAddress,SimNetSocketManager * >::iterator ep(_endpoints.find(addr)); if (ep == _endpoints.end()) return (SimNetSocketManager *)0; - return &(ep->second); -} - -std::vector SimNet::all() -{ - std::vector a; - Mutex::Lock _l(_lock); - for (std::map< InetAddress,SimNetSocketManager >::iterator ep(_endpoints.begin());ep!=_endpoints.end();++ep) - a.push_back(&(ep->second)); - return a; + return ep->second; } } // namespace ZeroTier diff --git a/testnet/SimNet.hpp b/testnet/SimNet.hpp index 6f7c5c11..4fdd6939 100644 --- a/testnet/SimNet.hpp +++ b/testnet/SimNet.hpp @@ -34,13 +34,12 @@ #include "../node/Constants.hpp" #include "../node/InetAddress.hpp" #include "../node/Mutex.hpp" -#include "../node/CMWC4096.hpp" #include "SimNetSocketManager.hpp" #define ZT_SIMNET_MAX_TESTNET_SIZE 1048576 -namespcae ZeroTier { +namespace ZeroTier { /** * A simulated headless IP network for testing @@ -52,9 +51,9 @@ public: ~SimNet(); /** - * @return New endpoint with random IP address + * @return New endpoint or NULL on failure */ - SimNetSocketManager *newEndpoint(); + SimNetSocketManager *newEndpoint(const InetAddress &addr); /** * @param addr Address to look up @@ -62,14 +61,8 @@ public: */ SimNetSocketManager *get(const InetAddress &addr); - /** - * @return All socket managers (pointers remain safe while SimNet is running-- these aren't cleaned) - */ - std::vector all(); - private: - std::map< InetAddress,SimNetSocketManager > _endpoints; - CMWC4096 _prng; + std::map< InetAddress,SimNetSocketManager * > _endpoints; Mutex _lock; }; diff --git a/testnet/SimNetSocketManager.cpp b/testnet/SimNetSocketManager.cpp index f6f24184..c75c864f 100644 --- a/testnet/SimNetSocketManager.cpp +++ b/testnet/SimNetSocketManager.cpp @@ -26,6 +26,7 @@ */ #include "SimNetSocketManager.hpp" +#include "SimNet.hpp" #include "../node/Constants.hpp" #include "../node/Socket.hpp" @@ -73,20 +74,20 @@ bool SimNetSocketManager::send(const InetAddress &to,bool tcp,bool autoConnectTc void SimNetSocketManager::poll(unsigned long timeout,void (*handler)(const SharedPtr &,void *,const InetAddress &,Buffer &),void *arg) { { - Mutex::Lock _l(_lock); - while (!_queue.empty()) { - handler(_mySocket,arg,_queue.front().first,_queue.front().second); - _queue.pop(); + Mutex::Lock _l(_inbox_m); + while (!_inbox.empty()) { + handler(_mySocket,arg,_inbox.front().first,_inbox.front().second); + _inbox.pop(); } } if (timeout) _waitCond.wait(timeout); else _waitCond.wait(); { - Mutex::Lock _l(_lock); - while (!_queue.empty()) { - handler(_mySocket,arg,_queue.front().first,_queue.front().second); - _queue.pop(); + Mutex::Lock _l(_inbox_m); + while (!_inbox.empty()) { + handler(_mySocket,arg,_inbox.front().first,_inbox.front().second); + _inbox.pop(); } } } diff --git a/testnet/SimNetSocketManager.hpp b/testnet/SimNetSocketManager.hpp index 031cbd0c..69f49556 100644 --- a/testnet/SimNetSocketManager.hpp +++ b/testnet/SimNetSocketManager.hpp @@ -32,7 +32,7 @@ #include #include -#include "Constants.hpp" +#include "../node/Constants.hpp" #include "../node/SocketManager.hpp" #include "../node/Mutex.hpp" #include "../node/Condition.hpp" @@ -76,7 +76,10 @@ public: inline TransferStats stats(const InetAddress &peer) const { Mutex::Lock _l(_stats_m); - return _stats[peer]; + std::map< InetAddress,TransferStats >::const_iterator s(_stats.find(peer)); + if (s == _stats.end()) + return TransferStats(); + return s->second; } /** diff --git a/testnet/TestEthernetTapFactory.cpp b/testnet/TestEthernetTapFactory.cpp index c4a8a988..836f586e 100644 --- a/testnet/TestEthernetTapFactory.cpp +++ b/testnet/TestEthernetTapFactory.cpp @@ -53,10 +53,6 @@ EthernetTap *TestEthernetTapFactory::open( Mutex::Lock _l(_taps_m); _taps.insert(tap); } - { - Mutex::Lock _l(_tapsByDevice_m); - _tapsByDevice[tap->deviceName()] = tap; - } { Mutex::Lock _l(_tapsByMac_m); _tapsByMac[mac] = tap; @@ -73,10 +69,6 @@ void TestEthernetTapFactory::close(EthernetTap *tap,bool destroyPersistentDevice Mutex::Lock _l(_taps_m); _taps.erase(tapp); } - { - Mutex::Lock _l(_tapsByDevice_m); - _tapsByDevice.erase(tapp->deviceName()); - } { Mutex::Lock _l(_tapsByMac_m); _tapsByMac.erase(tapp->mac()); diff --git a/testnet/TestEthernetTapFactory.hpp b/testnet/TestEthernetTapFactory.hpp index 5327b844..57d195d2 100644 --- a/testnet/TestEthernetTapFactory.hpp +++ b/testnet/TestEthernetTapFactory.hpp @@ -68,45 +68,10 @@ public: return t->second; } - inline SharedPtr getByDevice(const std::string &dev) const - { - Mutex::Lock _l(_tapsByDevice_m); - std::map< std::string,SharedPtr >::const_iterator t(_tapsByDevice.find(dev)); - if (t == _tapsByDevice.end()) - return SharedPtr(); - return t->second; - } - - inline SharedPtr getFirst() const - { - Mutex::Lock _l(_taps_m); - if (_taps.empty()) - return SharedPtr(); - return *(_taps.begin()); - } - - inline SharedPtr getRandom() const - { - Mutex::Lock _l(_taps_m); - Mutex::Lock _l2(_prng_m); - if (_taps.empty()) - return SharedPtr(); - unsigned int x = (const_cast(&_prng))->next32() % (unsigned int)_taps.size(); - unsigned int i = 0; - for(std::set< SharedPtr >::const_iterator t(_taps.begin());t!=_taps.end();++t) { - if (i++ == x) - return *t; - } - return SharedPtr(); // never reached - } - private: std::set< SharedPtr > _taps; Mutex _taps_m; - std::map > _tapsByDevice; - Mutex _tapsByDevice_m; - std::map > _tapsByMac; Mutex _tapsByMac_m; -- cgit v1.2.3