📄 dsr_support.ex.c
字号:
///////////////////////////////////////////////////////////////////////////
////////////// OPNET'S DSR MODEL SUPPORT PACKAGE SOURCE FILE /////////////////////////////////////////////////////////////////////////////////////////
///
/// Contains: A support for the address assignations in the nist dsr model///
/// Company: National Institute of Standards and Technology
/// Written by: Xavier Pallot
/// Date: 10/10/00
///
///////////////////////////////////////////////////////////////////////////
/// Description: This file provides a support to the nist dsr models./// It is used in order to check that the dsr addresses /// assigned manually to the nodes are valids, or to assign /// them automatically///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////// INCLUDE /////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include "dsr_support.h"///////////////////////////////////////////////////////////////////////////
/////////////////////// GLOBAL VARIABLES DECLARATION /////////////////////////////////////////////////////////////////////////////////////////////////
int number_of_dsr_nodes=0; // the number of dsr nodessFifo* ptr_fifo_test_addresses=OPC_NIL; // fifo structure used for checking and validation purposesFifo* ptr_fifo_addresses=OPC_NIL; // fifo structure used to find the mac_address or the dsr_address of a node from its objidObjid* nodes_objid_table; // table used to find the objid of a node from its dsr_addressint validated=0; // boolean indicating if every address has been validated or not ///////////////////////////////////////////////////////////////////////////
////////////////////////// FUNCTIONS DEFINITION ///////////////////////////
///////////////////////////////////////////////////////////////////////////
/************************** DSR_SUPPORT_START ****************************/
/* This function initializes the variables used by the dsr_support package/*************************************************************************/
void dsr_support_start(){// if the initialization was not already done by another dsr nodesif ((ptr_fifo_test_addresses==OPC_NIL) && (ptr_fifo_addresses==OPC_NIL)) { // initialize the package global variables number_of_dsr_nodes=0; validated=0; ptr_fifo_test_addresses=fifo_new(); ptr_fifo_addresses=fifo_new(); // if the memory allocation failled during this initialisation the simulation is stopped if ((ptr_fifo_test_addresses==OPC_NIL)||(ptr_fifo_addresses==OPC_NIL)) { op_sim_end("Dsr support initialization failure","","",""); } }}/******************* DSR_SUPPORT_DECLARE_NODE_ADDRESSES ******************/
/* This function is used to declare the dsr address of a new node using the /* dsr model in the network. In fact it ensures that this address is correct/* in order to run the dsr model, or/and assigns it automatically./* Objid node_objid: the Objid of the new node using the dsr model/* int dsr_address: the dsr address assigned to this new node. If it /* is equal to DSR_SUPPORT_AUTOMATIC_ASSIGNATION the dsr address will be /* assigned automatically/* int mac_address: the mac address already assigned to this node/* Return: the dsr address assigned if success/* otherwise the simulation is stopped and -1 returned/*************************************************************************/
int dsr_support_declare_node_addresses(Objid node_objid, int dsr_address, int mac_address){Objid* objidPtr=OPC_NIL;sAddresses* addresses;// test if the initialization of the dsr_support was doneif ((ptr_fifo_test_addresses==OPC_NIL)||(ptr_fifo_addresses==OPC_NIL)) { op_sim_end("The dsr_support_start function was not call before using the package","","",""); return(-1); }// if every addresses has been already validated then the addresses declaration phase is finishedif (validated) { // and the simulation is stopped since no new node can be registered op_sim_end("No new dsr address can be added after having validated the whole dsr address","","",""); return(-1); }// address validy control if (dsr_address==DSR_SUPPORT_AUTOMATIC_ASSIGNATION) { // check if the futur node automatic address is already used objidPtr=fifo_multiplex_read(ptr_fifo_test_addresses,number_of_dsr_nodes); }else { // check if the node dsr_address if already used objidPtr=fifo_multiplex_read(ptr_fifo_test_addresses,dsr_address); }// if the dsr_address is "free" if (objidPtr==OPC_NIL) { objidPtr=(int*)op_prg_mem_alloc(sizeof(Objid)); addresses=(sAddresses*)op_prg_mem_alloc(sizeof(sAddresses)); if (dsr_address==DSR_SUPPORT_AUTOMATIC_ASSIGNATION) { // assign an automatic dsr address to the node dsr_address=number_of_dsr_nodes; *objidPtr=node_objid; } else { // assign the given address to the node *objidPtr=node_objid; } // valid the new dsr address associated with the mac address and node_objid by storing their values in memory addresses->dsr_address=dsr_address; addresses->mac_address=mac_address; fifo_multiplex_insert(ptr_fifo_addresses,addresses,node_objid)
; // store also the dsr address in another fifo for checking and validation purpose fifo_multiplex_insert(ptr_fifo_test_addresses,objidPtr,dsr_address)
; }// if the dsr_address is busy => errorelse { if (dsr_address==DSR_SUPPORT_AUTOMATIC_ASSIGNATION) { op_sim_end("Multiple uses of the same dsr address due to the use of both manual and automatic address assignations","","",""); } else { printf("try to use the invalid address %d", dsr_address); op_sim_end("Multiple uses of the same dsr address","","",""); } return(-1); }// one more dsr node in the networknumber_of_dsr_nodes++;return(dsr_address);} /****************** DSR_SUPPORT_VALIDATE_ADDRESSES ************************/
/* This function check the validity of the whole dsr addresses /* To be valid every addresses must be in the range [0, number of dsr nodes-1]/* This condition is essential for the memory management of the dsr model/* return: 0 if sucess/* otherwise the simulation is stopped and -1 returned/*************************************************************************/
int dsr_support_validate_addresses(){Objid* objidPtr;int dsr_address=0;// if the dsr addresses were not validated beforeif (!validated) { // create the table of the nodes objid nodes_objid_table=(Objid*)op_prg_mem_alloc(number_of_dsr_nodes*sizeof(Objid)); // check that every addresses are valids and fill in the objid table (indexed by the dsr_address) objidPtr=fifo_multiplex_extract(ptr_fifo_test_addresses,dsr_address)
; while (objidPtr!=OPC_NIL) { nodes_objid_table[dsr_address]=*objidPtr; op_prg_mem_free(objidPtr); dsr_address++; objidPtr=fifo_multiplex_extract(ptr_fifo_test_addresses,dsr_address)
; } // and destroy the fifo used only for dsr addresses checking and validation fifo_destroy(ptr_fifo_test_addresses); ptr_fifo_test_addresses=OPC_NIL; // if at least one address is invalid the simulation is stopped and an error message displayed if (dsr_address!=number_of_dsr_nodes) { op_sim_end("Dsr addresses do not follow the rule: every dsr addresses must be in the range [0,number of nodes-1]","","",""); return(-1); } // otherwise the centralized structures containing every dsr addresses, mac addresses and node objid are validated else { printf("Dsr Addresses validated: the centralized structure containing every dsr and mac addresses can be used\n"); printf("There are %d node(s) using dsr in the network\n",number_of_dsr_nodes); validated=1; return(0); } }return(0);}/********************* DSR_SUPPORT_NUMBER_OF_NODES ***********************/
/* This function returns the number of nodes using the dsr model in the network/* Return: int: number of nodes using dsr in the network/*************************************************************************/
int dsr_support_number_of_nodes(){// if every address has been validatedif (validated) { // return the number of node using the dsr process model return number_of_dsr_nodes; }// otherwise the addresses declaration phase is not finised else { // and the simulation is stopped since other nodes can still declare their addresses op_sim_end("All the dsr address must be declare and validated before calling this function","","",""); }} /********************* DSR_SUPPORT_GET_NODE_OBJID ************************/
/* This function returns the objid of the node identified by its dsr address /* int dsr_address: the dsr address of the node for which the objid is /* required/* Return: the Objid of the node using the given dsr address/* -1 if error/*************************************************************************/
Objid dsr_support_get_node_objid(int dsr_address){// if every address was validatedif (validated) { // if the dsr address is known if (dsr_address<number_of_dsr_nodes) { // return the objid of the node using this dsr address return(nodes_objid_table[dsr_address]); } // otherwise the node dsr addresss is unknown by the dsr support package else { // thus the simulation is stopped op_sim_end("This dsr address is unknown by the dsr support","","",""); return(-1); } }// otherwise if the addresses was not validated beforeelse { // the simulation is stopped since other nodes can still declare their addresses op_sim_end("All the dsr address must be declare and validated before calling this function","","",""); return(-1); }}/********************* DSR_SUPPORT_GET_MAC_ADDRESS ***********************/
/* This function returns the mac address of the node identified by its /* dsr address./* int dsr_address: the dsr address of the node for which the objid is /* required/* Return: the mac_address of the node using the given dsr address/* -1 if error/*************************************************************************/
int dsr_support_get_mac_from_dsr_address(int dsr_address){sAddresses* addresses;// if every address was validatedif (validated) { // if the dsr address is known if (dsr_address<number_of_dsr_nodes) { // read and return the mac address of the node using this dsr address addresses=fifo_multiplex_read(ptr_fifo_addresses,nodes_objid_table[dsr_address])
; if (addresses!=OPC_NIL) { return(addresses->mac_address); } else { op_sim_end("dsr support amazing error: a declared node a diseapered from the memory !!","","",""); return(-1); } } // otherwise the node dsr addresss is unknown by the dsr support package else { // thus the simulation is stopped op_sim_end("This dsr address is unknown by the dsr support","","",""); return(-1); } }// otherwise if the addresses was not validated beforeelse { // the simulation is stopped since other nodes can still declare their addresses op_sim_end("All the dsr address must be declare and validated before calling this function","","",""); return(-1); }}/********************* DSR_SUPPORT_GET_MAC_ADDRESS ***********************/
/* This function returns the mac address of the node identified by its objid/* Objid node_Objid: the objid of the node for which the mac address is /* required/* Return: the mac_address of the node identified by the given objid/* -1 if error/*************************************************************************/
int dsr_support_get_mac_address(Objid node_objid){sAddresses* addresses;// if every address was validatedif (validated) { // read the mac address of the node identified by the given objid addresses=fifo_multiplex_read(ptr_fifo_addresses,node_objid)
; // if this value was found if (addresses!=OPC_NIL) { // this wanted mac address is returned return(addresses->mac_address); } // otherwise the Objid is unknown by the dsr support package else { // thus the simulation is stopped op_sim_end("This node objid is unknown by the dsr support","","",""); return(-1); } }// otherwise if the addresses was not validated beforeelse { // the simulation is stopped since other nodes can still declare their addresses op_sim_end("All the dsr address must be declare and validated before calling this function","","",""); return(-1); }}/********************* DSR_SUPPORT_GET_DSR_ADDRESS ***********************/
/* This function returns the dsr address of the node identified by its objid/* Objid node_Objid: the objid of the node for which the dsr address is /* required/* Return: the dsr_address of the node identified by the given objid/* -1 if error/*************************************************************************/
int dsr_support_get_dsr_address(Objid node_objid){sAddresses* addresses;// if every address was validatedif (validated) { // read the dsr address of the node identified by the given objid addresses=fifo_multiplex_read(ptr_fifo_addresses,node_objid)
; // if this value was found if (addresses!=OPC_NIL) { // this wanted dsr address is returned return(addresses->dsr_address); } // otherwise the Objid is unknown by the dsr support package else { // thus the simulation is stopped op_sim_end("This node objid is unknown by the dsr support","","",""); return(-1); } }// otherwise if the addresses was not validated beforeelse { // the simulation is stopped since other nodes can still declare their addresses op_sim_end("All the dsr address must be declare and validated before calling this function","","",""); return(-1); }}/************************** DSR_SUPPORT_END ******************************/
/* This function free the memory used by the dsr support package/*************************************************************************/
void dsr_support_end(){// if the dsr addresses checking and validation structure was not destroyed yet (ex: error during pre init phase)if (ptr_fifo_test_addresses!=OPC_NIL) { // free the memory used by this dynamic fifo structure fifo_destroy(ptr_fifo_test_addresses); ptr_fifo_test_addresses=OPC_NIL; }// if the closure was not already done by another dsr nodesif ((ptr_fifo_addresses!=OPC_NIL)||(nodes_objid_table!=OPC_NIL)) { // free the memory used by the dynamic addresses structure and reintialize the global variables if (ptr_fifo_addresses!=OPC_NIL) fifo_destroy(ptr_fifo_addresses); if (nodes_objid_table!=OPC_NIL) op_prg_mem_free(nodes_objid_table); ptr_fifo_addresses=OPC_NIL; validated=0; number_of_dsr_nodes=0; }}///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -