/* Process model C form file: regen_try3_5.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 regen_try3_5_pr_c [] = "MIL_3_Tfile_Hdr_ 60L 30A opnet 7 38CD80DD 38CD80DD 1 barley kyvernit 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 */

#define		PK_GEN	0
#define 	CALM	7
#define		BURST 	8

#define 	GENERATE_PKT	((op_intrpt_type() == OPC_INTRPT_SELF) && (op_intrpt_code() == PK_GEN))
#define		BEGSIM_INTRPT	(op_intrpt_type() == OPC_INTRPT_BEGSIM)
#define 	CALM_INTRPT		((op_intrpt_type() == OPC_INTRPT_SELF) && (op_intrpt_code() == CALM))
#define 	BURST_INTRPT	((op_intrpt_type() == OPC_INTRPT_SELF) && (op_intrpt_code() == BURST))

/* 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 */
	Packet *	               		pk_ptr;
	double	                 		burst_strength;
	double	                 		burst_interarriv;
	double	                 		burst_duration;
	Objid	                  		self_id;
	double	                 		pk_length;
	char*	                  		pk_format[255];
	double	                 		prev_pk_generation;
	Stathandle	             		pk_ia_stat;
	} regen_try3_5_state;

#define pr_state_ptr            		((regen_try3_5_state*) SimI_Mod_State_Ptr)
#define pk_ptr                  		pr_state_ptr->pk_ptr
#define burst_strength          		pr_state_ptr->burst_strength
#define burst_interarriv        		pr_state_ptr->burst_interarriv
#define burst_duration          		pr_state_ptr->burst_duration
#define self_id                 		pr_state_ptr->self_id
#define pk_length               		pr_state_ptr->pk_length
#define pk_format               		pr_state_ptr->pk_format
#define prev_pk_generation      		pr_state_ptr->prev_pk_generation
#define pk_ia_stat              		pr_state_ptr->pk_ia_stat

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


/* No Function Block */

enum { _block_origin = __LINE__ };

#if defined (__cplusplus)
extern "C" {
#endif
	void regen_try3_5 (void);
	Compcode regen_try3_5_init (void **);
	void regen_try3_5_diag (void);
	void regen_try3_5_terminate (void);
	void regen_try3_5_svar (void *, const char *, char **);
#if defined (__cplusplus)
} /* end of 'extern "C"' */
#endif




/* Process model interrupt handling procedure */


