⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dsr_routing_layer.pr.c

📁 afit的ad hoc路由协议源码
💻 C
📖 第 1 页 / 共 5 页
字号:

#if !defined (VOSD_NO_FIN)
#undef	BIN
#undef	BOUT
#define	BIN		FIN_LOCAL_FIELD(last_line_passed) = __LINE__ - _block_origin;
#define	BOUT	BIN
#define	BINIT	FIN_LOCAL_FIELD(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 */
	Objid	                  		my_objid;
	Objid	                  		my_node_objid;
	Objid	                  		my_network_objid;
	int	                    		my_mac_address;
	int	                    		my_dsr_address;
	char *	                 		my_name;
	sRoute *	               		route_cache;
	sRequestSent *	         		request_sent;
	int	                    		request_sequence_number;
	int**	                  		request_seen;
	sComplexIntrpt *	       		my_complex_intrpt_ptr;
	sFifo	                  		reply_fifo;
	int**	                  		reply_seen;
	sFifo	                  		received_packet_id_fifo;
	sFifo	                  		no_ack_fifo;
	int	                    		DEBUG;
	Stathandle	             		stat_node_ack;
	Stathandle	             		stat_node_buffer_size;
	Stathandle	             		stat_node_data_packets;
	Stathandle	             		stat_node_routing_packets;
	int	                    		node_ack;
	int	                    		node_buffer_size;
	int	                    		node_data_packets;
	int	                    		node_routing_packets;
	Stathandle	             		stat_node_forward_data;
	Stathandle	             		stat_node_forward_reply;
	Stathandle	             		stat_node_forward_request;
	int	                    		node_forward_data;
	int	                    		node_forward_reply;
	int	                    		node_forward_request;
	Stathandle	             		stat_node_error;
	int	                    		node_error;
	Stathandle	             		stat_node_reply_tx;
	Stathandle	             		stat_node_request_tx;
	Stathandle	             		stat_node_error_tx;
	int	                    		node_reply_tx;
	int	                    		node_request_tx;
	int	                    		node_error_tx;
	int**	                  		error_seen;
	sFifo	                  		data_fifo;
	int*	                   		data_waiting;
	Boolean	                		cache_trace_active;
	} dsr_routing_layer_state;

#define pr_state_ptr            		((dsr_routing_layer_state*) SimI_Mod_State_Ptr)
#define my_objid                		pr_state_ptr->my_objid
#define my_node_objid           		pr_state_ptr->my_node_objid
#define my_network_objid        		pr_state_ptr->my_network_objid
#define my_mac_address          		pr_state_ptr->my_mac_address
#define my_dsr_address          		pr_state_ptr->my_dsr_address
#define my_name                 		pr_state_ptr->my_name
#define route_cache             		pr_state_ptr->route_cache
#define request_sent            		pr_state_ptr->request_sent
#define request_sequence_number 		pr_state_ptr->request_sequence_number
#define request_seen            		pr_state_ptr->request_seen
#define my_complex_intrpt_ptr   		pr_state_ptr->my_complex_intrpt_ptr
#define reply_fifo              		pr_state_ptr->reply_fifo
#define reply_seen              		pr_state_ptr->reply_seen
#define received_packet_id_fifo 		pr_state_ptr->received_packet_id_fifo
#define no_ack_fifo             		pr_state_ptr->no_ack_fifo
#define DEBUG                   		pr_state_ptr->DEBUG
#define stat_node_ack           		pr_state_ptr->stat_node_ack
#define stat_node_buffer_size   		pr_state_ptr->stat_node_buffer_size
#define stat_node_data_packets  		pr_state_ptr->stat_node_data_packets
#define stat_node_routing_packets		pr_state_ptr->stat_node_routing_packets
#define node_ack                		pr_state_ptr->node_ack
#define node_buffer_size        		pr_state_ptr->node_buffer_size
#define node_data_packets       		pr_state_ptr->node_data_packets
#define node_routing_packets    		pr_state_ptr->node_routing_packets
#define stat_node_forward_data  		pr_state_ptr->stat_node_forward_data
#define stat_node_forward_reply 		pr_state_ptr->stat_node_forward_reply
#define stat_node_forward_request		pr_state_ptr->stat_node_forward_request
#define node_forward_data       		pr_state_ptr->node_forward_data
#define node_forward_reply      		pr_state_ptr->node_forward_reply
#define node_forward_request    		pr_state_ptr->node_forward_request
#define stat_node_error         		pr_state_ptr->stat_node_error
#define node_error              		pr_state_ptr->node_error
#define stat_node_reply_tx      		pr_state_ptr->stat_node_reply_tx
#define stat_node_request_tx    		pr_state_ptr->stat_node_request_tx
#define stat_node_error_tx      		pr_state_ptr->stat_node_error_tx
#define node_reply_tx           		pr_state_ptr->node_reply_tx
#define node_request_tx         		pr_state_ptr->node_request_tx
#define node_error_tx           		pr_state_ptr->node_error_tx
#define error_seen              		pr_state_ptr->error_seen
#define data_fifo               		pr_state_ptr->data_fifo
#define data_waiting            		pr_state_ptr->data_waiting
#define cache_trace_active      		pr_state_ptr->cache_trace_active

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


/* Function Block */

enum { _block_origin = __LINE__ };
///////////////////////////////////////////////////////////////
// DSR FUNCTION BLOCK
//
// Declaration of all the functions used in the dsr process model
///////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
///////////////////// INIT FUNCTIONS //////////////////////////
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

/*************************************************************/
/*                     DSR_PRE_INIT                          */
/*************************************************************/
/* This function pre-initializes the dsr state machine
/* That is initializes all Objid and the mac and dsr addresses
/* used by the node
/*************************************************************/
void dsr_pre_init()
{
char address_field[100];

// init the objid of the current dsr process
my_objid=op_id_self();
// init the objid of the node containing this process
my_node_objid=op_topo_parent(my_objid);
// init the objid ot the network containing this node
my_network_objid=op_topo_parent(my_node_objid);
// init the name of the node containing this process
my_name=(char*)op_prg_mem_alloc(25*sizeof(char));
op_ima_obj_attr_get(my_node_objid,"name",my_name);

// init the mac address used by the current node
op_ima_obj_attr_get(my_objid,"Mac Address Field",address_field);
// if address_field is not equal  to "name" then it must contain the name of field where the MAC address is defined
if  (strcmp(address_field,"name")!=0)
	{
	op_ima_obj_attr_get(my_node_objid,address_field,&my_mac_address);
	}
// otherwise the name of the node will be converted in integer and used as the node mac address
else
	{
	op_ima_obj_attr_get(my_node_objid,"name",address_field);
	my_mac_address=atoi(address_field);
	}
// init the dsr address used by the current node
op_ima_obj_attr_get(my_objid,"Dsr Address",&my_dsr_address);
// if the "use the mac address" option is choosen then the mac and dsr address are equal
if (my_dsr_address==DSR_SUPPORT_USE_THE_MAC_ADDRESS)
	{
	// declare both dsr and mac address to the dsr support
	my_dsr_address=dsr_support_declare_node_addresses(my_node_objid,my_mac_address,my_mac_address);
	}
// if the "automatic assignation" option is choosen then the dsr_support external library provides an address
else if (my_dsr_address==DSR_SUPPORT_AUTOMATIC_ASSIGNATION)
	{
	my_dsr_address=dsr_support_declare_node_addresses(my_node_objid,DSR_SUPPORT_AUTOMATIC_ASSIGNATION,my_mac_address);
	}
}

/*************************************************************/
/*             DSR_USER_PARAMETERS_INIT                      */
/*************************************************************/
/* This function initialiazes every parameters defined by the
/* user
/*************************************************************/
void dsr_user_parameter_init()
{
// read and calculate the time constants defined by the users for the request purpose
op_ima_obj_attr_get(my_objid,"Dsr Wait Request Min Time",&REQUEST_PERIOD);
op_ima_obj_attr_get(my_objid,"Dsr Wait Request Max Time",&MAX_REQUEST_PERIOD);
op_ima_obj_attr_get(my_objid,"Dsr Wait NonProp Request Timeout",&NONPROP_REQUEST_TIMEOUT);
op_ima_obj_attr_get(my_objid,"Dsr Wait Ack Time",&WAIT_ACK_TIME);

// read the flags to see which mechanism are activated
op_ima_obj_attr_get(my_objid,"Dsr Non Propagating Request",&NON_PROPA_REQUEST_MECHANISM);

// Get the value of the DEBUG attribute...the higher the value, the more info is printed
op_ima_obj_attr_get(my_node_objid, "DEBUG", &DEBUG);

// This attribute reads in the transmission range
// ***NOTE*** This attribute is not currently being used in the model
op_ima_sim_attr_get(OPC_IMA_DOUBLE,"Dsr Transmission Range",&TRANSMISSION_RANGE);
}

