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

📄 sl_aloha_ip.pr.c

📁 网络仿真软件OPNET Slotted-ALOHA模型文件
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Process model C form file: sl_aloha_ip.pr.c */
/* Portions of this file copyright 1992-2001 by OPNET Technologies, Inc. */



/* This variable carries the header into the object file */
static const char sl_aloha_ip_pr_c [] = "MIL_3_Tfile_Hdr_ 80C 30A modeler 7 3B21408E 3B21408E 1 WTN13025 opdemo 0 0 none none 0 0 none 0 0 0 0 0 0                                                                                                                                                                                                                                                                                                                                                                                                                      ";
#include <string.h>



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

#if defined (__cplusplus)
extern "C" {
#endif
FSM_EXT_DECS
#if defined (__cplusplus)
} /* end of 'extern "C"' */
#endif


/* Header Block */

#include "nato.h"
#include "ip_addr_v4.h"
#include "ip_dgram_sup.h"
#include "oms_pr.h"
#include "ip_rte_v4.h"
#include <math.h>

#define MIL				1000000
#define SUB_Q			0
#define TX_PACKET		0

#define	GATEWAY_NODE	0
#define	REMOTE_NODE		1

/* Conditional macros */
#define UPPER_ARRIVAL	((intrpt_type == OPC_INTRPT_STRM) && (intrpt_strm == ip_in_strm))
#define	LOWER_ARRIVAL	((intrpt_type == OPC_INTRPT_STRM) && (intrpt_strm == rx_in_strm))
#define Q_EMPTY			(op_subq_empty (SUB_Q))
#define BEG_SLOT		((intrpt_type == OPC_INTRPT_SELF) && (intrpt_code == TX_PACKET))
#define SET_SLOT 		(op_intrpt_schedule_self ( op_sim_time() + slot_length + guard_band, TX_PACKET))

static void	put_in_q (void);
static void	send_to_ip (void);


/* End of Header Block */


#if !defined (VOSD_NO_FIN)
#undef	BIN
#undef	BOUT
#define	BIN		FIN_LOCAL_FIELD(last_line_passed) = __LINE__ - _block_origin;
#define	BOUT	BIN
#define	BINIT	FIN_LOCAL_FIELD(last_line_passed) = 0; _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	                 		slot_length;
	double	                 		guard_band;
	Stathandle	             		chan_access_local_handle;
	Stathandle	             		chan_access_global_handle;
	IpT_Address	            		ip_address;
	int	                    		rx_in_strm;
	int	                    		ip_in_strm;
	int	                    		tx_out_strm;
	int	                    		ip_out_strm;
	Objid	                  		mod_objid;
	Objid	                  		node_objid;
	int	                    		node_type;
	} sl_aloha_ip_state;

#define pr_state_ptr            		((sl_aloha_ip_state*) SimI_Mod_State_Ptr)
#define slot_length             		pr_state_ptr->slot_length
#define guard_band              		pr_state_ptr->guard_band
#define chan_access_local_handle		pr_state_ptr->chan_access_local_handle
#define chan_access_global_handle		pr_state_ptr->chan_access_global_handle
#define ip_address              		pr_state_ptr->ip_address
#define rx_in_strm              		pr_state_ptr->rx_in_strm
#define ip_in_strm              		pr_state_ptr->ip_in_strm
#define tx_out_strm             		pr_state_ptr->tx_out_strm
#define ip_out_strm             		pr_state_ptr->ip_out_strm
#define mod_objid               		pr_state_ptr->mod_objid
#define node_objid              		pr_state_ptr->node_objid
#define node_type               		pr_state_ptr->node_type

/* This macro definition 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
#define FIN_PREAMBLE	sl_aloha_ip_state *op_sv_ptr = pr_state_ptr;


/* Function Block */

enum { _block_origin = __LINE__ };
static void
put_in_q (void)
	{
	Packet	*pkptr;

	FIN (put_in_q ())
	
    /* get the packet from the strm. */
	pkptr = op_pk_get (ip_in_strm);

	/* insert in q. */
    op_subq_pk_insert (SUB_Q, pkptr, OPC_QPOS_TAIL);

	FOUT
	}

static void
send_to_ip (void)
	{
	Packet				*pk_ptr;
	IpT_Dgram_Fields	*pk_fd_ptr;

	FIN (send_to_ip ())

	/* Obtain the packet off the incoming stream. */
	pk_ptr = op_pk_get (rx_in_strm);

	/* Access the fields from the packet. */
	op_pk_nfd_access (pk_ptr, "fields", &pk_fd_ptr);

	/* Check to see if this packet should be sent to IP or not. */
	if (node_type == GATEWAY_NODE)
		{
		if (pk_fd_ptr->src_addr == ip_address)
			op_pk_destroy (pk_ptr);
		else
			op_pk_send (pk_ptr, ip_out_strm);
		}
	else
		{
		if (pk_fd_ptr->dest_addr != ip_address)
			op_pk_destroy (pk_ptr);
		else
			op_pk_send (pk_ptr, ip_out_strm);
		}

	FOUT
	}

