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

📄 dsr_routing_layer.pr.c

📁 afit的ad hoc路由协议源码
💻 C
📖 第 1 页 / 共 5 页
字号:
op_stat_write(stat_total_gratuitous_replies,total_gratuitous_replies=0);

// init the "data packets" statistics
stat_total_data_served=op_stat_reg("total data served",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_data_served,total_data_served=0);
stat_total_data_in_buffer=op_stat_reg("total data in buffer",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_data_in_buffer,total_data_in_buffer=0);
stat_total_data_successfully_transmitted=op_stat_reg("total data successfully transmitted",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_data_successfully_transmitted,total_data_successfully_transmitted=0);
stat_total_data_efficiency=op_stat_reg("total data efficiency",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_data_efficiency,1);

// init the "error packets" statistic
stat_total_error_detected=op_stat_reg("total error detected",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_error_detected,total_error_detected=0);

// init the other global statistics
stat_total_ack=op_stat_reg("total ack",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_ack,total_ack=0);
stat_mean_hops_by_route=op_stat_reg("mean hops by route",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_mean_hops_by_route,0);
stat_amazing_errors=op_stat_reg("amazing errors",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_amazing_errors,amazing_errors=0);
stat_total_data_transmit=op_stat_reg("total data transmit",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_data_transmit,total_data_transmit=0);
stat_total_routing_packets=op_stat_reg("total routing packets",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_routing_packets,total_routing_packets=0);
stat_total_overhead=op_stat_reg("total percent overhead",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_overhead,100);
stat_total_forward_reply=op_stat_reg("total forward reply",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_forward_reply,total_forward_reply=0);
stat_total_forward_request=op_stat_reg("total forward request",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_forward_request,total_forward_request=0);
stat_total_forward_data=op_stat_reg("total forward data",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_forward_data,total_forward_data=0);
stat_total_reply_tx=op_stat_reg("total reply tx",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_reply_tx,total_reply_tx=0);
stat_total_request_tx=op_stat_reg("total request tx",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_request_tx,total_request_tx=0);
stat_total_error_tx=op_stat_reg("total error tx",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_error_tx,total_error_tx=0);
stat_total_data_bit_rx=op_stat_reg("total data bits received",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_data_bit_rx,total_data_bit_rx=0.0);
stat_total_control_bit_tx=op_stat_reg("total control bits transmitted",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_control_bit_tx,total_control_bit_tx=0.0);
stat_total_goodput_ratio=op_stat_reg("total goodput ratio",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_goodput_ratio,0);
stat_total_average_ete_delay=op_stat_reg("total average ete delay",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_average_ete_delay,0.0);
stat_tx_bit_throughput=op_stat_reg("total average tx throughput",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_tx_bit_throughput,total_bit_tx=0);

// init the other local statistics
stat_node_ack=op_stat_reg("node ack",OPC_STAT_INDEX_NONE,OPC_STAT_LOCAL);
op_stat_write(stat_node_ack,node_ack=0);
stat_node_buffer_size=op_stat_reg("node buffer size",OPC_STAT_INDEX_NONE,OPC_STAT_LOCAL);
op_stat_write(stat_node_buffer_size,node_buffer_size=0);
stat_node_data_packets=op_stat_reg("node data packets",OPC_STAT_INDEX_NONE,OPC_STAT_LOCAL);
op_stat_write(stat_node_data_packets,node_data_packets=0);
stat_node_routing_packets=op_stat_reg("node routing packets",OPC_STAT_INDEX_NONE,OPC_STAT_LOCAL);
op_stat_write(stat_node_routing_packets,node_routing_packets=0);
stat_node_forward_reply=op_stat_reg("node forward reply",OPC_STAT_INDEX_NONE,OPC_STAT_LOCAL);
op_stat_write(stat_node_forward_reply,node_forward_reply=0);
stat_node_forward_request=op_stat_reg("node forward request",OPC_STAT_INDEX_NONE,OPC_STAT_LOCAL);
op_stat_write(stat_node_forward_request,node_forward_request=0);
stat_node_forward_data=op_stat_reg("node forward data",OPC_STAT_INDEX_NONE,OPC_STAT_LOCAL);
op_stat_write(stat_node_forward_data,node_forward_data=0);
stat_node_error=op_stat_reg("node error",OPC_STAT_INDEX_NONE,OPC_STAT_LOCAL);
op_stat_write(stat_node_error,node_error=0);
stat_node_reply_tx=op_stat_reg("node reply tx",OPC_STAT_INDEX_NONE,OPC_STAT_LOCAL);
op_stat_write(stat_node_reply_tx,node_reply_tx=0);
stat_node_request_tx=op_stat_reg("node request tx",OPC_STAT_INDEX_NONE,OPC_STAT_LOCAL);
op_stat_write(stat_node_request_tx,node_request_tx=0);
stat_node_error_tx=op_stat_reg("node error tx",OPC_STAT_INDEX_NONE,OPC_STAT_LOCAL);
op_stat_write(stat_node_error_tx,node_error_tx=0);
}

