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

📄 dsr_routing_layer.pr.c

📁 afit的ad hoc路由协议源码
💻 C
📖 第 1 页 / 共 5 页
字号:
/* Process model C form file: dsr_routing_layer.pr.c */
/* Portions of this file copyright 1992-2001 by OPNET Technologies, Inc. */



/* This variable carries the header into the object file */
static const char dsr_routing_layer_pr_c [] = "MIL_3_Tfile_Hdr_ 80C 30A modeler 7 3C87916C 3C87916C 1 jballah Jason@Ballah 0 0 none none 0 0 none 0 0 0 0 0 0                                                                                                                                                                                                                                                                                                                                                                                                                 ";
#include <string.h>



/* 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 */

///////////////////////////////////////////////////////////////
// DSR HEADER BLOCK
//
// declaration of globale variables, type, constants ... used in
// the dsr process model
///////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////
//////////////////////// INCLUDE ////////////////////////////// 
///////////////////////////////////////////////////////////////

#include <math.h>
#include <stdlib.h>
#include <opnet.h>
#include <string.h>
#include "dsr_support.h"
#include "fifo.h"
#include "complex_intrpt.h"

///////////////////////////////////////////////////////////////
///////////////// CONSTANTS DEFINITION ////////////////////////
///////////////////////////////////////////////////////////////

// stream number definition
#define FROM_UPPER_LAYER_STRM 0
#define FROM_MAC_LAYER_STRM 1
#define TO_MAC_LAYER_STRM 0
#define TO_UPPER_LAYER_STRM 1

// packet types definition
#define DATA_PACKET_TYPE 7
#define REQUEST_PACKET_TYPE 2
#define REPLY_PACKET_TYPE 3
#define ERROR_PACKET_TYPE 4

// interrruption codes definition
#define ACK_CODE 1
#define ERROR_CODE 2
#define SELF_ERROR_CODE 3
#define NO_REPLY_CODE 4
#define SEND_REPLY_CODE 5
#define SEND_DATA_CODE 6

// constant used to store source and destination in a single integer
#define OPTION 10000    

// other constants definition
#define BROADCAST -1

// constant used to remove the old received data packet ids from memory
#define DATA_LIFE_TIME 45

// maximum number of hops in a route
#define  MAX_SIZE_ROUTE 15

// maximum number of routes maintained per destination
#define  MAX_CACHED_ROUTES 75

// maximum number of transmission retries before error packet is generated
#define  MAXRXTSHIFT 2

// 2 times the propagation delay (in seconds) of transmission 
#define  HOP_DELAY .0012

// Amount of time (in seconds) per hop a source node will wait before sending
// the next packet to the same destination to allow for an error packet
// to make it back to the source node
#define  ERROR_DELAY .03

// maximum number salvage attempts before packet is dropped
#define  MAX_SALVAGE_COUNT 15

///////////////////////////////////////////////////////////////
///////////// TRANSITION MACROS DEFINITION ////////////////////
///////////////////////////////////////////////////////////////

#define UPPER_LAYER_ARRIVAL (op_intrpt_type()==OPC_INTRPT_STRM && op_intrpt_strm()==FROM_UPPER_LAYER_STRM)
#define MAC_LAYER_ARRIVAL (op_intrpt_type()==OPC_INTRPT_STRM && op_intrpt_strm()==FROM_MAC_LAYER_STRM)
#define NO_REPLY (op_intrpt_type()==OPC_INTRPT_SELF && complex_intrpt_code(my_complex_intrpt_ptr)==NO_REPLY_CODE)
#define SEND_REPLY (op_intrpt_type()==OPC_INTRPT_SELF && complex_intrpt_code(my_complex_intrpt_ptr)==SEND_REPLY_CODE)
#define SEND_DATA (op_intrpt_type()==OPC_INTRPT_SELF && complex_intrpt_code(my_complex_intrpt_ptr)==SEND_DATA_CODE)
#define ERROR ((op_intrpt_type()==OPC_INTRPT_REMOTE && op_intrpt_code()==ERROR_CODE) || (op_intrpt_type()==OPC_INTRPT_SELF && complex_intrpt_code(my_complex_intrpt_ptr)==SELF_ERROR_CODE))
#define ACK (op_intrpt_type()==OPC_INTRPT_REMOTE && op_intrpt_code()==ACK_CODE)
#define END_SIM	(op_intrpt_type()==OPC_INTRPT_ENDSIM)

