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

📄 ramimo_pktgen1.pr.c

📁 用OPNET实现802.11MAC协议DCF协议
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Process model C form file: ramimo_pktgen.pr.c */
/* Portions of this file copyright 1992-2003 by OPNET Technologies, Inc. */



/* This variable carries the header into the object file */
const char ramimo_pktgen_pr_c [] = "MIL_3_Tfile_Hdr_ 100A 30A modeler 7 4020EB07 4020EB07 1 ibis pdm106 0 0 none none 0 0 none 0 0 0 0 0 0 0 0 90b 2                                                                                                                                                                                                                                                                                                                                                                                                               ";
#include <string.h>



/* OPNET system definitions */
#include <opnet.h>



/* Header Block */

#include <math.h>

//#define PKTGEN_INIT
//#define PKTGEN_OFF
//#define PKTGEN_ON
//#define PKTGEN_PKT_GEN

#define OUT_STRM 0

#define PKT 0 
#define TIME 1
#define PROC_INT 0

#define ON_TIME ( (op_intrpt_type() == OPC_INTRPT_SELF) && (op_intrpt_code() == TIME) )
#define OFF_TIME ( (op_intrpt_type() == OPC_INTRPT_SELF) && (op_intrpt_code() == TIME) ) 
#define GEN_PKT ( (op_intrpt_type() == OPC_INTRPT_SELF) && (op_intrpt_code() == PKT) )
#define PKT_INT ( (op_intrpt_type() == OPC_INTRPT_PROCESS) && (op_intrpt_code() == PROC_INT) )

/* End of Header Block */


#if !defined (VOSD_NO_FIN)
#undef	BIN
#undef	BOUT
#define	BIN		FIN_LOCAL_FIELD(_op_last_line_passed) = __LINE__ - _op_block_origin;
#define	BOUT	BIN
#define	BINIT	FIN_LOCAL_FIELD(_op_last_line_passed) = 0; _op_block_origin = __LINE__;
#else
#define	BINIT
#endif /* #if !defined (VOSD_NO_FIN) */



/* State variable definitions */
typedef struct
	{
	/* Internal state tracking for FSM */
	FSM_SYS_STATE
	/* State Variables */
	double	                 		channelload;
	double	                 		datarate;
	Objid	                  		proc_id;
	Objid	                  		node_id;
	double	                 		mean_off_time;
	double	                 		mean_on_time;
	Evhandle	               		offevent;
	Evhandle	               		genpktevent;
	Objid	                  		random_node;
	Evhandle	               		thisevent;
	Evhandle	               		nextevent;
	int	                    		first;
	int	                    		burstpktcount;
	Stathandle	             		burstlength;
	Stathandle	             		burstduration;
	double	                 		bursttime;
	double	                 		datapktsize;
	char	                   		packetformat[20];
	int	                    		srcuserid;
	int	                    		intnumnodes;
	double	                 		dblnumnodes;
	int	                    		destuserid;
	Objid	                  		mainprocid;
	Objid	                  		tx_id;
	Objid	                  		txcomp_id;
	Objid	                  		txch_id;
	double	                 		timebetweenpkts;
	double	                 		sequencenum;
	} ramimo_pktgen_state;