void
regen_try3_5 (void)
	{
	int _block_origin = 0;
	FIN (regen_try3_5 ());
	if (1)
		{
		double pk_len;


		FSM_ENTER (regen_try3_5)

		FSM_BLOCK_SWITCH
			{
			/*---------------------------------------------------------*/
			/** state (init) enter executives **/
			FSM_STATE_ENTER_FORCED (0, state0_enter_exec, "init", "regen_try3_5 () [init enter execs]")
				{
				/** Initialize passed simulation values **/
				
				self_id = op_id_self();
				
				op_ima_obj_attr_get (self_id, "Burst Strength", &burst_strength);
				op_ima_obj_attr_get (self_id, "Burst Interarrival", &burst_interarriv);
				op_ima_obj_attr_get (self_id, "Burst Duration", &burst_duration);
				op_ima_obj_attr_get (self_id, "Mean Pckt Length", &pk_length);
				op_ima_obj_attr_get (self_id, "packet format", pk_format);
				
				pk_ia_stat = op_stat_reg("Packet Interarrival Time", OPC_STAT_INDEX_NONE, OPC_STAT_LOCAL);
				
				}


			/** state (init) exit executives **/
			FSM_STATE_EXIT_FORCED (0, state0_exit_exec, "init", "regen_try3_5 () [init exit execs]")
				{
				/** "dist_name" is name of desired distribution; **/
				/** "dist_arg1" and "dist_arg2" are its arguments. **/
				/** dist_ptr = op_dist_load (dist_name, dist_arg1, dist_arg2); **/
				
				/** Schedule the first interrupt. **/
				/*rand_time = op_dist_exponential(2.5); */
				op_intrpt_schedule_self (op_dist_exponential(burst_strength), PK_GEN);
				
				/* Record the time to be used for interarrival statistic*/
				prev_pk_generation=0;
				
				/*Schedule a special interrupt for transitioning out of 'busy' sending mode into 'calm' */
				op_intrpt_schedule_self (burst_duration, CALM);
				
				/*Schedule burst interarrival time, an interrupt that will occur forcing 'calm' back to 'send' */
				op_intrpt_schedule_self (burst_interarriv, BURST);
				
				}


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



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


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


			/** state (busy) exit executives **/
			FSM_STATE_EXIT_UNFORCED (1, state1_exit_exec, "busy", "regen_try3_5 () [busy exit execs]")
				{
				/*Keep a statistic of the (times or interarrival times) of generated packets*/
				op_stat_write(pk_ia_stat, op_sim_time() - prev_pk_generation);
				
				/** "pk_format" is name of desired packet format; **/
				/** "out_str" is the index of the output stream. **/
				/**pk_ptr = op_pk_create_fmt (pk_format);**/
				/*pk_ptr = op_pk_create_fmt ("NONE");*/
				pk_len=op_dist_exponential(pk_length);
				pk_ptr = op_pk_create (pk_len);
				op_pk_send (pk_ptr, 0);
				prev_pk_generation = op_sim_time();
				
				/* Schedule the next interrupt. */
				/* rand_time = op_dist_exponential(2.5); */
				op_intrpt_schedule_self (op_sim_time() + op_dist_exponential(burst_strength), PK_GEN);
				
				}


			/** state (busy) transition processing **/
			FSM_INIT_COND (GENERATE_PKT)
			FSM_TEST_COND (CALM_INTRPT)
			FSM_TEST_LOGIC ("busy")

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



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


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


			/** state (calm) exit executives **/
			FSM_STATE_EXIT_UNFORCED (2, state2_exit_exec, "calm", "regen_try3_5 () [calm exit execs]")
				{
				/** DO NOTHING **/
				}


			/** state (calm) transition processing **/
			FSM_INIT_COND (BURST_INTRPT)
			FSM_DFLT_COND
			FSM_TEST_LOGIC ("calm")

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



			/** state (B_init) enter executives **/
			FSM_STATE_ENTER_FORCED (3, state3_enter_exec, "B_init", "regen_try3_5 () [B_init enter execs]")
				{
				/*Schedule the end of the 'burst'*/
				/*Hopefully this will happen before the agreed burst interarrival time*/
				op_intrpt_schedule_self (op_sim_time() + burst_duration, CALM);
				
				/*This interupt is to occur when process is in 'calm' mode*/
				/*Schedule the return to 'burst' from 'calm'*/
				op_intrpt_schedule_self (op_sim_time() + burst_interarriv, BURST);
				}


			/** state (B_init) exit executives **/
			FSM_STATE_EXIT_FORCED (3, state3_exit_exec, "B_init", "regen_try3_5 () [B_init exit execs]")
				{
				/*Schedule next packet to be generated */
				op_intrpt_schedule_self (op_sim_time() + op_dist_exponential(burst_strength), PK_GEN);
				
				}


			/** state (B_init) transition processing **/
			FSM_TRANSIT_FORCE (1, state1_enter_exec, ;)
				/*---------------------------------------------------------*/



			}


		FSM_EXIT (0,regen_try3_5)
		}
	}


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

	extern void				Vos_Vnop (void);

	FIN (regen_try3_5_init (gen_state_pptr))

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

		FRET (OPC_COMPCODE_SUCCESS)
		}
	}



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




void
regen_try3_5_terminate (void)
	{
	int _block_origin = __LINE__;

	FIN (regen_try3_5_terminate (void))

	if (1)
		{
		double pk_len;

		/* 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 regen_try3_5_svar function. */
#undef pk_ptr
#undef burst_strength
#undef burst_interarriv
#undef burst_duration
#undef self_id
#undef pk_length
#undef pk_format
#undef prev_pk_generation
#undef pk_ia_stat



void
regen_try3_5_svar (void * gen_ptr, const char * var_name, char ** var_p_ptr)
	{
	regen_try3_5_state		*prs_ptr;

	FIN (regen_try3_5_svar (gen_ptr, var_name, var_p_ptr))

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

	if (Vos_String_Equal ("pk_ptr" , var_name))
		{
		*var_p_ptr = (char *) (&prs_ptr->pk_ptr);
		goto func_return;
		}
	if (Vos_String_Equal ("burst_strength" , var_name))
		{
		*var_p_ptr = (char *) (&prs_ptr->burst_strength);
		goto func_return;
		}
	if (Vos_String_Equal ("burst_interarriv" , var_name))
		{
		*var_p_ptr = (char *) (&prs_ptr->burst_interarriv);
		goto func_return;
		}
	if (Vos_String_Equal ("burst_duration" , var_name))
		{
		*var_p_ptr = (char *) (&prs_ptr->burst_duration);
		goto func_return;
		}
	if (Vos_String_Equal ("self_id" , var_name))
		{
		*var_p_ptr = (char *) (&prs_ptr->self_id);
		goto func_return;
		}
	if (Vos_String_Equal ("pk_length" , var_name))
		{
		*var_p_ptr = (char *) (&prs_ptr->pk_length);
		goto func_return;
		}
	if (Vos_String_Equal ("pk_format" , var_name))
		{
		*var_p_ptr = (char *) (prs_ptr->pk_format);
		goto func_return;
		}
	if (Vos_String_Equal ("prev_pk_generation" , var_name))
		{
		*var_p_ptr = (char *) (&prs_ptr->prev_pk_generation);
		goto func_return;
		}
	if (Vos_String_Equal ("pk_ia_stat" , var_name))
		{
		*var_p_ptr = (char *) (&prs_ptr->pk_ia_stat);
		goto func_return;
		}

func_return:

	FOUT;
	}

