00001 #ifndef _NODEDESCR_H__
00002 #define _NODEDESCR_H__
00003
00004 #include <cassert>
00005
00006 #include <sys/types.h>
00007
00008
00009 #include <gea/API.h>
00010 #include <gea/Time.h>
00011
00012 #include <awds/BasePacket.h>
00013 #include <awds/NodeId.h>
00014 #include <awds/beacon.h>
00015 #include <awds/settings.h>
00016
00017 #include <iostream>
00018
00019
00020 namespace awds {
00021 struct NodeDescr {
00022
00023
00024 static const int LostTrigger = 8;
00025 static bool verbose;
00026
00027 NodeId id;
00028 BasePacket *lastBeacon;
00029 gea::AbsTime lastBeaconTime;
00030 gea::Duration beaconInterval;
00031
00032 u_int32_t beaconHist;
00033 bool active;
00034 bool mpr;
00035
00036 void updateActive( ) {
00037
00038 const bool last12received = (beaconHist > 0xFFF00000UL);
00039 const bool last4lost = (beaconHist < 0x08000000UL );
00040 const bool lastBeacon2old = (lastBeaconTime <
00041 GEA.lastEventTime - gea::Duration( (double)beaconInterval
00042 * (double)LostTrigger) );
00043 if ( !active && !lastBeacon2old && last12received) {
00044 active = true;
00045 if (verbose) {
00046 GEA.dbg() << "neighbor "
00047 << Beacon(*lastBeacon).getSrc() << " became active" << std::endl;
00048 }
00049 } else if ( active && ( last4lost || lastBeacon2old ) ) {
00050 active = false;
00051 if (verbose) {
00052 GEA.dbg() << "neighbor "
00053 << Beacon(*lastBeacon).getSrc() << " became inactive "
00054 << ( last4lost ? "(last 4 beacon lost)" : "(last beacon too old)")
00055 << std::endl;
00056 }
00057 }
00058 }
00059
00060 bool isGood() {
00061 updateActive();
00062 return active;
00063 }
00064
00065
00069 bool isBidiGood(const NodeId& myId) {
00070 if (!lastBeacon) {
00071 return false;
00072 }
00073 Beacon beacon(*lastBeacon);
00074 if (!beacon.hasNeigh(myId)) {
00075 return false;
00076 }
00077 return isGood();
00078 }
00079
00080 bool isTooOld() {
00081 return lastBeaconTime + (beaconInterval * 32) < GEA.lastEventTime;
00082
00083 }
00084
00085 NodeDescr() : beaconInterval(0.) {}
00086
00087 void init(const NodeId& _id, BasePacket *p, gea::AbsTime t) {
00088 active = false;
00089 mpr = true;
00090 beaconHist = 0;
00091 beaconInterval = (double)BEACON_INTERVAL / 1000.;
00092 this->id = _id;
00093 lastBeacon = p;
00094 lastBeaconTime = t;
00095 }
00096
00097 unsigned char quality() const {
00098 unsigned long x = beaconHist;
00099
00100 unsigned char n = 0;
00101
00102
00103
00104
00105 if (x) {
00106 do {
00107 ++n;
00108 } while (0 != (x = x&(x-1)));
00109 }
00110
00111 assert(n > 0);
00112 return n;
00113 }
00114 };
00115 }
00116
00117
00118 #endif //NODEDESCR_H__
00119
00120
00121
00122
00123
00124