/*************************************************************/
/*                 DSR_TABLES_INIT                           */
/*************************************************************/
/* This function initialiazes every tables using by the routing 
/* protocol
/*************************************************************/
void dsr_tables_init()
{
int i,j;

// count the number of moving nodes in the network (all must use dsr)
NUMBER_OF_NODES=dsr_support_number_of_nodes();

route_cache=(sRoute*)op_prg_mem_alloc(NUMBER_OF_NODES*sizeof(sRoute));
// inititialization of this route cache
for (i=0;i<NUMBER_OF_NODES;i++)
	{
	for (j=0;j<MAX_CACHED_ROUTES;j++)
		{
		dsr_route_init(route_cache,i,j);
		}
	}

// memory allocation of the request sent table => one request can be sent to each destination 
request_sent=(sRequestSent*)op_prg_mem_alloc(NUMBER_OF_NODES*sizeof(sRequestSent));
// inititialization of this table
for (i=0;i<NUMBER_OF_NODES;i++)
	{
	request_sent[i].sequence_number=0;
	request_sent[i].scheduling_time=0;
	request_sent[i].waiting_time=0;
	}
// init the request sequence number 
request_sequence_number=0;

// memory allocation of the reply seen table => store the sequence number of the last seen request identified by its source and destination
request_seen=(int**)op_prg_mem_alloc(NUMBER_OF_NODES*sizeof(int*));
// inititialization of this table
for (i=0;i<NUMBER_OF_NODES;i++)
	{
	request_seen[i]=(int*)op_prg_mem_alloc(NUMBER_OF_NODES*sizeof(int));; 
	for (j=0;j<NUMBER_OF_NODES;j++)
		{
		request_seen[i][j]=-1;	// no request seen
		}
	}
// memory allocation of the reply received table => store the sequence number of the last received reply identified by its source
reply_seen=(int**)op_prg_mem_alloc(NUMBER_OF_NODES*sizeof(int*));
for (i=0;i<NUMBER_OF_NODES;i++)
	{
	reply_seen[i]=(int*)op_prg_mem_alloc(NUMBER_OF_NODES*sizeof(int));; 
	for (j=0;j<NUMBER_OF_NODES;j++)
		{
		reply_seen[i][j]=-1;	// no reply seen
		}
	}

// memory allocation of the error seen table => reporting node and the error node
error_seen=(int**)op_prg_mem_alloc(NUMBER_OF_NODES*sizeof(int*));
for (i=0;i<NUMBER_OF_NODES;i++)
	{
	error_seen[i]=(int*)op_prg_mem_alloc(NUMBER_OF_NODES*sizeof(int));; 
	for (j=0;j<NUMBER_OF_NODES;j++)
		{
		error_seen[i][j]=0;	// no error seen
		}
	}

// memory allocation for the structure managing the complex intrpt
my_complex_intrpt_ptr=complex_intrpt_new();

// init the reply_fifo
fifo_init(&reply_fifo);
// init the received_packet_id_fifo
fifo_init(&received_packet_id_fifo);
// init the no_ack_fifo
fifo_init(&no_ack_fifo);
// init the send_data fifo
fifo_init(&data_fifo);

// memory allocation of the reply received table => store the sequence number of the last received reply identified by its source
data_waiting=(int*)op_prg_mem_alloc(NUMBER_OF_NODES*sizeof(int));
for (i=0;i<NUMBER_OF_NODES;i++)
	{
	data_waiting[i]=0; 
	}

}

/*************************************************************/
/*                 DSR_STATS_INIT                            */
/*************************************************************/
/* This function initialiazes and declare every statistics that
/* will be calculated and collected by the dsr process model
/*************************************************************/
void dsr_stats_init()
{
// init the "requests" statistics
stat_total_non_propa_requests=op_stat_reg("total non propa requests",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_non_propa_requests,total_non_propa_requests=0);
stat_total_propa_requests=op_stat_reg("total propa requests",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_propa_requests,total_propa_requests=0);
stat_total_renewed_propa_requests=op_stat_reg("total renewed propa requests",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_renewed_propa_requests,total_renewed_propa_requests=0);
stat_total_requests_from_error=op_stat_reg("total requests from error",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_requests_from_error,total_requests_from_error=0);
stat_total_requests=op_stat_reg("total requests",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_requests,total_requests=0);

// init the "replies" statistics
stat_total_replies=op_stat_reg("total replies",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_replies,total_replies);
stat_total_replies_from_target=op_stat_reg("total replies from target",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_replies_from_target,total_replies_from_target=0);
stat_total_replies_from_relay=op_stat_reg("total replies from relay",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_replies_from_relay,total_replies_from_relay=0);
stat_total_canceled_replies=op_stat_reg("total canceled replies",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_canceled_replies,total_canceled_replies=0);
stat_total_gratuitous_replies=op_stat_reg("total gratuitous replies",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -