/* Process model C form file: onu_sem_static.pr.c */
/* Portions of this file copyright 1986-2008 by OPNET Technologies, Inc. */



/* This variable carries the header into the object file */
const char onu_sem_static_pr_c [] = "MIL_3_Tfile_Hdr_ 145A 30A op_runsim 7 59052F50 59052F50 1 winxpvm FEKT 0 0 none none 0 0 none 0 0 0 0 0 0 0 0 1e80 8                                                                                                                                                                                                                                                                                                                                                                                                           ";
#include <string.h>



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



/* Header Block */

#define PK_ARRVL (op_intrpt_type () == OPC_INTRPT_STRM)
#define SEND  (op_intrpt_type () == OPC_INTRPT_SELF && op_intrpt_code () == 10)

/* 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 */
typedef struct
	{
	/* Internal state tracking for FSM */
	FSM_SYS_STATE
	/* State Variables */
	int	                    		adresa                                          ;
	Objid	                  		ID                                              ;
	Objid	                  		ppid                                            ;
	Objid	                  		sender_id                                       ;
	int	                    		source                                          ;
	int	                    		seed                                            ;
	int	                    		procento                                        ;
	double	                 		vel_paketu                                      ;
	double	                 		fyzicky_ramec                                   ;
	} onu_sem_static_state;

#define adresa                  		op_sv_ptr->adresa
#define ID                      		op_sv_ptr->ID
#define ppid                    		op_sv_ptr->ppid
#define sender_id               		op_sv_ptr->sender_id
#define source                  		op_sv_ptr->source
#define seed                    		op_sv_ptr->seed
#define procento                		op_sv_ptr->procento
#define vel_paketu              		op_sv_ptr->vel_paketu
#define fyzicky_ramec           		op_sv_ptr->fyzicky_ramec

/* 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	onu_sem_static_state *op_sv_ptr;
#define FIN_PREAMBLE_CODE	\
		op_sv_ptr = ((onu_sem_static_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

#if defined (__cplusplus)
extern "C" {
#endif
	void onu_sem_static (OP_SIM_CONTEXT_ARG_OPT);
	VosT_Obtype _op_onu_sem_static_init (int * init_block_ptr);
	void _op_onu_sem_static_diag (OP_SIM_CONTEXT_ARG_OPT);
	void _op_onu_sem_static_terminate (OP_SIM_CONTEXT_ARG_OPT);
	VosT_Address _op_onu_sem_static_alloc (VosT_Obtype, int);
	void _op_onu_sem_static_svar (void *, const char *, void **);


#if defined (__cplusplus)
} /* end of 'extern "C"' */
#endif




/* Process model interrupt handling procedure */


