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 #ifndef ns_energy_model_h_
00040 #define ns_energy_model_h_
00041
00042 #include <cstdlib>
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045 #include <assert.h>
00046
00047 #include "config.h"
00048 #include "trace.h"
00049 #include "rng.h"
00050
00051 const int CHECKFREQ = 1;
00052 const int MAX_WAITING_TIME = 11;
00053
00054 class EnergyModel;
00055
00056 class AdaptiveFidelityEntity : public Handler {
00057 public:
00058 AdaptiveFidelityEntity(EnergyModel *nid) : nid_(nid) {}
00059
00060 virtual void start();
00061 virtual void handle(Event *e);
00062
00063 virtual void adapt_it();
00064 inline void set_sleeptime(float t) {sleep_time_ = t;}
00065 inline void set_sleepseed(float t) {sleep_seed_ = t;}
00066
00067 protected:
00068 EnergyModel *nid_;
00069 Event intr;
00070 float sleep_time_;
00071 float sleep_seed_;
00072 float idle_time_;
00073 };
00074
00075 class SoftNeighborHandler : public Handler {
00076 public:
00077 SoftNeighborHandler(EnergyModel *nid) {
00078 nid_ = nid;
00079 }
00080 virtual void start();
00081 virtual void handle(Event *e);
00082 protected:
00083 EnergyModel *nid_;
00084 Event intr;
00085 };
00086
00087 class MobileNode;
00088 class EnergyModel : public TclObject {
00089 public:
00090 EnergyModel(MobileNode* n, double energy, double l1, double l2) :
00091 energy_(energy), er_(0), et_(0),ei_(0), es_(0),
00092 initialenergy_(energy),
00093 level1_(l1), level2_(l2), node_(n),
00094 sleep_mode_(0), total_sleeptime_(0), total_rcvtime_(0),
00095 total_sndtime_(0), powersavingflag_(0),
00096 last_time_gosleep(0), max_inroute_time_(300), maxttl_(5),
00097 adaptivefidelity_(1), node_on_(true)
00098 {
00099 neighbor_list.neighbor_cnt_ = 0;
00100 neighbor_list.head = NULL;
00101 }
00102
00103 inline double energy() const { return energy_; }
00104
00105 inline double et() const { return et_; }
00106 inline double er() const { return er_; }
00107 inline double ei() const { return ei_; }
00108 inline double es() const { return es_; }
00109
00110 inline double initialenergy() const { return initialenergy_; }
00111 inline double level1() const { return level1_; }
00112 inline double level2() const { return level2_; }
00113 inline void setenergy(double e) { energy_ = e; }
00114
00115 virtual void DecrTxEnergy(double txtime, double P_tx);
00116 virtual void DecrRcvEnergy(double rcvtime, double P_rcv);
00117 virtual void DecrIdleEnergy(double idletime, double P_idle);
00118
00119 virtual void DecrSleepEnergy(double sleeptime, double P_sleep);
00120 virtual void DecrTransitionEnergy(double transitiontime, double P_transition);
00121
00122 inline virtual double MaxTxtime(double P_tx) {
00123 return(energy_/P_tx);
00124 }
00125 inline virtual double MaxRcvtime(double P_rcv) {
00126 return(energy_/P_rcv);
00127 }
00128 inline virtual double MaxIdletime(double P_idle) {
00129 return(energy_/P_idle);
00130 }
00131
00132 void add_neighbor(u_int32_t);
00133 void scan_neighbor();
00134 inline int getneighbors() { return neighbor_list.neighbor_cnt_; }
00135
00136 double level1() { return level1_; }
00137 double level2() { return level2_; }
00138 inline int sleep() { return sleep_mode_; }
00139 inline int state() { return state_; }
00140 inline float state_start_time() { return state_start_time_; }
00141 inline float& max_inroute_time() { return max_inroute_time_; }
00142 inline int& adaptivefidelity() { return adaptivefidelity_; }
00143 inline int& powersavingflag() { return powersavingflag_; }
00144 inline bool& node_on() { return node_on_; }
00145 inline float& total_sndtime() { return total_sndtime_; }
00146 inline float& total_rcvtime() { return total_rcvtime_; }
00147 inline float& total_sleeptime() { return total_sleeptime_; }
00148
00149 inline float& total_idletime() { return total_idletime_;}
00150
00151 inline AdaptiveFidelityEntity* afe() { return afe_; }
00152 inline int& maxttl() { return maxttl_; }
00153
00154 virtual void set_node_sleep(int);
00155 virtual void set_node_state(int);
00156 virtual void add_rcvtime(float t) {total_rcvtime_ += t;}
00157 virtual void add_sndtime(float t) {total_sndtime_ += t;}
00158
00159 virtual void add_sleeptime(float t) {total_sleeptime_ += t;};
00160
00161 void start_powersaving();
00162
00163
00164 enum SleepState { WAITING = 0, POWERSAVING = 1, INROUTE = 2 };
00165
00166 protected:
00167 double energy_;
00168
00169 double er_;
00170 double et_;
00171 double ei_;
00172 double es_;
00173
00174 double initialenergy_;
00175 double level1_;
00176 double level2_;
00177
00178 MobileNode *node_;
00179
00180
00181 struct neighbor_list_item {
00182 u_int32_t id;
00183 int ttl;
00184 neighbor_list_item *next;
00185 };
00186
00187 struct {
00188 int neighbor_cnt_;
00189 neighbor_list_item *head;
00190 } neighbor_list;
00191 SoftNeighborHandler *snh_;
00192
00193 int sleep_mode_;
00194 float total_sleeptime_;
00195 float total_rcvtime_;
00196 float total_sndtime_;
00197
00198 float total_idletime_;
00199
00200 int powersavingflag_;
00201 float last_time_gosleep;
00202 float max_inroute_time_;
00203
00204 int maxttl_;
00205
00206 int state_;
00207 float state_start_time_;
00208 int adaptivefidelity_;
00209 AdaptiveFidelityEntity *afe_;
00210
00211 bool node_on_;
00212 };
00213
00214
00215 #endif // ns_energy_model_h