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

📄 dr_ack.pr.c

📁 实验室前辈用OPNET做的光突发交换网络的仿真实验
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Process model C form file: dr_ack.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 dr_ack_pr_c [] = "MIL_3_Tfile_Hdr_ 80C 30A modeler 7 3D030E98 3D030E98 1 notebook lhj 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 <string.h>
#include <stdlib.h>

#define APP_IN_STRM 	0
#define APP_OUT_STRM	0
#define RTE_IN_STRM		1
#define	RTE_OUT_STRM	1
#define TRUE				1
#define FALSE				0
#define RANDOM				0
#define MAX_NODES			100
#define	ACK_PK_SIZE			20

#define END_SIM	(op_intrpt_type () == OPC_INTRPT_ENDSIM)
#define BEG_SIM (op_intrpt_type () == OPC_INTRPT_BEGSIM)
#define APP_ARRIVAL (op_intrpt_type () == OPC_INTRPT_STRM && \
                     op_intrpt_strm () == APP_IN_STRM)
#define RTE_ARRIVAL	(op_intrpt_type () == OPC_INTRPT_STRM && \
					 op_intrpt_strm () == RTE_IN_STRM)
#define NOT_WAITING_ACK (sequence_number_wait == -1)
#define Q_NOT_EMPTY (queue_size > 0)
#define ACK_ARRIVAL (strcmp ("dr_ack", pk_format) == 0)
#define PK_ARRIVAL (!ACK_ARRIVAL)
#define VALID_ACK (valid_ack == OPC_TRUE)

int node_count = 0;
int num_pk_gen = 0;
int dup_pk = 0;
extern int	debug_sw;
int ack_reentry = 0;

/* 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 */
	int	                    		sequence_number_wait;
	int	                    		sequence_number_current;
	int	                    		node_address;
	int	                    		nodes_total;
	int	                    		queue_size;
	int	                    		ack;
	int	                    		dest_method;
	int	                    		pk_rcvd[MAX_NODES];
	char	                   		destination_scheme[128];
	Distribution*	          		address_dist;
	Objid	                  		sar_mod_objid;
	Objid	                  		node_id;
	Stathandle	             		num_pk_gen_gsh;
	Stathandle	             		dup_pk_gsh;
	} dr_ack_state;

#define pr_state_ptr            		((dr_ack_state*) SimI_Mod_State_Ptr)
#define sequence_number_wait    		pr_state_ptr->sequence_number_wait
#define sequence_number_current 		pr_state_ptr->sequence_number_current
#define node_address            		pr_state_ptr->node_address
#define nodes_total             		pr_state_ptr->nodes_total
#define queue_size              		pr_state_ptr->queue_size
#define ack                     		pr_state_ptr->ack
#define dest_method             		pr_state_ptr->dest_method
#define pk_rcvd                 		pr_state_ptr->pk_rcvd
#define destination_scheme      		pr_state_ptr->destination_scheme
#define address_dist            		pr_state_ptr->address_dist
#define sar_mod_objid           		pr_state_ptr->sar_mod_objid
#define node_id                 		pr_state_ptr->node_id
#define num_pk_gen_gsh          		pr_state_ptr->num_pk_gen_gsh
#define dup_pk_gsh              		pr_state_ptr->dup_pk_gsh

/* 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	dr_ack_state *op_sv_ptr = pr_state_ptr;


/* Function Block */

enum { _block_origin = __LINE__ };


/* 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 dr_ack (void);
	Compcode dr_ack_init (void **);
	void dr_ack_diag (void);
	void dr_ack_terminate (void);
	void dr_ack_svar (void *, const char *, char **);
#if defined (__cplusplus)
} /* end of 'extern "C"' */
#endif




/* Process model interrupt handling procedure */


