/* Process model C++ form file: abcGateWaySink.pr.cpp */
/* Portions of this file copyright 1986-2010 by OPNET Technologies, Inc. */


/*
 =========================== NOTE ==========================
 This file is automatically generated from abcGateWaySink.pr.m
 during a process model compilation.

 Do NOT manually edit this file.
 Manual edits will be lost during the next compilation.
 =========================== NOTE ==========================
*/



/* This variable carries the header into the object file */
const char abcGateWaySink_pr_cpp [] = "MIL_3_Tfile_Hdr_ 160A 30A op_runsim_opt 7 5170A543 5170A543 1 esl2-14 skathire 0 0 none none 0 0 none 0 0 0 0 0 0 0 0 2871 6                                                                                                                                                                                                                                                                                                                                                                                                   ";
#include <string.h>



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



/* Header Block */

#define FAST_IN_STRM 0
#define SLOW_IN_STRM 1

/* transition macros */
#define PACKET_FROM_FAST_LINK (op_intrpt_type () == \
OPC_INTRPT_STRM && op_intrpt_strm () == FAST_IN_STRM)

#define PACKET_FROM_SLOW_LINK (op_intrpt_type () == \
OPC_INTRPT_STRM && op_intrpt_strm () == SLOW_IN_STRM)

/* 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 */
class abcGateWaySink_state
	{
	private:
		/* Internal state tracking for FSM */
		FSM_SYS_STATE

	public:
		abcGateWaySink_state (void);

		/* Destructor contains Termination Block */
		~abcGateWaySink_state (void);

		/* State Variables */
		Stathandle	             		bits_rcvd_stathandle                            ;
		Stathandle	             		bitssec_rcvd_stathandle                         ;
		Stathandle	             		pkts_rcvd_stathandle                            ;
		Stathandle	             		pktssec_rcvd_stathandle                         ;
		Stathandle	             		ete_delay_stathandle                            ;
		Stathandle	             		bits_rcvd_gstathandle                           ;
		Stathandle	             		bitssec_rcvd_gstathandle                        ;
		Stathandle	             		pkts_rcvd_gstathandle                           ;
		Stathandle	             		pktssec_rcvd_gstathandle                        ;
		Stathandle	             		ete_delay_gstathandle                           ;
		Stathandle	             		ete_slow_packet_gsh                             ;

		/* FSM code */
		void abcGateWaySink (OP_SIM_CONTEXT_ARG_OPT);
		/* Diagnostic Block */
		void _op_abcGateWaySink_diag (OP_SIM_CONTEXT_ARG_OPT);

#if defined (VOSD_NEW_BAD_ALLOC)
		void * operator new (size_t) throw (VOSD_BAD_ALLOC);
#else
		void * operator new (size_t);
#endif
		void operator delete (void *);

		/* Memory management */
		static VosT_Obtype obtype;
	};

VosT_Obtype abcGateWaySink_state::obtype = (VosT_Obtype)OPC_NIL;

#define bits_rcvd_stathandle    		op_sv_ptr->bits_rcvd_stathandle
#define bitssec_rcvd_stathandle 		op_sv_ptr->bitssec_rcvd_stathandle
#define pkts_rcvd_stathandle    		op_sv_ptr->pkts_rcvd_stathandle
#define pktssec_rcvd_stathandle 		op_sv_ptr->pktssec_rcvd_stathandle
#define ete_delay_stathandle    		op_sv_ptr->ete_delay_stathandle
#define bits_rcvd_gstathandle   		op_sv_ptr->bits_rcvd_gstathandle
#define bitssec_rcvd_gstathandle		op_sv_ptr->bitssec_rcvd_gstathandle
#define pkts_rcvd_gstathandle   		op_sv_ptr->pkts_rcvd_gstathandle
#define pktssec_rcvd_gstathandle		op_sv_ptr->pktssec_rcvd_gstathandle
#define ete_delay_gstathandle   		op_sv_ptr->ete_delay_gstathandle
#define ete_slow_packet_gsh     		op_sv_ptr->ete_slow_packet_gsh

/* 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
#define FIN_PREAMBLE_DEC	abcGateWaySink_state *op_sv_ptr;
#define FIN_PREAMBLE_CODE	\
		op_sv_ptr = ((abcGateWaySink_state *)(OP_SIM_CONTEXT_PTR->_op_mod_state_ptr));


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

/* Undefine shortcuts to state variables because the */
/* following functions are part of the state class */
#undef bits_rcvd_stathandle
#undef bitssec_rcvd_stathandle
#undef pkts_rcvd_stathandle
#undef pktssec_rcvd_stathandle
#undef ete_delay_stathandle
#undef bits_rcvd_gstathandle
#undef bitssec_rcvd_gstathandle
#undef pkts_rcvd_gstathandle
#undef pktssec_rcvd_gstathandle
#undef ete_delay_gstathandle
#undef ete_slow_packet_gsh

