📄 gpr_route_layer.pr.c
字号:
/* Process model C form file: gpr_route_layer.pr.c */
/* Portions of this file copyright 1992-2003 by OPNET Technologies, Inc. */
/* This variable carries the header into the object file */
const char gpr_route_layer_pr_c [] = "MIL_3_Tfile_Hdr_ 100A 30A modeler 7 457FF7D6 457FF7D6 1 ztl lele 0 0 none none 0 0 none 0 0 0 0 0 0 0 0 8f3 1 ";
#include <string.h>
/* OPNET system definitions */
#include <opnet.h>
/* Header Block */
/* Address assignment definitions. */
#include "oms_auto_addr_support.h"
/* Topology analysis-related definitions. */
#include "oms_tan.h"
/* Process registry-related definitions. */
#include "oms_pr.h"
// stream number definition
#define FROM_UPPER_LAYER_STRM 1
#define FROM_MAC_LAYER_STRM 0
#define TO_MAC_LAYER_STRM 0
#define TO_UPPER_LAYER_STRM 1
#define EPIDEMIC 0
#define FULL_CONNECT 1
//timer
#define VAL_TIMER_NS 3
#define VAL_TIMER_PROBE 1
#define VAL_TIME_LOOP 1
#define VAL_TIME_RD 20
//set constant val
#define VAL_ROUTE_STALE_TIME VAL_TIMER_PROBE
//interrupt code
#define INT_NA_RCV 101
#define INT_ROUTE_CHANGE 102
#define INT_NA_TIMER 103
#define INT_CNSB_TIMER 104
#define INT_NS_TIMER 105
#define INT_MAC_ADDR_CFG 106
#define INT_RREQ_TIMER 107
#define MAC_BROADCAST -1
#define GPR_BROADCAST_ADDR -1
// TRANSITION MACROS DEFINITION
//define interupt condition
#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 NA_RCV ( (op_intrpt_type()==OPC_INTRPT_SELF) && (op_intrpt_code()==INT_NA_RCV) )
#define ROUTE_UPDATE ( (op_intrpt_type()==OPC_INTRPT_SELF) && (op_intrpt_code()==INT_ROUTE_CHANGE) )
//NA timer or just turn on
#define NA_TIMER ( (op_intrpt_type()==OPC_INTRPT_SELF) && (op_intrpt_code()==INT_NA_TIMER) &&(0) )
#define CNSB_TIMER ( (op_intrpt_type()==OPC_INTRPT_SELF) && (op_intrpt_code()==INT_CNSB_TIMER) && (0))
#define PROBE_TIMER (( op_intrpt_type()==OPC_INTRPT_SELF) && (op_intrpt_code()==INT_RREQ_TIMER) )
#define NS_TIMER (( op_intrpt_type()==OPC_INTRPT_SELF) && (op_intrpt_code()==INT_NS_TIMER) )
#define MAC_CFG (op_intrpt_type()==OPC_INTRPT_REMOTE && op_intrpt_code()==INT_MAC_ADDR_CFG)
#define MAX_SIZE_QUEUE 100
//size of subqueue :50
#define MAX_SIZE_NODES 50
//max number of group links cached in node
#define MAX_SIZE_ROUTE MAX_SIZE_NODES
//max group number of each group link
#define MAX_SIZE_GROUP MAX_SIZE_NODES
#define MAX_SIZE_GROUP_LINK 8
#define MAX_SIZE_TTL 30
// packet types definition
#define DATA_PACKET_TYPE 1
#define NA_PACKET_TYPE 2
#define NS_PACKET_TYPE 3
#define PPROB_PACKET_TYPE 4
#define REQUET_PACKET_TYPE 5
#define REPLY_PACKET_TYPE 6
//
#define ND_DATA 1
#define ND_REQEST 2
#define ND_REPLY 3
#define ALLOW_PASS (0)
//data structure
typedef struct
{
int data[5];
}gpr_queue_item;
typedef struct
{
int queue[MAX_SIZE_QUEUE];
int iHead;
int iTail;
}gpr_queue;
typedef struct
{
int Gp_route[MAX_SIZE_ROUTE];//sequence of groups
int size_route;//number of group node'
int iTimeStamp;
int iDistance;
int iHistory;
int iExpectedVal;//expected value of metric that the neigbhor should has at least
}LINK_NODE;
typedef struct //create when got NA
{
int szNeighborID;
int szNeighborGroupID;
int dest_addr;
int iINTflag;
int iPk_id;
int iCode;
int iWaitValue;
int iContinous;
int iTimeStamp;
}NEIGHBOR_LIST;
typedef struct
{
int pkt_id; // the index of pkt
Packet* pkt; // the packet
int iStale;
} sPkBuff;
typedef struct
{
sPkBuff queue[MAX_SIZE_QUEUE];
int iHead;
int iTail;
}gpr_pkt_queue;
//function declaration
Packet* gpr_extract_buffer(int destination_gpr_address);
void GPR_insert_buffer(Packet* pk_ptr, int destination_GPR_address);
void gpr_send_to_mac(Packet * pk_ptr, int gpr_addr);
void GPR_message(const char* message,int iType);
void gpr_send_NS_prox(int gpr_ns_dest_addr,int wanted_pk_id,int iCode);
void gpr_transmit_request_repeat(int destination_gpr_address);
void gpr_transmit_request(int destination_gpr_address);
void gpr_handle_request(Packet * pk_ptr);
void gpr_forward_request(Packet* pk_ptr);
void gpr_send_reply(Packet* pk_ptr);
void gpr_send_reply_proxy(Packet* pk_ptr);
void gpr_forward_reply(Packet* pk_ptr);
void gpr_update_route(Packet * pk_ptr,int destination_gpr_address);
int Select_Next_hop(int iDest,int iCode,int pkt_id);
//my queue
int InitQueue(gpr_queue * lpQ);
int AddItemToQ(int iItem,gpr_queue * lpQ );
int IsInQueue(int iItem,gpr_queue * lpQ );
int GetQItemAt(int iPos,int * lpVal, gpr_queue * lpQ);
int GetHeadIndex(gpr_queue * lpQ);
int Is_q_empty(gpr_queue * lpQ);
//pkt queue
int pkt_InitQueue(gpr_pkt_queue * lpQ);
int pkt_AddItemToQ(sPkBuff iItem,gpr_pkt_queue * lpQ );
int pkt_IsInQueue(sPkBuff iItem,gpr_pkt_queue * lpQ );
int pkt_GetQItemAt(int iPos,sPkBuff * lpVal, gpr_pkt_queue * lpQ);
int Pkt_GetQItemAt(int iPos,sPkBuff * lpVal, gpr_pkt_queue * lpQ);
int pkt_GetHeadIndex(gpr_pkt_queue * lpQ);
int Pkt_SetQItemAt(int iPos,sPkBuff Val, gpr_pkt_queue * lpQ);
sPkBuff gpr_remove_pkt_q(gpr_pkt_queue * lpQ);
sPkBuff gpr_access_pkt_q(gpr_pkt_queue * lpQ);
int Is_pkt_q_empty(gpr_pkt_queue * lpQ);
int GetNum_pkt_q(gpr_pkt_queue * lpQ);
//
int AddNeighbor(int gpr_addr,int group_id,int pkt_dest_gpr_address,int pk_id);
//Global var
int g_pkt_id;
char message[200]; // use as temporary variable to display some messages
char msg[50];
int iMap_Node[MAX_SIZE_NODES]; //cache the dest node's onower group, indexed by dest gpr address
//stat var
Stathandle stat_hop_per_pkt;
Stathandle stat_totla_fwd_times;
int g_local_total_fwd;
Stathandle stat_total_data_in_buffer;
int total_data_in_buffer;
Stathandle stat_total_data_in_buffer_part;
int total_data_in_buffer_part;
Stathandle stat_total_data_successfully_transmitted;
int total_data_successfully_transmitted;
int g_test;
/* 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 */
Objid my_module_id;
Objid my_node_id;
int my_mac_address;
int my_gpr_address;
int my_group_id;
LINK_NODE * GP_route_cache;
char * my_name;
int SeenPkt[5];
gpr_queue my_SeenQueue;
gpr_queue q_dest_list;
gpr_queue q_pkt_list;
int ** request_seen;
int ** reply_seen;
gpr_queue q_request_list;
gpr_pkt_queue q_pkt_req_list;
gpr_queue q_reply_list;
gpr_pkt_queue q_pkt_reply_list;
int i_local_node_fwd_data;
Stathandle stat_local_node_fwd_data;
int g_send_window;
NEIGHBOR_LIST list_neighbor[MAX_SIZE_NODES];
int iBeat;
int iSend_Window;
int flag_send;
int i_gpr_dest;
} gpr_route_layer_state;
#define pr_state_ptr ((gpr_route_layer_state*) (OP_SIM_CONTEXT_PTR->mod_state_ptr))
#define my_module_id pr_state_ptr->my_module_id
#define my_node_id pr_state_ptr->my_node_id
#define my_mac_address pr_state_ptr->my_mac_address
#define my_gpr_address pr_state_ptr->my_gpr_address
#define my_group_id pr_state_ptr->my_group_id
#define GP_route_cache pr_state_ptr->GP_route_cache
#define my_name pr_state_ptr->my_name
#define SeenPkt pr_state_ptr->SeenPkt
#define my_SeenQueue pr_state_ptr->my_SeenQueue
#define q_dest_list pr_state_ptr->q_dest_list
#define q_pkt_list pr_state_ptr->q_pkt_list
#define request_seen pr_state_ptr->request_seen
#define reply_seen pr_state_ptr->reply_seen
#define q_request_list pr_state_ptr->q_request_list
#define q_pkt_req_list pr_state_ptr->q_pkt_req_list
#define q_reply_list pr_state_ptr->q_reply_list
#define q_pkt_reply_list pr_state_ptr->q_pkt_reply_list
#define i_local_node_fwd_data pr_state_ptr->i_local_node_fwd_data
#define stat_local_node_fwd_data pr_state_ptr->stat_local_node_fwd_data
#define g_send_window pr_state_ptr->g_send_window
#define list_neighbor pr_state_ptr->list_neighbor
#define iBeat pr_state_ptr->iBeat
#define iSend_Window pr_state_ptr->iSend_Window
#define flag_send pr_state_ptr->flag_send
#define i_gpr_dest pr_state_ptr->i_gpr_dest
/* 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
#if defined (OPD_PARALLEL)
# define FIN_PREAMBLE_DEC gpr_route_layer_state *op_sv_ptr; OpT_Sim_Context * tcontext_ptr;
# define FIN_PREAMBLE_CODE \
if (VosS_Mt_Perform_Lock) \
VOS_THREAD_SPECIFIC_DATA_GET (VosI_Globals.simi_mt_context_data_key, tcontext_ptr, SimT_Context *); \
else \
tcontext_ptr = VosI_Globals.simi_sequential_context_ptr; \
op_sv_ptr = ((gpr_route_layer_state *)(tcontext_ptr->mod_state_ptr));
#else
# define FIN_PREAMBLE_DEC gpr_route_layer_state *op_sv_ptr;
# define FIN_PREAMBLE_CODE op_sv_ptr = pr_state_ptr;
#endif
/* Function Block */
#if !defined (VOSD_NO_FIN)
enum { _op_block_origin = __LINE__ };
#endif
void GPR_route_init(LINK_NODE* cache, int n)
{
int i;
// init every gpr_address contained in the route to -1
for(i=0;i<MAX_SIZE_ROUTE;i++)
{
GP_route_cache[n].Gp_route[i]=-1;
}
// init the size_route to 0
GP_route_cache[n].size_route=0;
GP_route_cache[n].iTimeStamp=-1;
GP_route_cache[n].iDistance=-1;
GP_route_cache[n].iHistory=-1;
GP_route_cache[n].iExpectedVal=0;
flag_send=0;
///////
}
void GPR_tables_init()
{
int i,j;
int iGroupNum;
iGroupNum=MAX_SIZE_GROUP;
// memory allocation of the route cache => one route for each destination group
GP_route_cache=(LINK_NODE*)op_prg_mem_alloc(iGroupNum*sizeof(LINK_NODE));
// inititialisation of this route cache
for (i=0;i<iGroupNum;i++)
{
GPR_route_init(GP_route_cache,i);
}
for (i=0;i<MAX_SIZE_NODES;i++)
iMap_Node[i]=i;
for (i=0;i<MAX_SIZE_NODES;i++)
{ list_neighbor[i].szNeighborID=-1;
list_neighbor[i].iINTflag=0;
list_neighbor[i].dest_addr=-1;
list_neighbor[i].iPk_id=-1;
list_neighbor[i].iWaitValue=-1;
list_neighbor[i].iCode=-1;
list_neighbor[i].iContinous=0;
}
SeenPkt[0]=-1;
InitQueue(& my_SeenQueue);
InitQueue(&q_dest_list);
InitQueue(&q_pkt_list);
//queue for request
InitQueue(&q_request_list);
Pkt_InitQueue(&q_pkt_req_list);
//queue for reply
InitQueue(&q_reply_list);
Pkt_InitQueue(&q_pkt_reply_list);
//GP_route_cache[40].Gp_route[0] = 0;//test for temp
//GP_route_cache[40].size_route=1;
//var ini
stat_hop_per_pkt= op_stat_reg ("Hop per Packet",OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
stat_totla_fwd_times=op_stat_reg ("Total fwd times",OPC_STAT_INDEX_NONE, OPC_STAT_GLOBAL);
op_stat_write(stat_totla_fwd_times,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_in_buffer_part=op_stat_reg("total data in buffer gap",OPC_STAT_INDEX_NONE,OPC_STAT_GLOBAL);
op_stat_write(stat_total_data_in_buffer_part,total_data_in_buffer_part=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_local_node_fwd_data=op_stat_reg("local data fwd times",OPC_STAT_INDEX_NONE,OPC_STAT_LOCAL);
op_stat_write(stat_local_node_fwd_data,0);
i_local_node_fwd_data=0;
////
g_pkt_id=0;
g_local_total_fwd=0;
g_test=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(MAX_SIZE_NODES*sizeof(int*));
// inititialisation of this table
for (i=0;i<MAX_SIZE_NODES;i++)
{
request_seen[i]=(int*)op_prg_mem_alloc(MAX_SIZE_NODES*sizeof(int));
for (j=0;j<MAX_SIZE_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(MAX_SIZE_NODES*sizeof(int*));
for (i=0;i<MAX_SIZE_NODES;i++)
{
reply_seen[i]=(int*)op_prg_mem_alloc(MAX_SIZE_NODES*sizeof(int));
for (j=0;j<MAX_SIZE_NODES;j++)
{
reply_seen[i][j]=-1; // no reply seen
}
}
iBeat =0;
printf("ini ok\n");
}
int InitQueue(gpr_queue * lpQ)
{
int i;
if (lpQ==NULL)
return -1;
for (i=0;i<MAX_SIZE_QUEUE;i++)
lpQ->queue[i]=-1;
lpQ->iHead=0;
lpQ->iTail=0;
return 0;
}
int AddItemToQ(int iItem,gpr_queue * lpQ )
{
int iPos;
iPos=-1;
if ( ( (lpQ->iTail+1)%MAX_SIZE_QUEUE ) ==lpQ->iHead )
{//full,overwrite from the head one
iPos=lpQ->iTail;
lpQ->queue[iPos]=iItem;// add to tail
lpQ->iTail=(lpQ->iTail+1)%MAX_SIZE_QUEUE;
lpQ->iHead=(lpQ->iHead+1)%MAX_SIZE_QUEUE;
}
else
{
iPos=lpQ->iTail;
lpQ->queue[iPos]=iItem;// add to tail
lpQ->iTail=(lpQ->iTail+1)%MAX_SIZE_QUEUE;
}
return iPos;
}
int Is_q_empty(gpr_queue * lpQ)
{
if (lpQ==NULL)
return (-2);
if (lpQ->iHead==lpQ->iTail)
return (1);
else
return (0);
}
int GetHeadIndex(gpr_queue * lpQ)
{
if (lpQ!=NULL)
return (lpQ->iHead);
else
return -1;
}
int GetQItemAt(int iPos,int * lpVal, gpr_queue * lpQ)
{
int iNext;
iNext=-1;
if (iPos!=lpQ->iTail)
{
*lpVal=lpQ->queue[iPos];
iNext=(++iPos)%MAX_SIZE_QUEUE;
if (iNext==lpQ->iTail)
iNext=-1;
}
return iNext;
}
int SetQItemAt(int iPos,int Val, gpr_queue * lpQ)
{
int iNext;
iNext=-1;
if (iPos!=lpQ->iTail)
{
lpQ->queue[iPos]=Val;
iNext=Val;
}
return iNext;
}
int IsInQueue(int iItem,gpr_queue * lpQ )
{
int iPt;
int iEnd;
iPt=lpQ->iHead;
iEnd=lpQ->iTail;
while (iPt!=iEnd)
{
if (iItem==lpQ->queue[iPt])
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -