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

📄 diff_prob.cc

📁 ns下的dd协议代码 可以进行wsn路由协议的仿真
💻 CC
📖 第 1 页 / 共 2 页
字号:

/*
 * diff_prob.cc
 * Copyright (C) 2000 by the University of Southern California
 * $Id: diff_prob.cc,v 1.10 2005/08/25 18:58:03 johnh Exp $
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 *
 * The copyright of this module includes the following
 * linking-with-specific-other-licenses addition:
 *
 * In addition, as a special exception, the copyright holders of
 * this module give you permission to combine (via static or
 * dynamic linking) this module with free software programs or
 * libraries that are released under the GNU LGPL and with code
 * included in the standard release of ns-2 under the Apache 2.0
 * license or under otherwise-compatible licenses with advertising
 * requirements (or modified versions of such code, with unchanged
 * license).  You may copy and distribute such a system following the
 * terms of the GNU GPL for this module and the licenses of the
 * other code concerned, provided that you include the source code of
 * that other code when and as the GNU GPL requires distribution of
 * source code.
 *
 * Note that people who make modified versions of this module
 * are not obligated to grant this special exception for their
 * modified versions; it is their choice whether to do so.  The GNU
 * General Public License gives permission to release a modified
 * version without this exception; this exception also makes it
 * possible to release a modified version which carries forward this
 * exception.
 *
 */

/****************************************************************/
/* diff_prob.cc : Chalermek Intanagonwiwat (USC/ISI)  05/18/99  */
/****************************************************************/

// Important Note: Work still in progress !

#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <signal.h>
#include <float.h>

#include <tcl.h>
#include <stdlib.h>

#include "diff_header.h"
#include "agent.h"
#include "tclcl.h"
#include "ip.h"
#include "config.h"
#include "packet.h"
#include "trace.h"
#include "random.h"
#include "classifier.h"
#include "node.h"
#include "diffusion.h"
#include "iflist.h"
#include "hash_table.h"
#include "arp.h"
#include "mac.h"
#include "ll.h"
#include "dsr/path.h"
#include "god.h"
#include "routing_table.h"
#include "diff_prob.h"


extern int dtype;
                 


static class DiffusionProbClass : public TclClass {
public:
  DiffusionProbClass() : TclClass("Agent/Diffusion/ProbGradient") {}
  TclObject* create(int , const char*const* ) {
    return(new DiffusionProb());
  }
} class_diffusion_probability;



void InterestTimer::expire(Event *)
{
  a_->InterestPropagate(pkt_, hashPtr_);
}


void EnergyTimer::expire(Event *)
{
  if (node_->energy_model()->energy() < threshold_) {    
    if (a_->NEG_REINF_ == true) {
         if (dtype < MAX_NEG_COUNTER)          
             a_->SendNegReinf();                //自己添加的
         else if (dtype >= MAX_NEG_COUNTER)     
              threshold_ = threshold_/X;        //自己添加的  
              
    }
    
      }

  if (threshold_ >= INIT_ENG_/Y) 
    resched(ENERGY_CHECK); 
}


DiffusionProb::DiffusionProb() : DiffusionAgent()
{
  is_low_power = false;
  num_neg_bcast_send = 0;
  num_neg_bcast_rcv = 0;
}


void DiffusionProb::recv(Packet* packet, Handler*)
{
  hdr_cdiff* dfh = HDR_CDIFF(packet);

  // Packet Hash Table is used to keep info about experienced pkts.

  Pkt_Hash_Entry *hashPtr= PktTable.GetHash(dfh->sender_id, dfh->pk_num);


#ifdef DEBUG_PROB
     printf("DF node %x recv %s (%x, %x, %d)\n",
	    THIS_NODE, MsgStr[dfh->mess_type], (dfh->sender_id).addr_, 
	    (dfh->sender_id).port_, dfh->pk_num);
#endif


     // Received this packet before ?

     if (hashPtr != NULL) {
       consider_old(packet);
       return;
     }

     // Never receive it before ? Put in hash table.

     PktTable.put_in_hash(dfh);

     // Take action for a new pkt.
       
     consider_new(packet);     
}