void
onu_sem_static (OP_SIM_CONTEXT_ARG_OPT)
	{
#if !defined (VOSD_NO_FIN)
	int _op_block_origin = 0;
#endif
	FIN_MT (onu_sem_static ());

		{


		FSM_ENTER ("onu_sem_static")

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

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


			/** state (idle) exit executives **/
			FSM_STATE_EXIT_UNFORCED (0, "idle", "onu_sem_static [idle exit execs]")


			/** state (idle) transition processing **/
			FSM_PROFILE_SECTION_IN ("onu_sem_static [idle trans conditions]", state0_trans_conds)
			FSM_INIT_COND (PK_ARRVL)
			FSM_TEST_COND (SEND)
			FSM_TEST_LOGIC ("idle")
			FSM_PROFILE_SECTION_OUT (state0_trans_conds)

			FSM_TRANSIT_SWITCH
				{
				FSM_CASE_TRANSIT (0, 1, state1_enter_exec, ;, "PK_ARRVL", "", "idle", "pk_check", "tr_0", "onu_sem_static [idle -> pk_check : PK_ARRVL / ]")
				FSM_CASE_TRANSIT (1, 2, state2_enter_exec, ;, "SEND", "", "idle", "send", "tr_4", "onu_sem_static [idle -> send : SEND / ]")
				}
				/*---------------------------------------------------------*/



			/** state (pk_check) enter executives **/
			FSM_STATE_ENTER_FORCED (1, "pk_check", state1_enter_exec, "onu_sem_static [pk_check enter execs]")
				FSM_PROFILE_SECTION_IN ("onu_sem_static [pk_check enter execs]", state1_enter_exec)
				{
				
				Packet * pk_arrived;
				//Packet * pk_send;
				
				int destination;
				
				double grant_start;
				double grant_size;
				
				
				double cas;
				
				
				char own_name[50];
				op_ima_obj_attr_get(ppid,"name",&own_name);
				
				
				pk_arrived = op_pk_get (op_intrpt_strm ());
				
				op_pk_nfd_get (pk_arrived,"Alloc-ID", &destination);
				op_pk_nfd_get (pk_arrived,"GrantSize", &grant_size);
				op_pk_nfd_get (pk_arrived,"StartTime", &grant_start);
				//grant_size_double=(double)grant_size/1000000;
				//grant_start_double=(double)grant_start/10000000;   
				//printf("grant size:%.9f\n", grant_size_double);
				//printf("grant size:%d\n", grant_size);
				cas=op_sim_time();
				
				if(destination == adresa){
				
				op_pk_send (pk_arrived, 1);
				
				if(grant_size>0){
				//--
				vel_paketu = 10000000000*grant_size;//grant_size_double
				//--
				
				
				printf("Paket v %s v case %.9f, cas zacatku grantu %.9f, konec %.9f\n",own_name,cas,grant_start,(grant_start+grant_size));
				printf("Prijata velikost grantu: %.9f\n\n", grant_size);
				
				op_intrpt_schedule_self (grant_start, 10);
				
				}}
				else
				{
				op_pk_send (pk_arrived, 0);
				
				
				}
				}
				FSM_PROFILE_SECTION_OUT (state1_enter_exec)

			/** state (pk_check) exit executives **/
			FSM_STATE_EXIT_FORCED (1, "pk_check", "onu_sem_static [pk_check exit execs]")


			/** state (pk_check) transition processing **/
			FSM_TRANSIT_FORCE (0, state0_enter_exec, ;, "default", "", "pk_check", "idle", "tr_1", "onu_sem_static [pk_check -> idle : default / ]")
				/*---------------------------------------------------------*/



			/** state (send) enter executives **/
			FSM_STATE_ENTER_FORCED (2, "send", state2_enter_exec, "onu_sem_static [send enter execs]")
				FSM_PROFILE_SECTION_IN ("onu_sem_static [send enter execs]", state2_enter_exec)
				{
				Packet * pk_send;
				PrgT_Random_Gen *my_rng;
				
				char own_name[50];
				double pozadavek;
				int random;
				
				op_ima_obj_attr_get(ppid,"name",&own_name);
				
				my_rng = op_prg_random_gen_create (seed);
				random = (op_prg_random_integer_gen (my_rng) % 40) + 1;
				seed=seed*random+5;
				printf("Random:%d\n",random);
				
				
				
				pk_send = op_pk_create_fmt ("burst"); 
				op_pk_total_size_set (pk_send, vel_paketu); 
				op_pk_nfd_set (pk_send,"ONU-ID", adresa);
				
				
				pozadavek=(random*0.01)*fyzicky_ramec;
				
				op_pk_nfd_set (pk_send,"BufOcc", pozadavek);
					
				printf("Jednotkou %s reportovan pozadavek: %.9f\n",own_name,pozadavek);
				
				
				op_pk_send (pk_send, 2);
				
				
				}
				FSM_PROFILE_SECTION_OUT (state2_enter_exec)

			/** state (send) exit executives **/
			FSM_STATE_EXIT_FORCED (2, "send", "onu_sem_static [send exit execs]")


			/** state (send) transition processing **/
			FSM_TRANSIT_FORCE (0, state0_enter_exec, ;, "default", "", "send", "idle", "tr_5", "onu_sem_static [send -> idle : default / ]")
				/*---------------------------------------------------------*/



			/** state (init) enter executives **/
			FSM_STATE_ENTER_FORCED_NOLABEL (3, "init", "onu_sem_static [init enter execs]")
				FSM_PROFILE_SECTION_IN ("onu_sem_static [init enter execs]", state3_enter_exec)
				{
				ID = op_id_self();
				ppid = op_topo_parent(ID);
				op_ima_obj_attr_get(ppid,"ONU-ID",&adresa);
				op_ima_obj_attr_get(ppid,"Seed",&seed);
				op_ima_obj_attr_get(ppid,"Frame size",&fyzicky_ramec);
				
				//op_ima_obj_attr_get(ID,"Seed",&seed);
				//op_ima_obj_attr_get(ID,"Aktivita",&procento);
				
				sender_id=op_id_from_name(ppid,OPC_OBJTYPE_PROC,"queue");
				
				
				
				}
				FSM_PROFILE_SECTION_OUT (state3_enter_exec)

			/** state (init) exit executives **/
			FSM_STATE_EXIT_FORCED (3, "init", "onu_sem_static [init exit execs]")


			/** state (init) transition processing **/
			FSM_TRANSIT_FORCE (0, state0_enter_exec, ;, "default", "", "init", "idle", "tr_6", "onu_sem_static [init -> idle : default / ]")
				/*---------------------------------------------------------*/



			}


		FSM_EXIT (3,"onu_sem_static")
		}
	}




