00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #ifndef ns_gaf_h
00050 #define ns_gaf_h
00051
00052 #include <assert.h>
00053 #include "agent.h"
00054 #include "node.h"
00055
00056 #define MAXQUEUE 1 // 1 = only my grid, 5 = all of my neighbos
00057 #define GN_UPDATE_INTERVAL 10 // how often to update neighbor list
00058 #define GAF_STARTUP_JITTER 1.0 // secs to jitter start of periodic activity
00059 #define GAF_NONSTART_JITTER 3.0
00060 #define MIN_DISCOVERY_TIME 1 // min interval to send discovery
00061 #define MAX_DISCOVERY_TIME 15 // max interval to send discovery
00062 #define MIN_SELECT_TIME 5 // start to tell your neighbor that
00063 #define MAX_SELECT_TIME 6 // you are the leader b/w, my need
00064
00065 #define MAX_DISCOVERY 10 // send selection after 10 time try
00066 #define MIN_LIFETIME 60
00067 #define GAF_LEADER_JITTER 3
00068 #define MIN_TURNOFFTIME 1
00069
00070 class GAFAgent;
00071
00072 typedef enum {
00073 GAF_DISCOVER, GAF_SELECT, GAF_DUTY
00074 } GafMsgType;
00075
00076 typedef enum {
00077 GAF_FREE, GAF_LEADER, GAF_SLEEP
00078 } GafNodeState;
00079
00080
00081
00082
00083
00084 struct DiscoveryMsg {
00085 u_int32_t gid;
00086 u_int32_t nid;
00087 u_int32_t state;
00088 u_int32_t ttl;
00089 u_int32_t stime;
00090
00091 };
00092
00093
00094
00095
00096 struct hdr_gaf {
00097
00098
00099 int seqno_;
00100 GafMsgType type_;
00101
00102
00103 static int offset_;
00104 inline static int& offset() { return offset_; }
00105 inline static hdr_gaf* access(const Packet* p) {
00106 return (hdr_gaf*) p->access(offset_);
00107 }
00108 };
00109
00110
00111
00112
00113 class GAFDiscoverTimer : public TimerHandler {
00114 public:
00115 GAFDiscoverTimer(GAFAgent *a) : TimerHandler(), a_(a) { }
00116 protected:
00117 virtual void expire(Event *);
00118 GAFAgent *a_;
00119 };
00120
00121
00122
00123 class GAFSelectTimer : public TimerHandler {
00124 public:
00125 GAFSelectTimer(GAFAgent *a) : TimerHandler(), a_(a) { }
00126 protected:
00127 virtual void expire(Event *);
00128 GAFAgent *a_;
00129 };
00130
00131
00132
00133
00134 class GAFDutyTimer : public TimerHandler {
00135 public:
00136 GAFDutyTimer(GAFAgent *a) : TimerHandler(), a_(a) { }
00137 protected:
00138 inline void expire(Event *);
00139 GAFAgent *a_;
00140 };
00141
00142 class GAFAgent : public Agent {
00143 public:
00144 GAFAgent(nsaddr_t id);
00145 virtual void recv(Packet *, Handler *);
00146 void timeout(GafMsgType);
00147
00148
00149 u_int32_t nodeid() {return nid_;}
00150 double myttl();
00151
00152 protected:
00153 int command(int argc, const char*const*argv);
00154
00155
00156 void node_on();
00157 void node_off();
00158 void duty_timeout();
00159 void send_discovery();
00160 void makeUpDiscoveryMsg(Packet *p);
00161 void processDiscoveryMsg(Packet *p);
00162 void schedule_wakeup(struct DiscoveryMsg);
00163 double beacon_;
00164 void setGAFstate(GafNodeState);
00165 int randomflag_;
00166 GAFDiscoverTimer timer_;
00167 GAFSelectTimer stimer_;
00168 GAFDutyTimer dtimer_;
00169 int seqno_;
00170 int gid_;
00171 int nid_;
00172 Node *thisnode;
00173 int maxttl_;
00174
00175 GafNodeState state_;
00176 int leader_settime_;
00177 int adapt_mobility_;
00178
00179
00180 };
00181
00182
00183
00184 class GAFPartner : public Connector {
00185 public:
00186 GAFPartner();
00187 void recv(Packet *p, Handler *h);
00188
00189 protected:
00190 int command(int argc, const char*const*argv);
00191 ns_addr_t here_;
00192 int gafagent_;
00193 int mask_;
00194 int shift_;
00195 };
00196
00197
00198
00199 #endif