void DiffusionProb::consider_old(Packet *pkt)
{
  hdr_cdiff* dfh = HDR_CDIFF(pkt);
  unsigned char msg_type = dfh->mess_type;
  unsigned int dtype = dfh->data_type;

  Pkt_Hash_Entry *hashPtr;
  From_List  *fromPtr;
  nsaddr_t from_nodeID, forward_nodeID;

  switch (msg_type) {
    case INTEREST :

      hashPtr = PktTable.GetHash(dfh->sender_id, dfh->pk_num);

      if (hashPtr->is_forwarded == true) {
	Packet::free(pkt);
	return;
      }

      from_nodeID = (dfh->sender_id).addr_;
      forward_nodeID = (dfh->forward_agent_id).addr_;


      hashPtr->num_from++;
      fromPtr = new From_List;
      AGT_ADDR(fromPtr) = dfh->forward_agent_id;
      fromPtr->rank = hashPtr->num_from;

      if (from_nodeID == forward_nodeID)
	fromPtr->is_sink = true;

      fromPtr->next = hashPtr->from_agent;
      hashPtr->from_agent = fromPtr;

      // Check if this hashPtr has timer, and if lists already exist,
      // to decide whether to create or update Out_List and In_List

      if (hashPtr->timer == NULL) {
	if (hashPtr->has_list==false) {
	  CreateIOList(hashPtr, dtype);
        } 
	else {
	  UpdateIOList(fromPtr, dtype);
	}
      }
      
      Packet::free(pkt);
      break;

    default : 
      Packet::free(pkt);        
      break;
  }
}


