IN BED
bed.cc
#include <bed/bed.h>
#include <bed/bed_packet.h>
#include <bed/select.h>
#include <random.h>
#include <cmu-trace.h>
#include <energy-model.h>
MobileNode *nadr[MAXI];
#define max(a,b) ( (a) > (b) ? (a) : (b) )
#define CURRENT_TIME Scheduler::instance().clock()
//#define DEBUG
// ======================================================================
// TCL Hooking Classes
// ======================================================================
int hdr_bed:
ffset_;
static class BEDHeaderClass : public PacketHeaderClass {
public:
BEDHeaderClass() : PacketHeaderClass("PacketHeader/BED", sizeof(hdr_all_bed)) {
bind_offset(&hdr_bed:
ffset_);
}
} class_rtProtoBED_hdr;
static class BEDclass : public TclClass {
public:
BEDclass() : TclClass("Agent/BED") {}
TclObject* create(int argc, const char*const* argv) {
assert(argc == 5);
return (new BED((nsaddr_t) Address::instance().str2addr(argv[4])));
}
} class_rtProtoBED;
int
BED::command(int argc, const char*const* argv) {
if(argc == 2) {
Tcl& tcl = Tcl::instance();
bed.h
#ifndef __bed_h__
#define __bed_h__
#define MAXI 1000
#include <cmu-trace.h>
#include <mobilenode.h>
#include <priqueue.h>
#include <classifier/classifier-port.h>
#include <mac/mac-802_11.h>
#define NETWORK_DIAMETER 64
#define DEFAULT_BEACON_INTERVAL 10 // seconds;
#define DEFAULT_ROUTE_EXPIRE 2*DEFAULT_BEACON_INTERVAL // seconds;
#define ROUTE_PURGE_FREQUENCY 2 // seconds
double dbr[MAXI];
double PT = 0.0;
float edd[MAXI];
float ddd[MAXI];
float energy_level[MAXI];
float delay_time[MAXI];
int nnn[MAXI];
double detax[MAXI];
double detay[MAXI];
int nodeno = 0;
int RFAC = 100;
int RLIMIT = 500;
int ORDER = 5;
int LIMS = 4;
bool cch[MAXI];
float TSL = 1.00;
float NSL = 0.00;
bool STAT = true;
int SFL=87;
int I = 0;
float WINSIZE = 1.2;
int TOTAL = 0;
#define ROUTE_FRESH 0x01
#define ROUTE_EXPIRED 0x02
#define ROUTE_FAILED 0x03
class BED;
// ======================================================================
// Timers : Beacon Timer, Route Cache Timer
// ======================================================================
class bedBeaconTimer : public Handler {
public:
bedBeaconTimer(BED* a) : agent(a) {}
void handle(Event*);
private:
BED *agent;
Event intr;
};
//
nsaddr_t next_hop(int xy){ return IP_BROADCAST;}
class bedRouteCacheTimer : public Handler {
public:
bedRouteCacheTimer(BED* a) : agent(a) {}
void handle(Event*);
private:
BED *agent;
Event intr;
};
// ======================================================================
// Route Cache Table
// ======================================================================
class RouteCache {
friend class BED;
public:
RouteCache(nsaddr_t bsrc, u_int32_t bid) { rt_dst = bsrc; rt_seqno = bid; }
protected:
LIST_ENTRY(RouteCache) rt_link;
u_int32_t rt_seqno; // route sequence number
nsaddr_t rt_dst; // route destination
nsaddr_t rt_nexthop; // next hop node towards the destionation
u_int32_t rt_xpos; // x position of destination;
u_int32_t rt_ypos; // y position of destination;
u_int8_t rt_state; // state of the route: FRESH, EXPIRED, FAILED (BROKEN)
u_int8_t rt_hopcount; // number of hops up to the destination (sink)
double rt_expire; // when route expires : Now + DEFAULT_ROUTE_EXPIRE
};
LIST_HEAD(bed_rtcache, RouteCache);
// ======================================================================
// BED Routing Agent : the routing protocol
// ======================================================================
class BED : public Agent {
friend class RouteCacheTimer;
friend class MobileNode;
public:
BED(nsaddr_t id);
void recv(Packet *p, Handler *);
int command(int, const char *const *);
// Agent Attributes
nsaddr_t index; // node address (identifier)
nsaddr_t seqno; // beacon sequence number (used only when agent is sink)
// Node Location
uint32_t posx; // position x;
uint32_t posy; // position y;
void decide_expel();
void decide_prox(MobileNode *tempn[],int num,int extra,int assigned);
void decide_proxb(MobileNode *tempn[],int num,int extra,int assigned);
MobileNode* search(double,double);
double nwpos(int pos);
double nwbpos(int pos);
// Routing Table Management
void rt_insert(nsaddr_t src, u_int32_t id, nsaddr_t nexthop, u_int32_t xpos, u_int32_t ypos, u_int8_t hopcount);
void rt_remove(RouteCache *rt);
void rt_purge();
RouteCache* rt_lookup(nsaddr_t dst);
void energy_scheme(struct hdr_bed_beacon* ,Packet * , struct hdr_ip *);
void delay_scheme(struct hdr_bed_beacon* , Packet *,struct hdr_ip *);
bool decide(int index);
// Remaining Energy
double iEnergy;
// Timers
bedBeaconTimer bcnTimer;
bedRouteCacheTimer rtcTimer;
// Caching Head
bed_rtcache rthead;
// Send Routines
void send_beacon();
void send_error(nsaddr_t unreachable_destination);
void forward(Packet *p, nsaddr_t nexthop, double delay);
// Recv Routines
void recv_data(Packet *p);
void recv_bed(Packet *p);
void recv_beacon(Packet *p);
void recv_error(Packet *p);
// Position Management
void update_position();
// Energy Management
void update_energy();
// This node;
MobileNode *iNode;
// A mechanism for logging the contents of the routing table.
Trace *logtarget;
// A pointer to the network interface queue that sits between the "classifier" and the "link layer"
PriQueue *ifqueue;
// Port classifier for passing packets up to agents
PortClassifier *dmux_;
};
bool valid(float f)
{
if(f>1 || f<0) return false;
return true;
}
float abso(float num)
{
return num<0 ? -num : num;
}
double avg_(double a,double b)
{
return ( (a+b)/2.00 );
}
int scl()
{
return rand()%SFL;
}
#endif /* __bed_h__ */
bed_packet.h
#ifndef __bed_packet_h__
#define __bed_packet_h__
// ======================================================================
// Packet Formats: Beacon, Data, Error
// ======================================================================
#define BED_BEACON 0x01
#define BED_ERROR 0x02
// ======================================================================
// Direct access to packet headers
// ======================================================================
#define HDR_BED(p) ((struct hdr_bed*)hdr_bed::access(p))
#define HDR_BED_BEACON(p) ((struct hdr_bed_beacon*)hdr_bed::access(p))
#define HDR_BED_ERROR(p) ((struct hdr_bed_error*)hdr_bed::access(p))
// ======================================================================
// Default BED packet
// ======================================================================
struct hdr_bed {
u_int8_t pkt_type;
// header access
static int offset_;
inline static int& offset() { return offset_;}
inline static hdr_bed* access(const Packet *p) {
return (hdr_bed*) p->access(offset_);
}
};
// ======================================================================
// Beacon Packet Format
// ======================================================================
struct hdr_bed_beacon {
u_int8_t pkt_type; // type of packet : Beacon or Error
u_int8_t beacon_hops; // hop count, increadecreases as beacon is forwarded
u_int32_t beacon_id; // unique identifier for the beacon
nsaddr_t beacon_src; // source address of beacon, this is sink address
u_int32_t beacon_posx; // x position of beacon source, if available
u_int32_t beacon_posy; // y position of beacon source, if available
double timestamp; // emission time of beacon message
inline int size() {
int sz = 0;
sz = sizeof(struct hdr_bed_beacon);
assert(sz>=0);
return sz;
}
};
// =====================================================================
// Error Packet Format
// =====================================================================
struct hdr_bed_error {
u_int8_t pkt_type; // type of packet : Beacon or Error
u_int8_t reserved; // reserved for future use
nsaddr_t error_src; // error packet source node;
nsaddr_t urch_dst; // unreachable destination
double timestamp; // emission time
inline int size() {
int sz = 0;
sz = sizeof(struct hdr_bed_error);
assert(sz>=0);
return sz;
}
};
// For size calculation of header-space reservation
union hdr_all_bed {
hdr_bed bed;
hdr_bed_beacon beacon;
hdr_bed_error error;
};
#endif /* __bed_packet_h__ */