Add awareness of online status, and put old OS-dep utils in OSUtils.

This commit is contained in:
Adam Ierymenko 2015-04-08 17:10:21 -07:00
parent 0751eaabd8
commit e34bc961db
5 changed files with 466 additions and 7 deletions

View file

@ -280,7 +280,7 @@
/**
* Timeout for overall peer activity (measured from last receive)
*/
#define ZT_PEER_ACTIVITY_TIMEOUT ((ZT_PEER_DIRECT_PING_DELAY * 2) + ZT_PING_CHECK_INVERVAL)
#define ZT_PEER_ACTIVITY_TIMEOUT (ZT_PEER_DIRECT_PING_DELAY + (ZT_PING_CHECK_INVERVAL * 3))
/**
* Stop relaying via peers that have not responded to direct sends

View file

@ -76,6 +76,7 @@ Node::Node(
_newestVersionSeen[0] = ZEROTIER_ONE_VERSION_MAJOR;
_newestVersionSeen[1] = ZEROTIER_ONE_VERSION_MINOR;
_newestVersionSeen[2] = ZEROTIER_ONE_VERSION_REVISION;
_online = false;
std::string idtmp(dataStoreGet("identity.secret"));
if ((!idtmp.length())||(!RR->identity.fromString(idtmp))||(!RR->identity.hasPrivate())) {
@ -187,19 +188,19 @@ class _PingPeersThatNeedPing
{
public:
_PingPeersThatNeedPing(const RuntimeEnvironment *renv,uint64_t now) :
lastReceiveFromSupernode(0),
lastReceiveFromUpstream(0),
RR(renv),
_now(now),
_supernodes(RR->topology->supernodeAddresses()) {}
uint64_t lastReceiveFromSupernode;
uint64_t lastReceiveFromUpstream;
inline void operator()(Topology &t,const SharedPtr<Peer> &p)
{
if (std::find(_supernodes.begin(),_supernodes.end(),p->address()) != _supernodes.end()) {
p->doPingAndKeepalive(RR,_now);
if (p->lastReceive() > lastReceiveFromSupernode)
lastReceiveFromSupernode = p->lastReceive();
if (p->lastReceive() > lastReceiveFromUpstream)
lastReceiveFromUpstream = p->lastReceive();
} else if (p->alive(_now)) {
p->doPingAndKeepalive(RR,_now);
}
@ -218,6 +219,8 @@ ZT1_ResultCode Node::processBackgroundTasks(uint64_t now,uint64_t *nextBackgroun
if ((now - _lastPingCheck) >= ZT_PING_CHECK_INVERVAL) {
_lastPingCheck = now;
// This is used as a floor for the desperation and online status
// calculations if we just started up or have been asleep.
if ((now - _startTimeAfterInactivity) > (ZT_PING_CHECK_INVERVAL * 3))
_startTimeAfterInactivity = now;
@ -225,7 +228,12 @@ ZT1_ResultCode Node::processBackgroundTasks(uint64_t now,uint64_t *nextBackgroun
_PingPeersThatNeedPing pfunc(RR,now);
RR->topology->eachPeer<_PingPeersThatNeedPing &>(pfunc);
_coreDesperation = (unsigned int)((now - std::max(_startTimeAfterInactivity,pfunc.lastReceiveFromSupernode)) / (ZT_PING_CHECK_INVERVAL * ZT_CORE_DESPERATION_INCREMENT));
const uint64_t lastActivityAgo = now - std::max(_startTimeAfterInactivity,pfunc.lastReceiveFromUpstream);
_coreDesperation = (unsigned int)(lastActivityAgo / (ZT_PING_CHECK_INVERVAL * ZT_CORE_DESPERATION_INCREMENT));
bool oldOnline = _online;
_online = (lastActivityAgo < ZT_PEER_ACTIVITY_TIMEOUT);
if (oldOnline != _online)
postEvent(_online ? ZT1_EVENT_ONLINE : ZT1_EVENT_OFFLINE);
} catch ( ... ) {
return ZT1_RESULT_FATAL_ERROR_INTERNAL;
}

View file

@ -172,7 +172,19 @@ public:
}
/**
* @return Overall system level of desperation based on how long it's been since an upstream node (supernode) has talked to us
* Get an overall current level of desperation
*
* The current level of desperation is based on how recently an upstream
* (a.k.a. supernode) peer has spoken to us. As such, it will change and
* return to 0 once something like tunneling (higher desperation link) is
* active. As a result, actual link desperation for outgoing messages
* should be the max of either this or the most recent link desperation
* for an incoming message from a given address. See Path.hpp and Peer.hpp.
*
* In other words think of this as 'the desperation we should try to
* escalate to right now.'
*
* @return Overall system level of desperation
*/
inline unsigned int coreDesperation() const throw() { return _coreDesperation; }
@ -198,6 +210,11 @@ public:
*/
inline int configureVirtualNetworkPort(uint64_t nwid,ZT1_VirtualNetworkConfigOperation op,const ZT1_VirtualNetworkConfig *nc) { return _virtualNetworkConfigFunction(reinterpret_cast<ZT1_Node *>(this),nwid,op,nc); }
/**
* @return True if we appear to be online
*/
inline bool online() const throw() { return _online; }
/**
* If this version is newer than the newest we've seen, post a new version seen event
*/
@ -231,6 +248,7 @@ private:
uint64_t _lastHousekeepingRun;
unsigned int _coreDesperation;
unsigned int _newestVersionSeen[3]; // major, minor, revision
bool _online;
};
} // namespace ZeroTier