void
dr_ack (void)
	{
	int _block_origin = 0;
	FIN (dr_ack ());
	if (1)
		{
		Packet*		pkptr;
		Packet*		ack_pkptr;
		Objid		my_id;
		Objid		dest_node_objid;
		char		pk_format[120];
		int			sequence_number_ack;
		int			sequence_number_rcv;
		int			source_address;
		int			destination_address;
		int			i;
		Boolean		valid_ack;


		FSM_ENTER (dr_ack)

		FSM_BLOCK_SWITCH
			{
			/*---------------------------------------------------------*/
			/** state (init) enter executives **/
			FSM_STATE_ENTER_FORCED_NOLABEL (0, "init", "dr_ack () [init enter execs]")
				{
				/* determine the obj id of the containing module */
				my_id = op_id_self ();
				
				/* determine the node id of the current node */
				node_id = op25_topo_parent (my_id);
				
				/* determine the node address of the current node */
				node_count++;
				node_address = node_count;
				
				/* assign the node address to the user id attribute */
				op_ima_obj_attr_set (node_id, "user id", node_address);
				
				/* initialize the current sequence number to be zero */
				sequence_number_current = 0;
				
				/* initialize the waiting sequence number to be -1 */
				sequence_number_wait = -1;
				
				/* determine the total number of end nodes connceted to the network */
				nodes_total = op_topo_object_count (OPC_OBJTYPE_IGEN);
				
				/* determine the objid of the SAR module */
				sar_mod_objid = op_id_from_name (node_id, OPC_OBJTYPE_PROC, 
								"sar");
				
				/* set up the address distribution */
				address_dist = op_dist_load ("uniform_int", 1, nodes_total);
				
				/* the queue size is zero at the beginning */
				queue_size = 0;
				
				/* determine destination addressing scheme */
				op_ima_obj_attr_get (my_id, "destination scheme", destination_scheme);
				if (toupper (destination_scheme[0]) == 'R')
					{
					dest_method = RANDOM;
					}
				else
					{
					dest_method = !RANDOM;
					}
				
				/* determine if ack is necessary */
				op_ima_obj_attr_get (my_id, "ack", &ack);
				
				/* initialize the counter of packets received from each source */
				for (i = 0; i < MAX_NODES; i++) {
					pk_rcvd[i] = -1;
				}
				
				/* register global stats and write an init point in each */
				num_pk_gen_gsh = op_stat_reg ("Global application packets generated (pks)", OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				op_stat_write (num_pk_gen_gsh, num_pk_gen);
				
				dup_pk_gsh = op_stat_reg ("Global duplicated application packets received (pks)",
					OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				op_stat_write (dup_pk_gsh, dup_pk);
				
				}


			/** state (init) exit executives **/
			FSM_STATE_EXIT_FORCED (0, "init", "dr_ack () [init exit execs]")
				{
				
				}


			/** state (init) transition processing **/
			FSM_TRANSIT_ONLY ((BEG_SIM), 1, state1_enter_exec, ;, "init", "BEG_SIM", "", "init", "wait")
				/*---------------------------------------------------------*/



			/** state (wait) enter executives **/
			FSM_STATE_ENTER_UNFORCED (1, state1_enter_exec, "wait", "dr_ack () [wait enter execs]")
				{
				}


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


			/** state (wait) exit executives **/
			FSM_STATE_EXIT_UNFORCED (1, "wait", "dr_ack () [wait exit execs]")
				{
				}


			/** state (wait) transition processing **/
			FSM_INIT_COND (APP_ARRIVAL)
			FSM_TEST_COND (RTE_ARRIVAL)
			FSM_TEST_COND (END_SIM)
			FSM_TEST_LOGIC ("wait")

			FSM_TRANSIT_SWITCH
				{
				FSM_CASE_TRANSIT (0, 2, state2_enter_exec, ;, "APP_ARRIVAL", "", "wait", "enqueue")
				FSM_CASE_TRANSIT (1, 5, state5_enter_exec, ;, "RTE_ARRIVAL", "", "wait", "pk type")
				FSM_CASE_TRANSIT (2, 7, state7_enter_exec, ;, "END_SIM", "", "wait", "end")
				}
				/*---------------------------------------------------------*/



			/** state (enqueue) enter executives **/
			FSM_STATE_ENTER_FORCED (2, state2_enter_exec, "enqueue", "dr_ack () [enqueue enter execs]")
				{
				/* obtain the app packet */
				pkptr = op_pk_get (APP_IN_STRM);
				num_pk_gen++;
				op_stat_write (num_pk_gen_gsh, num_pk_gen);
				
				/* set the sequence number field for this packet */
				op25_pk_nfd_set (pkptr, "sequence number", sequence_number_current);
				
				/* increment the current sequence number counter */
				sequence_number_current++;
				
				/* set the source, destination fields for this packet */
				op25_pk_nfd_set (pkptr, "source address", node_address);
				
				/* a node does not send packets to itself */
				if (dest_method == RANDOM) 
					{
					while ((destination_address = (int ) op_dist_outcome (address_dist)) == node_address) 
						{
				
						/* get the destination address from the random number generator */
						destination_address = op_dist_outcome (address_dist);
						}
					}
				else 
					{
					/* the destination address is fixed by the attribute destination_scheme */
				
					/* get the destionation node's objid */
					dest_node_objid = op_id_from_name (op25_topo_parent (node_id), OPC_OBJTYPE_NDFIX, destination_scheme);
				    
					/* get the destination node's address */
					op_ima_obj_attr_get (dest_node_objid, "user id", &destination_address);
					printf("dest address = %d\n", &destination_address);
				    }
				
				/* set the destination address to the packet */
				op25_pk_nfd_set (pkptr, "destination address", destination_address);
				
				/* insert the app packet into the buffer and increment the queue size */
				op_subq_pk_insert (0, pkptr, OPC_QPOS_TAIL);
				queue_size++;
				}


			/** state (enqueue) exit executives **/
			FSM_STATE_EXIT_FORCED (2, "enqueue", "dr_ack () [enqueue exit execs]")
				{
				}


			/** state (enqueue) transition processing **/
			FSM_INIT_COND (NOT_WAITING_ACK)
			FSM_DFLT_COND
			FSM_TEST_LOGIC ("enqueue")

			FSM_TRANSIT_SWITCH
				{
				FSM_CASE_TRANSIT (0, 4, state4_enter_exec, ;, "NOT_WAITING_ACK", "", "enqueue", "send")
				FSM_CASE_TRANSIT (1, 1, state1_enter_exec, ;, "default", "", "enqueue", "wait")
				}
				/*---------------------------------------------------------*/



			/** state (rcv_ack) enter executives **/
			FSM_STATE_ENTER_FORCED (3, state3_enter_exec, "rcv_ack", "dr_ack () [rcv_ack enter execs]")
				{
				/* determine which packet number this ack is acknowledging */
				op_pk_nfd_get (pkptr, "sequence number ack", &sequence_number_ack);
				
				/* destroy the ack packet */
				op_pk_destroy (pkptr);
				
				/* if debug switch is on */
				if (debug_sw)
					{
					printf("\t\tnode = %d, waiting ack = %d, rcv ack = %d\n", node_address, sequence_number_wait, sequence_number_ack);
				    }
				
				/* if this sequence number has been acked, the current ack is ignorred */
				/* otherwise destroy the packet at the head of buffer */
				if (sequence_number_ack != sequence_number_wait) 
					{
					/* this is not a valid ack */
					valid_ack = OPC_FALSE;
				    }
				else 
					{
					/* this is a valid ack */
					valid_ack = OPC_TRUE;
					sequence_number_wait = -1;
				
					/* inform sar to stop the timer */
				    op_intrpt_schedule_remote (op_sim_time (), 0, sar_mod_objid);
					}
				}


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


⌨️ 快捷键说明

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