#define pr_state_ptr            		((ramimo_pktgen_state*) (OP_SIM_CONTEXT_PTR->mod_state_ptr))
#define channelload             		pr_state_ptr->channelload
#define datarate                		pr_state_ptr->datarate
#define proc_id                 		pr_state_ptr->proc_id
#define node_id                 		pr_state_ptr->node_id
#define mean_off_time           		pr_state_ptr->mean_off_time
#define mean_on_time            		pr_state_ptr->mean_on_time
#define offevent                		pr_state_ptr->offevent
#define genpktevent             		pr_state_ptr->genpktevent
#define random_node             		pr_state_ptr->random_node
#define thisevent               		pr_state_ptr->thisevent
#define nextevent               		pr_state_ptr->nextevent
#define first                   		pr_state_ptr->first
#define burstpktcount           		pr_state_ptr->burstpktcount
#define burstlength             		pr_state_ptr->burstlength
#define burstduration           		pr_state_ptr->burstduration
#define bursttime               		pr_state_ptr->bursttime
#define datapktsize             		pr_state_ptr->datapktsize
#define packetformat            		pr_state_ptr->packetformat
#define srcuserid               		pr_state_ptr->srcuserid
#define intnumnodes             		pr_state_ptr->intnumnodes
#define dblnumnodes             		pr_state_ptr->dblnumnodes
#define destuserid              		pr_state_ptr->destuserid
#define mainprocid              		pr_state_ptr->mainprocid
#define tx_id                   		pr_state_ptr->tx_id
#define txcomp_id               		pr_state_ptr->txcomp_id
#define txch_id                 		pr_state_ptr->txch_id
#define timebetweenpkts         		pr_state_ptr->timebetweenpkts
#define sequencenum             		pr_state_ptr->sequencenum

/* These macro definitions will define a local variable called	*/
/* "op_sv_ptr" in each function containing a FIN statement.	*/
/* This variable points to the state variable data structure,	*/
/* and can be used from a C debugger to display their values.	*/
#undef FIN_PREAMBLE_DEC
#undef FIN_PREAMBLE_CODE
#if defined (OPD_PARALLEL)
#  define FIN_PREAMBLE_DEC	ramimo_pktgen_state *op_sv_ptr; OpT_Sim_Context * tcontext_ptr;
#  define FIN_PREAMBLE_CODE	\
		if (VosS_Mt_Perform_Lock) \
			VOS_THREAD_SPECIFIC_DATA_GET (VosI_Globals.simi_mt_context_data_key, tcontext_ptr, SimT_Context *); \
		else \
			tcontext_ptr = VosI_Globals.simi_sequential_context_ptr; \
		op_sv_ptr = ((ramimo_pktgen_state *)(tcontext_ptr->mod_state_ptr));
#else
#  define FIN_PREAMBLE_DEC	ramimo_pktgen_state *op_sv_ptr;
#  define FIN_PREAMBLE_CODE	op_sv_ptr = pr_state_ptr;
#endif


/* No Function Block */


#if !defined (VOSD_NO_FIN)
enum { _op_block_origin = __LINE__ };
#endif

/* Undefine optional tracing in FIN/FOUT/FRET */
/* The FSM has its own tracing code and the other */
/* functions should not have any tracing.		  */
#undef FIN_TRACING
#define FIN_TRACING

#undef FOUTRET_TRACING
#define FOUTRET_TRACING

#if defined (__cplusplus)
extern "C" {
#endif
	void ramimo_pktgen (OP_SIM_CONTEXT_ARG_OPT);
	VosT_Obtype ramimo_pktgen_init (int * init_block_ptr);
	VosT_Address ramimo_pktgen_alloc (VOS_THREAD_INDEX_ARG_COMMA VosT_Obtype, int);
	void ramimo_pktgen_diag (OP_SIM_CONTEXT_ARG_OPT);
	void ramimo_pktgen_terminate (OP_SIM_CONTEXT_ARG_OPT);
	void ramimo_pktgen_svar (void *, const char *, void **);


	VosT_Fun_Status Vos_Define_Object (VosT_Obtype * _op_obst_ptr, const char * _op_name, unsigned int _op_size, unsigned int _op_init_obs, unsigned int _op_inc_obs);
	VosT_Address Vos_Alloc_Object_MT (VOS_THREAD_INDEX_ARG_COMMA VosT_Obtype _op_ob_hndl);
	VosT_Fun_Status Vos_Poolmem_Dealloc_MT (VOS_THREAD_INDEX_ARG_COMMA VosT_Address _op_ob_ptr);
#if defined (__cplusplus)
} /* end of 'extern "C"' */
#endif




/* Process model interrupt handling procedure */