void DiffusionProb::consider_new(Packet *pkt)
{
  hdr_cdiff* dfh = HDR_CDIFF(pkt);
  unsigned char msg_type = dfh->mess_type;
  unsigned int dtype = dfh->data_type;

  Pkt_Hash_Entry *hashPtr;
  From_List  *fromPtr;
  Agent_List *agentPtr;
  Agent_List *cur;
  PrvCurPtr  RetVal;
  nsaddr_t   from_nodeID, forward_nodeID;

  int i;

  switch (msg_type) {
    case INTEREST : 

      hashPtr = PktTable.GetHash(dfh->sender_id, dfh->pk_num);

      // Check if it comes from sink agent of this node
      // If so we have to keep it in sink list 

      from_nodeID = (dfh->sender_id).addr_;
      forward_nodeID = (dfh->forward_agent_id).addr_;


      if (THIS_NODE == from_nodeID) {       

	// It's from a sink on this node.
	// Is it already in list ?

	RetVal = INTF_FIND(routing_table[dtype].sink, dfh->sender_id);

	if (RetVal.cur == NULL) {            
	  // No, it's not.
	  agentPtr = new Agent_List;
	  AGT_ADDR(agentPtr) = dfh->sender_id;
	  INTF_INSERT(routing_table[dtype].sink, agentPtr);

	  God::instance()->AddSink(dtype, THIS_NODE);
	}	

      }
      else {                             
	
	// It's not from a sink on this node.

	fromPtr = new From_List;
	hashPtr->from_agent = fromPtr;
	hashPtr->num_from = 1;
	AGT_ADDR(fromPtr) = dfh->forward_agent_id;
	fromPtr->rank = 1;
	fromPtr->next = NULL;    

	// Is the forwarder node is a sink node ? 
	// This information is useful when we do negative reinforcement.

	if ( from_nodeID == forward_nodeID )
	  fromPtr->is_sink = true;

      }


      // Do we have to request data ?

      if (routing_table[dtype].source == NULL) {     

	// No, we don't. Better forward the interest.

	hashPtr->timer = new InterestTimer(this, hashPtr, pkt);
	(hashPtr->timer)->sched(INTEREST_DELAY*Random::uniform(1.0));
      } 
      else {
	
	// Since this node has sources,it won't propagate the interest.
	// We need to create Out_List and In_List in routing table [dtype] now.

	CreateIOList(hashPtr, dtype);
       	data_request_all(dtype);

	Packet::free(pkt);
      }
      break;


    case POS_REINFORCE :

      if ( POS_REINF_ == false ) {
	printf("Hey, we are not in pos_reinf mode.\n");
	Packet::free(pkt);
	exit(-1);
      }

      IncGradient(dtype, dfh->forward_agent_id);
      CAL_RANGE(routing_table[dtype].active);

      if (routing_table[dtype].source == NULL) {
	if (is_low_power == false) {
	  FwdPosReinf(dtype, pkt);
	  return;
	}
      } 

      Packet::free(pkt);
      break;


    case NEG_REINFORCE :  

      if (NEG_REINF_ == false) {
	printf("Hey, we are not in neg_reinf mode.\n");
	Packet::free(pkt);
	exit(-1);
      }

      // Negative Reinforcement won't be forwarded.

      num_neg_bcast_rcv++;

      for (i=0; i<MAX_DATA_TYPE; i++) {
	RetVal = INTF_FIND(routing_table[i].active, dfh->sender_id);
	if (RetVal.cur == NULL ) {
	  continue;
	}

	// If it is not a sink, we decrease the gradient.

	if ( IS_SINK(RetVal.cur) == false) {
	  DecGradient(i, dfh->sender_id);
	  CAL_RANGE(routing_table[i].active);
	}
      }

      Packet::free(pkt);
      break;


    case DATA_READY :

      // put source_agent in source list of routing table

      agentPtr = new Agent_List;
      AGT_ADDR(agentPtr) = dfh->sender_id;
      agentPtr->next = routing_table[dtype].source;
      routing_table[dtype].source = agentPtr;

      if (routing_table[dtype].active != NULL ||
	  routing_table[dtype].sink != NULL) {
	SEND_MESSAGE(dtype, dfh->sender_id, DATA_REQUEST);
      }

      Packet::free(pkt);
      break;


    case DATA :

      DataForSink(pkt);

      routing_table[dtype].IncRecvCnt(dfh->forward_agent_id);
      ForwardData(pkt);

      if (routing_table[dtype].counter >= MAX_REINFORCE_COUNTER) {
	if (is_low_power == false) {
	  if (POS_REINF_ == true)
	    GenPosReinf(dtype);
	  return;
	}
	if (routing_table[dtype].sink != NULL) {
	  if (POS_REINF_ == true)
	    GenPosReinf(dtype);
	  return;
	}
      }
      break;


    case INHIBIT :

      if (routing_table[dtype].active == NULL) {
        Packet::free(pkt);
        return;
      }

      RetVal=INTF_FIND(routing_table[dtype].active, dfh->sender_id);
      if (RetVal.cur == NULL){
	Packet::free(pkt);
	return;
      }

      INTF_REMOVE(RetVal.prv, RetVal.cur);
      INTF_INSERT(routing_table[dtype].inactive, RetVal.cur);
      routing_table[dtype].num_active --;
      NORMALIZE(routing_table[dtype].active);
      CAL_RANGE(routing_table[dtype].active);

      if (routing_table[dtype].num_active < 1) {

	// *** You need to stop the source if you have one. *****

	if (routing_table[dtype].source != NULL ) {
	  for (cur=routing_table[dtype].source; cur != NULL; 
	       cur=AGENT_NEXT(cur)) {
	    SEND_MESSAGE(dtype, AGT_ADDR(cur), DATA_STOP);
	  }
	  Packet::free(pkt);
	  return;
	}

	// If not, send inhibit signal upstream

	SendInhibit(dtype);
      }
      
      Packet::free(pkt);
      return;


    case TX_FAILED :

      if (BACKTRACK_ == false) {
	printf("We are not in backtracking mode.\n");
	Packet::free(pkt);
	exit(-1);
      }

      if (routing_table[dtype].active == NULL) {
        ForwardTxFailed(pkt);
        return;
      }

      //      RetVal=INTF_FIND(routing_table[dtype].active, dfh->sender_id);

      RetVal=INTF_FIND(routing_table[dtype].active, dfh->forward_agent_id);

      if (RetVal.cur != NULL){
	INTF_REMOVE(RetVal.prv, RetVal.cur);
	INTF_INSERT(routing_table[dtype].inactive, RetVal.cur);
	routing_table[dtype].num_active --;
	NORMALIZE(routing_table[dtype].active);
	CAL_RANGE(routing_table[dtype].active);
      }

      if (routing_table[dtype].num_active < 1) {
	ForwardTxFailed(pkt);
	return;
      }
      
      ReTxData(pkt);

      Packet::free(pkt);
      return;


      case ENG_LXK :
      

        if (is_low_power == true) {
	     if (routing_table[dtype].counter <= MAX_NEG_COUNTER) {
               if (NEG_REINF_ == true)
                  GenNegReinf(dtype);
               return;
             }
             if (routing_table[dtype].counter >= MAX_NEG_COUNTER) {
               if (NEG_REINF_ == true)
                  routing_table[dtype].counter =0;
                  resched(ENERGY_CHECK);

⌨️ 快捷键说明

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