void
_op_onu_sem_static_diag (OP_SIM_CONTEXT_ARG_OPT)
	{
	/* No Diagnostic Block */
	}




void
_op_onu_sem_static_terminate (OP_SIM_CONTEXT_ARG_OPT)
	{

	FIN_MT (_op_onu_sem_static_terminate ())


	/* No Termination Block */

	Vos_Poolmem_Dealloc (op_sv_ptr);

	FOUT
	}


/* Undefine shortcuts to state variables to avoid */
/* syntax error in direct access to fields of */
/* local variable prs_ptr in _op_onu_sem_static_svar function. */
#undef adresa
#undef ID
#undef ppid
#undef sender_id
#undef source
#undef seed
#undef procento
#undef vel_paketu
#undef fyzicky_ramec

#undef FIN_PREAMBLE_DEC
#undef FIN_PREAMBLE_CODE

#define FIN_PREAMBLE_DEC
#define FIN_PREAMBLE_CODE

VosT_Obtype
_op_onu_sem_static_init (int * init_block_ptr)
	{
	VosT_Obtype obtype = OPC_NIL;
	FIN_MT (_op_onu_sem_static_init (init_block_ptr))

	obtype = Vos_Define_Object_Prstate ("proc state vars (onu_sem_static)",
		sizeof (onu_sem_static_state));
	*init_block_ptr = 6;

	FRET (obtype)
	}

VosT_Address
_op_onu_sem_static_alloc (VosT_Obtype obtype, int init_block)
	{
#if !defined (VOSD_NO_FIN)
	int _op_block_origin = 0;
#endif
	onu_sem_static_state * ptr;
	FIN_MT (_op_onu_sem_static_alloc (obtype))

	ptr = (onu_sem_static_state *)Vos_Alloc_Object (obtype);
	if (ptr != OPC_NIL)
		{
		ptr->_op_current_block = init_block;
#if defined (OPD_ALLOW_ODB)
		ptr->_op_current_state = "onu_sem_static [init enter execs]";
#endif
		}
	FRET ((VosT_Address)ptr)
	}



void
_op_onu_sem_static_svar (void * gen_ptr, const char * var_name, void ** var_p_ptr)
	{
	onu_sem_static_state		*prs_ptr;

	FIN_MT (_op_onu_sem_static_svar (gen_ptr, var_name, var_p_ptr))

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

	if (strcmp ("adresa" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->adresa);
		FOUT
		}
	if (strcmp ("ID" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->ID);
		FOUT
		}
	if (strcmp ("ppid" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->ppid);
		FOUT
		}
	if (strcmp ("sender_id" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->sender_id);
		FOUT
		}
	if (strcmp ("source" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->source);
		FOUT
		}
	if (strcmp ("seed" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->seed);
		FOUT
		}
	if (strcmp ("procento" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->procento);
		FOUT
		}
	if (strcmp ("vel_paketu" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->vel_paketu);
		FOUT
		}
	if (strcmp ("fyzicky_ramec" , var_name) == 0)
		{
		*var_p_ptr = (void *) (&prs_ptr->fyzicky_ramec);
		FOUT
		}
	*var_p_ptr = (void *)OPC_NIL;

	FOUT
	}