///////////////////////////////////////////////////////////////
///////////////////// TYPES DEFINITION ////////////////////////
///////////////////////////////////////////////////////////////

// definition of the sRoute structure
typedef struct
	{
	int route[MAX_SIZE_ROUTE];	// the route = array of integers
	int size_route;				// the size of the route
	double creation_time;		// time route was added
	int route_origination;		// maintains if this node is originator or salvager
	} sNodeRoute;

// definition of the sNodeRoute structure
// Maintains multiple routes to a single destination
typedef struct
	{
	sNodeRoute node_route[MAX_CACHED_ROUTES];
	} sRoute;

// definition of the sRequestSent structure
typedef struct
	{
	int sequence_number;	// the sequence number of the request
	double scheduling_time; // the time when a new request should be sent
	double waiting_time;	// the total time we have to wait before scheduling_time
	Evhandle evt;			// the event associated with the scheduling_time timer
	} sRequestSent;

// definition of the sReply structure used to store a scheduled reply 
typedef struct
	{
	int sequence_number; 	// the sequence number of the reply
	Packet* pk;				// the reply_packet
	Evhandle evt;			// the intrpt event which will "say" when to send the reply
	} sReply;

// definition of the sData structure used to store a scheduled data packet send 
typedef struct
	{
	int packet_id;		 	// the sequence number of the reply
	Packet* pk;				// the reply_packet
	Evhandle evt;			// the intrpt event which will "say" when to send the reply
	} sData;

// definition of the sNoAck structure used to store information about the last data packet sent 
// and to schedule an event that will say "no ack has been received for this packet => error "
typedef struct
	{
	Evhandle evt;			// event indicating that no ack has been received => error (either the MAC layer does not reply, or no explicit dsr ack has been received)
	double schedule;		// time at which this event is scheduled
	sNodeRoute route;		// route used by the data packet
	Packet* packet_copy; 	// copy of data packet to be transmitted
	int packet_id;			// packet_id of the data packet
	int error_tx_retry;		// Maintains the number of retries before error packet generated
	int original_source;	// Maintains if this node is the original source of the packet being transmitted
	} sNoAck;


///////////////////////////////////////////////////////////////
////////////// GLOBAL VARIABLES DECLARATION ///////////////////
///////////////////////////////////////////////////////////////

double TRANSMISSION_RANGE;			// the transmission range
double REQUEST_PERIOD;				// minimum lifetime to for a propagational request
double MAX_REQUEST_PERIOD;			// maximum lifetime to for a propagational request
double WAIT_NON_PROPA_REPLY_TIME;	// time to wait for a reply after sending a non propa request
double NONPROP_REQUEST_TIMEOUT; 	// lifetime of non propagating request
double WAIT_ACK_TIME;				// maximum waiting to wait for an ack or an error from the mac layer after sending a data packet1
int NUMBER_OF_NODES; 				// the number of nodes using dsr in the network
int NON_PROPA_REQUEST_MECHANISM;	// flag for the non propagating request mechanism
int MAC_LAYER_ACK=1;				// flag for the mac layer ack (activated since in this first model version we used 802.11 as mac layer)
int packet_id=0;					// the id that will be used by the next data packet to be transmitted
char message[200];					// use as temporary variable to display some messages



// requests statistics
Stathandle stat_total_non_propa_requests;
int total_non_propa_requests;
Stathandle stat_total_propa_requests;
int total_propa_requests;
Stathandle stat_total_renewed_propa_requests;
int total_renewed_propa_requests;
Stathandle stat_total_requests;
int total_requests;
Stathandle stat_total_requests_from_error;
int total_requests_from_error;

// replies statistics
Stathandle stat_total_replies;
int total_replies;
Stathandle stat_total_replies_from_target;
int total_replies_from_target;
Stathandle stat_total_replies_from_relay;
int total_replies_from_relay;
Stathandle stat_total_canceled_replies;
int total_canceled_replies;
Stathandle stat_total_gratuitous_replies;
int total_gratuitous_replies;

// data statistics
Stathandle stat_total_data_served;
int total_data_served;
Stathandle stat_total_data_in_buffer;
int total_data_in_buffer;
Stathandle stat_total_data_successfully_transmitted;
int total_data_successfully_transmitted;
Stathandle stat_total_data_efficiency;

