⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aodvii.h

📁 大名鼎鼎的传感器网络仿真实验室平台SENSE
💻 H
📖 第 1 页 / 共 2 页
字号:
	}	else	{	    // must forward the rreq	    forward_rreq(p,max_seq_num);	}    }}template <class PLD>void AODVII<PLD>::forward_rreq(packet_t* p, int max_seq_num){    if(p->hdr.TTL > 1)     {	packet_t* new_p = packet_t::alloc();	new_p->hdr.type = RREQ; 	new_p->hdr.TTL = p->hdr.TTL - 1; 	new_p->hdr.src_addr = p->hdr.src_addr;	new_p->hdr.dst_addr = p->hdr.dst_addr;	new_p->hdr.prev_hop_addr = MyEtherAddr;	new_p->hdr.hop_count = p->hdr.hop_count + 1;	new_p->hdr.rreq_id = p->hdr.rreq_id;	new_p->hdr.size = RREQ_SIZE; 	new_p->hdr.flags=p->hdr.flags;	new_p->hdr.dst_seq_num = max_seq_num;	 	to_mac_delay.Set(new_p,SimTime()+Random(ForwardDelay));    }}template <class PLD>void AODVII<PLD>::receive_rrep(packet_t* p){    UpdateRoutingTable(p);    if(MyEtherAddr != p->hdr.dst_addr)    {	// unicast the RREP back to the source via next hop node	// look up next hop in reverse routing table	routing_table_t::iterator rv_iter = m_routing_table.find(p->hdr.dst_addr);	if(rv_iter == m_routing_table.end())	{	    printf("[%f] net%d reverse routing error: no route back to the source!\n", SimTime(), (int)MyEtherAddr);	    return;	}	else	{   	    packet_t* new_p = packet_t::alloc();	    	    new_p->hdr.src_addr = p->hdr.src_addr;	    new_p->hdr.prev_hop_addr = MyEtherAddr;	    new_p->hdr.dst_addr = p->hdr.dst_addr; 	    new_p->hdr.type = RREP;	    new_p->hdr.dst_seq_num = p->hdr.dst_seq_num;	    new_p->hdr.hop_count = p->hdr.hop_count + 1;	    new_p->hdr.size = RREP_SIZE;	    new_p->hdr.flags.repair=0;	    new_p->hdr.flags.acknowledgement=0;	    /*Printf((DumpPackets,"aodv%d sending %s\n",(int)MyEtherAddr,new_p->dump().c_str()));*/	    if(rv_iter->second.valid==false)	    {		RepairLink(p->hdr.src_addr, rv_iter->first, rv_iter->second,rv_iter->second.hop_count);		m_packet_buffer.push_back(new_p);	    }	    else		BufferPacket(new_p);	}    }    else    {	// RREP reaches the source. 	/*Printf((DumpPackets,"%d AODVII:: Route %d->%d established\n", (int)MyEtherAddr,	       (int)MyEtherAddr, (int)p->hdr.src_addr));*/    }}template <class PLD>void AODVII<PLD>::to_mac_depart(packet_t*&p, unsigned int){    BufferPacket(p);}template <class PLD>void AODVII<PLD>::prepare_rerr(bool send, ether_addr_t dst_addr, int dst_seq){    static packet_t* p = NULL;    if(send==true)    {        if(p!=NULL)	{	    to_mac_delay.Set(p, SimTime()+Random(ForwardDelay));			    p=NULL;	}	return;    }    if(p==NULL)    {	p = packet_t::alloc();	p->hdr.unreachable_count = 0;	p->hdr.type = RERR;	p->hdr.prev_hop_addr = MyEtherAddr;	p->hdr.size = RERR_SIZE;    }    p->hdr.unreachable_dsts[p->hdr.unreachable_count] = dst_addr;    p->hdr.unreachable_seqs[p->hdr.unreachable_count] = dst_seq;    p->hdr.size += ether_addr_t::LENGTH + sizeof(int);    p->hdr.unreachable_count++;    if(p->hdr.unreachable_count>=MAX_UNREACHABLE)    {	to_mac_delay.Set(p, SimTime()+Random(ForwardDelay));			p=NULL;    }}template <class PLD>void AODVII<PLD>::init_rerr(ether_addr_t neighbor){    Printf((DumpPackets,"%d AODVII: INIT RERR %d -> %d \n", (int)MyEtherAddr, (int)MyEtherAddr, (int)neighbor));    routing_table_t::iterator iter;    for(iter=m_routing_table.begin();iter!=m_routing_table.end();iter++)    {	if( (iter->second).next_hop==neighbor)	{	    iter->second.dst_seq_num++;	    prepare_rerr(false, iter->first, iter->second.dst_seq_num);	    iter->second.valid = false;	}    }    prepare_rerr(true);}template <class PLD>void AODVII<PLD>::receive_rerr(packet_t* p){    routing_table_t::iterator iter;    for(int i=0;i<p->hdr.unreachable_count;i++)    {	iter=m_routing_table.find(p->hdr.unreachable_dsts[i]);	if(iter!=m_routing_table.end()&&p->hdr.prev_hop_addr==iter->second.next_hop&&iter->second.valid)	{	    iter->second.valid = false;	    iter->second.dst_seq_num = p->hdr.unreachable_seqs[i];	    if(iter->second.src_addr==MyEtherAddr)		init_rreq(iter->first,TimeToLive,iter->second.dst_seq_num); 	    else		prepare_rerr(false, iter->first, iter->second.dst_seq_num);	}    }    prepare_rerr(true);}template <class PLD>void AODVII<PLD>::SendDataPacket(packet_t* p, ether_addr_t dst, route_entry_t& route, unsigned int size){    neighbor_table_t::iterator iter = m_neighbor_table.find(route.next_hop);    if(iter == m_neighbor_table.end())    {	//printf("%f: AODVII%d has never heard from AODVII%d\n", SimTime(), (int)MyEtherAddr, (int)route.next_hop);		p->free();    }    else if ( iter->second < SimTime() - AllowedHelloLoss * HelloInterval )    {	/*printf("%f: AODVII%d hasn't heard from AODVII%d since %f\n", SimTime(), (int)MyEtherAddr,	       (int)route.next_hop,iter->second); */	m_packet_buffer.push_back(p);	if(route.in_local_repair)	{	    if(route.local_repair_time > SimTime() - LocalRepairTime)		return;	}	if( !route.in_local_repair && p->hdr.hop_count < MaxRepairTTL )	{	    // local repair	    int ttl = MinRepairTTL>2*p->hdr.hop_count?MinRepairTTL:p->hdr.hop_count/2+LocalAddTTL;	    init_rreq(dst, ttl, route.dst_seq_num);	    route.in_local_repair=true;	    route.local_repair_time=SimTime();	}	else	{	    init_rerr(route.next_hop);	}    }    else    {	BufferPacket(p);    }}template <class PLD>void AODVII<PLD>::HelloTimer(trigger_t& ){    simtime_t now = SimTime();    hello_timer.Set(HelloInterval+now);    if(m_last_hello_sent<now-HelloInterval && ( m_last_data_recv > now - HelloInterval*AllowedHelloLoss ) )    {	packet_t* p = packet_t::alloc();	p->hdr.prev_hop_addr = MyEtherAddr;	p->hdr.type = HELLO;	p->hdr.size = HELLO_SIZE;	//printf("%f: AODVII%d sends a hello message\n",SimTime(),(int)MyEtherAddr);	BufferPacket(p);    }    //else	//printf("[%f]: %d AODVII: avoids a hello message: %f %f\n",SimTime(), (int)MyEtherAddr,m_last_hello_sent,m_last_data_recv);}template <class PLD>void AODVII<PLD>::UpdateRoutingTable(packet_t* pkt){    // check if forward route for destination exists and update -    // if not, create route    routing_table_t::iterator iter = m_routing_table.find(pkt->hdr.src_addr);    if(iter == m_routing_table.end())    {	// create new entry	route_entry_t t;	t.dst_seq_num = pkt->hdr.dst_seq_num;	t.valid = true;	t.hop_count = pkt->hdr.hop_count;	t.next_hop = pkt->hdr.prev_hop_addr;	t.src_addr = pkt->hdr.dst_addr;		iter=m_routing_table.insert(make_pair(pkt->hdr.src_addr, t)).first;	CheckBuffer(pkt->hdr.src_addr);    }    else    {	if( !iter->second.valid || 	    pkt->hdr.dst_seq_num > iter->second.dst_seq_num || 	    ( pkt->hdr.dst_seq_num == iter->second.dst_seq_num &&		 pkt->hdr.hop_count < iter->second.hop_count ) )	{	    // update entry	    iter->second.dst_seq_num = pkt->hdr.dst_seq_num;	    iter->second.valid = true;	    iter->second.hop_count = pkt->hdr.hop_count ;	    iter->second.next_hop = pkt->hdr.prev_hop_addr;	    iter->second.in_local_repair = false;	    CheckBuffer(pkt->hdr.src_addr);	}    }}template <class PLD>void AODVII<PLD>::CheckBuffer(ether_addr_t& dst_addr){    std::vector<packet_t*> packets;    packet_buffer_t::iterator iter,curr;    iter=m_packet_buffer.begin();    while(iter!=m_packet_buffer.end())    {	curr = iter;	iter ++;	if((*curr)->hdr.dst_addr==dst_addr)	{	    /*Printf((DumpPackets,"aodv%d sending buffered packet to %d %s\n",(int)MyEtherAddr,	      (int)fw_iter->second.next_hop,(*curr)->dump().c_str()));*/	    //SendDataPacket(*curr, p->hdr.src_addr, fw_iter->second, (*curr)->hdr.size);	    packets.push_back(*curr);	    m_packet_buffer.erase(curr);	}    }    for(unsigned int i=0;i<packets.size();i++)	BufferPacket(packets[i]);}template <class PLD>bool AODVII<PLD>::SendPacket(packet_t* pkt){    if(pkt->hdr.type==RREP||pkt->hdr.type==DATA)    {	routing_table_t::iterator iter = m_routing_table.find(pkt->hdr.dst_addr);	if(iter == m_routing_table.end())	{	    printf("[%f] %d AODVII: no route - %s\n",SimTime(),(int)MyEtherAddr, pkt->dump().c_str());	    pkt->free();	    return false;	}	else	{	    if(iter->second.valid==false)	    {		m_packet_buffer.push_back(pkt);		return false;	    }	    else	    {		SentPackets++;		m_last_hello_sent = SimTime();		pkt->inc_ref();		m_active_packet=pkt;		to_mac(pkt,iter->second.next_hop,pkt->hdr.size);	    }	}    }    else    {	SentPackets++;	m_last_hello_sent = SimTime();	pkt->inc_ref();	m_active_packet=pkt;	to_mac(pkt,ether_addr_t::BROADCAST,pkt->hdr.size);    }    return true;}template <class PLD>void AODVII<PLD>::BufferPacket(packet_t* pkt){    if(m_mac_busy==false)    {	m_active_packet=pkt;	m_mac_busy=true;	if(!SendPacket(pkt))	{	    m_mac_busy=false;	}    }    else    {	m_mac_queue.push_back(pkt);    }}template <class PLD>void AODVII<PLD>::RepairLink(const ether_addr_t& src_addr, const ether_addr_t& dst_addr, route_entry_t& route, int hop_count){    Printf((DumpPackets, "%d AODVII: link -> %d -> %d broken \n", (int)MyEtherAddr,	   (int)route.next_hop, (int)dst_addr));	bool repair_needed = (!route.in_local_repair) || (route.local_repair_time < SimTime() - LocalRepairTime);	if( ( hop_count < MaxRepairTTL || src_addr==MyEtherAddr ) && repair_needed ) 	{	    Printf((DumpPackets, "[%f] %d AODVII: local repair -> %d -> %d \n", (int)MyEtherAddr,		   (int)route.next_hop, (int)dst_addr));	    // local repair	    int ttl = MinRepairTTL>2*hop_count?MinRepairTTL:hop_count/2+LocalAddTTL;	    if(src_addr==MyEtherAddr) ttl = route.hop_count + LocalAddTTL;  	    route.dst_seq_num ++;	    	    init_rreq(dst_addr, ttl, route.dst_seq_num);	    route.in_local_repair=true;	    route.local_repair_time=SimTime();	    route.valid=false;	}	else	{	    if( repair_needed )	    {		route.valid=false;		route.in_local_repair=false;		init_rerr(route.next_hop);	    }	}	repair_timer.Set(dst_addr,SimTime()+LocalRepairTime);	}template <class PLD>void AODVII<PLD>::RepairTimer(ether_addr_t& dst_addr, unsigned int){    Printf((DumpPackets,"%d AODVII: repair link to %d\n", (int)MyEtherAddr, (int)dst_addr));        packet_buffer_t::iterator pb_iter=m_packet_buffer.begin();    for(;pb_iter!=m_packet_buffer.end();pb_iter++)    {	if( (*pb_iter)->hdr.dst_addr==dst_addr && (*pb_iter)->hdr.type==DATA )	{	    routing_table_t::iterator iter = m_routing_table.find(dst_addr);	    assert(iter!=m_routing_table.end());	    if(iter->second.valid==false)	    {		RepairLink(MyEtherAddr,dst_addr,iter->second,0);		return;	    }	}    }    }template <class PLD>void AODVII<PLD>::from_mac_ack (bool success){    if(!success)    {	assert(m_active_packet!=NULL);	assert(m_active_packet->hdr.type==DATA||m_active_packet->hdr.type==RREP);	routing_table_t::iterator iter = m_routing_table.find(m_active_packet->hdr.dst_addr);	assert(iter!=m_routing_table.end());	RepairLink(m_active_packet->hdr.src_addr, iter->first,iter->second,iter->second.hop_count);	m_packet_buffer.push_back(m_active_packet);    }    else    {		m_active_packet->free();    }    while(!m_mac_queue.empty())    {	m_active_packet=m_mac_queue.front();	m_mac_queue.pop_front();	if(SendPacket(m_active_packet))	    return;    }        if(m_mac_queue.empty())    {	m_mac_busy=false;    }}/*** The inport that is called when the power manager wakes the node up.** This must be defined even if the protocol doesn't use it.*/template <class PLD>void AODVII<PLD>::from_pm_node_up(){  return;}template <class PLD>bool AODVII_Struct<PLD>::hdr_struct::dump(std::string& str) const {     char buffer[100];    std::string t;    switch(type)    {    case RREQ:        t="REQUEST";	break;        case RREP:        t="REPLY";	break;    case DATA:        t="DATA";	break;    case HELLO:        t="HELLO";	break;    case RERR:	t="ERR";	break;    default:	t="UNKNOWN";	break;    }    sprintf(buffer,"%s %d %d %d %d %d->%d %d (%d %d) ",	    t.c_str(),TTL,hop_count, rreq_id, (int)prev_hop_addr,	    (int)src_addr,(int)dst_addr,data_packet_id, src_seq_num,dst_seq_num);    str=buffer;    return type==DATA;}#endif /* AODVII_H */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -