• Main Page
  • Classes
  • Files
  • File List

/Users/yzchen/ns/ns-allinone-2.33/ns-2.33/um-olsr-0.8.8/backup/OLSR.h

00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Francisco J. Ros                                *
00003  *   fjrm@dif.um.es                                                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00030 
00031 #ifndef __OLSR_h__
00032 #define __OLSR_h__
00033 
00034 #include <olsr/OLSR_pkt.h>
00035 #include <olsr/OLSR_state.h>
00036 #include <olsr/OLSR_rtable.h>
00037 #include <olsr/OLSR_repositories.h>
00038 #include <trace.h>
00039 #include <classifier-port.h>
00040 #include <agent.h>
00041 #include <packet.h>
00042 #include <timer-handler.h>
00043 #include <random.h>
00044 #include <vector>
00045 
00046 /********** Useful macros **********/
00047 
00049 #ifndef MAX
00050 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
00051 #endif
00052 
00054 #ifndef MIN
00055 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
00056 #endif
00057 
00059 #ifndef NULL
00060 #define NULL 0
00061 #endif
00062 
00064 #define CURRENT_TIME    Scheduler::instance().clock()
00065 
00072 #define DELAY(time) (((time) < (CURRENT_TIME)) ? (0.000001) : \
00073         (time - CURRENT_TIME + 0.000001))
00074 
00076 #define OLSR_C          0.0625
00077 
00078 
00079 /********** Intervals **********/
00080 
00082 #define OLSR_HELLO_INTERVAL     2
00083 
00085 #define OLSR_TC_INTERVAL        5
00086 
00088 #define OLSR_MID_INTERVAL       OLSR_TC_INTERVAL
00089 
00095 #define OLSR_REFRESH_INTERVAL   2
00096 
00097 
00098 /********** Holding times **********/
00099 
00101 #define OLSR_NEIGHB_HOLD_TIME   3*OLSR_REFRESH_INTERVAL
00102 
00103 #define OLSR_TOP_HOLD_TIME      3*OLSR_TC_INTERVAL
00104 
00105 #define OLSR_DUP_HOLD_TIME      30
00106 
00107 #define OLSR_MID_HOLD_TIME      3*OLSR_MID_INTERVAL
00108 
00109 
00110 /********** Link types **********/
00111 
00113 #define OLSR_UNSPEC_LINK        0
00114 
00115 #define OLSR_ASYM_LINK          1
00116 
00117 #define OLSR_SYM_LINK           2
00118 
00119 #define OLSR_LOST_LINK          3
00120 
00121 /********** Neighbor types **********/
00122 
00124 #define OLSR_NOT_NEIGH          0
00125 
00126 #define OLSR_SYM_NEIGH          1
00127 
00128 #define OLSR_MPR_NEIGH          2
00129 
00130 
00131 /********** Willingness **********/
00132 
00134 #define OLSR_WILL_NEVER         0
00135 
00136 #define OLSR_WILL_LOW           1
00137 
00138 #define OLSR_WILL_DEFAULT       3
00139 
00140 #define OLSR_WILL_HIGH          6
00141 
00142 #define OLSR_WILL_ALWAYS        7
00143 
00144 
00145 /********** Miscellaneous constants **********/
00146 
00148 #define OLSR_MAXJITTER          OLSR_HELLO_INTERVAL/4
00149 
00150 #define OLSR_MAX_SEQ_NUM        65535
00151 
00152 #define OLSR_STATUS_NOT_SYM     0
00153 
00154 #define OLSR_STATUS_SYM         1
00155 
00156 #define JITTER                  (Random::uniform()*OLSR_MAXJITTER)
00157 
00158 
00159 class OLSR;                     // forward declaration
00160 
00161 
00162 /********** Timers **********/
00163 
00164 
00166 class OLSR_MsgTimer : public TimerHandler {
00167 public:
00168         OLSR_MsgTimer(OLSR* agent) : TimerHandler() {
00169                 agent_  = agent;
00170         }
00171 protected:
00172         OLSR*   agent_;                 
00173         virtual void expire(Event* e);
00174 };
00175 
00177 class OLSR_HelloTimer : public TimerHandler {
00178 public:
00179         OLSR_HelloTimer(OLSR* agent) : TimerHandler() { agent_ = agent; }
00180 protected:
00181         OLSR*   agent_;                 
00182         virtual void expire(Event* e);
00183 };
00184 
00185 
00187 class OLSR_TcTimer : public TimerHandler {
00188 public:
00189         OLSR_TcTimer(OLSR* agent) : TimerHandler() { agent_ = agent; }
00190 protected:
00191         OLSR*   agent_;                 
00192         virtual void expire(Event* e);
00193 };
00194 
00195 
00197 class OLSR_MidTimer : public TimerHandler {
00198 public:
00199         OLSR_MidTimer(OLSR* agent) : TimerHandler() { agent_ = agent; }
00200 protected:
00201         OLSR*   agent_;                 
00202         virtual void expire(Event* e);
00203 };
00204 
00205 
00207 class OLSR_DupTupleTimer : public TimerHandler {
00208 public:
00209         OLSR_DupTupleTimer(OLSR* agent, OLSR_dup_tuple* tuple) : TimerHandler() {
00210                 agent_ = agent;
00211                 tuple_ = tuple;
00212         }
00213 protected:
00214         OLSR*           agent_; 
00215         OLSR_dup_tuple* tuple_; 
00216         
00217         virtual void expire(Event* e);
00218 };
00219 
00220 
00222 class OLSR_LinkTupleTimer : public TimerHandler {
00230         bool                    first_time_;
00231 public:
00232         OLSR_LinkTupleTimer(OLSR* agent, OLSR_link_tuple* tuple) : TimerHandler() {
00233                 agent_          = agent;
00234                 tuple_          = tuple;
00235                 first_time_     = true;
00236         }
00237 protected:
00238         OLSR*                   agent_; 
00239         OLSR_link_tuple*        tuple_; 
00240         
00241         virtual void expire(Event* e);
00242 };
00243 
00244 
00246 class OLSR_Nb2hopTupleTimer : public TimerHandler {
00247 public:
00248         OLSR_Nb2hopTupleTimer(OLSR* agent, OLSR_nb2hop_tuple* tuple) : TimerHandler() {
00249                 agent_ = agent;
00250                 tuple_ = tuple;
00251         }
00252 protected:
00253         OLSR*                   agent_; 
00254         OLSR_nb2hop_tuple*      tuple_; 
00255         
00256         virtual void expire(Event* e);
00257 };
00258 
00259 
00261 class OLSR_MprSelTupleTimer : public TimerHandler {
00262 public:
00263         OLSR_MprSelTupleTimer(OLSR* agent, OLSR_mprsel_tuple* tuple) : TimerHandler() {
00264                 agent_ = agent;
00265                 tuple_ = tuple;
00266         }
00267 protected:
00268         OLSR*                   agent_; 
00269         OLSR_mprsel_tuple*      tuple_; 
00270         
00271         virtual void expire(Event* e);
00272 };
00273 
00274 
00276 class OLSR_TopologyTupleTimer : public TimerHandler {
00277 public:
00278         OLSR_TopologyTupleTimer(OLSR* agent, OLSR_topology_tuple* tuple) : TimerHandler() {
00279                 agent_ = agent;
00280                 tuple_ = tuple;
00281         }
00282 protected:
00283         OLSR*                   agent_; 
00284         OLSR_topology_tuple*    tuple_; 
00285         
00286         virtual void expire(Event* e);
00287 };
00288 
00289 
00291 class OLSR_IfaceAssocTupleTimer : public TimerHandler {
00292 public:
00293         OLSR_IfaceAssocTupleTimer(OLSR* agent, OLSR_iface_assoc_tuple* tuple) : TimerHandler() {
00294                 agent_ = agent;
00295                 tuple_ = tuple;
00296         }
00297 protected:
00298         OLSR*                   agent_; 
00299         OLSR_iface_assoc_tuple* tuple_; 
00300         
00301         virtual void expire(Event* e);
00302 };
00303 
00304 
00305 /********** OLSR Agent **********/
00306 
00307 
00315 class OLSR : public Agent {
00316         // Makes some friends.
00317         friend class OLSR_HelloTimer;
00318         friend class OLSR_TcTimer;
00319         friend class OLSR_MidTimer;
00320         friend class OLSR_DupTupleTimer;
00321         friend class OLSR_LinkTupleTimer;
00322         friend class OLSR_Nb2hopTupleTimer;
00323         friend class OLSR_MprSelTupleTimer;
00324         friend class OLSR_TopologyTupleTimer;
00325         friend class OLSR_IfaceAssocTupleTimer;
00326         friend class OLSR_MsgTimer;
00327         
00329         nsaddr_t        ra_addr_;
00330         
00332         u_int16_t       pkt_seq_;
00334         u_int16_t       msg_seq_;
00336         u_int16_t       ansn_;
00337         
00339         int             hello_ival_;
00341         int             tc_ival_;
00343         int             mid_ival_;
00345         int             willingness_;
00347         int             use_mac_;
00348         
00350         OLSR_rtable             rtable_;
00352         OLSR_state              state_;
00354         std::vector<OLSR_msg>   msgs_;
00355         
00356 protected:
00357         PortClassifier* dmux_;          
00358         Trace*          logtarget_;     
00359         
00360         OLSR_HelloTimer hello_timer_;   
00361         OLSR_TcTimer    tc_timer_;      
00362         OLSR_MidTimer   mid_timer_;     
00363         
00365         inline u_int16_t        pkt_seq() {
00366                 pkt_seq_ = (pkt_seq_ + 1)%(OLSR_MAX_SEQ_NUM + 1);
00367                 return pkt_seq_;
00368         }
00370         inline u_int16_t        msg_seq() {
00371                 msg_seq_ = (msg_seq_ + 1)%(OLSR_MAX_SEQ_NUM + 1);
00372                 return msg_seq_;
00373         }
00374         
00375         inline nsaddr_t&        ra_addr()       { return ra_addr_; }
00376         
00377         inline int&             hello_ival()    { return hello_ival_; }
00378         inline int&             tc_ival()       { return tc_ival_; }
00379         inline int&             mid_ival()      { return mid_ival_; }
00380         inline int&             willingness()   { return willingness_; }
00381         inline int&             use_mac()       { return use_mac_; }
00382         
00383         inline linkset_t&       linkset()       { return state_.linkset(); }
00384         inline mprset_t&        mprset()        { return state_.mprset(); }
00385         inline mprselset_t&     mprselset()     { return state_.mprselset(); }
00386         inline nbset_t&         nbset()         { return state_.nbset(); }
00387         inline nb2hopset_t&     nb2hopset()     { return state_.nb2hopset(); }
00388         inline topologyset_t&   topologyset()   { return state_.topologyset(); }
00389         inline dupset_t&        dupset()        { return state_.dupset(); }
00390         inline ifaceassocset_t& ifaceassocset() { return state_.ifaceassocset(); }
00391         
00392         void            recv_olsr(Packet*);
00393         
00394         void            mpr_computation();
00395         void            rtable_computation();
00396         
00397         void            process_hello(OLSR_msg&, nsaddr_t, nsaddr_t);
00398         void            process_tc(OLSR_msg&, nsaddr_t);
00399         void            process_mid(OLSR_msg&, nsaddr_t);
00400         
00401         void            forward_default(Packet*, OLSR_msg&, OLSR_dup_tuple*, nsaddr_t);
00402         void            forward_data(Packet*);
00403         
00404         void            enque_msg(OLSR_msg&, double);
00405         void            send_hello();
00406         void            send_tc();
00407         void            send_mid();
00408         void            send_pkt();
00409         
00410         void            link_sensing(OLSR_msg&, nsaddr_t, nsaddr_t);
00411         void            populate_nbset(OLSR_msg&);
00412         void            populate_nb2hopset(OLSR_msg&);
00413         void            populate_mprselset(OLSR_msg&);
00414 
00415         void            set_hello_timer();
00416         void            set_tc_timer();
00417         void            set_mid_timer();
00418 
00419         void            nb_loss(OLSR_link_tuple*);
00420         void            add_dup_tuple(OLSR_dup_tuple*);
00421         void            rm_dup_tuple(OLSR_dup_tuple*);
00422         void            add_link_tuple(OLSR_link_tuple*, u_int8_t);
00423         void            rm_link_tuple(OLSR_link_tuple*);
00424         void            updated_link_tuple(OLSR_link_tuple*);
00425         void            add_nb_tuple(OLSR_nb_tuple*);
00426         void            rm_nb_tuple(OLSR_nb_tuple*);
00427         void            add_nb2hop_tuple(OLSR_nb2hop_tuple*);
00428         void            rm_nb2hop_tuple(OLSR_nb2hop_tuple*);
00429         void            add_mprsel_tuple(OLSR_mprsel_tuple*);
00430         void            rm_mprsel_tuple(OLSR_mprsel_tuple*);
00431         void            add_topology_tuple(OLSR_topology_tuple*);
00432         void            rm_topology_tuple(OLSR_topology_tuple*);
00433         void            add_ifaceassoc_tuple(OLSR_iface_assoc_tuple*);
00434         void            rm_ifaceassoc_tuple(OLSR_iface_assoc_tuple*);
00435         
00436         nsaddr_t        get_main_addr(nsaddr_t);
00437         int             degree(OLSR_nb_tuple*);
00438 
00439         static bool     seq_num_bigger_than(u_int16_t, u_int16_t);
00440 
00441 public:
00442         OLSR(nsaddr_t);
00443         int     command(int, const char*const*);
00444         void    recv(Packet*, Handler*);
00445         void    mac_failed(Packet*);
00446         
00447         static double           emf_to_seconds(u_int8_t);
00448         static u_int8_t         seconds_to_emf(double);
00449         static int              node_id(nsaddr_t);
00450 };
00451 
00452 #endif

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