/* End of Function Block */

/* 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 sl_aloha_ip (void);
	Compcode sl_aloha_ip_init (void **);
	void sl_aloha_ip_diag (void);
	void sl_aloha_ip_terminate (void);
	void sl_aloha_ip_svar (void *, const char *, char **);
#if defined (__cplusplus)
} /* end of 'extern "C"' */
#endif




/* Process model interrupt handling procedure */


void
sl_aloha_ip (void)
	{
	int _block_origin = 0;
	FIN (sl_aloha_ip ());
	if (1)
		{
		/* Packet pointer*/
		Packet *pkptr;
		/* The current time. */
		double current_time;
		
		/* current slot.*/
		int time_slot;
		
		/* Time of next time slot. */
		double next_time_slot;
		
		double chan_access_delay;
		
		int					intrpt_type;
		int					intrpt_strm;
		int					intrpt_code;
		
		List*				proc_record_handle_list_ptr;
		Objid				tx_objid;
		Objid				rx_objid;
		Objid				strm_objid;
		Objid				instrm;
		Objid				outstrm;
		int					record_handle_list_size;
		OmsT_Pr_Handle		process_record_handle;
		IpT_Interface_Info* ip_iface_elem_ptr;
		int					i;
		IpT_Info*			ip_info_ptr;
		Objid				ip_module_objid;
		int					ip_iface_table_size;
		Boolean				ip_addrs_found;


		FSM_ENTER (sl_aloha_ip)

		FSM_BLOCK_SWITCH
			{
			/*---------------------------------------------------------*/
			/** state (q_empty) enter executives **/
			FSM_STATE_ENTER_UNFORCED (0, state0_enter_exec, "q_empty", "sl_aloha_ip () [q_empty enter execs]")
				{
				}


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


			/** state (q_empty) exit executives **/
			FSM_STATE_EXIT_UNFORCED (0, "q_empty", "sl_aloha_ip () [q_empty exit execs]")
				{
				/* Obtain the interrupt type.                            */
				intrpt_type = op_intrpt_type ();
				
				/* If this is a stream interrupt, obtain the stream.     */
				if (intrpt_type == OPC_INTRPT_STRM)
					intrpt_strm = op_intrpt_strm ();
				}


			/** state (q_empty) transition processing **/
			FSM_INIT_COND (UPPER_ARRIVAL)
			FSM_TEST_COND (LOWER_ARRIVAL)
			FSM_TEST_LOGIC ("q_empty")

			FSM_TRANSIT_SWITCH
				{
				FSM_CASE_TRANSIT (0, 1, state1_enter_exec, put_in_q();, "UPPER_ARRIVAL", "put_in_q()", "q_empty", "decide_next_time_slot")
				FSM_CASE_TRANSIT (1, 0, state0_enter_exec, send_to_ip();, "LOWER_ARRIVAL", "send_to_ip()", "q_empty", "q_empty")
				}
				/*---------------------------------------------------------*/



			/** state (decide_next_time_slot) enter executives **/
			FSM_STATE_ENTER_FORCED (1, state1_enter_exec, "decide_next_time_slot", "sl_aloha_ip () [decide_next_time_slot enter execs]")
				{
				/* Check the time of the global clock. */
				current_time = op_sim_time ();
				
				/* Get the current time slot. */
				time_slot = floor ((current_time * MIL) / ((slot_length + guard_band) * MIL));
				
				/* Figure out the next time slot. */
				next_time_slot = (time_slot + 1) * (slot_length + guard_band);
				
				/* Schedule a self intrpt for the next time slot*/
				op_intrpt_schedule_self (next_time_slot, TX_PACKET);
				}


			/** state (decide_next_time_slot) exit executives **/
			FSM_STATE_EXIT_FORCED (1, "decide_next_time_slot", "sl_aloha_ip () [decide_next_time_slot exit execs]")
				{
				}


			/** state (decide_next_time_slot) transition processing **/
			FSM_TRANSIT_FORCE (2, state2_enter_exec, ;, "default", "", "decide_next_time_slot", "q_not_empty")
				/*---------------------------------------------------------*/



			/** state (q_not_empty) enter executives **/
			FSM_STATE_ENTER_UNFORCED (2, state2_enter_exec, "q_not_empty", "sl_aloha_ip () [q_not_empty enter execs]")
				{
				}


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


			/** state (q_not_empty) exit executives **/
			FSM_STATE_EXIT_UNFORCED (2, "q_not_empty", "sl_aloha_ip () [q_not_empty exit execs]")
				{
				/* Obtain the interrupt type.                            */
				intrpt_type = op_intrpt_type ();
				
				/* If this is a stream interrupt, obtain the stream.     */
				if (intrpt_type == OPC_INTRPT_STRM)
					intrpt_strm = op_intrpt_strm ();
				
				/* If this is a self interrupt, obtain the code.         */
				if (intrpt_type == OPC_INTRPT_SELF)
					intrpt_code = op_intrpt_code ();
				}


			/** state (q_not_empty) transition processing **/
			FSM_INIT_COND (BEG_SLOT)
			FSM_TEST_COND (UPPER_ARRIVAL)
			FSM_TEST_COND (LOWER_ARRIVAL)
			FSM_TEST_LOGIC ("q_not_empty")

			FSM_TRANSIT_SWITCH
				{
				FSM_CASE_TRANSIT (0, 3, state3_enter_exec, ;, "BEG_SLOT", "", "q_not_empty", "transmit_packet")
				FSM_CASE_TRANSIT (1, 2, state2_enter_exec, put_in_q();, "UPPER_ARRIVAL", "put_in_q()", "q_not_empty", "q_not_empty")
				FSM_CASE_TRANSIT (2, 2, state2_enter_exec, send_to_ip();, "LOWER_ARRIVAL", "send_to_ip()", "q_not_empty", "q_not_empty")
				}
				/*---------------------------------------------------------*/



			/** state (transmit_packet) enter executives **/
			FSM_STATE_ENTER_FORCED (3, state3_enter_exec, "transmit_packet", "sl_aloha_ip () [transmit_packet enter execs]")
				{
				/* Remove the packet from the Queue. */
				pkptr = op_subq_pk_remove (SUB_Q, OPC_QPOS_HEAD);
				
				/* determine and record channel acces delay */
				chan_access_delay = op_sim_time () - op_pk_creation_time_get (pkptr);
				op_stat_write (chan_access_global_handle,
												 chan_access_delay * 1000.0);
				op_stat_write (chan_access_local_handle,
												 chan_access_delay * 1000.0);
				
				/* Send the packet at the begining of the slot. */
				op_pk_send (pkptr, tx_out_strm);
				
				
				}


			/** state (transmit_packet) exit executives **/
			FSM_STATE_EXIT_FORCED (3, "transmit_packet", "sl_aloha_ip () [transmit_packet exit execs]")
				{
				}


			/** state (transmit_packet) transition processing **/
			FSM_INIT_COND (Q_EMPTY)
			FSM_TEST_COND (!Q_EMPTY)
			FSM_TEST_LOGIC ("transmit_packet")

			FSM_TRANSIT_SWITCH
				{
				FSM_CASE_TRANSIT (0, 0, state0_enter_exec, ;, "Q_EMPTY", "", "transmit_packet", "q_empty")
				FSM_CASE_TRANSIT (1, 2, state2_enter_exec, SET_SLOT;, "!Q_EMPTY", "SET_SLOT", "transmit_packet", "q_not_empty")
				}
				/*---------------------------------------------------------*/



			/** state (init) enter executives **/
			FSM_STATE_ENTER_UNFORCED_NOLABEL (4, "init", "sl_aloha_ip () [init enter execs]")
				{
				/* Obtain the module and node object ids. */
				mod_objid = op_id_self ();
				node_objid = op_topo_parent (mod_objid);
				
				/* get the promoted attributes at run time */
				slot_length = 0.01;
				if (op_ima_sim_attr_exists ("ALOHA Slot Length") == OPC_TRUE)
					op_ima_sim_attr_get (OPC_IMA_DOUBLE, "ALOHA Slot Length", &slot_length);
				guard_band = 0.001;
				if (op_ima_sim_attr_exists ("ALOHA Guard Band") == OPC_TRUE)
					op_ima_sim_attr_get (OPC_IMA_DOUBLE, "ALOHA Guard Band", &guard_band);
				
				/* get the statistics handles */
				chan_access_local_handle = op_stat_reg ("Chan Access Delay (msec)",
												OPC_STAT_INDEX_NONE, OPC_STAT_LOCAL);
				chan_access_global_handle = op_stat_reg ("Chan Access Delay (msec)",
												OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				
				/* Obtain the object id of the connected transmitter. */
				tx_objid = op_topo_assoc (mod_objid, OPC_TOPO_ASSOC_OUT, OPC_OBJMTYPE_XMIT, 0);
				
				/* Obtain the index of the interconnecting stream. */
				strm_objid = op_topo_connect (mod_objid, tx_objid, OPC_OBJTYPE_STRM, 0);
				op_ima_obj_attr_get (strm_objid, "src stream", &tx_out_strm); 
				
				/* Obtain the object id of the connected receiver. */

⌨️ 快捷键说明

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