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

📄 almah.cc

📁 为NS2仿真中的一个overlay多播协议的协议模块
💻 CC
📖 第 1 页 / 共 3 页
字号:
        hdr_almah* hdr = hdr_almah::access(pkt);
        hdr_cmn* ch = HDR_CMN(pkt);
        hdr_ip* ip = hdr_ip::access(pkt);
		OBAMP_Overlay_Neighbor *nb;
		OBAMP_Overlay_Neighbor *nbn;
        int id_ = ch->uid();
        int src_ = ip->saddr();
        OBAMP_Overlay_Broadcast_Cache *b;
		this->d_purge();
		b = this->d_lookup(id_);
		 if(b == NULL){
			this->d_insert(src_, ip->ttl_, ch->uid_);
			nb = nbhead.lh_first;
			if(nb!=0){
				for(; nb; nb = nbn) {
					nbn = nb->nb_link.le_next;
					int dst = nb->nb_addr;
					if(ip->saddr()!=dst && dst != hdr->src_IP){	              
						Packet* new_pkt = pkt->copy();
						hdr_ip* ip_new = hdr_ip::access(new_pkt);
						hdr_cmn* ch_new = hdr_cmn::access(new_pkt);
						ip_new->daddr()=dst;
						ip_new->saddr()=here_.addr_;
						ch_new->direction() = hdr_cmn::DOWN;
						ch_new->num_forwards() = 0;
						ch_new->size_=size_;
						send(new_pkt,0);
					}
				}	
			}
			int dst1 = parent_in_switch;
			if(ip->saddr()!=dst1 && dst1 != -1 && dst1 != hdr->src_IP){	// in case of data src_IP is used in sence of last hop address              
						Packet* new_pkt1 = pkt->copy();
						hdr_ip* ip_new1 = hdr_ip::access(new_pkt1);
						hdr_cmn* ch_new1 = hdr_cmn::access(new_pkt1);
						ip_new1->daddr()=dst1;
						ip_new1->saddr()=here_.addr_;
						ch_new1->direction() = hdr_cmn::DOWN;
						ch_new1->num_forwards() = 0;
						ch_new1->size_=size_;
						send(new_pkt1,0);
			}
			int dst2 = parent_in;
			if(ip->saddr()!=dst2 && dst2 != -1 && dst2 != hdr->src_IP){	// in case of data src_IP is used in sence of last hop address              
						Packet* new_pkt2 = pkt->copy();
						hdr_ip* ip_new2 = hdr_ip::access(new_pkt2);
						hdr_cmn* ch_new2 = hdr_cmn::access(new_pkt2);
						ip_new2->daddr()=dst2;
						ip_new2->saddr()=here_.addr_;
						ch_new2->direction() = hdr_cmn::DOWN;
						ch_new2->num_forwards() = 0;
						ch_new2->size_=size_;
						send(new_pkt2,0);
			}
        } 
		Packet::free(pkt);
        return ;                             
}

void ALMAH_Agent::sendLOOP_RESET(){

		OBAMP_Overlay_Neighbor *nb;
		OBAMP_Overlay_Neighbor *nbn;
		nb = nbhead.lh_first;
		if(nb!=0){
			for(; nb; nb = nbn) {
				nbn = nb->nb_link.le_next;
				int dst = nb->nb_addr;
				Packet* pkt = allocpkt();
				hdr_almah* hdrret = hdr_almah::access(pkt);
				hdr_cmn* ch = HDR_CMN(pkt);
				hdr_ip* ipret = hdr_ip::access(pkt);
				hdrret->Message_ID = 24;
				hdrret->src_IP = here_.addr_;
				ipret->daddr() = dst;
				ipret->saddr() = here_.addr_;
				ipret->dport() = ipret->sport();
				ch->size_=8+8;
				send(pkt,0);				
			}	
		}	
        return ;                             
}

void ALMAH_Agent::recvLOOP_RESET(Packet* pkt){
    hdr_ip* hdrip = hdr_ip::access(pkt);
	if(hdrip->saddr() == this->parent_in){			
			if(Agents[parent_in]->parent_in!=index)	print_nam(index, parent_in,"background");
			SWITCHING = false;
			ON_PingTimer = 0;
			parent_in = -1;
			parent_in_switch = -1;					
			htimer.count = 0;
			if(!htimer.is_busy)	Scheduler::instance().schedule(&htimer,(new Event),0);
			this->sendLOOP_RESET();
			printf("loop reset at node :   %d\n", index);						
	}    
	sendLOOP_RESET_ACK(hdrip->src_.addr_);	
    Packet::free(pkt);
    return;
}

