Add optional function in DB change listener for member online events.

This commit is contained in:
Adam Ierymenko 2019-08-04 19:55:52 -07:00
parent 818b7e4a2e
commit a77b4ecddb
5 changed files with 38 additions and 21 deletions

View file

@ -61,6 +61,7 @@ public:
virtual void onNetworkUpdate(uint64_t networkId,const nlohmann::json &network) {} virtual void onNetworkUpdate(uint64_t networkId,const nlohmann::json &network) {}
virtual void onNetworkMemberUpdate(uint64_t networkId,uint64_t memberId,const nlohmann::json &member) {} virtual void onNetworkMemberUpdate(uint64_t networkId,uint64_t memberId,const nlohmann::json &member) {}
virtual void onNetworkMemberDeauthorize(uint64_t networkId,uint64_t memberId) {} virtual void onNetworkMemberDeauthorize(uint64_t networkId,uint64_t memberId) {}
virtual void onNetworkMemberOnline(uint64_t networkId,uint64_t memberId,const InetAddress &physicalAddress) {}
}; };
struct NetworkSummaryInfo struct NetworkSummaryInfo

View file

@ -33,7 +33,6 @@ FileDB::FileDB(const Identity &myId,const char *path) :
DB(myId,path), DB(myId,path),
_networksPath(_path + ZT_PATH_SEPARATOR_S + "network"), _networksPath(_path + ZT_PATH_SEPARATOR_S + "network"),
_tracePath(_path + ZT_PATH_SEPARATOR_S + "trace"), _tracePath(_path + ZT_PATH_SEPARATOR_S + "trace"),
_onlineChanged(false),
_running(true) _running(true)
{ {
OSUtils::mkdir(_path.c_str()); OSUtils::mkdir(_path.c_str());
@ -152,7 +151,6 @@ void FileDB::eraseNetwork(const uint64_t networkId)
_networkChanged(network,nullJson,true); _networkChanged(network,nullJson,true);
std::lock_guard<std::mutex> l(this->_online_l); std::lock_guard<std::mutex> l(this->_online_l);
this->_online.erase(networkId); this->_online.erase(networkId);
this->_onlineChanged = true;
} }
void FileDB::eraseMember(const uint64_t networkId,const uint64_t memberId) void FileDB::eraseMember(const uint64_t networkId,const uint64_t memberId)
@ -166,7 +164,6 @@ void FileDB::eraseMember(const uint64_t networkId,const uint64_t memberId)
_memberChanged(member,nullJson,true); _memberChanged(member,nullJson,true);
std::lock_guard<std::mutex> l(this->_online_l); std::lock_guard<std::mutex> l(this->_online_l);
this->_online[networkId].erase(memberId); this->_online[networkId].erase(memberId);
this->_onlineChanged = true;
} }
void FileDB::nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress) void FileDB::nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress)
@ -174,9 +171,15 @@ void FileDB::nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const
char mid[32],atmp[64]; char mid[32],atmp[64];
OSUtils::ztsnprintf(mid,sizeof(mid),"%.10llx",(unsigned long long)memberId); OSUtils::ztsnprintf(mid,sizeof(mid),"%.10llx",(unsigned long long)memberId);
physicalAddress.toString(atmp); physicalAddress.toString(atmp);
{
std::lock_guard<std::mutex> l(this->_online_l); std::lock_guard<std::mutex> l(this->_online_l);
this->_online[networkId][memberId][OSUtils::now()] = physicalAddress; this->_online[networkId][memberId][OSUtils::now()] = physicalAddress;
this->_onlineChanged = true; }
{
std::lock_guard<std::mutex> l2(_changeListeners_l);
for(auto i=_changeListeners.begin();i!=_changeListeners.end();++i)
(*i)->onNetworkMemberOnline(networkId,memberId,physicalAddress);
}
} }
} // namespace ZeroTier } // namespace ZeroTier

View file

@ -51,7 +51,6 @@ protected:
std::thread _onlineUpdateThread; std::thread _onlineUpdateThread;
std::map< uint64_t,std::map<uint64_t,std::map<int64_t,InetAddress> > > _online; std::map< uint64_t,std::map<uint64_t,std::map<int64_t,InetAddress> > > _online;
std::mutex _online_l; std::mutex _online_l;
bool _onlineChanged;
bool _running; bool _running;
}; };

View file

@ -383,6 +383,7 @@ void LFDB::eraseMember(const uint64_t networkId,const uint64_t memberId)
} }
void LFDB::nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress) void LFDB::nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress)
{
{ {
std::lock_guard<std::mutex> l(_state_l); std::lock_guard<std::mutex> l(_state_l);
auto nw = _state.find(networkId); auto nw = _state.find(networkId);
@ -396,5 +397,11 @@ void LFDB::nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const I
} }
} }
} }
{
std::lock_guard<std::mutex> l2(_changeListeners_l);
for(auto i=_changeListeners.begin();i!=_changeListeners.end();++i)
(*i)->onNetworkMemberOnline(networkId,memberId,physicalAddress);
}
}
} // namespace ZeroTier } // namespace ZeroTier

View file

@ -209,6 +209,7 @@ void PostgreSQL::eraseMember(const uint64_t networkId, const uint64_t memberId)
} }
void PostgreSQL::nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress &physicalAddress) void PostgreSQL::nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress &physicalAddress)
{
{ {
std::lock_guard<std::mutex> l(_lastOnline_l); std::lock_guard<std::mutex> l(_lastOnline_l);
std::pair<int64_t, InetAddress> &i = _lastOnline[std::pair<uint64_t,uint64_t>(networkId, memberId)]; std::pair<int64_t, InetAddress> &i = _lastOnline[std::pair<uint64_t,uint64_t>(networkId, memberId)];
@ -217,6 +218,12 @@ void PostgreSQL::nodeIsOnline(const uint64_t networkId, const uint64_t memberId,
i.second = physicalAddress; i.second = physicalAddress;
} }
} }
{
std::lock_guard<std::mutex> l2(_changeListeners_l);
for(auto i=_changeListeners.begin();i!=_changeListeners.end();++i)
(*i)->onNetworkMemberOnline(networkId,memberId,physicalAddress);
}
}
void PostgreSQL::initializeNetworks(PGconn *conn) void PostgreSQL::initializeNetworks(PGconn *conn)
{ {