• Main Page
  • Classes
  • Files
  • File List

/Users/yzchen/ns/ns-allinone-2.33/ns-2.33/imep/imep.h

00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) 1997 Regents of the University of California.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *      This product includes software developed by the Computer Systems
00017  *      Engineering Group at Lawrence Berkeley Laboratory.
00018  * 4. Neither the name of the University nor of the Laboratory may be used
00019  *    to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  *
00034  * Ported from CMU/Monarch's code
00035  *
00036  * $Header: /cvsroot/nsnam/ns-2/imep/imep.h,v 1.6 2006/02/21 15:20:18 mahrenho Exp $
00037  */
00038 
00039 #ifndef __imep_h__
00040 #define __imep_h__
00041 
00042 #include <stdarg.h>
00043 #include <sys/types.h>
00044 
00045 #include <config.h>
00046 #include <agent.h>
00047 #include <drop-tail.h>
00048 #include <trace.h>
00049 
00050 #include "lib/bsd-list.h"
00051 #include <rtproto/rtproto.h>
00052 #include <imep/rxmit_queue.h>
00053 #include <imep/dest_queue.h>
00054 #include <imep/imep_stats.h>
00055 #include <imep/imep_spec.h>
00056 
00057 // ======================================================================
00058 // ======================================================================
00059 
00060 #define IMEP_HDR_LEN    512
00061 
00062 #define BEACON_HDR_LEN  (IP_HDR_LEN + sizeof(struct hdr_imep))
00063 
00064 #define HELLO_HDR_LEN   (IP_HDR_LEN + sizeof(struct hdr_imep) + \
00065                          offsetof(struct imep_hello_block, hb_hello_list[0]) )
00066 
00067 #define ACK_HDR_LEN     (IP_HDR_LEN + sizeof(struct hdr_imep) + \
00068                          offsetof(struct imep_ack_block, ab_ack_list[0] ) )
00069 
00070 #define INT32_T(X)      *((int32_t*) &(X))
00071 #define U_INT16_T(X)    *((u_int16_t*) &(X))
00072 #define U_INT32_T(X)    *((u_int32_t*) &(X))
00073 
00074 // ======================================================================
00075 // ======================================================================
00076 
00077 class imepAgent;
00078 
00079 enum imepTimerType {
00080         BEACON_TIMER = 0x01,
00081         CONTROL_TIMER = 0x02,
00082         REXMIT_TIMER = 0x03,
00083         INCOMING_TIMER = 0x04
00084 };
00085 
00086 class imepTimer : public Handler {
00087 public:
00088         imepTimer(imepAgent* a, imepTimerType t) : busy_(0), agent(a), type_(t) {}
00089         void    handle(Event*);
00090         void    start(double time);
00091         void    cancel();
00092         int     busy() { return busy_; }
00093         double  timeLeft();
00094 private:
00095         Event           intr;
00096         int             busy_;
00097         imepAgent       *agent;
00098         imepTimerType   type_;
00099 };
00100 
00101 // ======================================================================
00102 // ======================================================================
00103 
00104 class imepLink {
00105         friend class imepAgent; // so the LIST macros work
00106 public:
00107         imepLink(nsaddr_t index) : index_(index), last_echo_(-BEACON_PERIOD) {}
00108 
00109         nsaddr_t&       index() { return index_; }
00110         u_int32_t&      status() { return status_;}
00111         u_int8_t&       lastSeq() { return last_seq_;}
00112         u_int8_t&       lastSeqValid() { return last_seq_;}
00113         double&         in_expire() { return in_expire_; }
00114         double&         out_expire() { return out_expire_; }
00115         double&         lastEcho() { return last_echo_; } 
00116 
00117 protected:
00118         LIST_ENTRY(imepLink) link;
00119 
00120 private:
00121         nsaddr_t        index_;
00122         u_int32_t       status_;
00123         double          in_expire_;
00124         double          out_expire_;
00125         double          last_echo_;
00126         u_int8_t        last_seq_;  // greatest seq heard from index_
00127         bool            last_seq_valid;
00128 };
00129 
00130 // ======================================================================
00131 // ======================================================================
00132 
00133 class imepAgent : public Agent {
00134         friend class imepTimer;
00135  public:
00136         imepAgent(nsaddr_t index);
00137 
00138         void recv(Packet *p, Handler *h);
00139         int command(int argc, const char*const* argv);
00140 
00141         // ============================================================
00142         // The IMEP API (imep-api.cc)
00143 
00144         void imepRegister(rtAgent *rt);
00145 
00146         void imepGetLinkStatus(nsaddr_t index, u_int32_t& status);
00147         void imepSetLinkInStatus(nsaddr_t index);
00148         void imepSetLinkOutStatus(nsaddr_t index);
00149         void imepSetLinkBiStatus(nsaddr_t index);
00150         void imepSetLinkDownStatus(nsaddr_t index);
00151 
00152         void imepPacketUndeliverable(Packet *p);
00153         // called by the MAC layer when it is unable to deliver
00154         // a packet to its next hop.
00155 
00156         void imepGetBiLinks(int*& nblist, int& nbcnt);
00157         // called by the routing layer to get a complete list of all
00158         // neighbors.
00159 
00160         ImepStat stats;
00161 
00162  protected:
00163         void Terminate();
00164 
00165         // ============================================================
00166         // Timer stuff
00167         void handlerTimer(imepTimerType t);
00168 
00169  private:
00170         imepTimer               beaconTimer;
00171         imepTimer               controlTimer;
00172         imepTimer               rexmitTimer;
00173         imepTimer               incomingTimer;
00174 
00175         void handlerBeaconTimer(void);
00176         void handlerControlTimer(void);
00177         void handlerReXmitTimer(void);
00178         void handlerIncomingTimer(void);
00179         // When this timer fires, holes in the sequence space
00180         // are ignored and objects are delivered to the upper
00181         // layers.
00182 
00183         // ============================================================
00184         // Packet processing functions
00185 
00186         void recv_incoming(Packet *p);
00187         void recv_outgoing(Packet *p);
00188 
00189         void imep_beacon_input(Packet *p);
00190         void imep_ack_input(Packet *p);
00191         void imep_hello_input(Packet *p);
00192         void imep_object_input(Packet *p);
00193         void imep_object_process(Packet *p);
00194         void imep_ack_object(Packet *p);
00195 
00196         void imep_input(Packet *p);
00197         void imep_output(Packet *p);
00198 
00199         void sendBeacon(void);
00200         void sendHello(nsaddr_t index);
00201         void sendAck(nsaddr_t index, u_int32_t seqno);
00202         
00203         // ============================================================
00204         // Utility routines
00205 
00206         imepLink* findLink(nsaddr_t index);
00207 
00208         void purgeLink(void);
00209         // purge all expired links from the cache.
00210 
00211         void purgeReXmitQ(nsaddr_t index);
00212         // remove index from any response lists in the rexmit q
00213 
00214         Packet* findObjectSequence(u_int8_t seqno);
00215         // find the packet corresponding to sequence number "seqno".
00216 
00217         void removeObjectResponse(Packet *p, nsaddr_t index);
00218         // remove IP address "index" from the object response list of "p".
00219 
00220         struct imep_ack_block* findAckBlock(Packet *p);
00221         struct imep_hello_block* findHelloBlock(Packet *p);
00222         struct imep_object_block* findObjectBlock(Packet *p);
00223         struct imep_response* findResponseList(Packet *p);
00224 
00225         void aggregateAckBlock(Packet *p);
00226         void aggregateHelloBlock(Packet *p);
00227         void aggregateObjectBlock(Packet *p);
00228         void createResponseList(Packet *p);
00229         int getResponseListSize();
00230 
00231         void scheduleReXmit(Packet *p);
00232         void scheduleIncoming(Packet *p, u_int32_t s);
00233 
00234         u_int8_t nextSequence() {
00235                 controlSequence++;
00236                 return (controlSequence & 0xFF);
00237         }
00238 
00239         inline int initialized() {
00240                 return recvtarget_ && sendtarget_ && rtagent_ && logtarget_;
00241         }
00242 
00243         void imep_dump_header(Packet *p);
00244 public:
00245         void trace(char* fmt, ...);
00246 private:
00247 
00248         void log_neighbor_list(void);
00249         char* dumpResponseList(Packet *p);
00250 
00251         // ============================================================
00252         // Routing Protocol Specific Routines
00253 
00254         void toraCreateHeader(Packet *p, char* dst, int length);
00255         void toraExtractHeader(Packet *p, char* dst);
00256         int toraHeaderLength(struct hdr_tora* t);
00257 
00258         // ============================================================
00259 
00260         nsaddr_t        ipaddr;
00261         // IP Address of this node.
00262 
00263         u_int32_t       controlSequence;
00264 
00265         NsObject        *recvtarget_;
00266         // classifier
00267 
00268         NsObject        *sendtarget_;
00269         // The link layer below the IMEP layer.
00270 
00271         rtAgent         *rtagent_;
00272         // pointer to the "upper layer" routing agent.
00273 
00274         Trace           *logtarget_;
00275         // allows the IMEP agent to write info to the log file.
00276 
00277         // XXX Must declare the first parameter in the macro, otherwise
00278         // msvc will be unhappy.
00279         LIST_HEAD(_dummy_imepLinkList,imepLink) imepLinkHead;
00280         // List of links to neighbors and corresponding status
00281 
00282         // ============================================================
00283         // Awaiting Broadcast (AB) Buffer
00284         PacketQueue ackQueue;
00285         PacketQueue helloQueue;
00286         PacketQueue objectQueue;
00287 
00288         // ============================================================
00289         // Waiting for ACK Buffer
00290         ReXmitQ rexmitq;
00291 
00292         // ============================================================
00293         // Waiting for delivery to the upper layer Buffer
00294         dstQueue incomingQ;
00295 };
00296 
00297 #endif /* __imep_h__ */

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