// errors statistics
int total_error_detected;
Stathandle stat_total_error_detected;
Stathandle stat_total_error;
int	total_error;
int amazing_errors;
Stathandle stat_amazing_errors;

// packet forwarding statistics
Stathandle	stat_total_forward_data;
Stathandle	stat_total_forward_reply;
Stathandle	stat_total_forward_request;
int	total_forward_data;
int	total_forward_reply;
int	total_forward_request;

// total transmission statistics
Stathandle	stat_total_data_transmit;
int total_data_transmit;
Stathandle	stat_total_reply_tx;
Stathandle	stat_total_request_tx;
Stathandle	stat_total_error_tx;
int	total_reply_tx;
int	total_request_tx;
int	total_error_tx;
Stathandle	stat_tx_bit_throughput;
double  total_bit_tx;
Stathandle	stat_total_control_bit_tx;
double	total_control_bit_tx;

// total receive statistics
Stathandle	stat_total_data_bit_rx;
double	total_data_bit_rx;

// other statistics
int total_ack;
Stathandle stat_total_ack;
Stathandle stat_mean_hops_by_route;
Stathandle stat_total_overhead;
Stathandle	stat_total_routing_packets;
int	total_routing_packets;
Stathandle	stat_total_goodput_ratio;
Stathandle stat_total_average_ete_delay;
double  total_ete_delay=0;
double  total_average_ete_delay;

///////////////////////////////////////////////////////////////
///////////////// DSR FUNCTIONS' HEADER ///////////////////////
///////////////////////////////////////////////////////////////

// init functions
void dsr_pre_init();
void dsr_user_parameter_init();
void dsr_tables_init();
void dsr_stats_init();
void dsr_route_init(sRoute* cache, int n, int p);

// route discovery functions
void dsr_transmit_request(int dest_dsr_address);
void dsr_transmit_request_from_error(int destination_dsr_address);
void dsr_handle_request(Packet * pk_ptr);
int dsr_request_already_seen(int source_dsr_address,int destination_dsr_address,int sequence_number);
void dsr_forward_request(Packet* pk_ptr);
void dsr_transmit_reply_from_target(Packet* pk_ptr);
int dsr_transmit_reply_from_relay(Packet *pk_ptr);
void  dsr_handle_reply(Packet* pk_ptr);
int dsr_reply_already_seen(int source_dsr_address,int destination_dsr_address,int sequence_number);
void dsr_forward_reply(Packet* pk_ptr);
void dsr_insert_reply_route_in_cache(Packet* pk_ptr);
void dsr_insert_request_route_in_cache(Packet* pk_ptr);
void dsr_insert_source_route_in_cache(Packet* pk_ptr);
void dsr_swap_route(sRoute* cache, int n);
void dsr_promiscuous_reply(Packet* pk_ptr);

// data transmission functions
void dsr_upper_layer_data_arrival (Packet* data_pk_ptr, int dest_dsr_address);
void dsr_transmit_data(Packet* pk_ptr, int dest_dsr_address, int orig_source);
void dsr_handle_data(Packet* pk_ptr);
void dsr_schedule_no_ack_event(Packet* pk_ptr, int orig_source);
int dsr_data_already_seen(int pk_id);
void dsr_forward_data(Packet* pk_ptr);

// route maintenance functions
void dsr_transmit_error(sNodeRoute route, int error_node_dsr_address);
void  dsr_handle_error(Packet* pk_ptr);
int dsr_check_gratuitous_reply(Packet* pk_ptr);
void dsr_transmit_gratuitous_reply(int current_node_index, Packet* pk_ptr);
void dsr_clean_cache(int first_node_dsr_address,int second_node_dsr_address);
int dsr_error_route(Packet* pk_ptr);

// other functions
int dsr_in_transmission_range(Packet* pk_ptr);
void dsr_insert_buffer(Packet* pk_ptr, int dest_dsr_address);
Boolean dsr_buffer_empty(int dest_dsr_address);
Packet* dsr_extract_buffer(int dest_dsr_address);
void dsr_no_loop_in_route(sNodeRoute* route);
void dsr_send_to_mac(Packet* pk, int next_destination);
void dsr_message(const char* message);
void dsr_end_simulation();

/* End of Header Block */

⌨️ 快捷键说明

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