• Main Page
  • Classes
  • Files
  • File List

/Users/yzchen/ns/ns-allinone-2.33/ns-2.33/diffusion3/filters/gear/gear.hh

00001 //
00002 // gear.hh         : GEAR Include File
00003 // authors         : Yan Yu and Fabio Silva
00004 //
00005 // Copyright (C) 2000-2002 by the University of Southern California
00006 // Copyright (C) 2000-2002 by the University of California
00007 // $Id: gear.hh,v 1.3 2005/09/13 04:53:48 tomh Exp $
00008 //
00009 // This program is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU General Public License,
00011 // version 2, as published by the Free Software Foundation.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License along
00019 // with this program; if not, write to the Free Software Foundation, Inc.,
00020 // 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00021 //
00022 // Linking this file statically or dynamically with other modules is making
00023 // a combined work based on this file.  Thus, the terms and conditions of
00024 // the GNU General Public License cover the whole combination.
00025 //
00026 // In addition, as a special exception, the copyright holders of this file
00027 // give you permission to combine this file with free software programs or
00028 // libraries that are released under the GNU LGPL and with code included in
00029 // the standard release of ns-2 under the Apache 2.0 license or under
00030 // otherwise-compatible licenses with advertising requirements (or modified
00031 // versions of such code, with unchanged license).  You may copy and
00032 // distribute such a system following the terms of the GNU GPL for this
00033 // file and the licenses of the other code concerned, provided that you
00034 // include the source code of that other code when and as the GNU GPL
00035 // requires distribution of source code.
00036 //
00037 // Note that people who make modified versions of this file are not
00038 // obligated to grant this special exception for their modified versions;
00039 // it is their choice whether to do so.  The GNU General Public License
00040 // gives permission to release a modified version without this exception;
00041 // this exception also makes it possible to release a modified version
00042 // which carries forward this exception.
00043 
00044 #ifndef _GEAR_HH_
00045 #define _GEAR_HH_
00046 
00047 #ifdef HAVE_CONFIG_H
00048 #include "config.h"
00049 #endif // HAVE_CONFIG_H
00050 
00051 #include <list>
00052 
00053 #ifdef HAVE_HASH_MAP
00054 #include <hash_map>
00055 #else
00056 #ifdef HAVE_EXT_HASH_MAP
00057 #include <ext/hash_map>
00058 #endif // HAVE_EXT_HASH_MAP
00059 #endif // HAVE_HASH_MAP
00060 
00061 #ifdef NS_DIFFUSION
00062 #include <mobilenode.h>
00063 #endif // NS_DIFFUSION
00064 
00065 #include "gear_attr.hh"
00066 #include "gear_tools.hh"
00067 
00068 #include "diffapp.hh"
00069 
00070 // Filter priorities for pre-processing and post-processing. The
00071 // gradient's filter priority has to be in between these two values in
00072 // order for GEAR to work properly
00073 #define GEOROUTING_PRE_FILTER_PRIORITY  170
00074 #define GEOROUTING_POST_FILTER_PRIORITY 20
00075 
00076 // Values used for defining timers
00077 #define BEACON_REQUEST_TIMER 150
00078 #define NEIGHBOR_TIMER       151
00079 
00080 // Energy values for GEAR
00081 #define GEO_INITIAL_ENERGY          1
00082 #define GEO_UNIT_ENERGY_FOR_SEND    0.001
00083 #define GEO_UNIT_ENERGY_FOR_RECV    0.001
00084 
00085 // Various beacon types
00086 enum geo_beacons {
00087   GEO_REQUEST = 1, // Beacon request. Nodes should send a beacon reply
00088                    // in response
00089   GEO_REPLY,       // Beacon reply. Sent in response to a beacon
00090                    // request. It also includes an heuristic value if
00091                    // requested on the beacon request
00092   GEO_UPDATE       // Includes updates to a particular heuristic value
00093                    // (for a given destination). It is send only if
00094                    // the new value is sufficiently different from the
00095                    // previous one
00096 };
00097 
00098 // Various actions taken when forwarding interests
00099 enum geo_actions {
00100   BROADCAST = 0,      // We are inside the target region, broadcast
00101                       // packet
00102   BROADCAST_SUPPRESS, // All our neighbors are outside the target
00103                       // region, we should not forward this interest
00104                       // message
00105   OUTSIDE_REGION      // We are still outside the target region,
00106                       // continue forwarding this interest message
00107                       // towards the region using unicast
00108 };
00109 
00110 #define GEO_BEACON_REPLY_PERIOD     100 // Sends at most one
00111                                         // beacon_reply message every
00112                                         // GEO_BEACON_REPLY_PERIOS
00113                                         // seconds
00114 #define GEO_NEIGHBOR_DELAY        30000 // In milli-seconds
00115 
00116 #define GEO_BEACON_REQUEST_CHECK_PERIOD 100000 // In milli-seconds
00117 #define GEO_NEIGHBOR_UPDATE                300 // In seconds
00118 #define GEO_NEIGHBOR_REQUEST_PERIOD (10 * GEO_NEIGHBOR_UPDATE) // In seconds
00119 
00120 #define GEO_NEIGHBOR_EXPIRED (5 * GEO_NEIGHBOR_UPDATE) // In seconds,
00121                                                        // this is how
00122                                                        // long a
00123                                                        // neighbor
00124                                                        // entry will
00125                                                        // last before
00126                                                        // being
00127                                                        // deleted
00128 
00129 // These values tell GEAR how much time to wait before sending a
00130 // beacon reply in response to a beacon request message
00131 #define GEO_BEACON_REPLY_DELAY   1500 // (msec) between receive and forward
00132 #define GEO_BEACON_REPLY_JITTER  1000 // (msec) jitter
00133 
00134 // These values tell GEAR how much to wait before sending a beacon
00135 // request or a beacon update message
00136 #define GEO_BEACON_DELAY   400 // (msec) between receive and forward
00137 #define GEO_BEACON_JITTER  200 // (msec) jitter
00138 
00139 #define INITIAL_ENERGY          1
00140 #define DEFAULT_VALID_PERIOD    10
00141 
00142 #define UNICAST_ORIGINAL 1
00143 #define BROADCAST_TYPE   2
00144 #define MAX_INT          10000
00145 
00146 #define MAX_PATH_LEN     200
00147 
00148 class Region {
00149 public:
00150   void operator= (Region p) {center_ = p.center_; radius_ = p.radius_;}
00151   void output()
00152   {
00153     center_.output();
00154     DiffPrint(DEBUG_IMPORTANT, "-%f", radius_);
00155   }
00156 
00157   GeoLocation center_;
00158   double radius_;
00159 };
00160 
00161 class GeoHeader {
00162 public:
00163   int16_t pkt_type_;  // BROADCAST or UNICAST_ORIGINAL
00164   int16_t path_len_;
00165   Region dst_region_;
00166 };
00167  
00168 class PktHeader {
00169 public:
00170   int32_t pkt_num_;
00171   int32_t rdm_id_;
00172   int32_t prev_hop_;
00173   int pkt_type_;
00174   int path_len_;
00175   Region dst_region_;
00176 };
00177 
00178 class NeighborEntry {
00179 public:
00180   NeighborEntry(int32_t id, double longitude, double latitude,
00181                 double remaining_energy) :
00182   id_(id), longitude_(longitude), latitude_(latitude),
00183   remaining_energy_(remaining_energy){
00184     valid_period_ = DEFAULT_VALID_PERIOD;
00185     GetTime(&tv_);
00186   }
00187 
00188   int32_t id_;
00189   double longitude_;
00190   double latitude_;
00191   double remaining_energy_;
00192   struct timeval tv_;
00193   double valid_period_; // in seconds
00194 };
00195 
00196 class GeoRoutingFilter;
00197 
00198 typedef list<NeighborEntry *> NeighborList;
00199 typedef list<PktHeader *> PacketList;
00200  
00201 class GeoFilterReceive : public FilterCallback {
00202 public:
00203   GeoFilterReceive(GeoRoutingFilter *app) : app_(app) {};
00204   void recv(Message *msg, handle h);
00205 
00206   GeoRoutingFilter *app_;
00207 };
00208 
00209 class GeoRoutingFilter : public DiffApp {
00210 public:
00211 #ifdef NS_DIFFUSION
00212   GeoRoutingFilter(const char *diffrtg);
00213   int command(int argc, const char*const* argv);
00214 #else
00215   GeoRoutingFilter(int argc, char **argv);
00216 #endif // NS_DIFFUSION
00217 
00218   virtual ~GeoRoutingFilter()
00219   {
00220     // Nothing but exit
00221   };
00222 
00223   void run();
00224   void recv(Message *msg, handle h);
00225 
00226   // Timers
00227   void messageTimeout(Message *msg);
00228   void beaconTimeout();
00229   void neighborTimeout();
00230   
00231 protected:
00232   // General Variables
00233   handle pre_filter_handle_;
00234   handle post_filter_handle_;
00235   int pkt_count_;
00236   int rdm_id_;
00237 
00238   // Keep track when last beacon reply was sent
00239   struct timeval last_beacon_reply_tv_;
00240 
00241   // Keep track when last beacon request was sent
00242   struct timeval last_neighbor_request_tv_;
00243   
00244   // Statistical data: location and remaining energy level
00245   double geo_longitude_;
00246   double geo_latitude_;
00247   int num_pkt_sent_;
00248   int num_pkt_recv_;
00249   double initial_energy_;
00250   double unit_energy_for_send_;
00251   double unit_energy_for_recv_;
00252 
00253   // List of all known neighbors, containing their location and energy
00254   // information
00255   NeighborList neighbors_list_;
00256 
00257   // List of messages currently being processed
00258   PacketList message_list_;
00259 
00260   // Forwarding table
00261   HeuristicValueTable h_value_table_;
00262   LearnedCostTable learned_cost_table_;
00263   
00264   // Receive Callback for the filter
00265   GeoFilterReceive *filter_callback_;
00266 
00267   // Setup the filter
00268   handle setupPostFilter();
00269   handle setupPreFilter();
00270 
00271   // Message Processing functions
00272   void preProcessFilter(Message *msg);
00273   void postProcessFilter(Message *msg);
00274 
00275   // Message processing functions
00276   PktHeader * preProcessMessage(Message *msg);
00277   PktHeader * stripOutHeader(Message *msg);
00278   PktHeader * retrievePacketHeader(Message *msg);
00279   bool extractLocation(Message *msg,
00280                        float *longitude_min, float *longitude_max,
00281                        float *latitude_min, float *latitude_max);
00282   GeoHeader * restoreGeoHeader(PktHeader *pkt_header, Message *msg);
00283   void takeOutAttr(NRAttrVec *attrs, int32_t key);
00284 
00285   // Neighbors related functions
00286   NeighborEntry * findNeighbor(int32_t neighbor_id);
00287   void updateNeighbor(int32_t neighbor_id, double neighbor_longitude,
00288                       double neighbor_latitude, double neighbor_energy);
00289   bool checkNeighbors();
00290   void sendNeighborRequest();
00291 
00292   // Energy related functions
00293   double remainingEnergy() {return INITIAL_ENERGY;}
00294 
00295   // Cost estimation related functions
00296   double retrieveLearnedCost(int neighbor_id, GeoLocation dst);
00297   double estimateCost(int neighbor_id, GeoLocation dst);
00298 
00299   // Routing related functions
00300   int32_t findNextHop(GeoHeader *geo_header, bool greedy);
00301   int floodInsideRegion(GeoHeader *geo_header);
00302 
00303   double retrieveHeuristicValue(GeoLocation dst);
00304   void broadcastHeuristicValue(GeoLocation dst, double new_heuristic_value);
00305 
00306   // GetNodeLocation --> This will move to the library in the future
00307   void getNodeLocation(double *longitude, double *latitude);
00308 };
00309 
00310 class GeoMessageSendTimer : public TimerCallback {
00311 public:
00312   GeoMessageSendTimer(GeoRoutingFilter *agent, Message *msg) :
00313     agent_(agent), msg_(msg) {};
00314   ~GeoMessageSendTimer()
00315   {
00316     delete msg_;
00317   };
00318   int expire();
00319 
00320   GeoRoutingFilter *agent_;
00321   Message *msg_;
00322 };
00323 
00324 class GeoNeighborsTimer : public TimerCallback {
00325 public:
00326   GeoNeighborsTimer(GeoRoutingFilter *agent) : agent_(agent) {};
00327   ~GeoNeighborsTimer() {};
00328   int expire();
00329 
00330   GeoRoutingFilter *agent_;
00331 };
00332 
00333 class GeoBeaconRequestTimer : public TimerCallback {
00334 public:
00335   GeoBeaconRequestTimer(GeoRoutingFilter *agent) : agent_(agent) {};
00336   ~GeoBeaconRequestTimer() {};
00337   int expire();
00338 
00339   GeoRoutingFilter *agent_;
00340 };
00341 
00342 #endif // !_GEAR_HH_

Generated on Tue Aug 10 2010 16:16:06 for ns-2.33 by  doxygen 1.7.1