/* Process model C form file: ptp.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 ptp_pr_c [] = "MIL_3_Tfile_Hdr_ 60L 30A opnet 7 3842DD79 3842DD79 1 barley jorgev 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 */

/* packet streams definitions */
#define RCV_IN_STRM 0
#define SRC_IN_STRM 1
#define XMT_OUT_STRM 0

/* transition macros */
#define SRC_ARRVL (op_intrpt_type () == OPC_INTRPT_STRM && op_intrpt_strm () == SRC_IN_STRM)
#define RCV_ARRVL (op_intrpt_type () == OPC_INTRPT_STRM && op_intrpt_strm () == RCV_IN_STRM )

/* Structure for data in packet */
typedef struct _MessageType {
   char info[31];} MessageType;



/* 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 */
	Stathandle	             		PackSentHnd;
	Stathandle	             		PackRecHnd;
	int	                    		PackSent;
	int	                    		PackRec;
	} ptp_state;

#define pr_state_ptr            		((ptp_state*) SimI_Mod_State_Ptr)
#define PackSentHnd             		pr_state_ptr->PackSentHnd
#define PackRecHnd              		pr_state_ptr->PackRecHnd
#define PackSent                		pr_state_ptr->PackSent
#define PackRec                 		pr_state_ptr->PackRec

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


/* No Function Block */

enum { _block_origin = __LINE__ };

#if defined (__cplusplus)
extern "C" {
#endif
	void ptp (void);
	Compcode ptp_init (void **);
	void ptp_diag (void);
	void ptp_terminate (void);
	void ptp_svar (void *, const char *, char **);
#if defined (__cplusplus)
} /* end of 'extern "C"' */
#endif




/* Process model interrupt handling procedure */


void
ptp (void)
	{
	int _block_origin = 0;
	FIN (ptp ());
	if (1)
		{
		Packet *pkptr;
		int PackNo;
		
		Objid LocalId;
		


		FSM_ENTER (ptp)

		FSM_BLOCK_SWITCH
			{
			/*---------------------------------------------------------*/
			/** state (init) enter executives **/
			FSM_STATE_ENTER_FORCED (0, state0_enter_exec, "init", "ptp () [init enter execs]")
				{
				PackSent = 0;
				PackRec  = 0;
				
				/* Registering variables for statistics  collection */
				PackSentHnd = op_stat_reg ("Packets sent",OPC_STAT_INDEX_NONE,OPC_STAT_LOCAL);
				PackRecHnd = op_stat_reg ("Packets received",OPC_STAT_INDEX_NONE,OPC_STAT_LOCAL);
				
				}


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


			/** state (init) transition processing **/
			FSM_TRANSIT_FORCE (2, state2_enter_exec, ;)
				/*---------------------------------------------------------*/



			/** state (xmt) enter executives **/
			FSM_STATE_ENTER_FORCED (1, state1_enter_exec, "xmt", "ptp () [xmt enter execs]")
				{
				pkptr = op_pk_get (SRC_IN_STRM);
				
				/* updating counter */
				PackSent++;
				
				/* sending packet */
				op_pk_send (pkptr,XMT_OUT_STRM);
				
				/* updating statistics collector */  
				op_stat_write (PackSentHnd,PackSent);
				
				
				}


			/** state (xmt) exit executives **/
			FSM_STATE_EXIT_FORCED (1, state1_exit_exec, "xmt", "ptp () [xmt exit execs]")
				{
				}


			/** state (xmt) transition processing **/
			FSM_TRANSIT_FORCE (2, state2_enter_exec, ;)
				/*---------------------------------------------------------*/



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


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


			/** state (idle) exit executives **/
			FSM_STATE_EXIT_UNFORCED (2, state2_exit_exec, "idle", "ptp () [idle exit execs]")
				{
				}


			/** state (idle) transition processing **/
			FSM_INIT_COND (SRC_ARRVL)
			FSM_TEST_COND (RCV_ARRVL)
			FSM_DFLT_COND
			FSM_TEST_LOGIC ("idle")

			FSM_TRANSIT_SWITCH
				{
				FSM_CASE_TRANSIT (0, 1, state1_enter_exec, ;)
				FSM_CASE_TRANSIT (1, 3, state3_enter_exec, ;)
				FSM_CASE_TRANSIT (2, 2, state2_enter_exec, ;)
				}
				/*---------------------------------------------------------*/



			/** state (rcv) enter executives **/
			FSM_STATE_ENTER_FORCED (3, state3_enter_exec, "rcv", "ptp () [rcv enter execs]")
				{
				/* Get packet from stream */
				pkptr = op_pk_get (RCV_IN_STRM);
				
				op_pk_destroy (pkptr);
				/* updating counter */
				PackRec++;
				
				/* updating statistics collector */
				op_stat_write (PackRecHnd,PackRec);
				}


			/** state (rcv) exit executives **/
			FSM_STATE_EXIT_FORCED (3, state3_exit_exec, "rcv", "ptp () [rcv exit execs]")
				{
				}


			/** state (rcv) transition processing **/
			FSM_TRANSIT_FORCE (2, state2_enter_exec, ;)
				/*---------------------------------------------------------*/



			}


		FSM_EXIT (0,ptp)
		}
	}


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

	extern void				Vos_Vnop (void);

	FIN (ptp_init (gen_state_pptr))

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

		FRET (OPC_COMPCODE_SUCCESS)
		}
	}



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




void
ptp_terminate (void)
	{
	int _block_origin = __LINE__;

	FIN (ptp_terminate (void))

	if (1)
		{
		Packet *pkptr;
		int PackNo;
		
		Objid LocalId;
		

		/* 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 ptp_svar function. */
#undef PackSentHnd
#undef PackRecHnd
#undef PackSent
#undef PackRec



void
ptp_svar (void * gen_ptr, const char * var_name, char ** var_p_ptr)
	{
	ptp_state		*prs_ptr;

	FIN (ptp_svar (gen_ptr, var_name, var_p_ptr))

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

	if (Vos_String_Equal ("PackSentHnd" , var_name))
		{
		*var_p_ptr = (char *) (&prs_ptr->PackSentHnd);
		goto func_return;
		}
	if (Vos_String_Equal ("PackRecHnd" , var_name))
		{
		*var_p_ptr = (char *) (&prs_ptr->PackRecHnd);
		goto func_return;
		}
	if (Vos_String_Equal ("PackSent" , var_name))
		{
		*var_p_ptr = (char *) (&prs_ptr->PackSent);
		goto func_return;
		}
	if (Vos_String_Equal ("PackRec" , var_name))
		{
		*var_p_ptr = (char *) (&prs_ptr->PackRec);
		goto func_return;
		}

func_return:

	FOUT;
	}

