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

📄 aodv_waypoint_mob.pr.c

📁 在OPNET中实现AODV路由协议
💻 C
📖 第 1 页 / 共 2 页
字号:
			/** state (init) exit executives **/			FSM_STATE_EXIT_FORCED (0, "init", "aodv_waypoint_mob () [init exit execs]")				{				}			/** state (init) transition processing **/			FSM_INIT_COND (MOBIL)			FSM_TEST_COND (FIX)			FSM_TEST_LOGIC ("init")			FSM_TRANSIT_SWITCH				{				FSM_CASE_TRANSIT (0, 1, state1_enter_exec, ;, "MOBIL", "", "init", "Init_Mvt")				FSM_CASE_TRANSIT (1, 3, state3_enter_exec, ;, "FIX", "", "init", "Idle")				}				/*---------------------------------------------------------*/			/** state (Init_Mvt) enter executives **/			FSM_STATE_ENTER_FORCED (1, state1_enter_exec, "Init_Mvt", "aodv_waypoint_mob () [Init_Mvt enter execs]")				{				/* Node is truly mobile-- We have to initiate the mouvement.				/* 1. Determine the avg speed of the current node.				/* 2. Determine the lenght of the distance, which the node				/*    has to cover in his next run.				/* 3. Given these two values, plus the mouvement step in time,				/*    then comput the distance to cover during each simulation 				/*    step (state variable "step_range")				/* 4. Determine a direction to follow.				*/								// Reset the PAUSE flag				PAUSE = 0;				// Avg speed				my_avg_speed = op_dist_uniform(SPEED_LIMIT);				// Pick a target position				x_target = x = op_dist_outcome(uniform_double_x);				y_target = y = op_dist_outcome(uniform_double_y);				// Read the current position				op_ima_obj_attr_get(node_id,"x position",&x_pos);				op_ima_obj_attr_get(node_id,"y position",&y_pos);				// trace				aodv_mob_print_minor("Init mvt",TRACE_LEV_1);				sprintf(msg,"Target  position  |X axis: %f   |Y axis: %f", x_target, y_target);				aodv_mob_print_minor(msg,TRACE_LEV_1);				sprintf(msg,"Current position  |X axis: %f   |Y axis: %f", x_pos, y_pos);				aodv_mob_print_minor(msg,TRACE_LEV_1);								// Compute the distance to cover for the next mvt				distance_to_cover = sqrt((x-x_pos)*(x-x_pos) + (y-y_pos)*(y-y_pos));				sprintf(msg,"Distance to cover = %f",distance_to_cover);				aodv_mob_print_minor(msg,TRACE_LEV_1);								// Step range				step_range      = MVT_STEP*my_avg_speed;				// Angle				if(x != x_pos)					{					direction_angle =  atan((y-y_pos)/(x-x_pos));///op_dist_outcome (one) * PI;					if(direction_angle < 0)						direction_angle = (direction_angle*(-1));					if(x_target > x_pos && y_target > y_pos)						direction_angle =  direction_angle;					else if(x_target > x_pos && y_target < y_pos)						direction_angle =  (direction_angle*(-1));					else if(x_target < x_pos && y_target > y_pos)						{										direction_angle =  PI - direction_angle;						}					else if(x_target < x_pos && y_target < y_pos)						{						direction_angle =  direction_angle - PI;								}					}				else if(y != y_pos)					direction_angle =  (y>y_pos?0.5*PI:-0.5*PI);				else					direction_angle =  0;				sprintf(msg,"direction angle = %f*PI", direction_angle/PI);				aodv_mob_print_minor(msg,TRACE_LEV_1);				}			/** state (Init_Mvt) exit executives **/			FSM_STATE_EXIT_FORCED (1, "Init_Mvt", "aodv_waypoint_mob () [Init_Mvt exit execs]")				{				}			/** state (Init_Mvt) transition processing **/			FSM_TRANSIT_FORCE (2, state2_enter_exec, ;, "default", "", "Init_Mvt", "Move")				/*---------------------------------------------------------*/			/** state (Move) enter executives **/			FSM_STATE_ENTER_FORCED (2, state2_enter_exec, "Move", "aodv_waypoint_mob () [Move enter execs]")				{				/* Move a step_range ahead and decrease the lenght of distance_to_cover*/				// Get the current position of the node				op_ima_obj_attr_get(node_id,"x position",&x_pos);				op_ima_obj_attr_get(node_id,"y position",&y_pos);				// Trace				x_pos = (double) x_pos;				y_pos = (double) y_pos;				sprintf(msg,"Target  position  |X axis: %f   |Y axis: %f", x_target, y_target);				aodv_mob_print_major(msg,TRACE_LEV_1);				sprintf(msg,"Current position  |X axis: %f   |Y axis: %f", x_pos, y_pos);				aodv_mob_print_minor(msg,TRACE_LEV_1);				// Compute the new x and y position according to the STEP_DIST and the current direction (angle)				x= x_pos+ (double) step_range*cos(direction_angle);				y= y_pos+ (double) step_range*sin(direction_angle);				// Make sure the new position is inside the grid				while(y<YMIN || YMAX<y || x<XMIN || XMAX<x)					{					aodv_mob_print_minor("Check ! boundary reached: changing direction\n",TRACE_LEV_1);					// Compute a random new direction					direction_angle= op_dist_outcome(one)*PI;					// Compute the new x and y position according to the STEP_DIST and the current direction (angle)					x= x_pos+ (double) step_range*cos(direction_angle);					y= y_pos+ (double) step_range*sin(direction_angle);					}				// Trace				x = (double) x;				y = (double) y;				// Record its last position in the last position map				last_position_map[node_addr].x = x_pos;				last_position_map[node_addr].y = y_pos;				// Update its position in the position map				position_map[node_addr].x = x;				position_map[node_addr].y = y;				sprintf(msg,"New position      |X axis: %f   |Y axis: %f", x, y);				aodv_mob_print_minor(msg,TRACE_LEV_1);				// set the new position of the node				op_ima_obj_attr_set(node_id, "x position",x);				op_ima_obj_attr_set(node_id, "y position",y);				// Decrease the value of the distance to cover				distance_to_cover = distance_to_cover-step_range;				// Check if target reached or not				if(distance_to_cover < step_range)					{					// Set the pause flag to 1							PAUSE = 1;					// Set avg speed to 0					my_avg_speed = 0;					// Schedule an interrupt for the end of the pause state					pause_time = floor(op_dist_uniform(PAUSE_TIME)/MVT_STEP)*MVT_STEP;//floor(PAUSE_TIME/MVT_STEP)*MVT_STEP; 					op_intrpt_schedule_self(op_sim_time()+pause_time,END_PAUSE_CODE);					sprintf(msg,"Target position reached: Enter Pause state\n\t\t- Distance to cover = %f\n\t\t- Pause time = %f",distance_to_cover,pause_time);					aodv_mob_print_minor(msg,TRACE_LEV_1);						}				else					{					sprintf(msg,"Target position not reached yet: Keep on moving\n\t\t- Distance still to cover = %f",distance_to_cover);					aodv_mob_print_minor(msg,TRACE_LEV_1);					op_intrpt_schedule_self(op_sim_time()+MVT_STEP,MOVE_CODE);					}								// Update connectivity map				aodv_mob_update_conn_map();				aodv_mob_print_conn_map();													}			/** state (Move) exit executives **/			FSM_STATE_EXIT_FORCED (2, "Move", "aodv_waypoint_mob () [Move exit execs]")				{				}			/** state (Move) transition processing **/			FSM_TRANSIT_FORCE (3, state3_enter_exec, ;, "default", "", "Move", "Idle")				/*---------------------------------------------------------*/			/** state (Idle) enter executives **/			FSM_STATE_ENTER_UNFORCED (3, state3_enter_exec, "Idle", "aodv_waypoint_mob () [Idle enter execs]")				{				/* If there still a distance to cover (distance_to_cover > 0)				/* then schedule an interrupt for the next mouvement after a				/* MVT_STEP sec period of time.				/* Else, schedule an interrupt to enter the Pause state now.				*/												}			/** blocking after enter executives of unforced state. **/			FSM_EXIT (7,aodv_waypoint_mob)			/** state (Idle) exit executives **/			FSM_STATE_EXIT_UNFORCED (3, "Idle", "aodv_waypoint_mob () [Idle exit execs]")				{				}			/** state (Idle) transition processing **/			FSM_INIT_COND (MOVE)			FSM_TEST_COND (STATS)			FSM_TEST_COND (END_PAUSE)			FSM_DFLT_COND			FSM_TEST_LOGIC ("Idle")			FSM_TRANSIT_SWITCH				{				FSM_CASE_TRANSIT (0, 2, state2_enter_exec, ;, "MOVE", "", "Idle", "Move")				FSM_CASE_TRANSIT (1, 3, state3_enter_exec, aodv_mob_collect_stats();, "STATS", "aodv_mob_collect_stats()", "Idle", "Idle")				FSM_CASE_TRANSIT (2, 1, state1_enter_exec, ;, "END_PAUSE", "", "Idle", "Init_Mvt")				FSM_CASE_TRANSIT (3, 3, state3_enter_exec, ;, "default", "", "Idle", "Idle")				}				/*---------------------------------------------------------*/			}		FSM_EXIT (0,aodv_waypoint_mob)		}	}#if defined (__cplusplus)	extern "C" { #endif	extern VosT_Fun_Status Vos_Catmem_Register (const char * , int , VosT_Void_Null_Proc, VosT_Address *);	extern VosT_Address Vos_Catmem_Alloc (VosT_Address, size_t);	extern VosT_Fun_Status Vos_Catmem_Dealloc (VosT_Address);#if defined (__cplusplus)	}#endifCompcodeaodv_waypoint_mob_init (void ** gen_state_pptr)	{	int _block_origin = 0;	static VosT_Address	obtype = OPC_NIL;	FIN (aodv_waypoint_mob_init (gen_state_pptr))	if (obtype == OPC_NIL)		{		/* Initialize memory management */		if (Vos_Catmem_Register ("proc state vars (aodv_waypoint_mob)",			sizeof (aodv_waypoint_mob_state), Vos_Vnop, &obtype) == VOSC_FAILURE)			{			FRET (OPC_COMPCODE_FAILURE)			}		}	*gen_state_pptr = Vos_Catmem_Alloc (obtype, 1);	if (*gen_state_pptr == OPC_NIL)		{		FRET (OPC_COMPCODE_FAILURE)		}	else		{		/* Initialize FSM handling */		((aodv_waypoint_mob_state *)(*gen_state_pptr))->current_block = 0;		FRET (OPC_COMPCODE_SUCCESS)		}	}voidaodv_waypoint_mob_diag (void)	{	/* No Diagnostic Block */	}voidaodv_waypoint_mob_terminate (void)	{	int _block_origin = __LINE__;	FIN (aodv_waypoint_mob_terminate (void))	if (1)		{		Objid	       params_comp_attr_objid;		Objid          params_attr_objid;		double	       rand, prev_angle,x,y,z,x_pos,y_pos,z_pos;		double         first_interval;		char           msg[64];		int            i;		double         pause_time;		/* No Termination Block */		}	Vos_Catmem_Dealloc (pr_state_ptr);	FOUT;	}/* Undefine shortcuts to state variables to avoid *//* syntax error in direct access to fields of *//* local variable prs_ptr in aodv_waypoint_mob_svar function. */#undef one#undef node_id#undef process_id#undef net_id#undef MOBILE#undef XMAX#undef XMIN#undef YMAX#undef YMIN#undef DEBUG#undef node_addr#undef TRANSMISSION_RANGE#undef num_nodes#undef my_avg_speed#undef distance_to_cover#undef PAUSE_TIME#undef MVT_STEP#undef SPEED_LIMIT#undef direction_angle#undef step_range#undef DISTANCE_LIMIT#undef uniform_double_x#undef uniform_double_y#undef PAUSE#undef x_target#undef y_targetvoidaodv_waypoint_mob_svar (void * gen_ptr, const char * var_name, char ** var_p_ptr)	{	aodv_waypoint_mob_state		*prs_ptr;	FIN (aodv_waypoint_mob_svar (gen_ptr, var_name, var_p_ptr))	if (var_name == OPC_NIL)		{		*var_p_ptr = (char *)OPC_NIL;		FOUT;		}	prs_ptr = (aodv_waypoint_mob_state *)gen_ptr;	if (strcmp ("one" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->one);		FOUT;		}	if (strcmp ("node_id" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->node_id);		FOUT;		}	if (strcmp ("process_id" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->process_id);		FOUT;		}	if (strcmp ("net_id" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->net_id);		FOUT;		}	if (strcmp ("MOBILE" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->MOBILE);		FOUT;		}	if (strcmp ("XMAX" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->XMAX);		FOUT;		}	if (strcmp ("XMIN" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->XMIN);		FOUT;		}	if (strcmp ("YMAX" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->YMAX);		FOUT;		}	if (strcmp ("YMIN" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->YMIN);		FOUT;		}	if (strcmp ("DEBUG" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->DEBUG);		FOUT;		}	if (strcmp ("node_addr" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->node_addr);		FOUT;		}	if (strcmp ("TRANSMISSION_RANGE" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->TRANSMISSION_RANGE);		FOUT;		}	if (strcmp ("num_nodes" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->num_nodes);		FOUT;		}	if (strcmp ("my_avg_speed" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->my_avg_speed);		FOUT;		}	if (strcmp ("distance_to_cover" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->distance_to_cover);		FOUT;		}	if (strcmp ("PAUSE_TIME" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->PAUSE_TIME);		FOUT;		}	if (strcmp ("MVT_STEP" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->MVT_STEP);		FOUT;		}	if (strcmp ("SPEED_LIMIT" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->SPEED_LIMIT);		FOUT;		}	if (strcmp ("direction_angle" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->direction_angle);		FOUT;		}	if (strcmp ("step_range" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->step_range);		FOUT;		}	if (strcmp ("DISTANCE_LIMIT" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->DISTANCE_LIMIT);		FOUT;		}	if (strcmp ("uniform_double_x" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->uniform_double_x);		FOUT;		}	if (strcmp ("uniform_double_y" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->uniform_double_y);		FOUT;		}	if (strcmp ("PAUSE" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->PAUSE);		FOUT;		}	if (strcmp ("x_target" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->x_target);		FOUT;		}	if (strcmp ("y_target" , var_name) == 0)		{		*var_p_ptr = (char *) (&prs_ptr->y_target);		FOUT;		}	*var_p_ptr = (char *)OPC_NIL;	FOUT;	}

⌨️ 快捷键说明

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