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), initialenergy_(energy),
00092 level1_(l1), level2_(l2), node_(n),
00093 sleep_mode_(0), total_sleeptime_(0), total_rcvtime_(0),
00094 total_sndtime_(0), powersavingflag_(0),
00095 last_time_gosleep(0), max_inroute_time_(300), maxttl_(5),
00096 adaptivefidelity_(0), node_on_(true)
00097 {
00098 neighbor_list.neighbor_cnt_ = 0;
00099 neighbor_list.head = NULL;
00100 }
00101
00102 inline double energy() const { return energy_; }
00103 inline double initialenergy() const { return initialenergy_; }
00104 inline double level1() const { return level1_; }
00105 inline double level2() const { return level2_; }
00106 inline void setenergy(double e) { energy_ = e; }
00107
00108 virtual void DecrTxEnergy(double txtime, double P_tx);
00109 virtual void DecrRcvEnergy(double rcvtime, double P_rcv);
00110 virtual void DecrIdleEnergy(double idletime, double P_idle);
00111 inline virtual double MaxTxtime(double P_tx) {
00112 return(energy_/P_tx);
00113 }
00114 inline virtual double MaxRcvtime(double P_rcv) {
00115 return(energy_/P_rcv);
00116 }
00117 inline virtual double MaxIdletime(double P_idle) {
00118 return(energy_/P_idle);
00119 }
00120
00121 void add_neighbor(u_int32_t);
00122 void scan_neighbor();
00123 inline int getneighbors() { return neighbor_list.neighbor_cnt_; }
00124
00125 double level1() { return level1_; }
00126 double level2() { return level2_; }
00127 inline int sleep() { return sleep_mode_; }
00128 inline int state() { return state_; }
00129 inline float state_start_time() { return state_start_time_; }
00130 inline float& max_inroute_time() { return max_inroute_time_; }
00131 inline int& adaptivefidelity() { return adaptivefidelity_; }
00132 inline int& powersavingflag() { return powersavingflag_; }
00133 inline bool& node_on() { return node_on_; }
00134 inline float& total_sndtime() { return total_sndtime_; }
00135 inline float& total_rcvtime() { return total_rcvtime_; }
00136 inline float& total_sleeptime() { return total_sleeptime_; }
00137 inline AdaptiveFidelityEntity* afe() { return afe_; }
00138 inline int& maxttl() { return maxttl_; }
00139
00140 virtual void set_node_sleep(int);
00141 virtual void set_node_state(int);
00142 virtual void add_rcvtime(float t) {total_rcvtime_ += t;}
00143 virtual void add_sndtime(float t) {total_sndtime_ += t;}
00144
00145 void start_powersaving();
00146
00147
00148 enum SleepState { WAITING = 0, POWERSAVING = 1, INROUTE = 2 };
00149
00150 protected:
00151 double energy_;
00152 double initialenergy_;
00153 double level1_;
00154 double level2_;
00155
00156 MobileNode *node_;
00157
00158
00159 struct neighbor_list_item {
00160 u_int32_t id;
00161 int ttl;
00162 neighbor_list_item *next;
00163 };
00164
00165 struct {
00166 int neighbor_cnt_;
00167 neighbor_list_item *head;
00168 } neighbor_list;
00169 SoftNeighborHandler *snh_;
00170
00171 int sleep_mode_;
00172 float total_sleeptime_;
00173 float total_rcvtime_;
00174 float total_sndtime_;
00175 int powersavingflag_;
00176 float last_time_gosleep;
00177 float max_inroute_time_;
00178
00179 int maxttl_;
00180
00181 int state_;
00182 float state_start_time_;
00183 int adaptivefidelity_;
00184 AdaptiveFidelityEntity *afe_;
00185
00186 bool node_on_;
00187 };
00188
00189
00190 #endif // ns_energy_model_h