/* Access from C kernel using C linkage */
extern "C"
{
	VosT_Obtype _op_abcGateWaySink_init (int * init_block_ptr);
	VosT_Address _op_abcGateWaySink_alloc (VosT_Obtype, int);
	void abcGateWaySink (OP_SIM_CONTEXT_ARG_OPT)
		{
		((abcGateWaySink_state *)(OP_SIM_CONTEXT_PTR->_op_mod_state_ptr))->abcGateWaySink (OP_SIM_CONTEXT_PTR_OPT);
		}

	void _op_abcGateWaySink_svar (void *, const char *, void **);

	void _op_abcGateWaySink_diag (OP_SIM_CONTEXT_ARG_OPT)
		{
		((abcGateWaySink_state *)(OP_SIM_CONTEXT_PTR->_op_mod_state_ptr))->_op_abcGateWaySink_diag (OP_SIM_CONTEXT_PTR_OPT);
		}

	void _op_abcGateWaySink_terminate (OP_SIM_CONTEXT_ARG_OPT)
		{
		/* The destructor is the Termination Block */
		delete (abcGateWaySink_state *)(OP_SIM_CONTEXT_PTR->_op_mod_state_ptr);
		}


} /* end of 'extern "C"' */




/* Process model interrupt handling procedure */


void
abcGateWaySink_state::abcGateWaySink (OP_SIM_CONTEXT_ARG_OPT)
	{
#if !defined (VOSD_NO_FIN)
	int _op_block_origin = 0;
#endif
	FIN_MT (abcGateWaySink_state::abcGateWaySink ());
	try
		{
		/* Temporary Variables */
		Packet*		pkptr;
		double		pk_size;
		double		ete_delay;
		/* End of Temporary Variables */


		FSM_ENTER ("abcGateWaySink")

		FSM_BLOCK_SWITCH
			{
			/*---------------------------------------------------------*/
			/** state (IDLE) enter executives **/
			FSM_STATE_ENTER_UNFORCED (0, "IDLE", state0_enter_exec, "abcGateWaySink [IDLE enter execs]")

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


			/** state (IDLE) exit executives **/
			FSM_STATE_EXIT_UNFORCED (0, "IDLE", "abcGateWaySink [IDLE exit execs]")


			/** state (IDLE) transition processing **/
			FSM_PROFILE_SECTION_IN ("abcGateWaySink [IDLE trans conditions]", state0_trans_conds)
			FSM_INIT_COND (PACKET_FROM_FAST_LINK)
			FSM_TEST_COND (PACKET_FROM_SLOW_LINK)
			FSM_TEST_LOGIC ("IDLE")
			FSM_PROFILE_SECTION_OUT (state0_trans_conds)

			FSM_TRANSIT_SWITCH
				{
				FSM_CASE_TRANSIT (0, 2, state2_enter_exec, ;, "PACKET_FROM_FAST_LINK", "", "IDLE", "processFastPacket", "tr_17", "abcGateWaySink [IDLE -> processFastPacket : PACKET_FROM_FAST_LINK / ]")
				FSM_CASE_TRANSIT (1, 3, state3_enter_exec, ;, "PACKET_FROM_SLOW_LINK", "", "IDLE", "processSlowPacket", "tr_21", "abcGateWaySink [IDLE -> processSlowPacket : PACKET_FROM_SLOW_LINK / ]")
				}
				/*---------------------------------------------------------*/



			/** state (INIT) enter executives **/
			FSM_STATE_ENTER_FORCED_NOLABEL (1, "INIT", "abcGateWaySink [INIT enter execs]")
				FSM_PROFILE_SECTION_IN ("abcGateWaySink [INIT enter execs]", state1_enter_exec)
				{
				/* Initilaize the statistic handles to keep	*/
				/* track of traffic sinked by this process.	*/
				
				bits_rcvd_gstathandle 		= op_stat_reg ("Sink.Traffic Received (bits)",			OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				bitssec_rcvd_gstathandle 	= op_stat_reg ("Sink.Traffic Received (bits/sec)",		OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				pkts_rcvd_gstathandle 		= op_stat_reg ("Sink.Traffic Received (packets)",		OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				pktssec_rcvd_gstathandle 	= op_stat_reg ("Sink.Traffic Received (packets/sec)",	OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				ete_delay_gstathandle		= op_stat_reg ("Sink.Fast Receiver Packet End-to-End Delay (seconds)",		OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				ete_slow_packet_gsh		    = op_stat_reg ("Sink.Slow Receiver Packet End-to-End Delay (seconds)",		OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
				}
				FSM_PROFILE_SECTION_OUT (state1_enter_exec)

			/** state (INIT) exit executives **/
			FSM_STATE_EXIT_FORCED (1, "INIT", "abcGateWaySink [INIT exit execs]")


			/** state (INIT) transition processing **/
			FSM_TRANSIT_FORCE (0, state0_enter_exec, ;, "default", "", "INIT", "IDLE", "tr_12", "abcGateWaySink [INIT -> IDLE : default / ]")
				/*---------------------------------------------------------*/



			/** state (processFastPacket) enter executives **/
			FSM_STATE_ENTER_FORCED (2, "processFastPacket", state2_enter_exec, "abcGateWaySink [processFastPacket enter execs]")
				FSM_PROFILE_SECTION_IN ("abcGateWaySink [processFastPacket enter execs]", state2_enter_exec)
				{
				/* Obtain the incoming packet.	*/
				pkptr = op_pk_get (FAST_IN_STRM);
				
				/* Caclulate metrics to be updated.		*/
				pk_size = (double) op_pk_total_size_get (pkptr);
				ete_delay = op_sim_time () - op_pk_creation_time_get (pkptr);
				
				/* Update global statistics.	*/
				op_stat_write (bits_rcvd_gstathandle, 		pk_size);
				op_stat_write (pkts_rcvd_gstathandle, 		1.0);
				op_stat_write (ete_delay_gstathandle, 		ete_delay);
				
				op_stat_write (bitssec_rcvd_gstathandle, 	pk_size);
				op_stat_write (bitssec_rcvd_gstathandle, 	0.0);
				op_stat_write (pktssec_rcvd_gstathandle, 	1.0);
				op_stat_write (pktssec_rcvd_gstathandle, 	0.0);
				
				/* Destroy the received packet.	*/
				op_pk_destroy (pkptr);
				}
				FSM_PROFILE_SECTION_OUT (state2_enter_exec)

			/** state (processFastPacket) exit executives **/
			FSM_STATE_EXIT_FORCED (2, "processFastPacket", "abcGateWaySink [processFastPacket exit execs]")


			/** state (processFastPacket) transition processing **/
			FSM_TRANSIT_FORCE (0, state0_enter_exec, ;, "default", "", "processFastPacket", "IDLE", "tr_19", "abcGateWaySink [processFastPacket -> IDLE : default / ]")
				/*---------------------------------------------------------*/



			/** state (processSlowPacket) enter executives **/
			FSM_STATE_ENTER_FORCED (3, "processSlowPacket", state3_enter_exec, "abcGateWaySink [processSlowPacket enter execs]")
				FSM_PROFILE_SECTION_IN ("abcGateWaySink [processSlowPacket enter execs]", state3_enter_exec)
				{
				/* Obtain the incoming packet.	*/
				pkptr = op_pk_get (SLOW_IN_STRM);
				
				/* Caclulate metrics to be updated.		*/
				pk_size = (double) op_pk_total_size_get (pkptr);
				ete_delay = op_sim_time () - op_pk_creation_time_get (pkptr);
				
				/* Update global statistics.	*/
				op_stat_write (bits_rcvd_gstathandle, 		pk_size);
				op_stat_write (pkts_rcvd_gstathandle, 		1.0);
				op_stat_write (ete_slow_packet_gsh, 		ete_delay);
				
				op_stat_write (bitssec_rcvd_gstathandle, 	pk_size);
				op_stat_write (bitssec_rcvd_gstathandle, 	0.0);
				op_stat_write (pktssec_rcvd_gstathandle, 	1.0);
				op_stat_write (pktssec_rcvd_gstathandle, 	0.0);
				
				/* Destroy the received packet.	*/
				op_pk_destroy (pkptr);
				}
				FSM_PROFILE_SECTION_OUT (state3_enter_exec)

			/** state (processSlowPacket) exit executives **/
			FSM_STATE_EXIT_FORCED (3, "processSlowPacket", "abcGateWaySink [processSlowPacket exit execs]")


			/** state (processSlowPacket) transition processing **/
			FSM_TRANSIT_FORCE (0, state0_enter_exec, ;, "default", "", "processSlowPacket", "IDLE", "tr_23", "abcGateWaySink [processSlowPacket -> IDLE : default / ]")
				/*---------------------------------------------------------*/



			}


		FSM_EXIT (1,"abcGateWaySink")
		}
	catch (...)
		{
		Vos_Error_Print (VOSC_ERROR_ABORT,
			(const char *)VOSC_NIL,
			"Unhandled C++ exception in process model (abcGateWaySink)",
			(const char *)VOSC_NIL, (const char *)VOSC_NIL);
		}
	}




void
abcGateWaySink_state::_op_abcGateWaySink_diag (OP_SIM_CONTEXT_ARG_OPT)
	{
	/* No Diagnostic Block */
	}

void
abcGateWaySink_state::operator delete (void* ptr)
	{
	FIN (abcGateWaySink_state::operator delete (ptr));
	Vos_Poolmem_Dealloc (ptr);
	FOUT
	}

abcGateWaySink_state::~abcGateWaySink_state (void)
	{

	FIN (abcGateWaySink_state::~abcGateWaySink_state ())


	/* No Termination Block */


	FOUT
	}


#undef FIN_PREAMBLE_DEC
#undef FIN_PREAMBLE_CODE

#define FIN_PREAMBLE_DEC
#define FIN_PREAMBLE_CODE

void *
abcGateWaySink_state::operator new (size_t)
#if defined (VOSD_NEW_BAD_ALLOC)
		throw (VOSD_BAD_ALLOC)
#endif
	{
	void * new_ptr;

	FIN_MT (abcGateWaySink_state::operator new ());

	new_ptr = Vos_Alloc_Object (abcGateWaySink_state::obtype);
#if defined (VOSD_NEW_BAD_ALLOC)
	if (new_ptr == VOSC_NIL) throw VOSD_BAD_ALLOC();
#endif
	FRET (new_ptr)
	}

/* State constructor initializes FSM handling */
/* by setting the initial state to the first */
/* block of code to enter. */

abcGateWaySink_state::abcGateWaySink_state (void) :
		_op_current_block (2)
	{
#if defined (OPD_ALLOW_ODB)
		_op_current_state = "abcGateWaySink [INIT enter execs]";
#endif
	}

VosT_Obtype
_op_abcGateWaySink_init (int * init_block_ptr)
	{
	FIN_MT (_op_abcGateWaySink_init (init_block_ptr))

	abcGateWaySink_state::obtype = Vos_Define_Object_Prstate ("proc state vars (abcGateWaySink)",
		sizeof (abcGateWaySink_state));
	*init_block_ptr = 2;

	FRET (abcGateWaySink_state::obtype)
	}

VosT_Address
_op_abcGateWaySink_alloc (VosT_Obtype, int)
	{
#if !defined (VOSD_NO_FIN)
	int _op_block_origin = 0;
#endif
	abcGateWaySink_state * ptr;
	FIN_MT (_op_abcGateWaySink_alloc ())

	/* New instance will have FSM handling initialized */
#if defined (VOSD_NEW_BAD_ALLOC)
	try {
		ptr = new abcGateWaySink_state;
	} catch (const VOSD_BAD_ALLOC &) {
		ptr = VOSC_NIL;
	}
#else
	ptr = new abcGateWaySink_state;
#endif
	FRET ((VosT_Address)ptr)
	}



void
_op_abcGateWaySink_svar (void * gen_ptr, const char * var_name, void ** var_p_ptr)
	{
	abcGateWaySink_state		*prs_ptr;

	FIN_MT (_op_abcGateWaySink_svar (gen_ptr, var_name, var_p_ptr))

	if (var_name == OPC_NIL)
		{
		*var_p_ptr = (void *)OPC_NIL;
		FOUT
		}
	prs_ptr = (abcGateWaySink_state *)gen_ptr;

	if (strcmp ("bits_rcvd_stathandle" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->bits_rcvd_stathandle);
		FOUT
		}
	if (strcmp ("bitssec_rcvd_stathandle" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->bitssec_rcvd_stathandle);
		FOUT
		}
	if (strcmp ("pkts_rcvd_stathandle" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->pkts_rcvd_stathandle);
		FOUT
		}
	if (strcmp ("pktssec_rcvd_stathandle" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->pktssec_rcvd_stathandle);
		FOUT
		}
	if (strcmp ("ete_delay_stathandle" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->ete_delay_stathandle);
		FOUT
		}
	if (strcmp ("bits_rcvd_gstathandle" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->bits_rcvd_gstathandle);
		FOUT
		}
	if (strcmp ("bitssec_rcvd_gstathandle" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->bitssec_rcvd_gstathandle);
		FOUT
		}
	if (strcmp ("pkts_rcvd_gstathandle" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->pkts_rcvd_gstathandle);
		FOUT
		}
	if (strcmp ("pktssec_rcvd_gstathandle" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->pktssec_rcvd_gstathandle);
		FOUT
		}
	if (strcmp ("ete_delay_gstathandle" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->ete_delay_gstathandle);
		FOUT
		}
	if (strcmp ("ete_slow_packet_gsh" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->ete_slow_packet_gsh);
		FOUT
		}
	*var_p_ptr = (void *)OPC_NIL;

	FOUT
	}

