Merge branch '_networks_vector' of https://github.com/mwarning/ZeroTierOne into adamierymenko-dev

This commit is contained in:
Adam Ierymenko 2015-06-22 10:34:55 -07:00
commit 4aa8449fe1
2 changed files with 43 additions and 23 deletions

View File

@ -236,13 +236,14 @@ ZT1_ResultCode Node::processBackgroundTasks(uint64_t now,volatile uint64_t *next
std::vector< SharedPtr<Network> > needConfig; std::vector< SharedPtr<Network> > needConfig;
{ {
Mutex::Lock _l(_networks_m); Mutex::Lock _l(_networks_m);
for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n) { for(std::vector< SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n) {
SharedPtr<NetworkConfig> nc(n->second->config2()); SharedPtr<NetworkConfig> nc((*n)->config2());
if (((now - n->second->lastConfigUpdate()) >= ZT_NETWORK_AUTOCONF_DELAY)||(!nc)) if (((now - (*n)->lastConfigUpdate()) >= ZT_NETWORK_AUTOCONF_DELAY)||(!nc))
needConfig.push_back(n->second); needConfig.push_back(*n);
if (nc) if (nc)
networkRelays.insert(networkRelays.end(),nc->relays().begin(),nc->relays().end()); networkRelays.insert(networkRelays.end(),nc->relays().begin(),nc->relays().end());
} }
std::sort(_networks.begin(),_networks.end());
} }
// Request updated configuration for networks that need it // Request updated configuration for networks that need it
@ -310,19 +311,24 @@ ZT1_ResultCode Node::processBackgroundTasks(uint64_t now,volatile uint64_t *next
ZT1_ResultCode Node::join(uint64_t nwid) ZT1_ResultCode Node::join(uint64_t nwid)
{ {
Mutex::Lock _l(_networks_m); Mutex::Lock _l(_networks_m);
SharedPtr<Network> &nwe = _networks[nwid]; SharedPtr<Network> nw = _network(nwid);
if (!nwe) if(!nw) {
nwe = SharedPtr<Network>(new Network(RR,nwid)); _networks.push_back(SharedPtr<Network>(new Network(RR,nwid)));
std::sort(_networks.begin(),_networks.end());
}
return ZT1_RESULT_OK; return ZT1_RESULT_OK;
} }
ZT1_ResultCode Node::leave(uint64_t nwid) ZT1_ResultCode Node::leave(uint64_t nwid)
{ {
Mutex::Lock _l(_networks_m); Mutex::Lock _l(_networks_m);
std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid)); std::vector< SharedPtr<Network> >::iterator nwi = std::lower_bound(_networks.begin(), _networks.end(), nwid, NetworkComparator());
if (nw != _networks.end()) { if(nwi != _networks.end() && (*nwi)->id() == nwid) {
nw->second->destroy(); (*nwi)->destroy();
_networks.erase(nw); // erase element (replace by last)
*nwi = _networks.back();
_networks.pop_back();
std::sort(_networks.begin(),_networks.end());
} }
return ZT1_RESULT_OK; return ZT1_RESULT_OK;
} }
@ -406,10 +412,10 @@ ZT1_PeerList *Node::peers() const
ZT1_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid) const ZT1_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid) const
{ {
Mutex::Lock _l(_networks_m); Mutex::Lock _l(_networks_m);
std::map< uint64_t,SharedPtr<Network> >::const_iterator nw(_networks.find(nwid)); SharedPtr<Network> nw = _network(nwid);
if (nw != _networks.end()) { if(nw) {
ZT1_VirtualNetworkConfig *nc = (ZT1_VirtualNetworkConfig *)::malloc(sizeof(ZT1_VirtualNetworkConfig)); ZT1_VirtualNetworkConfig *nc = (ZT1_VirtualNetworkConfig *)::malloc(sizeof(ZT1_VirtualNetworkConfig));
nw->second->externalConfig(nc); nw->externalConfig(nc);
return nc; return nc;
} }
return (ZT1_VirtualNetworkConfig *)0; return (ZT1_VirtualNetworkConfig *)0;
@ -426,8 +432,8 @@ ZT1_VirtualNetworkList *Node::networks() const
nl->networks = (ZT1_VirtualNetworkConfig *)(buf + sizeof(ZT1_VirtualNetworkList)); nl->networks = (ZT1_VirtualNetworkConfig *)(buf + sizeof(ZT1_VirtualNetworkList));
nl->networkCount = 0; nl->networkCount = 0;
for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n) for(std::vector< SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
n->second->externalConfig(&(nl->networks[nl->networkCount++])); (*n)->externalConfig(&(nl->networks[nl->networkCount++]));
return nl; return nl;
} }

View File

@ -155,19 +155,16 @@ public:
len); len);
} }
inline SharedPtr<Network> network(uint64_t nwid) inline SharedPtr<Network> network(uint64_t nwid) const
{ {
Mutex::Lock _l(_networks_m); Mutex::Lock _l(_networks_m);
std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid)); return _network(nwid);
return ((nw == _networks.end()) ? SharedPtr<Network>() : nw->second);
} }
inline std::vector< SharedPtr<Network> > allNetworks() const inline std::vector< SharedPtr<Network> > allNetworks() const
{ {
Mutex::Lock _l(_networks_m); Mutex::Lock _l(_networks_m);
std::vector< SharedPtr<Network> > nw; std::vector< SharedPtr<Network> > nw(_networks);
for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
nw.push_back(n->second);
return nw; return nw;
} }
@ -208,6 +205,23 @@ public:
#endif #endif
private: private:
// for binary search on _networks
struct NetworkComparator {
bool operator()(const SharedPtr<Network> &n,uint64_t nwid) const {
return n->id() < nwid;
}
};
inline SharedPtr<Network> _network(uint64_t nwid) const
{
std::vector< SharedPtr<Network> >::const_iterator iter = std::lower_bound(_networks.begin(), _networks.end(), nwid, NetworkComparator());
if(iter != _networks.end() && (*iter)->id() == nwid) {
return *iter;
} else {
return SharedPtr<Network>();
}
}
RuntimeEnvironment _RR; RuntimeEnvironment _RR;
RuntimeEnvironment *RR; RuntimeEnvironment *RR;
@ -223,7 +237,7 @@ private:
//Dictionary _localConfig; // persisted as local.conf //Dictionary _localConfig; // persisted as local.conf
//Mutex _localConfig_m; //Mutex _localConfig_m;
std::map< uint64_t,SharedPtr<Network> > _networks; std::vector< SharedPtr<Network> > _networks;
Mutex _networks_m; Mutex _networks_m;
Mutex _backgroundTasksLock; Mutex _backgroundTasksLock;