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

📄 ramimo_proc.pr.c

📁 用OPNET实现802.11MAC协议DCF协议
💻 C
📖 第 1 页 / 共 5 页
字号:
		double propdelay;
		Packet *rtspktptr;
		listentry* listentryptr;
		int lowpos;
		int highpos;
		int array[100];
		int x;
		int flag;
		int retry;
		double max_distance;
		double min_distance;
		Objid dest_node_id;
		double dest_xpos;
		double dest_ypos;
		double d_source;
		double d_destination;	
		int nexthop;
		int prevhop;
		double etoedelay;
		double variancedelay;


		FSM_ENTER ("ramimo_proc")

		FSM_BLOCK_SWITCH
			{
			/*---------------------------------------------------------*/
			/** state (INIT) enter executives **/
			FSM_STATE_ENTER_UNFORCED_NOLABEL (0, "INIT", "ramimo_proc [INIT enter execs]")
				FSM_PROFILE_SECTION_IN ("ramimo_proc [INIT enter execs]", state0_enter_exec)
				{
				/*Initialise Variables*/
				userid = 0;
				numnodes = 0;
				pcs_state = 0;
				vcs_state = 0;
				txstate = 0;
				busy = 0;
				txreq = 0;
				ssrc = 0;
				slrc = 0;
				numretries = 0;
				tx_range = 0.0;
				rx_range = 0.0;
				xpos = 0.0;
				ypos = 0.0;
				difs = 0.0;
				sifs = 0.0;
				vcs_duration = 0.0;
				rob = -1.0;
				sequencenum = -1.0;
				lastdelay = 0.0;
				
				/*Determine the number of mobile nodes*/
				numnodes = op_topo_object_count(OPC_OBJTYPE_NDMOB);
				
				/*Read in the simulation attributes*/
				op_ima_sim_attr_get(OPC_IMA_DOUBLE, "SIFS", &sifs);
				op_ima_sim_attr_get(OPC_IMA_DOUBLE, "tx_range", &tx_range);
				op_ima_sim_attr_get(OPC_IMA_DOUBLE, "rx_range", &rx_range);
				op_ima_sim_attr_get(OPC_IMA_DOUBLE, "DATA_pktsize", &datapktsize);
				op_ima_sim_attr_get(OPC_IMA_DOUBLE, "ACK_pktsize", &ackpktsize);
				op_ima_sim_attr_get(OPC_IMA_DOUBLE, "RTS_pktsize", &rtspktsize);
				op_ima_sim_attr_get(OPC_IMA_DOUBLE, "CTS_pktsize", &ctspktsize);
				op_ima_sim_attr_get(OPC_IMA_DOUBLE, "aSlotTime", &aSlotTime);
				op_ima_sim_attr_get(OPC_IMA_INTEGER, "retrylimit", &retrylimit);
				op_ima_sim_attr_get(OPC_IMA_DOUBLE, "USER_LOAD", &userload);
				op_ima_sim_attr_get(OPC_IMA_STRING, "scenario", &scenario);
				op_ima_sim_attr_get(OPC_IMA_DOUBLE, "resulttime", &resulttime);
				
				/*Register Statistics*/
				etoedelaystat = op_stat_reg("ENDTOENDDLY", OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				variancedlystat = op_stat_reg("VARIANCEDLY", OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				txstat = op_stat_reg("TXSTAT", OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				rxstat = op_stat_reg("RXSTAT", OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				discstat = op_stat_reg("DISCSTAT", OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				linkstat = op_stat_reg("LINKSTAT", OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				
				/*Initialise Variables*/
				for(i=0;i<7;i++)
				    cw_vals[i] = pow(2,(i+4)) - 1.0;
				
				cw_current = cw_vals[0];
				
				if(userid == 0)
				    for(i=0;i<numnodes;i++)
					    for(j=0;j<numnodes;j++)
						{
						    pktar[i][j] = 0;
							pktdi[i][j] = 0;
							pktqu[i][j] = 0;
							pktrx[i][j] = 0;
					    }
				
				/*Create a list to keep a record of received packets*/
				listptr = op_prg_list_create();
				
				/*Determine the corresponding DIFS period*/
				difs = sifs + (2.0*aSlotTime);
				
				/*Obtain the required object ID's for this node*/
				proc_id = op_id_self();
				node_id = op_topo_parent(proc_id);
				subnet_id = op_topo_parent(node_id);
				
				tx_id = op_topo_assoc(proc_id, OPC_TOPO_ASSOC_OUT, OPC_OBJTYPE_RATX, 0);
				txcomp_id = op_topo_child(tx_id, OPC_OBJTYPE_COMP, 0);
				txch_id = op_topo_child(txcomp_id, OPC_OBJTYPE_RATXCH, 0);
				
				rx_id = op_topo_assoc(proc_id, OPC_TOPO_ASSOC_IN, OPC_OBJTYPE_RARX, 0);
				rxcomp_id = op_topo_child(rx_id, OPC_OBJTYPE_COMP, 0);
				rxch_id = op_topo_child(rxcomp_id, OPC_OBJTYPE_RARXCH, 0);
				
				/*Obtain the datarate of the transmitter channel*/
				op_ima_obj_attr_get(txch_id, "data rate", &datarate);
				
				/****************************************************************************************/
				/*Derive the appropriate user ID from the node name and set the user ID object attribute*/
				/****************************************************************************************/
				
				/*Read the node name into a string*/
				op_ima_obj_attr_get(node_id, "name", &nodename);
				
				/*Extract the numerical part of the node name*/
				namelength = strlen(nodename);
				
				for(i=12; i<=namelength; i++)
				{
				    i_temp = nodename[i];
					i_temp = i_temp - '0';
					
					if(i_temp >=0) 
				     	nodenum = 10*nodenum + i_temp;
				}
				
				/*Set the user ID object attribute*/
				userid = nodenum;
				op_ima_obj_attr_set(node_id, "user id", userid);
				
#ifdef PRINT_INIT
				    printf("INIT STATE - ENTER EXECS\n");
				    printf("Scenario = %s\n", scenario);
				    printf("Sifs = %lf\n", sifs);
				    printf("Transmission Range = %lf\n", tx_range);
				    printf("Receiving Range = %lf\n", rx_range);
				    printf("Data Packet Size = %lf\n", datapktsize);
				    printf("ACK Packet Size = %lf\n", ackpktsize);
				    printf("RTS Packet Size = %lf\n", rtspktsize);
				    printf("CTS Packet Size = %lf\n", ctspktsize);
				    printf("aSlotTime = %lf\n", aSlotTime);
				    printf("retry limit = %d\n", retrylimit);
				    for(i=0;i<7;i++)
					    printf("cw_vals[%d] = %lf\n", i, cw_vals[i]);
				    printf("cw_current = %lf\n", cw_current);
				    printf("Difs = %lf\n", difs);   
				    printf("TX Channel Data Rate = %lf\n", datarate);
				    printf("Node Name = %s\n", nodename);
				    op_ima_obj_attr_get(node_id, "user id", &tempint);
					printf("Object Attribute User ID = %d\n", tempint);
#endif
				
				/*Schedule an interrupt to carry out the rest of the initialisation once all user IDs have been set*/
				op_intrpt_schedule_self(op_sim_time(), 0);	
				
				/*Get a handle to the root process model of the packet generator*/
				srcobjid = op_id_from_name(node_id, OPC_OBJTYPE_PROC, "SRC");
				srcprohandle = op_pro_root(srcobjid);
				
				/*Schedule an interrupt to carry out the initialisation of the packet generator for the appropriate nodes once all user IDs have been set*/
				if((strcmp(scenario, "singleline") == 0) && (userid == 0))
					op_intrpt_schedule_process(srcprohandle, op_sim_time(), PROC_INT);
				
				if((strcmp(scenario, "dualline") == 0) && ((userid == 0) || (userid == 11)))
					op_intrpt_schedule_process(srcprohandle, op_sim_time(), PROC_INT);
				
				if(((strcmp(scenario, "squaregrid") == 0) || (strcmp(scenario, "hexgrid") == 0)) && ((userid == 0) || (userid == 21)))
				    op_intrpt_schedule_process(srcprohandle, op_sim_time(), PROC_INT);
				
				op_prg_odb_bkpt("INIT");
				op_prg_odb_bkpt("TEST");
				}
				FSM_PROFILE_SECTION_OUT (state0_enter_exec)

			/** blocking after enter executives of unforced state. **/
			FSM_EXIT (1,"ramimo_proc")


			/** state (INIT) exit executives **/
			FSM_STATE_EXIT_UNFORCED (0, "INIT", "ramimo_proc [INIT exit execs]")
				FSM_PROFILE_SECTION_IN ("ramimo_proc [INIT exit execs]", state0_exit_exec)
				{
				/*********************************************************************************************/
				/*Determine the receiving range of the nodes and hence determine which nodes are within      */
				/*communications range. Set the receiver group as appropriate.                               */
				/*********************************************************************************************/
				
				/*Determine the location of this node*/
				op_ima_obj_attr_get(node_id, "x position", &xpos);
				op_ima_obj_attr_get(node_id, "y position", &ypos);
				
				for(i=0;i<numnodes;i++)
				{
				    /*Get the object ID of a node in the subnet*/
				    other_node_id = op_topo_child(subnet_id, OPC_OBJTYPE_NDMOB, i);
				   
				    /*Determine the location of the another node*/
				    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));
						
					/*Add them to the receiver group if they are within receiving range*/
				    if(distance < rx_range)
					{
					 	/*Get the object ID of the other nodes receiver channel*/
						other_proc_id = op_topo_child(other_node_id, OPC_OBJTYPE_QUEUE, 0);
						other_rx_id = op_topo_assoc(other_proc_id, OPC_TOPO_ASSOC_IN, OPC_OBJTYPE_RARX, 0);
						other_rxcomp_id = op_topo_child(other_rx_id, OPC_OBJTYPE_COMP, 0);
						other_rxch_id = op_topo_child(other_rxcomp_id, OPC_OBJTYPE_RARXCH, 0);
						
						/*Add the node to the receiver group of this transmitter*/
					    op_radio_txch_rxch_add(txch_id, other_rxch_id);
					}
				}
				
				/***************************************/
				/*Set up the routing table information */
				/***************************************/
				
				for(i=0;i<numnodes;i++)
				{
				    max_distance = 0; //max distance from source to intermediate hop
					min_distance = 1e10; //min distance from intermediate hop to destination
					r_table[i][0] = -1; //initialise routing table, next node 
					r_table[i][1] = max_distance; //initialise routing table, next node 
					r_table[i][2] = min_distance; //initialise routing table, next node 
					
					dest_node_id = op_id_from_userid(subnet_id, OPC_OBJTYPE_NDMOB, i);
					op_ima_obj_attr_get(dest_node_id, "x position", &dest_xpos);
					op_ima_obj_attr_get(dest_node_id, "y position", &dest_ypos);
				
					for(j=0;j<numnodes;j++)
					{
						//get Objids of other nodes in the system
						other_node_id = op_id_from_userid(subnet_id, OPC_OBJTYPE_NDMOB, j);
						op_ima_obj_attr_get(other_node_id, "x position", &other_xpos);
						op_ima_obj_attr_get(other_node_id, "y position", &other_ypos);
				
						//get distance of other node from source and eventual destination
						d_source = sqrt ((other_xpos - xpos)*(other_xpos - xpos) + (other_ypos - ypos)*(other_ypos - ypos));
						d_destination = sqrt ((other_xpos - dest_xpos)*(other_xpos - dest_xpos) + (other_ypos - dest_ypos)*(other_ypos - dest_ypos)); 	
							
						//printf("Sour UID %d Int UID %d Dest UID %d d_source = %.2f d_destination %.2f\n", user_id, int_uid, dest_uid, d_source,d_destination);
							
						//determine whether new node should be an intermediate node in the routing table
						if ((d_source <= tx_range) && (d_destination <= min_distance))
						{
							//printf("New Next Node Sour UID %d Int UID %d Dest UID %d d_source = %.2f d_destination %.2f\n", user_id, int_uid, dest_uid, d_source,d_destination);
							max_distance = d_source;
							min_distance = d_destination;
							r_table[i][0] = j;
							r_table[i][1] = max_distance;
							r_table[i][2] = min_distance;
							//printf("Source Node %d\n", user_id);
							//printf(    "Dest NN  SD   DD\n");
							//printf("%d    %d   %d   %d\n", i,r_table[dest_uid][0],r_table[dest_uid][1],r_table[dest_uid][2]);
									
						}	
					} 
				}
				
#ifdef PRINT_INIT
				    printf("INIT STATE - EXIT EXECS\n");
				    printf("Number of Mobile Nodes = %d\n", numnodes);
				    printf("Obtaining Information On The Receiver Group\n");
				    op_radio_txch_rxgroup_get(txch_id, &numreceivers, &receivers);
				    printf("Number of Receivers = %d\n", numreceivers);
				    for(i=0;i<numreceivers;i++)
					{
				        op_ima_obj_attr_get(op_topo_parent(op_topo_assoc(op_topo_parent(op_topo_parent(receivers[i])), OPC_TOPO_ASSOC_OUT, OPC_OBJTYPE_QUEUE, 0)), "user id", &tempint);
						printf("Receiver User ID = %d\n", tempint); 
					}
					for(i=0;i<numnodes;i++)
					{
					    printf("Routing Table Information\n");
						printf("Source Node %d\n", userid);
						printf(    "Dest NN  SD   DD\n");
						printf("%d    %d   %d   %d\n", i,r_table[i][0],r_table[i][1],r_table[i][2]);
					}
				
#endif
				
				if(userid == 0)
				op_prg_odb_bkpt("TEST");
				}
				FSM_PROFILE_SECTION_OUT (state0_exit_exec)


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



			/** state (WAIT) enter executives **/
			FSM_STATE_ENTER_UNFORCED (1, "WAIT", state1_enter_exec, "ramimo_proc [WAIT enter execs]")
				FSM_PROFILE_SECTION_IN ("ramimo_proc [WAIT enter execs]", state1_enter_exec)
				{
				}
				FSM_PROFILE_SECTION_OUT (state1_enter_exec)

			/** blocking after enter executives of unforced state. **/
			FSM_EXIT (3,"ramimo_proc")


			/** state (WAIT) exit executives **/
			FSM_STATE_EXIT_UNFORCED (1, "WAIT", "ramimo_proc [WAIT exit execs]")
				FSM_PROFILE_SECTION_IN ("ramimo_proc [WAIT exit execs]", state1_exit_exec)
				{
				}
				FSM_PROFILE_SECTION_OUT (state1_exit_exec)


			/** state (WAIT) transition processing **/
			FSM_PROFILE_SECTION_IN ("ramimo_proc [WAIT trans conditions]", state1_trans_conds)
			FSM_INIT_COND (END)
			FSM_TEST_COND (SRC_ARVL)
			FSM_TEST_COND (CTS)
			FSM_TEST_COND (RTS)

⌨️ 快捷键说明

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