void
ramimo_pktgen (OP_SIM_CONTEXT_ARG_OPT)
	{

#if !defined (VOSD_NO_FIN)
	int _op_block_origin = 0;
#endif
	FIN_MT (ramimo_pktgen ());
	if (1)
		{
		Packet *pktptr;
		double number;
		int index;
		int tempint;
		double tempdouble;


		FSM_ENTER ("ramimo_pktgen")

		FSM_BLOCK_SWITCH
			{
			/*---------------------------------------------------------*/
			/** state (init) enter executives **/
			FSM_STATE_ENTER_UNFORCED_NOLABEL (0, "init", "ramimo_pktgen [init enter execs]")
				FSM_PROFILE_SECTION_IN ("ramimo_pktgen [init enter execs]", state0_enter_exec)
				{
				}
				FSM_PROFILE_SECTION_OUT (state0_enter_exec)

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


			/** state (init) exit executives **/
			FSM_STATE_EXIT_UNFORCED (0, "init", "ramimo_pktgen [init exit execs]")
				FSM_PROFILE_SECTION_IN ("ramimo_pktgen [init exit execs]", state0_exit_exec)
				{
				/*Read in the simulation attributes*/
				op_ima_sim_attr_get(OPC_IMA_DOUBLE, "CHANNEL_LOAD", &channelload);
				op_ima_sim_attr_get(OPC_IMA_DOUBLE, "MEAN_ON_TIME", &mean_on_time);
				op_ima_sim_attr_get(OPC_IMA_DOUBLE, "MEAN_OFF_TIME", &mean_off_time);
				op_ima_sim_attr_get(OPC_IMA_STRING, "PACKET_FORMAT", &packetformat);
				op_ima_sim_attr_get(OPC_IMA_DOUBLE, "DATA_pktsize", &datapktsize);
				
				/*Declare Statistics*/
				burstlength = op_stat_reg("BURST_LENGTH", OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				burstduration = op_stat_reg("BURST_DURATION", OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				
				/*******************************************************************************************/
				
				/*Obtain the ID number of the process module*/
				proc_id = op_id_self();
				
				/*Obtain the ID number of the node at the network level*/
				node_id = op_topo_parent(proc_id);
				
				/*Obtain the User ID number of the node*/
				op_ima_obj_attr_get(node_id, "user id", &srcuserid);
				
				/*Obtain the channel datarate*/
				mainprocid = op_topo_assoc(proc_id, OPC_TOPO_ASSOC_OUT, OPC_OBJTYPE_QUEUE, 0);
				tx_id = op_topo_assoc(mainprocid, 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);
				op_ima_obj_attr_get(txch_id, "data rate", &datarate);
				
				/*Get the number of mobile nodes in the network*/
				intnumnodes = op_topo_object_count(OPC_OBJTYPE_NDMOB);
				dblnumnodes = (double) intnumnodes;
				
				/*Initialise variables*/
				sequencenum = dblnumnodes;
				
				timebetweenpkts = (datapktsize / datarate) * (dblnumnodes / channelload) * (mean_on_time / (mean_off_time + mean_on_time));
				
				//TEMPORARY WHEN ONLY ONE NODE IS TRANSMITTING
				//timebetweenpkts = (datapktsize / datarate) * (1.0 / channelload) * (mean_on_time / (mean_off_time + mean_on_time));
				
				
#ifdef PKTGEN_INIT
				    printf("PKTGEN_INIT\n");
				    printf("Node User ID = %d\n", srcuserid);
				    printf("Integer Number of Nodes = %d\n", intnumnodes);
				    printf("Double Number of Nodes = %lf\n", dblnumnodes);
				    printf("Channel Load = %lf\n", channelload);
				    printf("Channel Datarate = %lf\n", datarate);
				    printf("Mean ON Time = %lf\n", mean_on_time);
				    printf("Mean OFF Time = %lf\n", mean_off_time);
				    printf("Packet Format = %s\n", packetformat);
				    printf("Data Packet Size = %lf\n", datapktsize);
				    printf("Time Between Packets = %lf\n", timebetweenpkts);
#endif
				
				op_prg_odb_bkpt("TEST");
				
				}
				FSM_PROFILE_SECTION_OUT (state0_exit_exec)


			/** state (init) transition processing **/
			FSM_TRANSIT_ONLY ((PKT_INT), 1, state1_enter_exec, ;, init, "PKT_INT", "", "init", "OFF")
				/*---------------------------------------------------------*/



			/** state (OFF) enter executives **/
			FSM_STATE_ENTER_UNFORCED (1, "OFF", state1_enter_exec, "ramimo_pktgen [OFF enter execs]")
				FSM_PROFILE_SECTION_IN ("ramimo_pktgen [OFF enter execs]", state1_enter_exec)
				{
				/*TEMPORARY - ONLY ALLOW NODE 0 TO GENERATE PACKETS*/
				//if(srcuserid == 0)
				//{
				    /*Obtain an exponentially distributed number with specified mean*/
				    tempdouble = mean_off_time - 1.0;
				    number = op_dist_exponential(tempdouble) + 1.0;
				
				    /*Schedule an interrupt for the next ON time*/
				    op_intrpt_schedule_self( op_sim_time()+number, TIME);
				  
				    #ifdef PKTGEN_OFF
				       printf("PKTGEN_OFF\n");
				       printf("Next ON time in: %f seconds\n", number);
				    #endif
				//}
					
				op_prg_odb_bkpt("PKTGEN_OFF");
				op_prg_odb_bkpt("TEST");
				}
				FSM_PROFILE_SECTION_OUT (state1_enter_exec)

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


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


			/** state (OFF) transition processing **/
			FSM_TRANSIT_ONLY ((ON_TIME), 2, state2_enter_exec, ;, OFF, "ON_TIME", "", "OFF", "ON")
				/*---------------------------------------------------------*/



			/** state (ON) enter executives **/
			FSM_STATE_ENTER_UNFORCED (2, "ON", state2_enter_exec, "ramimo_pktgen [ON enter execs]")
				FSM_PROFILE_SECTION_IN ("ramimo_pktgen [ON enter execs]", state2_enter_exec)
				{
				if(ON_TIME)
				{
				    /*Obtain an exponentially distributed number with a specified mean*/
				    tempdouble = mean_on_time - 1.0;    
				    number = op_dist_exponential(tempdouble) + 1.0;
					   
				    /*Schedule an interrupt for the next OFF TIME*/
				    offevent = op_intrpt_schedule_self( op_sim_time()+number, TIME);
				
				    /*Schedule an interrupt for the generation of the first packet within a burst*/
				    genpktevent = op_intrpt_schedule_self( op_sim_time(), PKT);
				
					/*Pick a random node that will be the destination of this burst and obtain its user ID*/
				    do
					{	
					    index = rand()%(intnumnodes);
				        random_node = op_topo_object(OPC_OBJTYPE_NDMOB, index);
					    op_ima_obj_attr_get(random_node, "user id", &destuserid);
				
					} while(destuserid == srcuserid);	
				
					/*TEMP - SET THE DESTINATION OF ALL PACKETS TO BE NODE 3*/
					//destuserid = 3;
					
				    #ifdef PKTGEN_ON
					    printf("PKTGEN_ON\n");
				        printf("Next OFF time in: %f seconds\n", number);
					    printf("Destination Node User ID = %d\n", destuserid);
				    #endif
					
				    /*Initialise variables for determination of burst statistics*/
				    first = 1;
				    burstpktcount = 0;
				    bursttime = op_sim_time();
					
				    op_prg_odb_bkpt("PKTGEN_ON");
				}
				
				op_prg_odb_bkpt("TEST");
				}
				FSM_PROFILE_SECTION_OUT (state2_enter_exec)

			/** blocking after enter executives of unforced state. **/
			FSM_EXIT (5,"ramimo_pktgen")


			/** state (ON) exit executives **/
			FSM_STATE_EXIT_UNFORCED (2, "ON", "ramimo_pktgen [ON exit execs]")
				FSM_PROFILE_SECTION_IN ("ramimo_pktgen [ON exit execs]", state2_exit_exec)
				{

⌨️ 快捷键说明

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