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

📄 ramimo_proc_par.pr.c

📁 用OPNET实现802.11MAC协议DCF协议
💻 C
📖 第 1 页 / 共 5 页
字号:
					/*Work out the propagation delay between the source to destination node*/
				    propdelay = distance/300000000.0;
					
					/*Determine the expected duration of the subsequent exchange of data*/
					/*This is the sum of the packet durations less RTS, two SIFS and 3 propdelays*/
				    tempdouble = ((ctspktsize+datapktsize+ackpktsize)/datarate) + (2.0*sifs) + (3.0*propdelay); 
					
					/*Set the fields of the CTS packet*/
					op_pk_nfd_set(pktptr, "SRC", userid);
					op_pk_nfd_set(pktptr, "DEST", commnodeid);
					op_pk_nfd_set(pktptr, "DURATION", tempdouble);
					op_pk_nfd_set(pktptr, "number", sequencenum);
					
					#ifdef PRINT_TX
						    printf("PARENT PROC\n");
				    	printf("Transmitting a CTS Packet\n");
					    printf("Packet Size = %d\n", op_pk_total_size_get(pktptr));
					    op_pk_nfd_get(pktptr, "SRC", &srcid);
					    printf("Packet Source = %d\n", srcid);
					    op_pk_nfd_get(pktptr, "DEST", &destid);
					    printf("Packet Destination = %d\n", destid);
					    printf("Distance Between Nodes = %lf\n", distance);
					    printf("Propagation Delay Between Nodes = %1.10lf\n", propdelay);
					    op_pk_nfd_get(pktptr, "DURATION", &tempdouble);
					    printf("Packet Duration Field = %lf\n", tempdouble);
					 	op_pk_nfd_get(pktptr, "number", &tempdouble);
					    printf("Packet Sequence Number = %lf\n", tempdouble);	
				    #endif
				}
				/*If transmission of a DATA packet*/
				else if(DATA)
				{
				    /*Access the relevant packet in the subqueue*/
				    i = 0; 
				
				    do
					{
				        /*Access the packet at the new position*/
					    origpktptr = op_subq_pk_access(SRC_QUEUE, i);
						
						/*Determine the ID of the corresponding source node*/
						op_pk_nfd_get(origpktptr, "SRC", &srcid);
				
						/*Increment the index variable*/
						i++;
						
					} while(srcid != sharedptr->partxsrcid);
				    
					/*Create a copy of the packet for transmission*/
				    pktptr = op_pk_copy(origpktptr);
				
				  	/*Determine the location of the destination node*/
				    other_node_id = op_id_from_userid(subnet_id, OPC_OBJTYPE_NDMOB, commnodeid); 
					
					op_ima_obj_attr_get(other_node_id, "x position", &other_xpos);
				    op_ima_obj_attr_get(other_node_id, "y position", &other_ypos);
				
					/*Work out the distance between nodes*/
				    distance = sqrt((other_xpos - xpos)*(other_xpos - xpos) + (other_ypos - ypos)*(other_ypos - ypos));
					
					/*Work out the propagation delay between the source to destination node*/
				    propdelay = distance/300000000.0;
					
					/*Schedule an interrupt for timeout if a CTS packet is not received*/
					tempdouble = 1.1 * ((datapktsize/datarate) + (2.0*propdelay) + (ackpktsize/datarate) + sifs);
					timeout = op_intrpt_schedule_self(op_sim_time() + tempdouble, TX_TIMEOUT_INT);
				
				 	/*Record statistics if from node 0 to 3*/
				    /*BOTH THESE STATISTICS ARE CONTRIVED FOR SPECIFIC SCENARIO*/
					if( (userid == 0) && (commnodeid == 9) )
				        op_stat_write_t(txstat, 3, op_sim_time());
					
					op_stat_write_t(linkstat, commnodeid, op_sim_time());
					
					//printf("NODE ID = %d\n", userid);
					//printf("Transmitting Data Packet at %lf time\n", op_sim_time());
					//op_prg_odb_bkpt("WIN");
					
					#ifdef PRINT_TX
					    printf("PARENT PROC\n");
				    	printf("Transmitting a copy of a DATA Packet\n");
					    printf("Packet Size = %d\n", op_pk_total_size_get(pktptr));
					    op_pk_nfd_get(pktptr, "SRC", &srcid);
					    printf("Packet Source = %d\n", srcid);
					    op_pk_nfd_get(pktptr, "DEST", &destid);
					    printf("Packet Destination = %d\n", destid);
					    op_pk_nfd_get(pktptr, "number", &tempdouble);
					    printf("Packet Sequence Number = %lf\n", tempdouble);
					    printf("Scheduled a DATA Timeout Interrupt for %lfs\n", op_ev_time(timeout));
				    #endif
					
				    #ifdef PACKET
					    printf("Transmitting a Data Packet from Node %d!\n", userid);
					    op_pk_nfd_get(pktptr, "SRC", &tempint);
					 	printf("SRC Node = %d\n", tempint);
					    op_pk_nfd_get(pktptr, "DEST", &tempint);
					 	printf("DEST Node = %d\n", tempint);
					    op_prg_odb_bkpt("PACKET");
				    #endif
				}
				/*If transmission of an ACK packet*/
				else if(ACK)
				{
				    /*Create an ACK packet*/
				    pktptr = op_pk_create_fmt("ACK_pkt");
				    
					/*set the appropriate size of the packet - An ACK packet is 14 bytes*/
					op_pk_total_size_set(pktptr, ackpktsize);
				
					/*Set the fields of the ACK packet*/
					op_pk_nfd_set(pktptr, "SRC", userid);
					op_pk_nfd_set(pktptr, "DEST", commnodeid);
					op_pk_nfd_set(pktptr, "number", sequencenum);
					
					#ifdef PRINT_TX
						    printf("PARENT PROC\n");
				    	printf("Transmitting an ACK Packet\n");
					    printf("Packet Size = %d\n", op_pk_total_size_get(pktptr));
					    op_pk_nfd_get(pktptr, "SRC", &srcid);
					    printf("Packet Source = %d\n", srcid);
					    op_pk_nfd_get(pktptr, "DEST", &destid);
					    printf("Packet Destination = %d\n", destid);
					 	op_pk_nfd_get(pktptr, "number", &tempdouble);
					    printf("Packet Sequence Number = %lf\n", tempdouble);	
				    #endif
				}
				
				
				/*Reset the transmission required variable - now that the transmission is taking place*/
				txreq = 0;
				
				/*Reset the random backoff interval*/
				rob = -1.0;
				
				/*Transmit the packet*/
				op_pk_send(pktptr, TX_STRM);
				
				op_prg_odb_bkpt("TX");
				op_prg_odb_bkpt("TEST");
				}

				FSM_PROFILE_SECTION_OUT (ramimo_proc_par [TX enter execs], state4_enter_exec)

			/** state (TX) exit executives **/
			FSM_STATE_EXIT_FORCED (4, "TX", "ramimo_proc_par [TX exit execs]")
				FSM_PROFILE_SECTION_IN (ramimo_proc_par [TX exit execs], state4_exit_exec)
				{
				}
				FSM_PROFILE_SECTION_OUT (ramimo_proc_par [TX exit execs], state4_exit_exec)


			/** state (TX) transition processing **/
			FSM_TRANSIT_FORCE (1, state1_enter_exec, ;, "default", "", "TX", "WAIT")
				/*---------------------------------------------------------*/



			/** state (SENSE) enter executives **/
			FSM_STATE_ENTER_FORCED (5, "SENSE", state5_enter_exec, "ramimo_proc_par [SENSE enter execs]")
				FSM_PROFILE_SECTION_IN (ramimo_proc_par [SENSE enter execs], state5_enter_exec)
				{
				/******************************************/
				/*Code for physical carrier sensing update*/
				/******************************************/
				if(PCS)
				{
				    /*Read the update from the receiver or transmitter channels*/
				    pcs_state = (int) op_stat_local_read(op_intrpt_stat());
						
					#ifdef PRINT_SENSE
					    printf("PARENT PROC\n");
					    printf("SENSE STATE\n");
					    printf("Node ID = %d\n", userid);
				     	printf("Updating the Physical Carrier Sensing Status!\n");
					    printf("Updated PCS_STATE = %d\n", pcs_state);
				    #endif
					
					/*If the channel has become busy then any pending transmission or rob calculation interrupts need to be cancelled*/
					if(pcs_state == 1)
					{
						/*Cancel the transmission event if scheduled and not for a DATA or ACK packet transmission*/
						/*Remember that no regard is given to the state of the channel when tx a DATA or ACK packet*/
						if(op_ev_valid(nexttxevent) == OPC_TRUE)
						{
				        	/*Update the backoff time if one has been set*/
				    		if(rob != -1.0)
					   		{
					    	    #ifdef PRINT_SENSE
								    printf("PARENT PROC\n");
					                printf("Updating backoff time!\n");
				                    printf("Original backoff time was %lfs\n", rob);
				                #endif
				
					    	    rob = op_ev_time(nexttxevent) - op_sim_time();
				       	   		
					 	        #ifdef PRINT_SENSE
								    printf("PARENT PROC\n");
				          	        printf("New backoff time is %lfs\n", rob);
				                #endif
					         }	
				    	
							 op_ev_cancel(nexttxevent);
							
				     		 #ifdef PRINT_SENSE
							 	    printf("PARENT PROC\n");
				                 printf("Cancelling a Scheduled Transmission Event!\n");
				             #endif
						}
						
						/*Cancel the transmission event if it is not for a DATA or ACK packet transmission*/
						/*Remember that no regard is given to the state of the channel when tx a DATA or ACK packet*/
						if(op_ev_valid(robevent) == OPC_TRUE)
						{
						    op_ev_cancel(robevent);
							
				   			#ifdef PRINT_SENSE
								    printf("PARENT PROC\n");
				           	     printf("Cancelling a Scheduled ROB Event!\n");
				               #endif
						}
					}
				}
				/******************************************************/
				/*Code for virtual carrier sensing busy channel update*/
				/******************************************************/
				else if(VCS_busy)
				{
				    #ifdef PRINT_SENSE
					    printf("PARENT PROC\n");
					    printf("SENSE STATE\n");
					    printf("Node ID = %d\n", userid);
				        printf("Channel is sensed BUSY via VCS\n");
				    #endif
				 	
					/*Cancel the transmission event if scheduled and not for a DATA or ACK packet transmission*/
					/*Remember that no regard is given to the state of the channel when tx a DATA or ACK packet*/
					if(op_ev_valid(nexttxevent) == OPC_TRUE)
					{
				       	/*Update the backoff time if one has been set*/
				    	if(rob != -1.0)
						{
					   	    #ifdef PRINT_SENSE
							    printf("PARENT PROC\n");
					            printf("Updating backoff time!\n");
				                printf("Original backoff time was %lfs\n", rob);
				            #endif
				
					  	    rob = op_ev_time(nexttxevent) - op_sim_time();
				      	   		
					        #ifdef PRINT_SENSE
									    printf("PARENT PROC\n");
				       	        printf("New backoff time is %lfs\n", rob);
				             #endif
					     }	
				    	
						 op_ev_cancel(nexttxevent);
							
				         #ifdef PRINT_SENSE
						 	    printf("PARENT PROC\n");
				             printf("Cancelling a Scheduled Transmission Event!\n");
				         #endif
				    }
						
					/*Cancel the transmission event if it is not for a DATA or ACK packet transmission*/
					/*Remember that no regard is given to the state of the channel when tx a DATA or ACK packet*/
					if(op_ev_valid(robevent) == OPC_TRUE)
					{
					    op_ev_cancel(robevent);
							
				   		#ifdef PRINT_SENSE
							    printf("PARENT PROC\n");
				            printf("Cancelling a Scheduled ROB Event!\n");
				        #endif
				    }
				    
					/*Determine if the channel is already sensed busy*/
				    if(vcs_state == 1)
					{
				        #ifdef PRINT_SENSE
						    printf("PARENT PROC\n");
					        printf("Channel was Already Sensed BUSY via VCS!\n");
							printf("Time of Previous Free Interrupt = %lf\n", op_ev_time(chfreeint));
							printf("New Time for Free Channel = %lf\n", op_sim_time() + vcs_duration);
				        #endif
						
					    /*Reschedule the channel free interrupt if the new duration is later than the previous*/
					    if((op_sim_time() + vcs_duration) > op_ev_time(chfreeint)) 
				        {
						    op_ev_cancel(chfreeint);
							chfreeint = op_intrpt_schedule_self(op_sim_time() + vcs_duration, VCS_FREE_INT);
							
							 #ifdef PRINT_SENSE
								    printf("PARENT PROC\n");
					            printf("Rescheduling Channel Free Time\n");
					    		printf("Channel Free Interrupt Scheduled for %lf seconds\n", op_sim_time() + vcs_duration);
				            #endif
						}	
					}	
					else
					{	
					    chfreeint = op_intrpt_schedule_self(op_sim_time() + vcs_duration, VCS_FREE_INT);
				  	    
						#ifdef PRINT_SENSE
							    printf("PARENT PROC\n");
					        printf("Channel was not already sensed BUSY via VCS!\n");
							printf("Time for Free Channel = %lf\n", op_sim_time() + vcs_duration);
				        #endif
				     }
						
				     /*Update the vcs state variable*/
				     vcs_state = 1;
				}
				/******************************************************/
				/*Code for virtual carrier sensing free channel update*/
				/******************************************************/
				else if(VCS_free)
				{
				    #ifdef PRINT_SENSE
					    printf("PARENT PROC\n");
					    printf("SENSE STATE\n");
					    printf("Node ID = %d\n", userid);
				        printf("Channel is sensed FREE via VCS!\n");
				    #endif
				
					/*Update the vcs state variable*/
				    vcs_state = 0;
					
				}

⌨️ 快捷键说明

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