void ALMAH_Agent::sendLOOP_RESET_ACK(int node){
		

        Packet* pkt = allocpkt();
        hdr_almah* hdrret = hdr_almah::access(pkt);
        hdr_cmn* ch = HDR_CMN(pkt);
        hdr_ip* ipret = hdr_ip::access(pkt);
        hdrret->Message_ID = 25;
        hdrret->src_IP = here_.addr_;
        ipret->daddr() = node;
		ipret->saddr() = index;
        ipret->dport() = ipret->sport();
		ch->size_=8+8;
        send(pkt, 0);
		return;
}

void ALMAH_Agent::recvLOOP_RESET_ACK(Packet* pkt, Handler*){        		
		hdr_ip* ipret = hdr_ip::access(pkt);
		int node;
		node = ipret->saddr();
		nb_delete(node);
		LOOP_RESET_ = false;
        Packet::free(pkt);
    return;
}



void
ALMAH_Agent::print_nam(int src_,int dst_,char* color ) {
	if(ON_PRINT_NAM == 1){
        Tcl_Channel namChan_;
		int mode,n;
		Tcl& tcl = Tcl::instance();
		namChan_ = Tcl_GetChannel(tcl.interp(),Tcl_GetVar(tcl.interp(),"chan",TCL_GLOBAL_ONLY), &mode);
		sprintf(nwrk_,
        "l -t %f -s %d -d %d -S COLOR -r 2000000 -D 0.01 -c %s -O right-down",
			  Scheduler::instance().clock(),
			  src_,
			  dst_,
        color
			  );
		n = strlen(nwrk_);
		nwrk_[n] = '\n';
		nwrk_[n+1] = 0;
		Tcl_Write(namChan_,nwrk_,n+1);
      }
 return;
}

        
void
ALMAH_Agent::nb_insert(nsaddr_t id) {
OBAMP_Overlay_Neighbor *nb = new OBAMP_Overlay_Neighbor(id);
    nb->distance = Agents[id]->req_last_ttl;
    assert(nb);
    nb->nb_expire = CURRENT_TIME +
                + 1.5*2*TREE_INT;
    LIST_INSERT_HEAD(&nbhead, nb, nb_link);
    this->print_nam(index, id,"red");
}

OBAMP_Overlay_Neighbor* ALMAH_Agent::nb_lookup(nsaddr_t id) {	
	
	OBAMP_Overlay_Neighbor *nb = nbhead.lh_first;
	OBAMP_Overlay_Neighbor *res = NULL; 
	if (nb!=0) {
		for(; nb; nb = nb->nb_link.le_next) {
			if(nb->nb_addr == id) {
				res=nb;
				break;
			}
		 }
	}
	return res;
}


void
ALMAH_Agent::nb_delete(nsaddr_t id) {
OBAMP_Overlay_Neighbor *nb = nbhead.lh_first;

if (nb!=0) {
	 for(; nb; nb = nb->nb_link.le_next) {
	   if(nb->nb_addr == id) {
		 LIST_REMOVE(nb,nb_link);
		 if (Agents[nb->nb_addr]->nb_lookup(index)==NULL) print_nam(index, id,"background");
		 delete nb;
		 break;
	   }
	 }
 }
}


void
ALMAH_Agent::b_insert(int src, int TTL_, int Uid_) {

	OBAMP_Overlay_Broadcast_Cache *b = new OBAMP_Overlay_Broadcast_Cache(src, TTL_, Uid_);
	assert(b);
	b->b_expire = CURRENT_TIME + expiry_interval;
	LIST_INSERT_HEAD(&bhead, b, b_pck);

}

OBAMP_Overlay_Broadcast_Cache* ALMAH_Agent::b_lookup(int Uid_) {

	OBAMP_Overlay_Broadcast_Cache *b = bhead.lh_first;

	for(; b; b = b->b_pck.le_next) {
		if(b->pkt_id == Uid_) break;
	}
	return b;
}


