/* Process model C form file: cct_rx.pr.c */
/* Portions of this file copyright 1992, 1996, 1998 by MIL 3, Inc. */



/* This variable carries the header into the object file */
static const char cct_rx_pr_c [] = "MIL_3_Tfile_Hdr_ 60L 30A opnet 7 38E114ED 38E114ED 1 barley nse 0 0 none none 0 0 none 0 0 0 0 0 0";


/* 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 */

/* Input stream from bus receiver */
#define IN_STRM 0

/* Conditional Macros */
#define PKT_RCVD (op_intrpt_type() == OPC_INTRPT_STRM)
#define END_SIM (op_intrpt_type() == OPC_INTRPT_ENDSIM)

/*Global Variable */
int subm_pkts = 0; 



/* End of Header Block */


#if !defined (VOSD_NO_FIN)
#undef	BIN
#undef	BOUT
#define	BIN		_fstack_local_info.last_line_passed = __LINE__ - _block_origin;
#define	BOUT	BIN
#define	BINIT	_fstack_local_info.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                     		rcvd_pkts;
	double                  		ete_delay;
	} cct_rx_state;

#define pr_state_ptr            		((cct_rx_state*) SimI_Mod_State_Ptr)
#define rcvd_pkts               		pr_state_ptr->rcvd_pkts
#define ete_delay               		pr_state_ptr->ete_delay

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


/* Function Block */

enum { _block_origin = __LINE__ };
/* This function gets the received packet, destroys it, and then */
/* logs the incremented received packet total */
proc_pkt()
  { 
   Packet* in_pkt; 
   /* Get packet from bus receiver input stream */
   in_pkt = op_pk_get(IN_STRM);
   ete_delay = ete_delay + op_sim_time() - op_pk_creation_time_get(in_pkt);
   
   /* Destroy the received packet */
   op_pk_destroy(in_pkt);

   /* Increment the count of received packets */
   ++rcvd_pkts;
   }

/* This function writes the end-of-simulation channel traffic and */
/* channel throughput statistics */
record_stats()
      { 
       /* Record final statistics */ 
       op_stat_scalar_write ("Channel Traffic G",
           (double) subm_pkts / op_sim_time());
       op_stat_scalar_write ("Channel Throughput S",
            (double) rcvd_pkts /op_sim_time());
       op_stat_scalar_write ("Average End to End Delay D", 
            (double) ((ete_delay/rcvd_pkts) * (subm_pkts/rcvd_pkts)));
      }

/* End of Function Block */

#if defined (__cplusplus)
extern "C" {
#endif
	void cct_rx (void);
	Compcode cct_rx_init (void **);
	void cct_rx_diag (void);
	void cct_rx_terminate (void);
	void cct_rx_svar (void *, const char *, char **);
#if defined (__cplusplus)
} /* end of 'extern "C"' */
#endif




/* Process model interrupt handling procedure */


void
cct_rx (void)
	{
	int _block_origin = 0;
	FIN (cct_rx ());
	if (1)
		{
		


		FSM_ENTER (cct_rx)

		FSM_BLOCK_SWITCH
			{
			/*---------------------------------------------------------*/
			/** state (init) enter executives **/
			FSM_STATE_ENTER_FORCED (0, state0_enter_exec, "init", "cct_rx () [init enter execs]")
				{
				/* Initialize Accumulator */
				rcvd_pkts = 0; 
				ete_delay = 0.0;
				
				}


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


			/** state (init) transition processing **/
			FSM_INIT_COND (PKT_RCVD)
			FSM_TEST_COND (END_SIM)
			FSM_TEST_LOGIC ("init")

			FSM_TRANSIT_SWITCH
				{
				FSM_CASE_TRANSIT (0, 1, state1_enter_exec, proc_pkt();)
				FSM_CASE_TRANSIT (1, 1, state1_enter_exec, record_stats();)
				}
				/*---------------------------------------------------------*/



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


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


			/** state (idle) exit executives **/
			FSM_STATE_EXIT_UNFORCED (1, state1_exit_exec, "idle", "cct_rx () [idle exit execs]")
				{
				}


			/** state (idle) transition processing **/
			FSM_INIT_COND (END_SIM)
			FSM_TEST_COND (PKT_RCVD)
			FSM_DFLT_COND
			FSM_TEST_LOGIC ("idle")

			FSM_TRANSIT_SWITCH
				{
				FSM_CASE_TRANSIT (0, 1, state1_enter_exec, record_stats();)
				FSM_CASE_TRANSIT (1, 1, state1_enter_exec, proc_pkt();)
				FSM_CASE_TRANSIT (2, 1, state1_enter_exec, ;)
				}
				/*---------------------------------------------------------*/



			}


		FSM_EXIT (0,cct_rx)
		}
	}


Compcode
cct_rx_init (void ** gen_state_pptr)
	{
	int _block_origin = 0;
	static VosT_Cm_Obtype	obtype = OPC_NIL;

	extern void				Vos_Vnop (void);

	FIN (cct_rx_init (gen_state_pptr))

	if (obtype == OPC_NIL)
		{
		/* Initialize memory management */
		if (Vos_Catmem_Register ("proc state vars (cct_rx)",
			sizeof (cct_rx_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 */
		((cct_rx_state *)(*gen_state_pptr))->current_block = 0;

		FRET (OPC_COMPCODE_SUCCESS)
		}
	}



void
cct_rx_diag (void)
	{
	/* No Diagnostic Block */
	}




void
cct_rx_terminate (void)
	{
	int _block_origin = __LINE__;

	FIN (cct_rx_terminate (void))

	if (1)
		{
		

		/* 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 cct_rx_svar function. */
#undef rcvd_pkts
#undef ete_delay



void
cct_rx_svar (void * gen_ptr, const char * var_name, char ** var_p_ptr)
	{
	cct_rx_state		*prs_ptr;

	FIN (cct_rx_svar (gen_ptr, var_name, var_p_ptr))

	*var_p_ptr = (char *)VOS_NIL;
	prs_ptr = (cct_rx_state *)gen_ptr;

	if (Vos_String_Equal ("rcvd_pkts" , var_name))
		{
		*var_p_ptr = (char *) (&prs_ptr->rcvd_pkts);
		goto func_return;
		}
	if (Vos_String_Equal ("ete_delay" , var_name))
		{
		*var_p_ptr = (char *) (&prs_ptr->ete_delay);
		goto func_return;
		}

func_return:

	FOUT;
	}