/*************************************************************/
/*                   DSR_ROUTE_INIT                          */
/*************************************************************/
/* This function initialiazes the nth route of the sRoute table
/* given in  parameter
/* 		sRoute cache: the cache in which the route must be initialized
/* 		int n: the index of the route (nth) tp initialize
/*************************************************************/
void dsr_route_init(sRoute* cache, int n, int p)
{
int i;

// init every dsr_address contained in the route to -1
for(i=0;i<MAX_SIZE_ROUTE;i++)
	{
	cache[n].node_route[p].route[i]=-1;
	}

// init the route variables
cache[n].node_route[p].size_route=0;
cache[n].node_route[p].creation_time=0;
cache[n].node_route[p].route_origination=0;
}


///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
//////////////// ROUTE DISCOVERY FUNCTIONS ////////////////////
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

/*************************************************************/
/*               DSR_TRANSMIT_REQUEST                        */
/*************************************************************/
/* This function builds and sends a request packet to the
/* specified destination
/* 		
/* int destination_dsr_address: the dsr address of the desired destination
/*************************************************************/
void dsr_transmit_request(int destination_dsr_address)
{
Packet* pk_ptr;
double timer;
double random;
Distribution* reply_waiting_dist;
int *destination_dsr_address_ptr;
int temp_seq_num;

// Create the request packet and set its Type field
pk_ptr=op_pk_create_fmt("dsr_request");
op_pk_nfd_set(pk_ptr,"Option_Type",REQUEST_PACKET_TYPE);

// set the SRC field of the packet (I am the source)
op_pk_nfd_set(pk_ptr,"SRC",my_dsr_address);
// set the DEST field of the packet with desired destination dsr address
op_pk_nfd_set(pk_ptr,"DEST",BROADCAST);
// set the Node_0 field of the packet to the target address
op_pk_nfd_set(pk_ptr,"Node_0",destination_dsr_address);
// set the Opt_Data_Len to the size of the number of source route hops defined
op_pk_nfd_set(pk_ptr,"Opt_Data_Len",1);

// set creation_time field for TTL (Time To Live) purpose
op_pk_nfd_set(pk_ptr,"Creation_Time",op_sim_time());

// give packet next sequence number
op_pk_nfd_set(pk_ptr,"Identification",request_sequence_number);
temp_seq_num=request_sequence_number;
++request_sequence_number;

// check if it is the first request to the destination or if a previous request has failed 
timer=request_sent[destination_dsr_address].waiting_time;

// if it is the first request of the sequence and the non propagating request mechanism is activated
if ((timer==0)&&(NON_PROPA_REQUEST_MECHANISM))
	{
	// a non propagating request (IP_TTL=0) will be sent
	op_pk_nfd_set(pk_ptr,"IP_TTL",0);
	
	// a specific timer is assigned to the non propagating request
	timer=NONPROP_REQUEST_TIMEOUT;
		
	sprintf(message,"I am sending a NON PROPA REQUEST to %d with the seq number %d\n",destination_dsr_address,temp_seq_num);
	dsr_message(message);
	
	// "requests" statistics calculation
	++total_non_propa_requests;
	++total_requests;
	op_stat_write(stat_total_non_propa_requests,total_non_propa_requests);
	op_stat_write(stat_total_requests,total_requests);
	}

// else a propagating request must be sent
else
	{
	// if it is the first propagating request of the sequence to be sent
	if ((timer==NONPROP_REQUEST_TIMEOUT)||(timer==0))
		{
		op_pk_nfd_set(pk_ptr,"IP_TTL",MAX_SIZE_ROUTE-2);

		// the timer is set to its minimal value
		timer=REQUEST_PERIOD;
		// update request statictics
		if (timer!=0)
			{
			++total_propa_requests;
			++total_requests;
			op_stat_write(stat_total_propa_requests,total_propa_requests);
			op_stat_write(stat_total_requests,total_requests);
			}
		sprintf(message,"I am sending my first PROPA REQUEST to %d with the seq number %d, and I am going to wait %f s\n",destination_dsr_address,temp_seq_num,timer);
		dsr_message(message);
		}
	// if the propagating request has failed
	else
		{
		op_pk_nfd_set(pk_ptr,"IP_TTL",MAX_SIZE_ROUTE-5);
		// a longer timer is calculated for the new one
		if (timer!=MAX_REQUEST_PERIOD)
			{
			// find a random number between 0 and 1
			reply_waiting_dist=op_dist_load("uniform",0.0,1.0);
			random=op_dist_outcome(reply_waiting_dist);
			// Set a new time that is at a maximum double the last timeout
			timer=timer+(random*timer);
			if (timer>MAX_REQUEST_PERIOD) timer=MAX_REQUEST_PERIOD;
			}
		
		// update request statistics
		++total_renewed_propa_requests;
		op_stat_write(stat_total_renewed_propa_requests,total_renewed_propa_requests);
		++total_propa_requests;
		op_stat_write(stat_total_propa_requests,total_propa_requests);
		++total_requests;
		op_stat_write(stat_total_requests,total_requests);
		
		sprintf(message,"I am sending RENEW PROPA REQUEST to %d with the seq number %d, and I am going to wait %f s\n",destination_dsr_address,temp_seq_num,timer);
		dsr_message(message);
		}
	}

// set the sequence number of the packet and increment the sequence number for the next packet
// Store the features of the new request in the request_sent table and schedule an event associated with the timer
request_sent[destination_dsr_address].sequence_number=temp_seq_num;
request_sent[destination_dsr_address].scheduling_time = op_sim_time()+timer;
request_sent[destination_dsr_address].waiting_time = timer;

destination_dsr_address_ptr=(int*)op_prg_mem_alloc(sizeof(int));
*destination_dsr_address_ptr=destination_dsr_address;
request_sent[destination_dsr_address].evt = complex_intrpt_schedule_self(my_complex_intrpt_ptr,op_sim_time()+timer,NO_REPLY_CODE,destination_dsr_address_ptr);

// for TR (Transmission Range) purpose
op_pk_nfd_set(pk_ptr,"TR_source",my_node_objid);

// since I am the request initiator I store the fact that I have already seen the request
dsr_request_already_seen(my_dsr_address,destination_dsr_address,request_sent[destination_dsr_address].sequence_number);
// send the request to the MAC layer which will BROADCAST it
dsr_send_to_mac(pk_ptr,BROADCAST);

// breakpoint for debugging purpose
if (1)
	{
	op_prg_odb_bkpt("transmit_request");
	}
}

