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

📄 cmu-trace.cc

📁 此代码是基于moav协议的qos改进代码
💻 CC
📖 第 1 页 / 共 3 页
字号:
                abort();        }}voidCMUTrace::nam_format(Packet *p, int offset){	Node* srcnode = 0 ;	Node* dstnode = 0 ;	Node* nextnode = 0 ;        struct hdr_cmn *ch = HDR_CMN(p);	struct hdr_ip *ih = HDR_IP(p);	char op = (char) type_;	char colors[32];	int next_hop = -1 ;// change wrt Mike's code	assert(type_ != EOT);	//<zheng: add for 802.15.4>	//Actually we only need to handle MAC layer for nam (but should display dropping for other layers)	//if (strcmp(tracename,"MAC") != 0)	//if ((op != 'D')&&(op != 'd'))	//	return;	struct hdr_mac802_11 *mh = HDR_MAC802_11(p);	char ptype[11];	strcpy(ptype,	((ch->ptype() == PT_MAC) ? (	  (mh->dh_fc.fc_subtype == MAC_Subtype_RTS) ? "RTS"  :	  (mh->dh_fc.fc_subtype == MAC_Subtype_CTS) ? "CTS"  :	  (mh->dh_fc.fc_subtype == MAC_Subtype_ACK) ? "ACK"  :	  (mh->dh_fc.fc_subtype == MAC_Subtype_Beacon) ? "BCN"  :		//Beacon	  (mh->dh_fc.fc_subtype == MAC_Subtype_Command_AssoReq) ? "CM1"  :	//CMD: Association request	  (mh->dh_fc.fc_subtype == MAC_Subtype_Command_AssoRsp) ? "CM2"  :	//CMD: Association response	  (mh->dh_fc.fc_subtype == MAC_Subtype_Command_DAssNtf) ? "CM3"  :	//CMD: Disassociation notification	  (mh->dh_fc.fc_subtype == MAC_Subtype_Command_DataReq) ? "CM4"  :	//CMD: Data request	  (mh->dh_fc.fc_subtype == MAC_Subtype_Command_PIDCNtf) ? "CM5"  :	//CMD: PAN ID conflict notification	  (mh->dh_fc.fc_subtype == MAC_Subtype_Command_OrphNtf) ? "CM6"  :	//CMD: Orphan notification	  (mh->dh_fc.fc_subtype == MAC_Subtype_Command_BconReq) ? "CM7"  :	//CMD: Beacon request	  (mh->dh_fc.fc_subtype == MAC_Subtype_Command_CoorRea) ? "CM8"  :	//CMD: Coordinator realignment	  (mh->dh_fc.fc_subtype == MAC_Subtype_Command_GTSReq) ? "CM9"  :	//CMD: GTS request	  "UNKN"	  ) : packet_info.name(ch->ptype())));	//</zheng: add for 802.15.4>        int dst = Address::instance().get_nodeaddr(ih->daddr());	nextnode = Node::get_node_by_address(ch->next_hop_);        if (nextnode) next_hop = nextnode->nodeid(); 	srcnode = Node::get_node_by_address(src_);	dstnode = Node::get_node_by_address(ch->next_hop_);	double distance = 0;        if ((srcnode) && (dstnode)) {	   MobileNode* tmnode = (MobileNode*)srcnode;	   MobileNode* rmnode = (MobileNode*)dstnode;	   distance = tmnode->propdelay(rmnode) * 300000000 ;	}	double energy = -1;	double initenergy = -1;	//default value for changing node color with respect to energy depletion	double l1 = 0.5; 	double l2 = 0.2;		if (srcnode) {	    if (srcnode->energy_model()) {		    energy = srcnode->energy_model()->energy();		    initenergy = srcnode->energy_model()->initialenergy();		    l1 = srcnode->energy_model()->level1();		    l2 = srcnode->energy_model()->level2();	    }	}        int energyLevel = 0 ;        double energyLeft = (double)(energy/initenergy) ;        if ((energyLeft <= 1 ) && (energyLeft >= l1 )) energyLevel = 3;	        if ((energyLeft >= l2 ) && (energyLeft < l1 )) energyLevel = 2;	        if ((energyLeft > 0 ) && (energyLeft < l2 )) energyLevel = 1;		if (energyLevel == 0) 		strcpy(colors,"-c black -o red");        else if (energyLevel == 1) 		strcpy(colors,"-c red -o yellow");        else if (energyLevel == 2) 		strcpy(colors,"-c yellow -o green");        else if (energyLevel == 3) 		strcpy(colors,"-c green -o black");	// A simple hack for scadds demo (fernandez's visit) -- Chalermek	int pkt_color = 0;	if (ch->ptype()==PT_DIFF) {		hdr_cdiff *dfh= HDR_CDIFF(p);		if (dfh->mess_type != DATA) {			pkt_color = 1;		}	}	//<zheng: add for 802.15.4>	if (Nam802_15_4::Nam_Status)	{		ATTRIBUTELINK *attr;		int t_src,t_dst;		if (ch->ptype() == PT_MAC)		{			t_src = p802_15_4macSA(p);			t_dst = p802_15_4macDA(p);;		}		else		{			t_src = HDR_IP(p)->saddr();			t_dst = HDR_IP(p)->daddr();		}		attr = findAttrLink(HDR_CMN(p)->ptype(),t_src,t_dst);		if (attr == NULL)			attr = findAttrLink(HDR_CMN(p)->ptype());		if (attr != NULL)			HDR_LRWPAN(p)->attribute = attr->attribute;		else			HDR_LRWPAN(p)->attribute = 0;		if (HDR_LRWPAN(p)->attribute >= 32)			pkt_color = HDR_LRWPAN(p)->attribute;	}	//</zheng: add for 802.15.4>	// convert to nam format 	if (op == 's') op = 'h' ;	if (op == 'D') op = 'd' ;	if (op == 'h') {		sprintf(pt_->nbuffer(),			"+ -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s ",			Scheduler::instance().clock(),			src_,                           // this node			next_hop,			ptype,			//<zheng: modify for 802.15.4>packet_info.name(ch->ptype()),			ch->size(),			pkt_color,   			ch->uid(),			tracename);		offset = strlen(pt_->nbuffer());		pt_->namdump();		sprintf(pt_->nbuffer() ,			"- -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s",			Scheduler::instance().clock(),			src_,                           // this node			next_hop,			ptype,			//<zheng: modify for 802.15.4>packet_info.name(ch->ptype()),			ch->size(),			pkt_color,			ch->uid(),			tracename);				offset = strlen(pt_->nbuffer());		pt_->namdump();	}        // if nodes are too far from each other	// nam won't dump SEND event 'cuz it's	// gonna be dropped later anyway	// this value 250 is pre-calculated by using 	// two-ray ground refelction model with fixed	// transmission power 3.652e-10//	if ((type_ == SEND)  && (distance > 250 )) return ;	if(tracetype == TR_ROUTER && type_ == RECV && dst != -1 ) return ;	if(type_ == RECV && dst == -1 )dst = src_ ; //broadcasting event        if (energy != -1) { //energy model being turned on	   if (src_ >= MAX_NODE) {		   fprintf (stderr, "node id must be < %d\n",			    MAX_NODE);	       exit(0);	   }	   if (nodeColor[src_] != energyLevel ) { //only dump it when node  	       sprintf(pt_->nbuffer() ,                    //color change	          "n -t %.9f -s %d -S COLOR %s",	           Scheduler::instance().clock(),	           src_,                           // this node	           colors);               offset = strlen(pt_->nbuffer());               pt_->namdump();	       nodeColor[src_] = energyLevel ;	    }           }	sprintf(pt_->nbuffer() ,		"%c -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s",		op,		Scheduler::instance().clock(),		src_,                           // this node		next_hop,		ptype,			//<zheng: modify for 802.15.4>packet_info.name(ch->ptype()),		ch->size(),		pkt_color,		ch->uid(),		tracename);//<zheng: ns 2.27 removed the following part, but we need it to control the broadcast radius>if (Nam802_15_4::Nam_Status){	if ((strcmp(tracename, "AGT") != 0) || ((u_int32_t)(ih->daddr()) == IP_BROADCAST))		//<zheng: add: next_hop info not available at agent level>											//(doesn't really matter -- seems agent level has no effect on nam)	if (next_hop == -1 && op == 'h') {		// print extra fields for broadcast packets		// bradius is calculated assuming 2-ray ground reflectlon		// model using default settings of Phy/WirelessPhy and		// Antenna/OmniAntenna		if (bradius == 0.0) calculate_broadcast_parameters();		double radius = bradius*radius_scaling_factor_; 		// duration is calculated based on the radius and		// the speed of light (299792458 m/s)		double duration = (bradius/299792458.0)*duration_scaling_factor_;		//<zheng: add -- the duration in 802.15.4 could be very small and rounded to 0.0>		if (Nam802_15_4::Nam_Status)		if (duration < 0.000000001)			duration = 0.000000001;		//</zheng: add>		sprintf(pt_->nbuffer() + strlen(pt_->nbuffer()),			" -R %.2f -D %.2f",			radius,			duration);	}}//</zheng>	offset = strlen(pt_->nbuffer());	pt_->namdump();}void CMUTrace::format(Packet* p, const char *why){	hdr_cmn *ch = HDR_CMN(p);	int offset = 0;	/*	 * Log the MAC Header	 */	format_mac_common(p, why, offset);	if (pt_->namchannel()) 		nam_format(p, offset);	offset = strlen(pt_->buffer());	switch(ch->ptype()) {	case PT_MAC:	case PT_SMAC:		break;	case PT_ARP:		format_arp(p, offset);		break;	default:		format_ip(p, offset);		offset = strlen(pt_->buffer());		switch(ch->ptype()) {		case PT_AODV:			format_aodv(p, offset);			break;		case PT_TORA:                        format_tora(p, offset);                        break;                case PT_IMEP:                        format_imep(p, offset);                        break;		case PT_DSR:			format_dsr(p, offset);			break;		case PT_MESSAGE:		case PT_UDP:			format_msg(p, offset);			break;		case PT_TCP:		case PT_ACK:			format_tcp(p, offset);			break;		case PT_SCTP:			/* Armando L. Caro Jr. <acaro@@cis,udel,edu> 6/5/2002			 */			format_sctp(p, offset);			break;		case PT_CBR:			format_rtp(p, offset);			break;	        case PT_DIFF:			break;		case PT_GAF:		case PT_PING:			break;		default:		/*<zheng: del -- there are many more new packet types added, like PT_EXP (poisson traffic belongs to this type)>			fprintf(stderr, "%s - invalid packet type (%s).\n",				__PRETTY_FUNCTION__, packet_info.name(ch->ptype()));			exit(1);		</zheng: del>*/			break;		//zheng: add		}	}}intCMUTrace::command(int argc, const char*const* argv){	        if(argc == 3) {                if(strcmp(argv[1], "node") == 0) {                        node_ = (MobileNode*) TclObject::lookup(argv[2]);                        if(node_ == 0)                                return TCL_ERROR;                        return TCL_OK;                }		if (strcmp(argv[1], "newtrace") == 0) {			newtrace_ = atoi(argv[2]);		        return TCL_OK;		}        }	return Trace::command(argc, argv);}/*ARGSUSED*/voidCMUTrace::recv(Packet *p, Handler *h){	if (!node_energy()) {		Packet::free(p);		return;	}        assert(initialized());        /*         * Agent Trace "stamp" the packet with the optimal route on         * sending.         */        if (tracetype == TR_AGENT && type_ == SEND) {                God::instance()->stampPacket(p);        }#if 0        /*         * When the originator of a packet drops the packet, it may or may         * not have been stamped by GOD.  Stamp it before logging the         * information.         */        if(src_ == src && type_ == DROP) {                God::instance()->stampPacket(p);        }#endif	format(p, "---");	pt_->dump();	//namdump();	if(target_ == 0)		Packet::free(p);	else		send(p, h);}voidCMUTrace::recv(Packet *p, const char* why){        assert(initialized() && type_ == DROP);	if (!node_energy()) {		Packet::free(p);		return;	}#if 0        /*         * When the originator of a packet drops the packet, it may or may         * not have been stamped by GOD.  Stamp it before logging the         * information.         */        if(src_ == ih->saddr()) {                God::instance()->stampPacket(p);        }#endif	format(p, why);	pt_->dump();	//namdump();	Packet::free(p);}int CMUTrace::node_energy(){	Node* thisnode = Node::get_node_by_address(src_);	double energy = 1;	if (thisnode) {		if (thisnode->energy_model()) {			energy = thisnode->energy_model()->energy();		}	} 	if (energy > 0) return 1;	return 0;}//<zheng: ns 2.27 removed the following part, but we need it to control the broadcast radius>void CMUTrace::calculate_broadcast_parameters() {	// Calculate the maximum distance at which a packet can be received	// based on the two-ray reflection model using the current default	// values for Phy/WirelessPhy and Antenna/OmniAntenna.	double P_t, P_r, G_t, G_r, h, L;	Tcl& tcl = Tcl::instance();	tcl.evalc("Phy/WirelessPhy set Pt_");	P_t = atof(tcl.result());	tcl.evalc("Phy/WirelessPhy set RXThresh_");	P_r = atof(tcl.result());	tcl.evalc("Phy/WirelessPhy set L_");	L = atof(tcl.result());	tcl.evalc("Antenna/OmniAntenna set Gt_");	G_t = atof(tcl.result());	tcl.evalc("Antenna/OmniAntenna set Gr_");	G_r = atof(tcl.result());	tcl.evalc("Antenna/OmniAntenna set Z_");	h = atof(tcl.result());	bradius = pow(P_t*G_r*G_t*pow(h,4.0)/(P_r*L), 0.25);	//<zheng: add for 802.15.4>	//the above calculation is not accurate for short distance	double PI,freq,lambda,crossover_dist;	PI = 3.14159265359;	tcl.evalc("Phy/WirelessPhy set freq_");	freq = atof(tcl.result());	lambda = 3.0e8/freq;	crossover_dist = (4 * PI * h * h) / lambda;	if (bradius < crossover_dist)	//need re-calculation		bradius = pow(P_t * G_r * G_t * pow(lambda, 2.0)/(P_r * L), 0.5)/(4 * PI);	//</zheng: add for 802.15.4>	// Also get the scaling factors	tcl.evalc("CMUTrace set radius_scaling_factor_");	radius_scaling_factor_ = atof(tcl.result());	tcl.evalc("CMUTrace set duration_scaling_factor_");	duration_scaling_factor_ = atof(tcl.result());}//</zheng>

⌨️ 快捷键说明

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