void
ALMAH_Agent::b_delete(int Uid_) {
	OBAMP_Overlay_Broadcast_Cache *b = bhead.lh_first;

 for(; b; b = b->b_pck.le_next) {
   if(b->pkt_id == Uid_) {
     LIST_REMOVE(b,b_pck);
     delete b;
     break;
   }
 }
}


void ALMAH_Agent::b_purge() {
	OBAMP_Overlay_Broadcast_Cache *b = bhead.lh_first;
	OBAMP_Overlay_Broadcast_Cache *bn;
	OBAMP_Overlay_Broadcast_Cache *bn2; // Andrea
	double now = CURRENT_TIME;
	bn2=NULL;
	for(; b; b = bn) {
		bn = b->b_pck.le_next;
		if(bn!=NULL && bn->b_expire <= now) {
			b->b_pck.le_next=NULL;
			bn2=bn;
			break;
		}
	}
	if (bn2!=NULL) {
		for (; bn2; bn2=bn) {
			bn=bn2->b_pck.le_next;
			delete bn2;
		}
	}
}

void
ALMAH_Agent::d_insert(int src, int TTL_, int Uid_) {

	OBAMP_Overlay_Broadcast_Cache *b = new OBAMP_Overlay_Broadcast_Cache(src, TTL_, Uid_);
	assert(b);
	b->b_expire = CURRENT_TIME + expiry_interval;
	LIST_INSERT_HEAD(&dhead, b, b_pck);
}

OBAMP_Overlay_Broadcast_Cache* ALMAH_Agent::d_lookup(int Uid_) {

	OBAMP_Overlay_Broadcast_Cache *b = dhead.lh_first;
	for(; b; b = b->b_pck.le_next) {
		if(b->pkt_id == Uid_) break;
	}
	return b;
}

void
ALMAH_Agent::d_delete(int Uid_) {
  	 OBAMP_Overlay_Broadcast_Cache *b = dhead.lh_first;
	 for(; b; b = b->b_pck.le_next) {
	   if(b->pkt_id == Uid_) {
	     LIST_REMOVE(b,b_pck);
	     delete b;
	     break;
	   }
	 }
}

void ALMAH_Agent::d_purge() {
	OBAMP_Overlay_Broadcast_Cache *b = dhead.lh_first;
	OBAMP_Overlay_Broadcast_Cache *bn;
	OBAMP_Overlay_Broadcast_Cache *bn2; // Andrea
	double now = CURRENT_TIME;
	bn2=NULL;
	for(; b; b = bn) {
		bn = b->b_pck.le_next;
		if(bn!=NULL && bn->b_expire <= now) {
			b->b_pck.le_next=NULL;
			bn2=bn;
			break;
		}
	}
	if (bn2!=NULL) {
		for (; bn2; bn2=bn) {
			bn=bn2->b_pck.le_next;
			delete bn2;
		}
	}

}


void
ALMAHSendRequestTimer::handle(Event*) {
  
	if(1){
		if(1 && agent->index!=0){
			is_busy = false;
			if(count==0){
				agent->req_last_ttl = agent->I_TTL;
				request_number=0;
				agent->found_neighbour = false;
				agent->sendJOIN_REQ();
				double intervall = 3*10*T_TX_MAX*agent->req_last_ttl;
				Scheduler::instance().schedule(this, &intr, intervall);
				is_busy = true;            
				count++;
			}
        request_number++;
        if(request_number > 1 && agent->found_neighbour == false){ 
			
			agent->req_last_ttl +=agent->INTERVAL_TTL; // Andrea JOIN ReQ
			agent->req_last_ttl=min(agent->MAX_TTL,agent->req_last_ttl);

            if(agent->req_last_ttl<=agent->MAX_TTL){
                agent->sendJOIN_REQ();
                double intervall = 3*10*T_TX_MAX*agent->req_last_ttl;
                Scheduler::instance().schedule(this, &intr, intervall);
				is_busy = true;
			}
        }
	}
  }

}

void
ALMAH_TOPOLOGY_REQUEST_TIMER::handle(Event* h) {
				  Packet* pkt = (Packet*)h;
				  int node;
				  hdr_ip* ipret = hdr_ip::access(pkt);
				  node = ipret->daddr();
				  if(agent->REQUEST_TOPOLOGY_STATUS == true){
					  Packet* pkt = (Packet*)h;
					  Packet* new_pkt = pkt->copy();
					  agent->send(pkt,0);
					  Scheduler::instance().schedule(this, new_pkt, 1);
				  }
}

void
ALMAHPingTimer::handle(Event* e) {
   if(1){
      if(agent->ON_PingTimer==1){      		 
		  if(agent->parent_in!=-1){
				agent->sendHello(agent->parent_in);
		  }
		  Scheduler::instance().schedule(this, &intr, agent->TREE_INT);
      }else
		  Scheduler::instance().schedule(this, &intr, 1);
   }
}

void
ALMAHSendSwitchRequestTimer::handle(Event* h) {

				  Packet* pkt = (Packet*)h;
				  int node;
				  hdr_ip* ipret = hdr_ip::access(pkt);
				  node = ipret->daddr();
				  if(agent->NACKING || agent->SWITCHING){
					  Packet* pkt = (Packet*)h;
					  Packet* new_pkt = pkt->copy();
					  agent->send(pkt,0);
					  Scheduler::instance().schedule(this, new_pkt, 1);

				  }else{ 
					  	Packet* pkt = (Packet*)h;
						Packet::free(pkt);						
				  }
		
}

void
ALMAH_LOOP_RESET::handle(Event* h) {

			Packet* pkt = (Packet*)h;
			int node;
			hdr_ip* ipret = hdr_ip::access(pkt);
			node = ipret->daddr();
			if(agent->LOOP_RESET_){
					Packet* pkt = (Packet*)h;
					Packet* new_pkt = pkt->copy();
					agent->send(pkt,0);
					Scheduler::instance().schedule(this, new_pkt, 1);

			}else{ 
					Packet* pkt = (Packet*)h;
					Packet::free(pkt);						
			}	

}

void ALMAHSwitchStartTimer::handle(Event* h) {
	o_agent->switch_parent();		
	return;
}

void
ALMAHSwitchEndTimer::handle(Event*) {
	agent->switch_time_on = false;
}

void
ALMAH_Agent::mb_insert(nsaddr_t id) {

	OBAMP_Overlay_Neighbor *nb = new OBAMP_Overlay_Neighbor(id);
    nb->distance = Agents[id]->req_last_ttl;
    assert(nb);
    nb->nb_expire = CURRENT_TIME + 1000;
    LIST_INSERT_HEAD(&mbhead, nb, nb_link);
}


OBAMP_Overlay_Neighbor* ALMAH_Agent::mb_lookup(nsaddr_t id) {
	OBAMP_Overlay_Neighbor *nb = mbhead.lh_first;
	if(nb!=NULL)	
	 for(; nb; nb = nb->nb_link.le_next) {
	   if(nb->nb_addr == id) break;
	 }
	 return nb;
}

void
ALMAH_Agent::mb_delete(nsaddr_t id) {
OBAMP_Overlay_Neighbor *nb = mbhead.lh_first;
 for(; nb; nb = nb->nb_link.le_next) {
   if(nb->nb_addr == id) {
     LIST_REMOVE(nb,nb_link);
     delete nb;
     break;
   }
 }

}


void
ALMAH_Agent::mb_purge() {
OBAMP_Overlay_Neighbor *nb = mbhead.lh_first;
OBAMP_Overlay_Neighbor *nbn;
double now = CURRENT_TIME;

 for(; nb; nb = nbn) {
   nbn = nb->nb_link.le_next;
   if(nb->nb_expire <= now) {
     mb_delete(nb->nb_addr);
   }
 }
}

void
ALMAH_Agent::mb_purge_all() {

 OBAMP_Overlay_Neighbor *nb = mbhead.lh_first;
 OBAMP_Overlay_Neighbor *nbn;
 for(; nb; nb = nbn) {
   nbn = nb->nb_link.le_next;
   mb_delete(nb->nb_addr);
 }
 mbhead.lh_first = NULL;
	
}

void
ALMAH_Agent::mb_initialize() {
for(int i =0; i<this->M_cast; i++) {
        if(i != index) {
            OBAMP_Overlay_Neighbor *nb = new OBAMP_Overlay_Neighbor(i);
            nb->nb_expire = CURRENT_TIME + expiry_interval;
            LIST_INSERT_HEAD(&mbhead, nb, nb_link);
        }
    }
}

⌨️ 快捷键说明

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