/*************************************************************/
/*            DSR_TRANSMIT_REQUEST_FROM_ERROR                */
/*************************************************************/
/* This function builds and sends a request packet destined to the
/* specified destination after an error detection or error 
/* packet received. 
/*
/* int destination_dsr_address: the dsr address of the wanted 
/* destination
/*************************************************************/
void dsr_transmit_request_from_error(int destination_dsr_address)
{
// init the scheduling time to 0 to send out new non-propagating request
request_sent[destination_dsr_address].scheduling_time=0;

dsr_transmit_request(destination_dsr_address);

// update request statistics
++total_requests_from_error;
op_stat_write(stat_total_requests_from_error,total_requests_from_error);

// debug breakpoint purpose
if (1)
	{
	op_prg_odb_bkpt("transmit_request_from_error");
	}
}

/*************************************************************/
/*                 DSR_HANDLE_REQUEST                        */
/*************************************************************/
/* This function handles any request packets received by the node
/*
/* 		Packet* pk_ptr: a pointer to the request packet to process
/*************************************************************/
void dsr_handle_request(Packet * pk_ptr)
{
int source_dsr_address;
int destination_dsr_address;
int sequence_number;
double creation_time;
int size_route;
int seg_left;
int route_found;

// read in the fields of the packet
op_pk_nfd_get (pk_ptr,"SRC",&source_dsr_address);
op_pk_nfd_get (pk_ptr,"Node_0",&destination_dsr_address); 
op_pk_nfd_get (pk_ptr,"Identification",&sequence_number);
op_pk_nfd_get(pk_ptr,"Creation_Time",&creation_time); 
op_pk_nfd_get (pk_ptr,"Opt_Data_Len",&size_route);
op_pk_nfd_get (pk_ptr,"IP_TTL",&seg_left);

// Add routes to this node's route cache from the packet routing information
dsr_insert_request_route_in_cache(pk_ptr);

// if this request (identified by its source, destination and sequence_number) 
// is received for the first time and no other replies from neighboring nodes have
// been seen for this request, then handle the request
if (!dsr_request_already_seen(source_dsr_address,destination_dsr_address,sequence_number) &&
	reply_seen[source_dsr_address][destination_dsr_address]<sequence_number)
	{
	sprintf(message,"I handle a request originating from %d and destined to %d, with sequence_number %d\n",source_dsr_address,destination_dsr_address,sequence_number);
	dsr_message(message);

	// if the current node is the destination of the request
	if (my_dsr_address==destination_dsr_address)
		{
		// a reply packet must be built and sent
		dsr_transmit_reply_from_target(pk_ptr);
		}
	// otherwise if the node is not the destination
	else 
		{
		route_found=0;
    	// if a route to the destination is already known and stored in the Route_Cache
		if ((route_cache[destination_dsr_address].node_route[0].size_route > 0) && 
		   ((route_cache[destination_dsr_address].node_route[0].size_route + size_route) < MAX_SIZE_ROUTE))
			{ 
			// a reply (from relay) packet must be built and sent
			route_found = dsr_transmit_reply_from_relay(pk_ptr);

⌨️ 快捷键说明

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