Ir para conteúdo
Fórum Script Brasil
  • 0

Funções não declaradas


jaderson

Pergunta

Bom é o seguinte tenho um trabalha de faculdade onde tenho de utilizar um simulador de redes chamado sense ( http://www.ita.cs.rpi.edu/sense/index.html ), venho a dias tentando fazer ele funcionar mas não entendia qual era o problema, não sou muito bom em C/C++. Encontrei com um amigo meu que tem um conhecimento um pouco mais avantajado de C/C++ ele analisou o programa, corrigiu alguns erros de Scripts mas disse que ainda tinha muita coisa a fazer por que tinha APIs legadas depreciadas em uso, infelizmente esse amigo disse que não poderia me ajudar pois estava muito atarefado, mas já me ajudou bastante né. Depois que ele terminou me enviou um arquivo contendo algumas modificações, mas ainda tem alguns erros aparecendo na consola, seriam erros tais como esse

g++ -Wall -O2 -DVISUAL_ROUTE -DVR_SIZE=30 -c -o sim_shr.o sim_shr.cxx

../../net/shr-common.h: In member function ‘void compcxx_SHR_9::dump(FILE*, int, int) const’:

../../net/shr-common.h:426: error: ‘MyForwardDelay’ was not declared in this scope

../../net/shr-common.h: In member function ‘void compcxx_SHR_9::from_mac_data(smart_packet_t<SHR_Struct<CBR_Struct::packet_t>::hdr_struct, CBR_Struct::packet_t>*, double)’:

../../net/shr-common.h:683: error: ‘FirstPacketID’ was not declared in this scope

../../net/shr-common.h:686: error: ‘incNumNeighbors’ was not declared in this scope

../../net/shr-common.h:687: error: ‘NumNeighbors’ was not declared in this scope

../../net/shr-common.h:687: error: ‘setMyForwardDelay’ was not declared in this scope

../../net/shr-common.h:688: error: ‘MyForwardDelay’ was not declared in this scope

../../net/shr-common.h:693: error: ‘setFirstPacketID’ was not declared in this scope

../../net/shr-common.h:694: error: ‘incNumNeighbors’ was not declared in this scope

../../net/shr-common.h:695: error: ‘NumNeighbors’ was not declared in this scope

../../net/shr-common.h:695: error: ‘setMyForwardDelay’ was not declared in this scope

../../net/shr-common.h:696: error: ‘MyForwardDelay’ was not declared in this scope

../../net/shr-common.h: In member function ‘void compcxx_SHR_9::process_New(smart_packet_t<SHR_Struct<CBR_Struct::packet_t>::hdr_struct, CBR_Struct::packet_t>*, PktInfo*, Location, smart_packet_t<SHR_Struct<CBR_Struct::packet_t>::hdr_struct, CBR_Struct::packet_t>*)’:

../../net/shr-common.h:863: error: ‘MyForwardDelay’ was not declared in this scope

../../net/shr-common.h:868: error: ‘MyForwardDelay’ was not declared in this scope

../../net/shr-common.h:874: warning: deprecated conversion from string constant to ‘char*’

../../net/shr-common.h:880: error: ‘MyForwardDelay’ was not declared in this scope

../../net/shr-common.h:889: error: ‘MyForwardDelay’ was not declared in this scope

../../net/shr-common.h:901: warning: deprecated conversion from string constant to ‘char*’

../../net/shr-common.h:907: error: ‘struct SHR_Struct<CBR_Struct::packet_t>::hdr_struct::info’ has no member named ‘prev_addr’

../../net/shr-common.h:908: error: ‘struct SHR_Struct<CBR_Struct::packet_t>::hdr_struct::info’ has no member named ‘prev_addr’

../../net/shr-common.h: In member function ‘void compcxx_SHR_9::process_Listen(smart_packet_t<SHR_Struct<CBR_Struct::packet_t>::hdr_struct, CBR_Struct::packet_t>*, PktInfo*, Location, smart_packet_t<SHR_Struct<CBR_Struct::packet_t>::hdr_struct, CBR_Struct::packet_t>*, bool)’:

../../net/shr-common.h:1084: error: ‘MyForwardDelay’ was not declared in this scope

../../net/shr-common.h:1093: error: ‘MyForwardDelay’ was not declared in this scope

../../net/shr-common.h:1110: error: ‘MyForwardDelay’ was not declared in this scope

../../net/shr.h: In member function ‘void compcxx_SHR_9::recvNewDATA(smart_packet_t<SHR_Struct<CBR_Struct::packet_t>::hdr_struct, CBR_Struct::packet_t>*, PktInfo*)’:

../../net/shr.h:494: warning: deprecated conversion from string constant to ‘char*’

../../net/shr.h:552: warning: deprecated conversion from string constant to ‘char*’

../../net/shr.h: In member function ‘void compcxx_SHR_9::process_Possible(smart_packet_t<SHR_Struct<CBR_Struct::packet_t>::hdr_struct, CBR_Struct::packet_t>*, PktInfo*, Location, smart_packet_t<SHR_Struct<CBR_Struct::packet_t>::hdr_struct, CBR_Struct::packet_t>*)’:

../../net/shr.h:609: warning: deprecated conversion from string constant to ‘char*’

../../net/shr.h: In member function ‘void compcxx_SHR_9::from_mac_ack(bool)’:

../../net/shr.h:978: warning: deprecated conversion from string constant to ‘char*’

../../net/shr.h:992: warning: deprecated conversion from string constant to ‘char*’

sim_shr.h: In member function ‘void compcxx_RoutingSim_17::Setup()’:

sim_shr.h:674: warning: suggest parentheses around ‘&&’ within ‘||’

make: ** [sim_shr.o] Erro 1

Eu dei uma olhada nesse arquivo shr-common.h e achei esse MyForwardDelay e esses outros que ele cita como não declarados, mas sinceramente não sei como corrigir esse erro, se alguém puder me dizer pois acredito que deva ser algo bem facil, a seguir o código do shr-common.h

/* $CVSHeader: sense/code/net/shr-common.h,v 1.1.6.3 2007/09/05 12:03:02 chris Exp $ */

/*************************************************************************
 *   @<title> Functionality common to the SHR family of protocols </title>@
 *
 *   @<!-- Copyright 2007 Mark Lisee, Boleslaw K. Szymanski and
 *   Rensselaer Polytechnic Institute. All worldwide rights reserved.  A
 *   license to use, copy, modify and distribute this software for
 *   non-commercial research purposes only is hereby granted, provided that
 *   this copyright notice and accompanying disclaimer is not modified or
 *   removed from the software.
 *
 *   DISCLAIMER: The software is distributed "AS IS" without any express or
 *   implied warranty, including but not limited to, any implied warranties of
 *   merchantability or fitness for a particular purpose or any warranty of
 *   non-infringement of any current or pending patent rights. The authors of
 *   the software make no representations about the suitability of this
 *   software for any particular purpose. The entire risk as to the quality
 *   and performance of the software is with the user. Should the software
 *   prove defective, the user assumes the cost of all necessary servicing,
 *   repair or correction. In particular, neither Rensselaer Polytechnic
 *   Institute, nor the authors of the software are liable for any indirect,
 *   special, consequential, or incidental damages related to the software,
 *   to the maximum extent the law permits.-->@
 *
 *************************************************************************/

#ifndef _shr_common_h_
#define _shr_common_h_

#ifdef    DEBUG_PRINT
#define dbgDoPrint    (true)
#define    dbgPrint(x)     printf x
#else    // DEBUG_PRINT
#define    dbgPrint(x)
#define    dbgDoPrint    (false)
#endif    // DEBUG_PRINT

enum Location    { LocSendQueue, LocActivePacket, LocNotFound};
const char    *LocationString( Location loc);
const char    *PktStateString( PktState state);

template <class PLD> bool    SHR<PLD>::doubleMsgPrinted = false;
template <class PLD> Visualizer    *SHR<PLD>::visualizer = NULL;

template <class PLD>
void SHR<PLD>::setVisualizer(
  Visualizer    *ptr)
{
  visualizer = ptr;
  return;
}

template <class PLD>
unsigned int SHR<PLD>::seq_number_t::hopCount() const
{
  switch( state)
  {
    case Initial:
      return 0;
    case Steady:
      return currentHC;
    default:
      assert( 0);    // something really bad happened
      return 0;
  }
}

template <class PLD>
void SHR<PLD>::seq_number_t::hopCount(
  unsigned int    newHC)
{
  switch( state)
  {
    case Initial:
      currentHC = newHC;
      state = Steady;
      break;
    case Steady:
      currentHC = newHC;
      break;
    default:
      assert( 0);    // something really bad happened
  }
  return;
}

template <class PLD>
void SHR<PLD>::startProtocolTimer(
  PktInfo    *pi,
  packet_t    *pkt,
  double    delay,
  void        *data,
  char            *str)
{
  startProtocolTimer( pi, makePktID( pkt), delay, data, str);
  return;
}

template <class PLD>
void SHR<PLD>::startProtocolTimer(
  PktInfo    *pi,
  PktID        *pID,
  double    delay,
  void        *data,
  char            *str)
{
  // note: the PktID is retained by the timer
  dbgPrint( ("start-Pro-Tmr-%s: ea %3d; %f + %f; %p; (%3d->%3d, sn%08X)\n", str,
         (int) MyEtherAddr, SimTime(), delay, pID, (int) pID->flow.src,
         (int) pID->flow.dest, pID->seqNumber));
  pi->timerIndex = Protocol_Timer.Set( pID, SimTime() + delay);
  pi->timerActive = true;
  pi->savedData = data;
  return;
}

template <class PLD>
FlowInfo *SHR<PLD>::getFlow(
  packet_t    *pkt)
{
  PktID        *id = makePktID( pkt);
  FlowInfo    *fi = getFlow( id->flow);
  delete id;
  return fi;
}

template <class PLD>
FlowInfo *SHR<PLD>::getFlow(
  FlowID    &flow)
{
 FlowIDMap::iterator    fIter = APIDs.find( flow);
  
  // if the map is empty or FlowID is not found
  if( fIter == APIDs.end())
  {
    FlowInfo    *info = new FlowInfo;

    // add FlowID to map
    APIDs.insert( make_pair( flow, info));
    fIter = APIDs.find( flow);
  }
  return fIter->second;
}

template <class PLD>
void SHR<PLD>::updateSLRPL(
  packet_t    *pkt)
{
#ifdef    USE_ENERGY_AWARE_THRESHOLD
  FlowIDMap::iterator    fIter;

  for( fIter = APIDs.begin(); fIter != APIDs.end(); fIter++)
    if( fIter->first.dest == pkt->hdr.dst_addr)
    {
      dbgPrint( ("uSLRPL: ea %3d; sn%08X; %3d->%3d; %f->%f @ %f\n",
         (int) MyEtherAddr, pkt->hdr.seq_number, (int) fIter->first.src,
         (int) fIter->first.dest, fIter->second->savedLRPL,
         pkt->hdr.senderPower, SimTime()));
      fIter->second->savedLRPL = pkt->hdr.senderPower;
    }
#else    // USE_ENERGY_AWARE_THRESHOLD
  assert( 0);
#endif    // USE_ENERGY_AWARE_THRESHOLD
  return;
}

template <class PLD>
PktInfo *SHR<PLD>::getPacketState(
  packet_t    *pkt)
{
  PktID        *id = makePktID( pkt);
  PktInfo    *pi = getPacketState( *id);
  delete id;
  return pi;
}

template <class PLD>
PktInfo *SHR<PLD>::getPacketState(PktID    &id)
{
    PktInfo    *pi;
    
    do
    {
#ifdef    DEBUG_PRINT_1
    {
        printf( "get-Pkt-State: Addr: %1d; APIDs ", (int) MyEtherAddr);
        FlowIDMap::iterator    fIter = APIDs.begin();
        for(; fIter != APIDs.end(); fIter++)
        printf( "(%d,%d) ", (int) fIter->first.src, (int) fIter->first.dest);
        printf( "@ %f\n", SimTime());
    }
#endif    // DEBUG_PRINT_1
    
    FlowInfo        *fInfo = getFlow( id.flow);
    if( fInfo->pMap.size() == 0)
    {
        pi = new PktInfo( true);
        (fInfo->pMap)[ id.seqNumber] = pi;
        break;
    }
    
#ifdef    DEBUG_PRINT_1
    {
        printf( "get-Pkt-State: Addr: %3d; PktIDMap [src:%3d,dest:%3d] ", (int) MyEtherAddr, 
            (int) id.flow.src, (int) id.flow.dest);
        PktIDMap::iterator    iter = fInfo->pMap.begin();
        for(; iter != fInfo->pMap.end(); iter++)
        printf( "(sn%08X,%s) ", iter->first, PktStateString( (iter->second)->state));
        printf( "\n");
    }
#endif    // DEBUG_PRINT_1
    
    PktIDMap::iterator    pIter = fInfo->pMap.find( id.seqNumber);
    
    // if sequence # is found, update time and return state
    if( pIter != fInfo->pMap.end())
    {
        pi = pIter->second;
        break;
    }
    
    // sequence # is not found; create one that is either New or Ignore
    // if seq # is less than smallest, the state is Ignore, otherwise the state is New
    pi = new PktInfo( id.seqNumber >= fInfo->pMap.begin()->first);
#ifdef    DEBUG_PRINT_1
    printf( "get-Pkt-State: Addr: %3d; adding sn%08X; %s\n", (int) MyEtherAddr,
        (int) id.seqNumber, PktStateString( pi->state));
#endif    // DEBUG_PRINT_1
    (fInfo->pMap)[ id.seqNumber] = pi;
    }
    while( false);
    return pi;
}

template <class PLD>
PktInfo *SHR<PLD>::getPacketState(
  packet_t    *pkt,
  Location    &loc,
  packet_t    *&outgoingPkt)
{
  loc = locatePacket( pkt->hdr.src_addr, pkt->hdr.seq_number, &outgoingPkt);
  return getPacketState( pkt);
}

template <class PLD>
void SHR<PLD>::setPacketState(
  packet_t    *pkt,
  PktState    newState)
{
  PktID        id;

  id.flow.src = pkt->hdr.src_addr;
  id.flow.dest = pkt->hdr.dst_addr;
  id.seqNumber = pkt->hdr.seq_number;
  setPacketState( id, newState);
  return;
}

template <class PLD>
void SHR<PLD>::setPacketState(
  PktID        &id,
  PktState    newState)
{
  if( newState == PSNone)
    return;

  // Once in the state PSIgnore, do not leave it.
  PktInfo    *pi = getPacketState( id);
  if( pi->state == PSIgnore)
    return;

  dbgPrint( ("set-Pkt-State: ea %3d; sn%08X; %s->%s @ %f\n", (int) MyEtherAddr,
         id.seqNumber, PktStateString( pi->state), PktStateString( newState),
         SimTime()));
  pi->state = newState;
  
  if( newState == PSIgnore)
    CancelTimer( id);
  return;
}

template <class PLD>
void SHR<PLD>::trimAPID(
  packet_t    *pkt)
{
  PktID        *pID = makePktID( pkt);
  trimAPID( *pID);
  delete pID;
  return;
}

template <class PLD>
void SHR<PLD>::trimAPID(
  PktID        &id)
{
  PktInfo    *pInfo;
  FlowInfo    *fInfo = getFlow( id.flow);

  // if map is too big, remove entry from beginning
  if( fInfo->pMap.size() > 5)
  {
    PktID    newID;        // *pID is read only
    newID.flow.src = id.flow.src;
    newID.flow.dest = id.flow.dest;
    newID.seqNumber = fInfo->pMap.begin()->first;
    setPacketState( newID, PSIgnore);
  }

  // remove sequence #s with ignore state from front of map
  // leave at least one entry in map
  while( fInfo->pMap.size() > 1 && fInfo->pMap.begin()->second->state == PSIgnore)
  {
    pInfo = fInfo->pMap.begin()->second;
    delete pInfo;
    fInfo->pMap.erase( fInfo->pMap.begin());
  }
  return;
}

template <class PLD>
void *SHR<PLD>::createRequestPkt(
  ether_addr_t    dst,
  int        hopCount,
  bool        newSeqNumber)
{
  packet_t    *newPkt = (packet_t *) createBasicPkt( dst, newSeqNumber);

  newPkt->hdr.type = REQUEST;
  newPkt->hdr.info.size = REQUEST_SIZE;
  newPkt->hdr.expected_hop  =  1;
  newPkt->hdr.actual_hop = hopCount;
  newPkt->hdr.info.newSeqNumber = newSeqNumber;
#ifdef    USE_MAX_HOP
  newPkt->hdr.max_hop = 40;
#endif    // USE_MAX_HOP
  return newPkt;
}

template <class PLD>
void *SHR<PLD>::createReplyPkt(
  ether_addr_t    src,
  unsigned int    expectedHop,
  unsigned int    seqNumber)
{
  packet_t    *newPkt = (packet_t *) createBasicPkt( src, false);

  newPkt->hdr.type = REPLY;
  newPkt->hdr.info.size = REPLY_SIZE;
  newPkt->hdr.expected_hop = expectedHop;
  newPkt->hdr.seq_number = seqNumber;
#ifdef    USE_MAX_HOP
  newPkt->hdr.max_hop = hopCountStretch( expectedHop);
#endif    // USE_MAX_HOP
  return newPkt;
}

#define    makeiS    char iS[81]; for( int i = indent-1; i >= 0; i--) iS[i] = ' '; iS[indent] = 0
#define    pIndent    fputs( iS, fp)

template <class PLD>
void SHR<PLD>::seq_number_t::dumpStatic(
  FILE        *,
  int,
  int) const
{
  return;
}

template <class PLD>
void SHR<PLD>::seq_number_t::dump(
  FILE        *fp,
  int        dest,
  int        indent,
  int        offset) const
{
  makeiS;
  pIndent;
  fprintf( fp, "%3d { ", dest);
  switch( state)
  {
    case Initial:
      fprintf( fp, "currentHC 0, state Initial");
      break;
    case Steady:
      fprintf( fp, "currentHC %d, state Steady", currentHC);
      break;
  }
  fprintf( fp, " }\n");
  return;
}

template <class PLD>
void SHR<PLD>::dumpStatic(
  FILE        *fp,
  int        indent,
  int        offset) const
{
//  indent += offset;
//  makeiS;
//  pIndent;
//  fprintf( fp, "hopCounts %d\n", HCArraySize);
  return;
}

template <class PLD>
void SHR<PLD>::dumpCacheStatic(
  FILE        *fp,
  int        indent,
  int        offset) const
{
  m_seq_cache.begin()->second.dumpStatic( fp, indent, offset);
  return;
}

template <class PLD>
void SHR<PLD>::dump(
  FILE        *fp,
  int        indent,
  int        offset) const
{
  indent += offset;
  makeiS;
  pIndent;
  fprintf( fp, "addr %d, seqNumber %d, MyForwardDelay %f,\n", (int) MyEtherAddr,
       m_seq_number, MyForwardDelay);
  pIndent;
  fprintf( fp, "RXThresh %f,\n", RXThresh);
  return;
}

template <class PLD>
void SHR<PLD>::dumpCostTable(
  FILE        *fp,
  int        indent,
  int        offset) const
{
  cache_t::const_iterator    iter;

  indent += offset;
  makeiS;
  for( iter = m_seq_cache.begin(); iter != m_seq_cache.end(); iter++)
    iter->second.dump( fp, (int) iter->first, indent+offset, offset);
  return;
}

template <class PLD>
SHR<PLD>::SHR()
{
  connect Protocol_Timer.to_component, timerExpired;
  return;
}

template <class PLD>
SHR<PLD>::~SHR()
{
  return;
}

template <class PLD>
void SHR<PLD>::Start() 
{
  m_mac_busy = false;
  m_active_packet = NULL;
  m_seq_number = (unsigned int) MyEtherAddr << 16;
  m_seq_cache.insert(make_pair(MyEtherAddr,seq_number_t()));
  return;
}

template <class PLD>
void SHR<PLD>::Stop() 
{
  return;
}

template <class PLD>
void SHR<PLD>::from_transport(
  payload_t    &pld,
  ether_addr_t    &dst,
  unsigned int    size)
{
//  if( size > 1511)
//    printf( "ft: ea%3d; Oversize packet (%d) to %3d\n", (int) MyEtherAddr,
//        size, (int) dst);
  //create the data packet
  packet_t    *pkt = (packet_t *) createDataPkt( pld, dst, size);
  stats->appSend( pkt->getStatsId());

  dbgPrint( ("fm_trnsprt: %3d->%3d *%s @ %f ea %3d\n", (int) MyEtherAddr, (int) dst,
         pkt->dump().c_str(), SimTime(), (int) MyEtherAddr));

  // look up destination in the cost table
  cache_t::iterator iter = m_seq_cache.find(dst);

  // If destination wasn't found, send a DREQ packet and put the data packet
  // on the queue of packets to be sent.
  FlowInfo *fInfo = getFlow(pkt);

  if( (iter == m_seq_cache.end() || iter->second.noDistance() == true) && ((SimTime() - fInfo->DREQSentTime) > 8.0 || fInfo->DREQSentTime == 0))
    {
      printf("XXX %f: Flow: %3d->%3d, DREQ Sent: %f, Time Diff: %f\n",SimTime(),(int)MyEtherAddr,(int)dst,fInfo->DREQSentTime,SimTime() - fInfo->DREQSentTime);

      // the destination is not in the active node table
      // must send a REQUEST packet first;
      packet_t    *rp = (packet_t *) createRequestPkt( dst, 1, true);
      SendPacket( rp);
      m_data_buffer.push_back( pkt);
      fInfo->DREQSentTime = SimTime();
      printf("XXX %f: DREQTX, flow: %3d->%3d\n",SimTime(),(int)MyEtherAddr,(int)dst);
    }
  else if (iter == m_seq_cache.end() || iter->second.noDistance() == true)
    {
      printf("XXX %f: Flow: %3d->%3d, DREQ Sent: %f, Time Diff: %f\n",SimTime(),(int)MyEtherAddr,(int)dst,fInfo->DREQSentTime,SimTime() - fInfo->DREQSentTime);

      //    fInfo->DREQAttempt ++;
      //      printf("%f: DREQTX = %d, flow: %3d->%3d\n",SimTime(),fInfo->DREQAttempt,(int)MyEtherAddr,(int)dst);
      return;
    }
  // otherwise, set the expected and maximum hops from the cost table
  else
    {
      // The SSR/SHR specs call for decrementing expected_hop before transmitting.
      // In SENSE, >1 nodes reference same packet in memory, so can't decrement
      pkt->hdr.expected_hop = (*iter).second.hopCount();
#ifdef    USE_MAX_HOP
      pkt->hdr.max_hop = hopCountStretch( pkt->hdr.expected_hop);
#endif    // USE_MAX_HOP
      SendPacket( pkt);
    }
  return;
}

template <class PLD>
void SHR<PLD>::AddToSendQueue(
  packet_t    *pkt)
{
  m_send_queue.push_back( pkt);
  return;
}

/*
** The purpose of this method is to simulate the small amount of time that
** elapses between when the hardware stops sensing the carrier and it is able
** to actually start transmission of the packet.
**
** This member function had been more complicated, but the transition timer
** functionality was moved to the MAC component.
*/
template <class PLD>
void SHR<PLD>::SendToMac(
  packet_t    *pkt)
{
  m_mac_busy = true;
  to_mac( pkt, pkt->getStatsId(), pkt->hdr.info.size, 0);
  return;
}

/*
** Find the packet's source address in this node's cache. If the entry is not
** found, create it. If the hop count in the cache is greater than the hop
** count of the packet, update the cache.
**
** Return true if the entry was created or updated, false otherwise.
*/
template <class PLD>
bool SHR<PLD>::updateHopCountInCache(
  ether_addr_t    dst,
  unsigned int    hopCount,
  unsigned int    seqNumber)
{
  if( dst == MyEtherAddr)
    return false;
  cache_t::iterator    iter;

  iter = m_seq_cache.find( dst); 
  // If the destination address is not found, create a new record.
  if( iter == m_seq_cache.end())
  {
    dbgPrint( ("update-HC-in-cache: ea %3d; sn%08X; dst %3d; hc %2d new @ %f\n",
           (int) MyEtherAddr, seqNumber, (int) dst, hopCount, SimTime()));
    iter = (m_seq_cache.insert( make_pair( dst, seq_number_t()))).first;
    iter->second.hopCount( hopCount);
    return true;
  }

  // if the new packet won't update the cost table entry, return false
  if( iter->second.noDistance() == false &&
      iter->second.hopCount() <= hopCount)
    return false;

  // update the cost table entry
  dbgPrint( ("uHCIC: ea %3d; sn%08X; dst %3d; hc %2d->%2d update @ %f\n",
         (int) MyEtherAddr, seqNumber, (int) dst, iter->second.hopCount(),
         hopCount, SimTime()));
  iter->second.hopCount( hopCount);
  return true;
}

/*
** If there is an entry for the destination, increase the distance by 2.
*/
template <class PLD>
unsigned int SHR<PLD>::increaseHopCountInCache(
  ether_addr_t    dst,
  unsigned int    seqNumber)
{
  unsigned int    hc;

  if( dst == MyEtherAddr)
    return 0;
  cache_t::iterator    iter;

  iter = m_seq_cache.find( dst); 
  if( iter == m_seq_cache.end())
  {
    printf( "***** increaseHopCountInCache: entry not found for %d\n",
        (int) dst);
    assert( 0);
  }
  hc = iter->second.hopCount() + 2;
  dbgPrint( ("iHCIC: ea %3d; sn%08X; dst %3d; hc %2d increase @ %f\n",
         (int) MyEtherAddr, seqNumber, (int) dst, hc, SimTime()));
  iter->second.hopCount( hc);
  return hc;
}

template <class PLD>
PktID *SHR<PLD>::makePktID(
  packet_t    *pkt)
{
  PktID        *id = new PktID;

  id->flow.src = pkt->hdr.src_addr;
  id->flow.dest = pkt->hdr.dst_addr;
  id->seqNumber = pkt->hdr.seq_number;
  id->statsId = pkt->getStatsId();
  return id;
}

/*******************************************************************
 * We are now receiving a packet from the mac layer.
 *******************************************************************/

template <class PLD>
void SHR<PLD>::from_mac_data(
    packet_t    *pkt,
    double)    //power
{
    ether_addr_t    src = pkt->hdr.src_addr;
    bool        costTableUpdated = false;
    Location            loc;
    packet_t            *outgoingPkt;

    addToNeighborMatrix( (int) pkt->hdr.info.cur_addr, (int) MyEtherAddr);
    dbgPrint( ("fm_mac_data: ea %3d<-%3d; sn%08X; %s; ahc %d; ehc %d;"
#ifdef    USE_ENERGY_AWARE_THRESHOLD
           " %f;"
#endif    // USE_ENERGY_AWARE_THRESHOLD
           " @ %f\n",
           (int) MyEtherAddr, (int) pkt->hdr.info.cur_addr,
           pkt->hdr.seq_number, pkt->hdr.getType(), pkt->hdr.actual_hop,
           pkt->hdr.expected_hop,
#ifdef    USE_ENERGY_AWARE_THRESHOLD
           pkt->hdr.lastResponderPower,
#endif    // USE_ENERGY_AWARE_THRESHOLD
           SimTime()));
    
/*
** The result of updating the cost table is used if the packet is a DREQ or
** DREP. ACK packets do not update the cost table.
*/
    if( pkt->hdr.type != ACK)
    costTableUpdated = updateHopCountInCache( src, pkt->hdr.actual_hop,
                          pkt->hdr.seq_number);
    else
    costTableUpdated = false;
    if( pkt->hdr.type == DATA)
    updateHopCountInCache( pkt->hdr.dst_addr, pkt->hdr.expected_hop + 1,
                   pkt->hdr.seq_number);

    /*******************Variable lambda work*****************/
    //    printf("aaa%03d FirstPacketID: sn%08X, Current Packet: sn%08X\n",(int)MyEtherAddr,FirstPacketID,pkt->hdr.seq_number);
    if (FirstPacketID == pkt->hdr.seq_number)
      {
    //printf("aaaMultiple Occurance of Packet\n");
    incNumNeighbors();
    setMyForwardDelay(NumNeighbors * 0.015);
    printf("# aaa %03d NumNeighbors: %d, Delay: %f\n",(int)MyEtherAddr,NumNeighbors,MyForwardDelay);
      }
    else if (FirstPacketID == 0)
      {
    //    printf("aaaFirst Occurance of Packet\n");
    setFirstPacketID(pkt->hdr.seq_number);
    incNumNeighbors();
    setMyForwardDelay(NumNeighbors * 0.015);
    printf("# aaa %03d NumNeighbors: %d, Delay: %f\n",(int)MyEtherAddr,NumNeighbors,MyForwardDelay);
    //    printf("aaa%03d New FirstPacketID: %08X\n",(int)MyEtherAddr,
      }      
    
    /*****************end******************/
    
/*
** The find method is guaranteed to find the source since the function call
** above will have added an entry.
*/
    PktInfo    *pi = getPacketState( pkt, loc, outgoingPkt);
    pi->pktType = pkt->hdr.type;
    stats->netReceive( pkt->getStatsId(), pi->state == PSNew);
    
    switch( pi->state)
    {
    case PSNew:
        process_New( pkt, pi, loc, outgoingPkt);
        break;
    case PSListen:
        process_Listen( pkt, pi, loc, outgoingPkt, costTableUpdated);
        break;
    case PSDelay:
        // cost table has already been updated
        break;
    case PSPossible:
        process_Possible( pkt, pi, loc, outgoingPkt);
        break;
#ifndef shr_pp
    case PSWait:
        process_Wait( pkt, pi, loc, outgoingPkt);
        break;
#endif //shr_pp
    case PSOwner:
        process_Owner( pkt, pi, loc, outgoingPkt);
        break;
    case PSFather:
        process_Father( pkt, pi, loc, outgoingPkt);        break;
    case PSResend:
        process_Resend( pkt, pi, loc, outgoingPkt);
        break;
    case PSIgnore:
        process_Ignore( pkt, pi, loc, outgoingPkt);
        break;
#ifndef shr_pp
    case PSEnergy:
        process_Energy( pkt, pi, loc, outgoingPkt);
        break;
#endif //shr_pp
    case PSSendAck:
        process_SendAck( pkt, pi, loc, outgoingPkt);
        break;
    default:
        printf( "***** from_mac_data: ea %3d; Invalid packet state %s (%d)\n",
            (int) MyEtherAddr, PktStateString( pi->state), (int) pi->state);
        assert( 0);
        break;
    }
    trimAPID( pkt);
    pkt->free();
    return;
}

template <class PLD>
void SHR<PLD>::timerExpired(
    PktID        *pID,
    unsigned int    index)
{
    PktInfo            *pi = getPacketState( *pID);
    bool        retained = false;
    
    pi->timerActive = false;
    dbgPrint( ("time-Exp: ea %3d; sn%08X; @ %f\n", (int) MyEtherAddr,(int) pID->seqNumber, SimTime()));
    dbgPrint( ("time-Exp: ea %3d; %p; %08X->%08X\n", (int) MyEtherAddr, pID, (int) pID->flow.src, (int) pID->flow.dest));
    
    switch( pi->state)
    {
    case PSNew:
    case PSIgnore:
        printf( "***** timerExpired: ea %3d; sn%08X; %s\n", (int) MyEtherAddr,
            pID->seqNumber, PktStateString( pi->state));
        assert( 0);
        break;
    case PSDelay:
        retained = timer_Delay( pID, pi);
        break;
    case PSListen:
        retained = timer_Listen( pID, pi);
        break;
    case PSPossible:
        retained = timer_Possible( pID, pi);
        break;
#ifndef shr_pp
    case PSWait:
        retained = timer_Wait( pID, pi);
        break;
#endif //shr_pp
    case PSOwner:
        retained = timer_Owner( pID, pi);
        break;
    case PSFather:
        retained = timer_Father( pID, pi);
        break;
    case PSResend:
        retained = timer_Resend( pID, pi);
        break;
#ifndef shr_pp
    case PSEnergy:
        retained = timer_Energy( pID, pi);
        break;
#endif //shr_pp
    case PSSendAck:
        retained = timer_SendAck( pID, pi);
        break;
    default:
        printf( "***** timerExpired: ea %3d; Invalid packet state %s (%d)\n",
            (int) MyEtherAddr, PktStateString( pi->state), (int) pi->state);
        assert( 0);
        break;
    }
    trimAPID( *pID);
    if( retained == false)
    delete pID;
    return;
}

/*
** Generate the path trace for the packet.
*/
template <class PLD>
void SHR<PLD>::generatePathTrace(
  packet_t    *pkt,
  bool        success)
{
  packet_t    *dupPkt = (packet_t *) copyPkt( pkt, false);
  dupPkt->hdr.info.path.AddNode( MyEtherAddr);
#ifdef VISUAL_ROUTE
  // this printf is used by the compareHC utility
  if( success == true)
    {
      printf( "# VR: %f: %3d->%3d: sn%08X: %f: %d: %s\n",SimTime(),(int) dupPkt->hdr.src_addr, (int) dupPkt->hdr.dst_addr, dupPkt->hdr.seq_number, (SimTime() - dupPkt->hdr.info.send_time), dupPkt->hdr.actual_hop, dupPkt->hdr.info.path.ToString());
    }
  
#endif //VISUAL_ROUTE
  visualizer->writePath( (int) dupPkt->hdr.src_addr, (int) dupPkt->hdr.dst_addr,
             dupPkt->hdr.seq_number, dupPkt->hdr.info.path, success);
  dupPkt->free();
  return;
}

template <class PLD>
void SHR<PLD>::process_New(
  packet_t    *pkt,
  PktInfo    *pi,
  Location    loc,
  packet_t    *outgoingPkt)
{
  double    delay;
  //  FlowInfo *fInfo = getFlow(pkt);
  
  switch( pkt->hdr.type)
    {
    case REQUEST:
      if( pkt->hdr.dst_addr == MyEtherAddr)
    {
      printf("XXX %f: DREQRX, flow: %3d->%3d\n",SimTime(),(int)pkt->hdr.src_addr,(int)pkt->hdr.dst_addr);
      printf("%03d %f received from -1 04-DREQ-Dest sn%08X\n",(int)pkt->hdr.info.cur_addr,SimTime(),pkt->hdr.seq_number);
      delay = 10 * MyForwardDelay;
      setPacketState( pkt, PSListen);
    }
      else
    {
      delay = Random( MyForwardDelay * (log10((double) pkt->hdr.actual_hop) + 1));
      dbgPrint( ("proc-New: ea %3d; DREQ; sn%08X; rc %2d; @ %f + %f seconds\n",
             (int) MyEtherAddr, pkt->hdr.seq_number, pkt->refcount,
             SimTime(), delay));
      setPacketState( pkt, PSDelay);
    }
      startProtocolTimer( pi, pkt, delay, NULL, "pN1");
      break;
    case REPLY:
      if( pkt->hdr.dst_addr == MyEtherAddr)
    {
      printf("%03d %f received from -1 08-DREP-Dest sn%08X (process_NEW, DREP)\n",(int)MyEtherAddr,SimTime(),pkt->hdr.seq_number);
      delay = 1.5 * MyForwardDelay;
      //      delay = ForwardDelay;
      printf("XXX %f: DREPRX, flow: %3d->%3d\n",SimTime(),(int)pkt->hdr.src_addr,(int)pkt->hdr.dst_addr);
    }
      else
    {
      cache_t::iterator    iter = m_seq_cache.find( pkt->hdr.dst_addr);
      if( iter != m_seq_cache.end() && iter->second.noDistance() == false)
        {
          delay = Random( MyForwardDelay * (log10((double) pkt->hdr.actual_hop) + 1));
          dbgPrint( ("pN: ea %3d; DREP; sn%08X; rc %2d; @ %f + %f seconds\n",
             (int) MyEtherAddr, pkt->hdr.seq_number, pkt->refcount,
             SimTime(), delay));
        }
      else
        {
          delay = 0.0;    // destination not in cache
        }
    }
      if( delay != 0.0)
    {
      startProtocolTimer( pi, pkt, delay, NULL, "pN2");
      setPacketState( pkt, PSDelay);
    }
      break;
    case DATA:

      if (pkt->hdr.info.prev_addr == -1)
    pkt->hdr.info.prev_addr = pkt->hdr.src_addr;

      recvNewDATA( pkt, pi);
      break;
    case ACK:
      recvNewACK( pkt, pi);
      break;
    default:
      printf( "***** process_New: ea %3d; Received invalid packet type 0x%08x\n",
          (int) MyEtherAddr, pkt->hdr.type);
      assert( 0);
    }
  return;
}

template <class PLD>
bool SHR<PLD>::timer_Delay(
  PktID        *id,
  PktInfo    *pi)
{
  bool        retained;

  switch( pi->pktType)
  {
    case REQUEST:
      retained = DREQ_D_TimerRtn( id, pi);
      break;
    case REPLY:
      retained = DREP_D_TimerRtn( id, pi);
      break;
    default:
      printf( "***** timer_Delay: ea %3d; bad packet type %d\n", (int) MyEtherAddr, (int) pi->pktType);
      assert( 0);
      retained = false;
  }
  return retained;
}

template <class PLD>
bool SHR<PLD>::DREQ_D_TimerRtn(
  PktID        *id,
  PktInfo    *pi)
{
  dbgPrint( ("DREQ_DTR: ea %3d; %3d->%3d; sn%08X @ %f\n", (int) MyEtherAddr,
         (int) id->flow.src, (int) id->flow.dest, id->seqNumber,
         SimTime()));
  // The destination node doesn't use the Delay state.
  packet_t    *rp = (packet_t *) createRequestPkt( id->flow.dest,
                             getHCFromCostTable( id->flow.src)+1,
                             false);
  rp->hdr.seq_number = id->seqNumber;
  rp->hdr.src_addr = id->flow.src;
  rp->hdr.info.nextState = PSListen;
  rp->setStatsId( id->statsId);
  SendPacket( rp);
  return false;
}

template <class PLD>
bool SHR<PLD>::DREP_D_TimerRtn(
  PktID        *id,
  PktInfo    *pi)
{
  dbgPrint( ("DREP_DTR: ea %3d; %3d->%3d; sn%08X @ %f\n", (int) MyEtherAddr,
         (int) id->flow.src, (int) id->flow.dest, id->seqNumber,
         SimTime()));

  unsigned int    sDistance = getHCFromCostTable( id->flow.src);

  if( id->flow.dest == MyEtherAddr)
  {
      CheckBuffer( id->flow.src, sDistance);
      setPacketState( *id, PSIgnore);
    // id not retained, so return false
    return false;
  }

  // intermediate node: forward DREP
  packet_t    *rp = (packet_t *) createReplyPkt( id->flow.dest,
                           getHCFromCostTable( id->flow.dest),
                           id->seqNumber);
  rp->hdr.src_addr = id->flow.src;
  rp->hdr.actual_hop = sDistance+1;
  rp->hdr.info.nextState = PSListen;
  rp->setStatsId( id->statsId);
  SendPacket( rp);
  // id retained, so return true
  return true;
}

template <class PLD>
bool SHR<PLD>::timer_Listen(
  PktID        *id,
  PktInfo    *pi)
{
  bool        retained;

  switch( pi->pktType)
  {
    case REQUEST:
      retained = DREQ_L_TimerRtn( id, pi);
      break;
    case REPLY:
      retained = DREP_L_TimerRtn( id, pi);
      break;
    default:
      printf( "***** timer_Listen: ea %3d; bad packet type %d\n",
          (int) MyEtherAddr, (int) pi->pktType);
      assert( 0);
      retained = false;
  }
  return retained;
}

template <class PLD>
bool SHR<PLD>::DREQ_L_TimerRtn(
  PktID        *id,
  PktInfo    *pi)
{
  dbgPrint( ("DREQ_LTR: ea %3d; %3d->%3d; sn%08X @ %f\n", (int) MyEtherAddr,
         (int) id->flow.src, (int) id->flow.dest, id->seqNumber,
         SimTime()));
  if( id->flow.dest == MyEtherAddr)
  {
    packet_t    *rp = (packet_t *) createReplyPkt( id->flow.src,
                           getHCFromCostTable( id->flow.src),
                           ++m_seq_number);
    //Visualization
    printf("%03d %f sending to -1 05-DREP-Orig sn%08X (DREQ_L_TimerRtn, DREP)\n",(int)MyEtherAddr,SimTime(),rp->hdr.seq_number);
    printf("XXX %f: DREPTX, flow: %3d->%3d\n",SimTime(),(int)id->flow.src,(int)id->flow.dest);

    setPacketState( rp, PSIgnore);
#ifdef VISUAL_ROUTE
    // this printf is used by the compareHC utility
    printf( "# ASN: sn%08X; %s; @ %f\n", rp->hdr.seq_number, rp->hdr.getType(),
      SimTime());
#endif // VISUAL_ROUTE
    SendPacket( rp);
  }
  setPacketState( *id, PSIgnore);
  return false;
}

template <class PLD>
bool SHR<PLD>::DREP_L_TimerRtn(
  PktID        *id,
  PktInfo    *pi)
{
  dbgPrint( ("DREP_LTR: ea %3d; %3d->%3d; sn%08X @ %f\n", (int) MyEtherAddr,
         (int) id->flow.src, (int) id->flow.dest, id->seqNumber,
         SimTime()));
  setPacketState( *id, PSIgnore);
  return false;
}

template <class PLD>
void SHR<PLD>::process_Listen(
  packet_t    *pkt,
  PktInfo    *pi,
  Location    loc,
  packet_t    *outgoingPkt,
  bool        costTableUpdated)
{
  PktID        *id;

  switch( pkt->hdr.type)
  {
      case REQUEST:
      //Visualization
      printf("%03d %f received from -1 02-DREQ-Recv sn%08X (process_listen, DREQ)\n",(int)MyEtherAddr,SimTime(),pkt->hdr.seq_number);

      id = makePktID( pkt);
      if( pkt->hdr.dst_addr == MyEtherAddr)
      {
          CancelTimer( *id);
          // pass id to Timer
          startProtocolTimer( pi, id, 10.0 * MyForwardDelay, NULL, "pL1");
          // stay in state Listen
      }
      else    // intermediate node
      {
          if( costTableUpdated == true)
          {
          CancelTimer( *id);
          // pass id to Timer
          startProtocolTimer( pi, id, Random( MyForwardDelay * (log10((double) pkt->hdr.actual_hop) + 1)), 
                      NULL, "pL2");
          setPacketState( *id, PSDelay);
          }
          else
          delete id;
      }
      break;
      case REPLY:
      //Visualization
    printf("%03d %f received from -1 06-DREP-Recv sn%08X (process_listen, DREQ)\n",(int)MyEtherAddr,SimTime(),pkt->hdr.seq_number);
      // must be an intermediate node
      if( costTableUpdated == true)
      {
          id = makePktID( pkt);
          CancelTimer( *id);
          // pass id to Timer
          startProtocolTimer( pi, id, Random( MyForwardDelay * (log10((double) pkt->hdr.actual_hop) + 1)), 
                  NULL, "pL3");
          setPacketState( *id, PSDelay);
      }
      break;
      case DATA:
      case ACK:
      printf( "***** process_Listen: ea %3d; DATA/ACK packet\n", (int) MyEtherAddr);
      assert( 0);
    default:
      printf( "***** process_Listen: ea %3d; Received invalid packet type %s\n", (int) MyEtherAddr,
          pkt->hdr.getType());
      assert( 0);
  }
  return;
}

template <class PLD>
void SHR<PLD>::process_Ignore(
  packet_t    *pkt,
  PktInfo    *pi,
  Location    loc,
  packet_t    *outgoingPkt)
{
  switch( pkt->hdr.type)
  {
    case REQUEST:
    break;
    case REPLY:
    break;
      case ACK:
      break;
      case DATA:
      if( MyEtherAddr == pkt->hdr.dst_addr)
      {
          stats->appReceive( pkt->getStatsId(),
                 SimTime() - pkt->hdr.info.send_time,
                 pkt->hdr.actual_hop);
          generatePathTrace( pkt, true);
      }
    break;
      default:
      printf( "***** process_Ignore: ea %3d; Received invalid packet type 0x%08x\n", (int) MyEtherAddr,
          pkt->hdr.type);
      assert( 0);
      break;
  }
  return;
}

template <class PLD>
unsigned int SHR<PLD>::getHCFromCostTable(
  int        addr)
{
  cache_t::iterator    iter;
  ether_addr_t        ea = addr;

  iter = m_seq_cache.find( ea);
  if( iter == m_seq_cache.end() || iter->second.noDistance() == true)
    return 999;
  return iter->second.hopCount();
}

/*
** Receive notification of receive-receive collisions. This must be defined
** since the cxx compiler doesn't support #ifdef.
*/
template <class PLD>
void SHR<PLD>::from_mac_recv_recv_coll(
  packet_t    *pkt1,
  packet_t    *pkt2)
{
  dbgPrint( ("fmrrc: ea %3d; sn%08X (%3d) & sn%08X (%3d) @ %f\n",
         (int) MyEtherAddr, pkt1->hdr.seq_number, (int) pkt1->hdr.info.cur_addr,
         pkt2->hdr.seq_number, (int) pkt2->hdr.info.cur_addr, SimTime()));
  return;
}

/*
** Locate the packet identified by the pair (src ID, seq #). The outgoing
** packet could be:
** 1. On the send queue awaiting access to the MAC layer.
** 2. The packet being transmitted by the MAC layer (m_active_packet).
** 3. Not found.
*/
template <class PLD>
Location SHR<PLD>::locatePacket(
  ether_addr_t    &src,
  unsigned int    seqNumber,
  packet_t    **outgoingPkt)
{
  packet_queue_t::iterator iter;

  // Look for the packet in m_send_queue.
  for( iter = m_send_queue.begin(); iter != m_send_queue.end(); iter++)
    if( (*iter)->hdr.src_addr == src && (*iter)->hdr.seq_number == seqNumber)
      break;
  if( iter != m_send_queue.end())
  {
    *outgoingPkt = (*iter);
    return LocSendQueue;
  }

  // Check the packet being transmitted by the MAC layer.
  if( m_mac_busy == true)
  {
    if( m_active_packet != NULL && m_active_packet->hdr.src_addr == src &&
    m_active_packet->hdr.seq_number == seqNumber)
    {
      *outgoingPkt = m_active_packet;
      return LocActivePacket;
    }
  }

  return LocNotFound;
}

/*
** Cancel the specified packet from the specified location.
*/
template <class PLD>
void SHR<PLD>::Cancel(
  packet_t    *incomingPkt,
  Location    location,
  packet_t    *outgoingPkt)
{
  if( location == LocNotFound)    // waiting for delay timer to expire
    CancelTimer( incomingPkt);
  else    // outgoing packet on its way to the MAC layer
    CancelPacket( outgoingPkt, location);
  return;
}

template <class PLD>
void SHR<PLD>::CancelPacket(
  packet_t    *outgoingPkt,
  Location    location)
{
  packet_queue_t::iterator iter;

#ifdef    DEBUG_PRINT
  if( location != LocNotFound)
    dbgPrint( ("CP: ea %3d; %p; sn%08X; %s; %f\n", (int) MyEtherAddr,
           outgoingPkt, outgoingPkt->hdr.seq_number,
           LocationString( location), SimTime()));
#endif    // DEBUG_PRINT

  switch( location)
  {
    case LocSendQueue:
      m_send_queue.remove( outgoingPkt);
      outgoingPkt->free();
      break;
    case LocActivePacket:
      m_active_packet->hdr.info.canceled = true;
      cancel();
      break;
    case LocNotFound:
      break;
  }
  return;
}

/*
** Cancel the timer associated with the specified packet.
*/
template <class PLD>
void SHR<PLD>::CancelTimer(
  packet_t    *pkt)
{
  PktID        *id = makePktID( pkt);

  CancelTimer( *id);
  delete id;
  return;
}

template <class PLD>
void SHR<PLD>::CancelTimer(
  PktID        &id)
{
  PktInfo    *pi = getPacketState( id);

  if( pi->timerActive == true)
  {
    Protocol_Timer.Cancel( pi->timerIndex);
    pi->timerActive = false;
    if( pi->savedData != NULL)
    {
      ((packet_t *) pi->savedData)->free();
      pi->savedData = NULL;
    }
  }
  return;
}

/*
** If there are packets waiting for the MAC, send the first one.
*/
template <class PLD>
void SHR<PLD>::sendNextPkt()
{
  if( m_send_queue.size() != 0)
  {
    m_active_packet = m_send_queue.front();
    m_send_queue.pop_front();
    dbgPrint( ("sNP: ea %3d; sn%08X; sending from queue\n", (int) MyEtherAddr,
           m_active_packet->hdr.seq_number));
    m_active_packet->inc_ref();
    SendToMac( m_active_packet);
  }
  else
  {
    m_mac_busy = false;
    m_active_packet = NULL;
  }
  return;
}

template <class PLD>
void SHR<PLD>::CheckBuffer(
  ether_addr_t    src_addr,
  unsigned int    expected_hop)
{
  packet_queue_t::iterator db_iter;
  db_iter = m_data_buffer.begin();
  while(db_iter != m_data_buffer.end())
  {
    if( (*db_iter)->hdr.dst_addr == src_addr)
    {
      packet_queue_t::iterator t=db_iter;
      db_iter++;
      (*t)->hdr.expected_hop = expected_hop;
#ifdef    USE_MAX_HOP
      (*t)->hdr.max_hop = hopCountStretch( expected_hop);
#endif    // USE_MAX_HOP
      SendPacket( *t);
      m_data_buffer.erase( t);
    }
    else
      db_iter++;
  }
  return;
}

template <class PLD>
const char *SHR_Struct<PLD>::hdr_struct::getType() const 
{ 
  const char    *str;
  static char    pkt_type[][10] = { "REQ", "REPLY", "DATA", "ACK"};

  switch( type)
  {
    case REQUEST:
    case REPLY:
    case DATA:
    case ACK:
      str = pkt_type[type];
      break;
    default:
      printf( "***** Attempting to print type of invalid packet %p, sn%08X\n",
          this, seq_number);
      str = NULL;
      assert( 0);
      break;
  }
  return str;
}

template <class PLD>
bool SHR_Struct<PLD>::hdr_struct::dump(
  std::string    &str) const 
{ 
  char        buffer[40];

  sprintf( buffer, "%s %d->%d sn%08X %d", getType(), (int) src_addr,
       (int) dst_addr, seq_number, info.size); 
  str = buffer;
  return type == DATA;
}

const char *LocationString(
  Location    loc)
{
  const char    *str;
  switch( loc)
  {
    case LocNotFound:
      str = "NotFound";
      break;
    case LocSendQueue:
      str = "SendQueue";
      break;
    case LocActivePacket:
      str = "ActivePkt";
      break;
    default:
      str = "error";
      break;
  }
  return str;
}

#endif /*_shr_common_h_*/
e aqui o codigo do shr.h
/* $CVSHeader: sense/code/net/shr.h,v 1.15.2.2.4.1 2007/09/05 10:55:57 chris Exp $ */

/*************************************************************************
 *   @<title> Self Healing Routing </title>@
 *
 *   @<!-- Copyright 2007, 2006, 2003 Mark Lisee, Boleslaw K. Szymanski and
 *   Rensselaer Polytechnic Institute. All worldwide rights reserved.  A
 *   license to use, copy, modify and distribute this software for
 *   non-commercial research purposes only is hereby granted, provided that
 *   this copyright notice and accompanying disclaimer is not modified or
 *   removed from the software.
 *
 *   DISCLAIMER: The software is distributed "AS IS" without any express or
 *   implied warranty, including but not limited to, any implied warranties of
 *   merchantability or fitness for a particular purpose or any warranty of
 *   non-infringement of any current or pending patent rights. The authors of
 *   the software make no representations about the suitability of this
 *   software for any particular purpose. The entire risk as to the quality
 *   and performance of the software is with the user. Should the software
 *   prove defective, the user assumes the cost of all necessary servicing,
 *   repair or correction. In particular, neither Rensselaer Polytechnic
 *   Institute, nor the authors of the software are liable for any indirect,
 *   special, consequential, or incidental damages related to the software,
 *   to the maximum extent the law permits.-->@
 *
 *************************************************************************/

#ifndef _shr_h_
#define _shr_h_
#include <map>
#include <list>

#define    USE_IGNORE_COUNT
#define    USE_MAX_HOP

#include "shr-APID.h"

template <class PLD>
struct SHR_Struct
{
  // BASE: type + src_addr + dst_addr + seq_number
  enum { BASE_SIZE = sizeof( PktType) + 2 * ether_addr_t::LENGTH + sizeof( unsigned int) };
  // DREQ: base + actHop
  enum { REQUEST_SIZE = BASE_SIZE + sizeof( unsigned int) };
  // DREP: base + actHop + expHop
  enum { REPLY_SIZE = BASE_SIZE + 2 * sizeof( unsigned int) };
  // DATA: base + actHop + expHop + maxHop
  enum { DATA_SIZE = BASE_SIZE + 3 * sizeof( unsigned int) };
  // ACK: base
  enum { ACK_SIZE = BASE_SIZE };
    
  struct hdr_struct
  {
    /*
    ** This is information that is included in the transmitted packet.
    */
    PktType        type;
    ether_addr_t    src_addr;
    ether_addr_t    dst_addr;
    unsigned int    seq_number;
    unsigned int    actual_hop;
    unsigned int    expected_hop;
    unsigned int    max_hop;
    /*
    ** This is information that the simulation knows about the packet but does
    ** not get transmitted.
    */
    struct info
    {
      ether_addr_t    cur_addr;
      unsigned int    size;
      double        send_time;
      double        delay;
      bool        canceled;
      bool        newSeqNumber;
      PktState        nextState;
      Path        path;
    } info;
    bool        dump( std::string &str) const;
    const char        *getType() const;
  };
  typedef PLD    payload_t;
  typedef smart_packet_t<hdr_struct,PLD>    packet_t;
};

template <class PLD>
component SHR : public TypeII, public SHR_Struct<PLD>
{
 public:
  void        Start();
  void        Stop();
  void        initStats( NodeStats *s)    { stats = s; }
        SHR();
  virtual    ~SHR();
  unsigned int    getHCFromCostTable( int addr);
  void        dumpStatic( FILE *fp, int indent, int offset) const;
  void        dumpCacheStatic( FILE *fp, int indent, int offset) const;
  void        dump( FILE *fp, int indent, int offset) const;
  void        dumpCostTable( FILE *fp, int indent, int offset) const;
  static void    setVisualizer( Visualizer *ptr);
  static void    setIgnoreCount( int);
  static int    getIgnoreCount();
  static void    setRetryMultiplier( double t)    { retryMultiplier = t; }
 private:
  static int    baseIgnoreCount;
  static double    retryMultiplier;

 public:
  void        setEtherAddr( ether_addr_t addr){ MyEtherAddr = addr; }
  void        setForwardDelay( simtime_t t)    { ForwardDelay = t; }
  void        enableDumpPackets()        { DumpPackets = true; }
  void        disableDumpPackets()        { DumpPackets = false; }
  void        setRXThresh( double thresh)    { RXThresh = thresh; }
 private:
  ether_addr_t    MyEtherAddr;
  simtime_t    ForwardDelay;
  double    RXThresh;
  bool        DumpPackets;
  bool        m_mac_busy;
  unsigned int    m_seq_number;        // current sequence number
    
  // statistics
  NodeStats    *stats;
  static Visualizer    *visualizer;

 public:
  // connections to the application layer
  inport inline void    from_transport( payload_t& pld, ether_addr_t& dst,
                    unsigned int size);
  outport void        to_transport( payload_t &pld);

  // connections to the MAC layer
  inport inline void    from_mac_data( packet_t* pkt, double power);
  inport inline void    from_mac_ack( bool errflag);
  inport inline void    from_mac_recv_recv_coll( packet_t *pkt1, packet_t *pkt2);
  outport void        cancel();
  outport void        to_mac( packet_t *pkt, unsigned int statsId,
                unsigned int size, double backoff);

  // This is called when the power manager wakes up the node.
  inport void        from_pm_node_up();

  // This is called to get the battery's remaining energy.
  outport double    getBatteryPower();

 private:
  Location    locatePacket( ether_addr_t &src, unsigned int seqNumber,
                  packet_t **outgoingPkt);
  bool        updateHopCountInCache( ether_addr_t src, unsigned int hopCount,
                       unsigned int seqNumber);
  unsigned int    increaseHopCountInCache( ether_addr_t src,
                     unsigned int seqNumber);
  void        Cancel( packet_t *incoming, Location, packet_t *outgoing);
  void        CancelPacket( packet_t *pkt, Location);
  void        CancelTimer( packet_t *pkt);
  void        CancelTimer( PktID &id);
  void        SendPacket( packet_t *pkt);
  void        SendToMac( packet_t *pkt);
  void        sendNextPkt();
  void        SendEmptyData( ether_addr_t src, unsigned int seq_number,
                   int actual_hop, ether_addr_t cur);
  void        CheckBuffer( ether_addr_t, unsigned int);
  void        AddToSendQueue( packet_t *pkt);
  
  // these return pointers to packet_t, but I wasn't able to figure out
  // how to avoid compiler errors, therefore cast them to void *
  void        *createBasicPkt( ether_addr_t dst, bool newStatsId);
  void        *createDataPkt( payload_t &pld, ether_addr_t dst,
                unsigned int size);
  void        *createRequestPkt( ether_addr_t dst, int hopCount,
                   bool newSeqNumber);
  void        *createReplyPkt( ether_addr_t src, unsigned int expectedHop,
                 unsigned int seqNumber);
  void        *createAckPkt( ether_addr_t src, ether_addr_t dest,
                   unsigned int seq_number);
  void        *copyPkt( packet_t *pkt, bool incAHC);

  typedef std::list<packet_t* > packet_queue_t;

  // Data structures and methods for the Active Packet IDs (APIDs)

 public:
  InfiTimer <PktID *> Protocol_Timer;
  void        startProtocolTimer( PktInfo *pi, packet_t *pkt, double delay, void *data, char *str);
  void        startProtocolTimer( PktInfo *pi, PktID *, double delay, void *data, char *str);
  inport inline void    timerExpired( PktID *id, unsigned int index);

 private:
  PktID        *makePktID( packet_t *);
  bool        FlowIDCompare( FlowID a, FlowID b);
  FlowInfo    *getFlow( packet_t *pkt);
  FlowInfo    *getFlow( FlowID &id);
  PktInfo    *getPacketState( PktID &id);
  PktInfo    *getPacketState( packet_t *);
  PktInfo    *getPacketState( packet_t *, Location &, packet_t *&pkt);
  void        setPacketState( packet_t *pkt, PktState);
  void        setPacketState( PktID &id, PktState);
  void        trimAPID( packet_t *);
  void        trimAPID( PktID &id);
  void        updateSLRPL( packet_t *);
  FlowIDMap    APIDs;

  class seq_number_t
  {
  public:
    seq_number_t () : state( Initial), current(0), bits(0u) {}
    unsigned int    hopCount() const;
    void        hopCount( unsigned int newHC);
    bool        noDistance() const    { return state != Steady; }
    void        dumpStatic( FILE *fp, int indent, int offset) const;
    void        dump( FILE *fp, int dest, int indent, int offset) const;
  private:
    unsigned int    currentHC;
    enum { Initial, Steady}    state;
    int        current;
    uint32_t    bits;
  };

/*******************************************************************
 * For each source, we must maintain a @seq_number_t@ object, so we better
 * keep them in a map.
 *******************************************************************/

  typedef std::map<ether_addr_t, seq_number_t, ether_addr_t::compare> cache_t;
  cache_t    m_seq_cache;
  packet_queue_t m_data_buffer;    
 private:
  void        recvNewDATA( packet_t *, PktInfo *);
  void        recvNewACK( packet_t *, PktInfo *);
  void        process_Energy( packet_t *pkt, PktInfo *, Location,
                packet_t *outgoingPkt);
  void        process_Father( packet_t *pkt, PktInfo *, Location,
                packet_t *outgoingPkt);
  void        process_Ignore( packet_t *pkt, PktInfo *, Location,
                packet_t *outgoingPkt);
  void        process_Listen( packet_t *pkt, PktInfo *, Location,
                packet_t *outgoingPkt, bool costTableUpdated);
  void        process_New( packet_t *pkt, PktInfo *, Location, packet_t *);
  void        process_Owner( packet_t *pkt, PktInfo *, Location,
                   packet_t *outgoingPkt);
  void        process_Possible( packet_t *pkt, PktInfo *, Location,
                  packet_t *outgoingPkt);
  void        process_Resend( packet_t *pkt, PktInfo *, Location,
                packet_t *outgoingPkt);
  void        process_SendAck( packet_t *pkt, PktInfo *, Location,
                 packet_t *outgoingPkt);
  void        process_Wait( packet_t *pkt, PktInfo *, Location,
                  packet_t *outgoingPkt);
  bool        timer_Delay( PktID *id, PktInfo *pi);
  bool        timer_Energy( PktID *id, PktInfo *pi);
  bool        timer_Father( PktID *id, PktInfo *pi);
  bool        timer_Listen( PktID *id, PktInfo *pi);
  bool        timer_Owner( PktID *id, PktInfo *pi);
  bool        timer_Possible( PktID *id, PktInfo *pi);
  bool        timer_Resend( PktID *id, PktInfo *pi);
  bool        timer_SendAck( PktID *id, PktInfo *pi);
  bool        timer_Wait( PktID *id, PktInfo *pi);
  bool        DREQ_D_TimerRtn( PktID *id, PktInfo *pi);
  bool        DREQ_L_TimerRtn( PktID *id, PktInfo *pi);
  bool        DREP_D_TimerRtn( PktID *id, PktInfo *pi);
  bool        DREP_L_TimerRtn( PktID *id, PktInfo *pi);
  void        generatePathTrace( packet_t *pkt, bool success);
  static bool    doubleMsgPrinted;
  packet_t    *m_active_packet;
  packet_queue_t m_send_queue;
};

template <class PLD> int    SHR<PLD>::baseIgnoreCount = 0;
template <class PLD> double    SHR<PLD>::retryMultiplier = 1.5;

unsigned int hopCountStretch(
  unsigned int    hc)
{
//#error    select a ratio of max hop to expected hop
//#ifdef    SimulateExperimentalResults
/*
** Use this when simulating the experimental topologies. The three path
** topology requires that the maximum hop be large enough to allow packets to
** use the longer path.
*/
  return 3 * hc;
//#else
/*
** Use this for the large scale simulations. These topologies are dense enough
** so that when one node dies, only one or two extra hops are required to get
** around the break.
*/
/*  unsigned int    stretch = hc;
  for(; hc != 0; hc >>= 1)
    stretch++;
  return stretch;
  #endif*/
}

#include "shr-common.h"

template <class PLD>
void *SHR<PLD>::copyPkt(
  packet_t    *pkt,
  bool        incAHC)
{
  packet_t    *newPkt = pkt->copy();

  associateWithPacket( newPkt, newPkt->hdr.seq_number);
  newPkt->hdr.info.cur_addr = MyEtherAddr;
  if( incAHC == true)
    newPkt->hdr.actual_hop++;
  newPkt->hdr.info.newSeqNumber = false;
  newPkt->hdr.info.path.AddNode( MyEtherAddr);
  return (void *) newPkt;
}

template <class PLD>
void *SHR<PLD>::createBasicPkt(
  ether_addr_t    dest,
  bool        newStatsId)
{
  packet_t    *newPkt = packet_t::alloc();

  if( newStatsId == true)
    newPkt->newStatsId();
  // type, ack_type, addresses set by caller
  newPkt->hdr.seq_number = 0xFFFFFFFF;
  // size set by caller
  newPkt->hdr.info.send_time = SimTime();    // used to compute end-to-end delay
  newPkt->hdr.actual_hop = 1;
  newPkt->hdr.expected_hop = 0;
  newPkt->hdr.max_hop = 1;
  newPkt->hdr.info.canceled = false;
  newPkt->hdr.info.newSeqNumber = false;
  newPkt->hdr.src_addr = MyEtherAddr;
  newPkt->hdr.dst_addr = dest;
  newPkt->hdr.info.cur_addr = MyEtherAddr;
  newPkt->hdr.info.nextState = PSNone;
  return newPkt;
}

/*
** There is no delay when the source sends an packet.
** This method isn't called when a packet is forwarded. Instead, the incoming
** packet is copied to make the outgoing packet.
*/
template <class PLD>
void *SHR<PLD>::createDataPkt(
  payload_t    &pld,
  ether_addr_t    dst,
  unsigned int    size)
{
  packet_t    *newPkt = (packet_t *) createBasicPkt( dst, true);

  newPkt->hdr.type = DATA;
  newPkt->hdr.info.size = size + DATA_SIZE;
  newPkt->hdr.info.newSeqNumber = true;
  newPkt->pld = pld;
  return newPkt;
}

template <class PLD>
void *SHR<PLD>::createAckPkt(
  ether_addr_t    src,
  ether_addr_t    dest,
  unsigned int    seq_number)
{
  packet_t    *newPkt = (packet_t *) createBasicPkt( dest, false);

  newPkt->hdr.type = ACK;
  newPkt->hdr.info.size = ACK_SIZE;
  newPkt->hdr.src_addr = src;
  newPkt->hdr.seq_number = seq_number;
  return newPkt;
}

template <class PLD>
void SHR<PLD>::setIgnoreCount(
  int        st)
{
  baseIgnoreCount = st;
  return;
}

template <class PLD>
int SHR<PLD>::getIgnoreCount()
{
  return baseIgnoreCount;
}

/*
** Sending a packet from the network (SHR) layer to the MAC layer consists of
** calling two methods in order:
** 1. SendPacket: If applicable, assign a sequence number to the packet. Either
**    forward the packet to SendToMac (MAC is idle) or put it on the send
**    queue. from_mac_ack will remove packets from the send queue and forward
**    them to SendToMac.
** 2. SendToMac: Set the MAC busy flag and forward the packet to to_mac.
*/

/*
** Schedule a packet for transmission.
** If the packet doesn't have a delay, send the packet to the MAC layer.
** Otherwise, add the packet to the list of delayed packets and schedule an
** event for the end of the packet's delay.
*/
template <class PLD>
void SHR<PLD>::SendPacket(
  packet_t    *pkt)
{
  pkt->hdr.info.canceled = false;
  if( pkt->hdr.info.newSeqNumber == true && pkt->hdr.src_addr == MyEtherAddr)
  {
    m_seq_number++;
    pkt->hdr.seq_number = m_seq_number;
    associateWithPacket( pkt, m_seq_number);
#ifdef VISUAL_ROUTE
    // this printf is used by the compareHC utility
    printf( "ASN: sn%08X; max hop %d; %s; @ %f\n", pkt->hdr.seq_number,
        pkt->hdr.max_hop, pkt->hdr.getType(), SimTime());
#endif // VISUAL_ROUTE
    // The only time a new sequence # is assigned, it is by the source node.
    // The DREQ and SHR-M automata require that the state become Ignore after
    // transmitting the packet. (DREP sequence #s are assigned elsewhere.)
    switch( pkt->hdr.type)
    {
      case REQUEST:
    setPacketState( pkt, PSIgnore);
    break;
      case DATA:
    setPacketState( pkt, PSPossible);
    pkt->hdr.info.nextState = PSOwner;
    break;
      case REPLY:
      default:
    printf( "***** SendPacket: Assigned sequence # to packet type %s\n",
        pkt->hdr.getType());
    assert( 0);
    }
    PktInfo    *pi = getPacketState( pkt);
    pi->pktType = pkt->hdr.type;
  }
  pkt->hdr.info.newSeqNumber = false;
  dbgPrint( ("SP: ea %3d; sending %s; sn%08X; actHC %2d; rc %2d\n",
         (int) MyEtherAddr, pkt->hdr.getType(), pkt->hdr.seq_number,
         pkt->hdr.actual_hop, pkt->refcount));

  if( m_mac_busy == false)
  {
    m_mac_busy = true;
    m_active_packet = pkt;
    m_active_packet->inc_ref();
    SendToMac( pkt);
  }
  else
  {
    dbgPrint( ("SP: ea %3d; sn%08X; mac busy, delaying\n", (int) MyEtherAddr,
           pkt->hdr.seq_number));
    AddToSendQueue( pkt);
  }
  return;
}

/*
** The inport that is called when the power manager wakes the node up.
** If the configuration file is in use, this must be defined, but it must not
** do anything
*/
template <class PLD>
void SHR<PLD>::from_pm_node_up()
{
  return;
}

template <class PLD>
void SHR<PLD>::recvNewDATA(
  packet_t    *pkt,
  PktInfo    *pi)
{
  // packet at destination
  if( MyEtherAddr == pkt->hdr.dst_addr)
  {
    // update stats
    stats->appReceive( pkt->getStatsId(), SimTime() - pkt->hdr.info.send_time,
               pkt->hdr.actual_hop);
    // Send the incoming packet to the application
    pkt->inc_pld_ref();
    to_transport( pkt->pld);
    generatePathTrace( pkt, true);

    // send ACK< S, D, s#>
    packet_t    *ackPkt = (packet_t *) createAckPkt( pkt->hdr.src_addr, pkt->hdr.dst_addr,
                             pkt->hdr.seq_number);
    ackPkt->setStatsId( pkt->getStatsId());
    SendPacket( ackPkt);
    // goto SendAck (Safe to do so before transmission completes since there
    // is no reason to cancel ACK.)
    startProtocolTimer( pi, pkt, 10 * ForwardDelay, NULL, "rND0");
    setPacketState( pkt, PSSendAck);
    return;
  }

  // packet at intermediate

  // locate destination in cost table
  cache_t::iterator iter = m_seq_cache.find( pkt->hdr.dst_addr);
  if( iter == m_seq_cache.end() || iter->second.noDistance() == true)
  {
    dbgPrint( ("rND: ea %3d; dest %3d; sn%08X; not in cache\n",
           (int) MyEtherAddr, (int) pkt->hdr.dst_addr,
           pkt->hdr.seq_number));
    return;
  }
  unsigned int    distToDest = iter->second.hopCount();

  // if DATA< S, D, s#, *, >ld, *, *>
  // if this node is not closer, then drop the packet
  if( pkt->hdr.expected_hop <= distToDest)
  {
    dbgPrint( ("rND: ea %3d; dest %3d; sn%08X; %d <= %d\n",
           (int) MyEtherAddr, (int) pkt->hdr.dst_addr, pkt->hdr.seq_number,
           pkt->hdr.expected_hop, distToDest));
    return;
  }

/*
** This node is closer to the destination.
** If this node is supposed to be ignoring packets for this flow ID, then
** decrement the counter and ignore the packet.
*/
  FlowInfo        *fInfo = getFlow( pkt);
  if( fInfo->ignoreCount > 0)
  {
    dbgPrint( ("rND: ea %3d; dest %3d; sn%08X; ignoring %d\n",
           (int) MyEtherAddr, (int) pkt->hdr.dst_addr, pkt->hdr.seq_number,
           fInfo->ignoreCount));
    fInfo->ignoreCount--;
    generatePathTrace( pkt, false);
    setPacketState( pkt, PSIgnore);
    return;
  }
  
/*
** Forward the packet.
**
** Copy the packet, set the expected hop count, calculate the back off and
** send the packet.
*/
  double    delay = Random( ForwardDelay);
  packet_t    *fwdPkt = (packet_t *) copyPkt( pkt, true);

  dbgPrint( ("rND: ea %3d; dest %3d; sn%08X; expHC %2d; ct %2d; backoff %f\n",
         (int) MyEtherAddr, (int) fwdPkt->hdr.dst_addr,
         fwdPkt->hdr.seq_number, pkt->hdr.expected_hop, distToDest, delay));
  fwdPkt->hdr.info.delay = delay;    // for statistics
  startProtocolTimer( pi, pkt, delay, fwdPkt, "rND1");
  setPacketState( pkt, PSPossible);
  pi->savedAHC = pkt->hdr.actual_hop;
  pi->savedEHC = pkt->hdr.expected_hop;
  pi->maxHop = pkt->hdr.max_hop;
  return;
}

template <class PLD>
void SHR<PLD>::recvNewACK(
  packet_t    *pkt,
  PktInfo    *pi)
{
  setPacketState( pkt, PSIgnore);
  return;
}

template <class PLD>
bool SHR<PLD>::timer_Possible(
  PktID        *id,
  PktInfo    *pi)
{
  if( pi->pktType != DATA)
  {
    printf( "***** timer_Possible: ea %3d; packet type not DATA\n", (int) MyEtherAddr);
    assert( 0);
  }
  packet_t    *pkt = (packet_t *) pi->savedData;
  dbgPrint( ("tP: ea %3d; %3d->%3d; sn%08X @ %f\n", (int) MyEtherAddr,
         (int) pkt->hdr.src_addr, (int) pkt->hdr.dst_addr,
         pkt->hdr.seq_number, SimTime()));
  // It is safe to modify the packet, a copy was made before starting the timer
  pkt->hdr.expected_hop = getHCFromCostTable( pkt->hdr.dst_addr);
  pkt->hdr.info.nextState = PSOwner;
  SendPacket( pkt);
  // start next timer when transmission ends
  // id not retained, so return false
  return false;
}

template <class PLD>
void SHR<PLD>::process_Possible(
  packet_t    *pkt,
  PktInfo    *pi,
  Location    loc,
  packet_t    *outgoingPkt)
{
  switch( pkt->hdr.type)
  {
    case REQUEST:
    case REPLY:
      printf( "***** process_Possible: ea %3d; DREQ/DREP packet\n", (int) MyEtherAddr);
      assert( 0);
    case DATA:
      if( pkt->hdr.expected_hop < pi->savedEHC)
      {
    Cancel( pkt, loc, outgoingPkt);        // cancel timer
    startProtocolTimer( pi, makePktID( pkt), 0.25 * ForwardDelay, NULL, "pP1");
    setPacketState( pkt, PSWait);        // move to state Wait
      }
      break;
    case ACK:
      Cancel( pkt, loc, outgoingPkt);        // cancel timer
      getFlow( pkt)->ignoreCount = baseIgnoreCount;
      // goto Ignore
      setPacketState( pkt, PSIgnore);
      break;
    default:
      printf( "***** process_Possible: ea %3d; Received invalid packet type 0x%08x\n",
          (int) MyEtherAddr, pkt->hdr.type);
      assert( 0);
      break;
  }
  return;
}

template <class PLD>
bool SHR<PLD>::timer_Owner(
  PktID        *id,
  PktInfo    *pi)
{
  if( pi->pktType != DATA)
  {
    printf( "***** timer_Owner: ea %3d; packet type not DATA\n", (int) MyEtherAddr);
    assert( 0);
  }
  packet_t    *pkt = (packet_t *) pi->savedData;
  dbgPrint( ("tO: ea %3d; %3d->%3d; sn%08X @ %f\n", (int) MyEtherAddr,
         (int) pkt->hdr.src_addr, (int) pkt->hdr.dst_addr,
         pkt->hdr.seq_number, SimTime()));
  // packet passed in is a copy which may be in use by other nodes
  // create a new one that can be modified
  // do not increase the actual hop count
  packet_t    *fwdPkt = (packet_t *) copyPkt( pkt, false);
  fwdPkt->hdr.expected_hop = getHCFromCostTable( pkt->hdr.dst_addr);
  fwdPkt->hdr.info.nextState = PSResend;
  SendPacket( fwdPkt);
  pkt->free();
  return false;
}

template <class PLD>
void SHR<PLD>::process_Owner(
  packet_t    *pkt,
  PktInfo    *pi,
  Location    loc,
  packet_t    *outgoingPkt)
{
  switch( pkt->hdr.type)
  {
    case REQUEST:
    case REPLY:
      printf( "***** process_Owner: DREQ/DREP packet\n");
      assert( 0);
/*
** Receiving a DATA packet while in the Owner state is a bit different than
** other transitions since the timer is not canceled. But, there is a race
** condition. Consider the case when the timer expires, so the node sends
** another DATA packet. The node stays in the Owner state until the packet
** transmission finishes. It is possible for the transmission to be delayed
** because the MAC is receiving a packet. If this incoming packet is supposed
** to move the automaton to the Father state, it should instead cancel the
** outgoing packet and move the automaton to the Ignore state. The timer to
** move the automaton from the Father to Ignore state has already expired.
*/
    case DATA:
      if( pkt->hdr.expected_hop <= getHCFromCostTable( pkt->hdr.dst_addr))
      {
    CancelPacket( outgoingPkt, loc);    // will not cancel timer, only outgoing packet
    if( pi->timerActive == true)
      setPacketState( pkt, PSFather);
    else
      setPacketState( pkt, PSIgnore);
      }
      break;
    case ACK:
      Cancel( pkt, loc, outgoingPkt);    // cancel timer or outgoing packet
      setPacketState( pkt, PSIgnore);
      break;
    default:
      printf( "***** process_Owner: ea %3d; Received invalid packet type 0x%08x\n",
          (int) MyEtherAddr, pkt->hdr.type);
      assert( 0);
  }
  return;
}

template <class PLD>
bool SHR<PLD>::timer_Resend(
  PktID        *id,
  PktInfo    *pi)
{
  if( pi->pktType != DATA)
  {
    printf( "***** timer_Resend: ea %3d; packet type not DATA\n", (int) MyEtherAddr);
    assert( 0);
  }
  // ld += 2
  unsigned int    distToDest = increaseHopCountInCache( id->flow.dest, id->seqNumber);
  packet_t    *pkt = (packet_t *) pi->savedData;

  // if( ld+sts < smh)
  //   send DATA< S, D, s#, sts+1, ld, smh, sdata>
  // pkt->hdr.actual_hop is from the last broadcast packet (i.e. incoming+1)
  if( distToDest + pi->savedAHC-1 <= pi->maxHop)
  {
    packet_t    *fwdPkt = (packet_t *) copyPkt( pkt, false);
    fwdPkt->hdr.expected_hop = distToDest;
    SendPacket( fwdPkt);
  }
  pkt->free();
  setPacketState( *id, PSIgnore);
  return false;
}

template <class PLD>
void SHR<PLD>::process_Resend(
  packet_t    *pkt,
  PktInfo    *pi,
  Location    loc,
  packet_t    *outgoingPkt)
{
  switch( pkt->hdr.type)
  {
    case REQUEST:
    case REPLY:
      printf( "***** process_Resend: ea %3d; Received DREQ/DREP packet\n", (int) MyEtherAddr);
      assert( 0);
      break;
    case DATA:
      if( pkt->hdr.expected_hop <= getHCFromCostTable( pkt->hdr.dst_addr))
      {
    Cancel( pkt, loc, outgoingPkt);
    setPacketState( pkt, PSIgnore);
      }
      break;
    case ACK:
      Cancel( pkt, loc, outgoingPkt);
      setPacketState( pkt, PSIgnore);
      break;
    default:
      printf( "***** process_Resend: ea %3d; Received invalid packet type 0x%08x\n",
          (int) MyEtherAddr, pkt->hdr.type);
      assert( 0);
      break;
  }
  return;
}

template <class PLD>
bool SHR<PLD>::timer_SendAck(
  PktID        *id,
  PktInfo    *pi)
{
  if( pi->pktType != DATA && pi->pktType != ACK)
  {
    printf( "***** timer_SendAck: ea %3d; packet type not DATA or ACK\n", (int) MyEtherAddr);
    assert( 0);
  }
  setPacketState( *id, PSIgnore);
  return false;
}

template <class PLD>
void SHR<PLD>::process_SendAck(
  packet_t    *pkt,
  PktInfo    *,
  Location    loc,
  packet_t    *outgoingPkt)
{
  switch( pkt->hdr.type)
  {
    case DATA:
      if( MyEtherAddr == pkt->hdr.dst_addr)
      {
    stats->appReceive( pkt->getStatsId(),
               SimTime() - pkt->hdr.info.send_time,
               pkt->hdr.actual_hop);
    generatePathTrace( pkt, true);
    packet_t    *ackPkt = (packet_t *) createAckPkt( pkt->hdr.src_addr, pkt->hdr.dst_addr,
                                 pkt->hdr.seq_number);
    ackPkt->setStatsId( pkt->getStatsId());
    SendPacket( ackPkt);
      }
      else
      {
    printf( "***** process_SendAck: ea %3d; sn%08X; not at destination\n",
        (int) MyEtherAddr, pkt->hdr.seq_number);
    assert( 0);
      }
      break;
    case ACK:
      break;
    default:
      printf( "***** process_SendAck: ea %3d; Received invalid packet type %s\n",
          (int) MyEtherAddr, pkt->hdr.getType());
      assert( 0);
      break;
  }
  return;
}

template <class PLD>
bool SHR<PLD>::timer_Father(
  PktID        *id,
  PktInfo    *pi)
{
  dbgPrint( ("tF: ea %3d; %3d->%3d; sn%08X @ %f\n", (int) MyEtherAddr,
         (int) id->flow.src, (int) id->flow.dest, id->seqNumber,
         SimTime()));
  setPacketState( *id, PSIgnore);
  return false;
}

template <class PLD>
void SHR<PLD>::process_Father(
  packet_t    *pkt,
  PktInfo    *pi,
  Location    loc,
  packet_t    *outgoingPkt)
{
  switch( pkt->hdr.type)
  {
    case REQUEST:
    case REPLY:
      printf( "***** process_Father: ea %3d; Received DREQ/DREP packet\n", (int) MyEtherAddr);
      assert( 0);
    case DATA:
      if( pkt->hdr.expected_hop < getHCFromCostTable( pkt->hdr.dst_addr))
      {
    Cancel( pkt, loc, outgoingPkt);
    packet_t    *ackPkt = (packet_t *) createAckPkt( pkt->hdr.src_addr, pkt->hdr.dst_addr,
                                 pkt->hdr.seq_number);
    ackPkt->setStatsId( pkt->getStatsId());
    SendPacket( ackPkt);
    setPacketState( pkt, PSIgnore);
      }
      break;
    case ACK:
      Cancel( pkt, loc, outgoingPkt);
      setPacketState( pkt, PSIgnore);
      break;
    default:
      printf( "***** process_Father: ea %3d; Received invalid packet type 0x%08x\n",
          (int) MyEtherAddr, pkt->hdr.type);
      assert( 0);
  }
  return;
}

template <class PLD>
bool SHR<PLD>::timer_Wait(
  PktID        *id,
  PktInfo    *pi)
{
  dbgPrint( ("tW: ea %3d; %3d->%3d; sn%08X @ %f\n", (int) MyEtherAddr,
         (int) id->flow.src, (int) id->flow.dest, id->seqNumber,
         SimTime()));
  setPacketState( *id, PSIgnore);
  return false;
}

template <class PLD>
void SHR<PLD>::process_Wait(
  packet_t    *pkt,
  PktInfo    *pi,
  Location    loc,
  packet_t    *outgoingPkt)
{
  switch( pkt->hdr.type)
  {
    case REQUEST:
    case REPLY:
      printf( "***** process_Wait: ea %3d; Received DREQ/DREP packet\n", (int) MyEtherAddr);
      assert( 0);
      break;
    case DATA:
      // if DATA< S, D, s#, *, <eds, *, *>
      //   cancel timer
      //   goto Ignore
      // else
      //   NA
      if( pkt->hdr.expected_hop < pi->savedEHC)
      {
    CancelTimer( pkt);
    setPacketState( pkt, PSIgnore);
      }
      break;
    case ACK:
      // ignore = IGNORECOUNT
      getFlow( pkt)->ignoreCount = baseIgnoreCount;
      // cancel timer
      CancelTimer( pkt);
      setPacketState( pkt, PSIgnore);
      break;
    default:
      printf( "***** process_Wait: Received invalid packet type 0x%08x\n",
          pkt->hdr.type);
      assert( 0);
      break;
  }
  return;
}

template <class PLD>
void SHR<PLD>::process_Energy(
  packet_t    *pkt,
  PktInfo    *pi,
  Location    loc,
  packet_t    *outgoingPkt)
{
  printf( "***** process_Energy: ea %3d\n", (int) MyEtherAddr);
  assert( 0);
}

template <class PLD>
bool SHR<PLD>::timer_Energy(
  PktID        *id,
  PktInfo    *pi)
{
  printf( "***** timer_Energy: ea %3d\n", (int) MyEtherAddr);
  assert( 0);
}

/*
** The routing layer has received an acknowledgement from the mac layer.
** False indicates that the transmission failed.
*/
template <class PLD>
void SHR<PLD>::from_mac_ack(
  bool        success)
{
  if( m_active_packet->hdr.info.canceled == false)
  {
/*
** If the packet transmission failed, drop the packet.
*/
    if( success == false)
    {
      dbgPrint( ("fma: ea %3d; sn%08X; %s; trans failed\n", (int) MyEtherAddr,
         m_active_packet->hdr.seq_number, m_active_packet->hdr.getType()));
      stats->netSendFail( m_active_packet->getStatsId());
    }

/*
** Packet was sent successfully.
*/
    else
      stats->netSend( m_active_packet->getStatsId(), m_active_packet->hdr.info.delay);

    PktInfo    *pi = getPacketState( m_active_packet);
    double    delay;
    switch( m_active_packet->hdr.type)
    {
/*
** DATA packet sent from the Possible or Owner state require starting a timer
** after the transmission has completed. Do this even if the transmission
** failed.
*/
      case DATA:
    switch( pi->state)
    {
      case PSPossible:
      case PSOwner:
        delay = Random( 0.5 * ForwardDelay) + (retryMultiplier - 0.25) * ForwardDelay;
        m_active_packet->inc_ref();        // retain copy of packet
        startProtocolTimer( pi, m_active_packet, delay, m_active_packet, "fma1");
        break;
      case PSFather:
        printf( "***** fma: ea %3d; state Father\n", (int) MyEtherAddr);
        assert( 0);
      default:
        break;
    }
    break;
      case REPLY:
      case REQUEST:
    if( pi->state == PSDelay)        // must be intermediate node
    {
      delay = 10.0 * ForwardDelay;    // this is part of DREP automaton
      startProtocolTimer( pi, m_active_packet, delay, NULL, "fma2");
    }
    break;
      default:
    break;
    }
    // set the new state for the PktID
    setPacketState( m_active_packet, m_active_packet->hdr.info.nextState);
  }
  else
  {
    dbgPrint( ("fma: ea %3d; sn%08X; %s; canceled\n", (int) MyEtherAddr,
           m_active_packet->hdr.seq_number, m_active_packet->hdr.getType()));
  }

  m_active_packet->free();
  sendNextPkt();
  return;
}

#endif /*_shr_h_*/

Bom é isso, gostaria de agradecer desde já e de antecipação me desculpar se cometi algum erro nas regras da comunidade, espero encarecidamente a ajuda de vocês

Editado por jaderson
Link para o comentário
Compartilhar em outros sites

4 respostass a esta questão

Posts Recomendados

  • 0
Verifica os includes se estão corretos. Veja se o arquivo que contém as funções que ele dá erro se está sendo chamado.

Abraço.

Bom to fazendo isso agora olhando cada arquivo para ver se descubro de onde vem esse MyForwardDelay FIrstPacketID e por ai vai, mas enquanto isso o outro erro que da falando alguma coisa a respeito de string *char e sei lá mais o que, tipo como é que deveria ser a declaração então

Link para o comentário
Compartilhar em outros sites

  • 0
Verifica os includes se estão corretos. Veja se o arquivo que contém as funções que ele dá erro se está sendo chamado.

Abraço.

Bom to fazendo isso agora olhando cada arquivo para ver se descubro de onde vem esse MyForwardDelay FIrstPacketID e por ai vai, mas enquanto isso o outro erro que da falando alguma coisa a respeito de string *char e sei lá mais o que, tipo como é que deveria ser a declaração então

Olha achei um arquivo chamado shr-pp.h que contem todas aquelas variaveis estranhas de MyForwardDelay FIrstPacketID... e por ai vai, a seguir o arquivo que eu encontrei

/* $CVSHeader: sense/code/net/shr-pp.h,v 1.1.2.2 2007/09/05 12:03:02 chris Exp $ */



/*************************************************************************

 *   @<title> Self Healing Routing </title>@

 *

 *   @<!-- Copyright 2007, 2006, 2003 Mark Lisee, Boleslaw K. Szymanski and

 *   Rensselaer Polytechnic Institute. All worldwide rights reserved.  A

 *   license to use, copy, modify and distribute this software for

 *   non-commercial research purposes only is hereby granted, provided that

 *   this copyright notice and accompanying disclaimer is not modified or

 *   removed from the software.

 *

 *   DISCLAIMER: The software is distributed "AS IS" without any express or

 *   implied warranty, including but not limited to, any implied warranties of

 *   merchantability or fitness for a particular purpose or any warranty of

 *   non-infringement of any current or pending patent rights. The authors of

 *   the software make no representations about the suitability of this

 *   software for any particular purpose. The entire risk as to the quality

 *   and performance of the software is with the user. Should the software

 *   prove defective, the user assumes the cost of all necessary servicing,

 *   repair or correction. In particular, neither Rensselaer Polytechnic

 *   Institute, nor the authors of the software are liable for any indirect,

 *   special, consequential, or incidental damages related to the software,

 *   to the maximum extent the law permits.-->@

 *

 *************************************************************************/



#ifndef _shr_h_

#define _shr_h_

#include <map>

#include <list>

#define shr_pp



#define    USE_MAX_HOP



#include "shr-APID.h"



template <class PLD>

struct SHR_Struct

{

  // BASE: type + src_addr + dst_addr + seq_number

  enum { BASE_SIZE = sizeof( PktType) + 2 * ether_addr_t::LENGTH + sizeof( unsigned int) };

  // DREQ: base + actHop

  enum { REQUEST_SIZE = BASE_SIZE + sizeof( unsigned int) };

  // DREP: base + actHop + expHop

  enum { REPLY_SIZE = BASE_SIZE + 2 * sizeof( unsigned int) };

  // DATA: base + actHop + expHop + maxHop

  enum { DATA_SIZE = BASE_SIZE + 3 * sizeof( unsigned int) };

  // ACK: base

  enum { ACK_SIZE = BASE_SIZE };

    

  struct hdr_struct

  {

    /*

    ** This is information that is included in the transmitted packet.

    */

    PktType        type;

    ether_addr_t    src_addr;

    ether_addr_t    dst_addr;

    unsigned int    seq_number;

    unsigned int    actual_hop;

    unsigned int    expected_hop;

    unsigned int    max_hop;

    /*

    ** This is information that the simulation knows about the packet but does

    ** not get transmitted.

    */

    struct info

    {

      ether_addr_t    cur_addr;

      ether_addr_t      prev_addr;

      double            prev_x,prev_y;

      unsigned int    size;

      double        send_time;

      double        delay;

      bool        canceled;

      bool        newSeqNumber;

      PktState        nextState;

      Path        path;

    } info;

    bool        dump( std::string &str) const;

    const char        *getType() const;

  };

  typedef PLD    payload_t;

  typedef smart_packet_t<hdr_struct,PLD>    packet_t;

};



template <class PLD>

component SHR : public TypeII, public SHR_Struct<PLD>

{

 public:

  void        Start();

  void        Stop();

  void        initStats( NodeStats *s)    { stats = s; }

        SHR();

  virtual    ~SHR();

  unsigned int    getHCFromCostTable( int addr);

  void        dumpStatic( FILE *fp, int indent, int offset) const;

  void        dumpCacheStatic( FILE *fp, int indent, int offset) const;

  void        dump( FILE *fp, int indent, int offset) const;

  void        dumpCostTable( FILE *fp, int indent, int offset) const;

  static void    setVisualizer( Visualizer *ptr);

  static void    setRetryMultiplier( double t)    { retryMultiplier = t; }

 private:

  static double    retryMultiplier;



 public:

  void        setEtherAddr( ether_addr_t addr){ MyEtherAddr = addr; }

  void          setPosition (double x, double y) { MyX = x; MyY = y; }

  void        setForwardDelay( simtime_t t)    { ForwardDelay = t; }

  void          setMyForwardDelay( simtime_t t) { MyForwardDelay = t; }

  void          incNumNeighbors ()              { NumNeighbors ++; }

  void          setFirstPacketID (unsigned int p) { FirstPacketID = p; }

  void        enableDumpPackets()        { DumpPackets = true; }

  void        disableDumpPackets()        { DumpPackets = false; }

  void        setRXThresh( double thresh)    { RXThresh = thresh; }

 private:

  ether_addr_t    MyEtherAddr;

  double        MyX, MyY;

  simtime_t    ForwardDelay;

  simtime_t     MyForwardDelay;

  int           NumNeighbors;

  int           FirstPacketID;

  double    RXThresh;

  bool        DumpPackets;

  bool        m_mac_busy;

  unsigned int    m_seq_number;        // current sequence number

    

  // statistics

  NodeStats    *stats;

  static Visualizer    *visualizer;



 public:

  // connections to the application layer

  inport inline void    from_transport( payload_t& pld, ether_addr_t& dst,

                    unsigned int size);

  outport void        to_transport( payload_t &pld);



  // connections to the MAC layer

  inport inline void    from_mac_data( packet_t* pkt, double power);

  inport inline void    from_mac_ack( bool errflag);

  inport inline void    from_mac_recv_recv_coll( packet_t *pkt1, packet_t *pkt2);

  outport void        cancel();

  outport void        to_mac( packet_t *pkt, unsigned int statsId,

                unsigned int size, double backoff);



  // This is called when the power manager wakes up the node.

  inport void        from_pm_node_up();



  // This is called to get the battery's remaining energy.

  outport double    getBatteryPower();



 private:

  Location    locatePacket( ether_addr_t &src, unsigned int seqNumber,

                  packet_t **outgoingPkt);

  bool        updateHopCountInCache( ether_addr_t src, unsigned int hopCount,

                       unsigned int seqNumber);

  unsigned int    increaseHopCountInCache( ether_addr_t src,

                     unsigned int seqNumber);

  void        Cancel( packet_t *incoming, Location, packet_t *outgoing);

  void        CancelPacket( packet_t *pkt, Location);

  void        CancelTimer( packet_t *pkt);

  void        CancelTimer( PktID &id);

  void        SendPacket( packet_t *pkt);

  void        SendToMac( packet_t *pkt);

  void        sendNextPkt();

  void        SendEmptyData( ether_addr_t src, unsigned int seq_number,

                   int actual_hop, ether_addr_t cur);

  void        CheckBuffer( ether_addr_t, unsigned int);

  void        AddToSendQueue( packet_t *pkt);

  

  // these return pointers to packet_t, but I wasn't able to figure out

  // how to avoid compiler errors, therefore cast them to void *

  void        *createBasicPkt( ether_addr_t dst, bool newStatsId);

  void        *createDataPkt( payload_t &pld, ether_addr_t dst,

                unsigned int size);

  void        *createRequestPkt( ether_addr_t dst, int hopCount,

                   bool newSeqNumber);

  void        *createReplyPkt( ether_addr_t src, unsigned int expectedHop,

                 unsigned int seqNumber);

  void        *createAckPkt( ether_addr_t src, ether_addr_t dest,

                   unsigned int seq_number);

  void        *copyPkt( packet_t *pkt, bool incAHC);



  typedef std::list<packet_t* > packet_queue_t;



  // Data structures and methods for the Active Packet IDs (APIDs)



 public:

  InfiTimer <PktID *> Protocol_Timer;

  void        startProtocolTimer( PktInfo *pi, packet_t *pkt, double delay, void *data, char *str);

  void        startProtocolTimer( PktInfo *pi, PktID *, double delay, void *data, char *str);

  inport inline void    timerExpired( PktID *id, unsigned int index);



 private:

  PktID        *makePktID( packet_t *);

  bool        FlowIDCompare( FlowID a, FlowID b);

  FlowInfo    *getFlow( packet_t *pkt);

  FlowInfo    *getFlow( FlowID &id);

  PktInfo    *getPacketState( PktID &id);

  PktInfo    *getPacketState( packet_t *);

  PktInfo    *getPacketState( packet_t *, Location &, packet_t *&pkt);

  void        setPacketState( packet_t *pkt, PktState);

  void        setPacketState( PktID &id, PktState);

  void        trimAPID( packet_t *);

  void        trimAPID( PktID &id);

  void        updateSLRPL( packet_t *);

  FlowIDMap    APIDs;



  class seq_number_t

  {

  public:

    seq_number_t () : state( Initial), current(0), bits(0u) {}

    unsigned int    hopCount() const;

    void        hopCount( unsigned int newHC);

    bool        noDistance() const    { return state != Steady; }

    void        dumpStatic( FILE *fp, int indent, int offset) const;

    void        dump( FILE *fp, int dest, int indent, int offset) const;

  private:

    unsigned int    currentHC;

    enum { Initial, Steady}    state;

    int        current;

    uint32_t    bits;

  };



/*******************************************************************

 * For each source, we must maintain a @seq_number_t@ object, so we better

 * keep them in a map.

 *******************************************************************/



  typedef std::map<ether_addr_t, seq_number_t, ether_addr_t::compare> cache_t;

  cache_t    m_seq_cache;

  packet_queue_t m_data_buffer;    

 private:

  void        recvNewDATA( packet_t *, PktInfo *);

  void        recvNewACK( packet_t *, PktInfo *);

  void        process_Father( packet_t *pkt, PktInfo *, Location,

                packet_t *outgoingPkt);

  void        process_Ignore( packet_t *pkt, PktInfo *, Location,

                packet_t *outgoingPkt);

  void        process_Listen( packet_t *pkt, PktInfo *, Location,

                packet_t *outgoingPkt, bool costTableUpdated);

  void        process_New( packet_t *pkt, PktInfo *, Location, packet_t *);

  void        process_Owner( packet_t *pkt, PktInfo *, Location,

                   packet_t *outgoingPkt);

  void        process_Possible( packet_t *pkt, PktInfo *, Location,

                  packet_t *outgoingPkt);

  void        process_Resend( packet_t *pkt, PktInfo *, Location,

                packet_t *outgoingPkt);

  void        process_SendAck( packet_t *pkt, PktInfo *, Location,

                 packet_t *outgoingPkt);

  bool        timer_Delay( PktID *id, PktInfo *pi);

  bool        timer_Father( PktID *id, PktInfo *pi);

  bool        timer_Listen( PktID *id, PktInfo *pi);

  bool        timer_Owner( PktID *id, PktInfo *pi);

  bool        timer_Possible( PktID *id, PktInfo *pi);

  bool        timer_Resend( PktID *id, PktInfo *pi);

  bool        timer_SendAck( PktID *id, PktInfo *pi);

  bool        DREQ_D_TimerRtn( PktID *id, PktInfo *pi);

  bool        DREQ_L_TimerRtn( PktID *id, PktInfo *pi);

  bool        DREP_D_TimerRtn( PktID *id, PktInfo *pi);

  bool        DREP_L_TimerRtn( PktID *id, PktInfo *pi);

  void        generatePathTrace( packet_t *pkt, bool success);

  static bool    doubleMsgPrinted;

  packet_t    *m_active_packet;

  packet_queue_t m_send_queue;

};



template <class PLD> double    SHR<PLD>::retryMultiplier = 1.5;



unsigned int hopCountStretch(

  unsigned int    hc)

{

//  set the ratio of max hop to expected hop

//    return 3 * hc;

    return hc + 2;

}



#include "shr-common.h"



template <class PLD>

void *SHR<PLD>::copyPkt(

  packet_t    *pkt,

  bool        incAHC)

{

  packet_t    *newPkt = pkt->copy();



  associateWithPacket( newPkt, newPkt->hdr.seq_number);

  newPkt->hdr.info.cur_addr = MyEtherAddr;

  if( incAHC == true)

    newPkt->hdr.actual_hop++;

  newPkt->hdr.info.newSeqNumber = false;

  newPkt->hdr.info.path.AddNode( MyEtherAddr);

  return (void *) newPkt;

}



template <class PLD>

void *SHR<PLD>::createBasicPkt(

  ether_addr_t    dest,

  bool        newStatsId)

{

  packet_t    *newPkt = packet_t::alloc();



  if( newStatsId == true)

    newPkt->newStatsId();

  // type, ack_type, addresses set by caller

  newPkt->hdr.seq_number = 0xFFFFFFFF;

  // size set by caller

  newPkt->hdr.info.send_time = SimTime();    // used to compute end-to-end delay

  newPkt->hdr.actual_hop = 1;

  newPkt->hdr.expected_hop = 0;

  newPkt->hdr.max_hop = 1;

  newPkt->hdr.info.canceled = false;

  newPkt->hdr.info.newSeqNumber = false;

  newPkt->hdr.src_addr = MyEtherAddr;

  newPkt->hdr.dst_addr = dest;

  newPkt->hdr.info.cur_addr = MyEtherAddr;

  newPkt->hdr.info.nextState = PSNone;

  return newPkt;

}



/*

** There is no delay when the source sends an packet.

** This method isn't called when a packet is forwarded. Instead, the incoming

** packet is copied to make the outgoing packet.

*/

template <class PLD>

void *SHR<PLD>::createDataPkt(

  payload_t    &pld,

  ether_addr_t    dst,

  unsigned int    size)

{

  packet_t    *newPkt = (packet_t *) createBasicPkt( dst, true);



  newPkt->hdr.type = DATA;

  newPkt->hdr.info.size = size + DATA_SIZE;

  newPkt->hdr.info.newSeqNumber = true;

  newPkt->pld = pld;

  newPkt->hdr.info.prev_addr = -1;

  return newPkt;

}



template <class PLD>

void *SHR<PLD>::createAckPkt(

  ether_addr_t    src,

  ether_addr_t    dest,

  unsigned int    seq_number)

{

  packet_t    *newPkt = (packet_t *) createBasicPkt( dest, false);



  newPkt->hdr.type = ACK;

  newPkt->hdr.info.size = ACK_SIZE;

  newPkt->hdr.src_addr = src;

  newPkt->hdr.seq_number = seq_number;

  return newPkt;

}



/*

** Sending a packet from the network (SHR) layer to the MAC layer consists of

** calling two methods in order:

** 1. SendPacket: If applicable, assign a sequence number to the packet. Either

**    forward the packet to SendToMac (MAC is idle) or put it on the send

**    queue. from_mac_ack will remove packets from the send queue and forward

**    them to SendToMac.

** 2. SendToMac: Set the MAC busy flag and forward the packet to to_mac.

*/



/*

** Schedule a packet for transmission.

** If the packet doesn't have a delay, send the packet to the MAC layer.

** Otherwise, add the packet to the list of delayed packets and schedule an

** event for the end of the packet's delay.

*/

template <class PLD>

void SHR<PLD>::SendPacket(

  packet_t    *pkt)

{

  pkt->hdr.info.canceled = false;

  if( pkt->hdr.info.newSeqNumber == true && pkt->hdr.src_addr == MyEtherAddr)

    {

      m_seq_number++;

      pkt->hdr.seq_number = m_seq_number;

      associateWithPacket( pkt, m_seq_number);

      

#ifdef VISUAL_ROUTE

      // this printf is used by the compareHC utility

      printf( "# ASN: sn%08X; max hop %d; %s; @ %f\n", pkt->hdr.seq_number,

          pkt->hdr.max_hop, pkt->hdr.getType(), SimTime());

#endif // VISUAL_ROUTE

      // The only time a new sequence # is assigned, it is by the source node.

      // The DREQ and SHR-M automata require that the state become Ignore after

      // transmitting the packet. (DREP sequence #s are assigned elsewhere.)

      

      switch( pkt->hdr.type)

    {

    case REQUEST:

      //Visualization DREQ

      printf("%03d %f sending to -1 01-DREQ-Orig sn%08X (SendPacket, New)\n",(int)MyEtherAddr,SimTime(),pkt->hdr.seq_number);

      setPacketState( pkt, PSIgnore);

      break;

    case DATA:

      //Visualization Data

      printf("%03d %f sending to -1 09-Data-Orig sn%08X (SendPacket, New)\n",(int)MyEtherAddr,SimTime(),pkt->hdr.seq_number);

      setPacketState( pkt, PSPossible);

      pkt->hdr.info.nextState = PSOwner;

      break;

    case REPLY:

    default:

      printf( "***** SendPacket: Assigned sequence # to packet type %s\n",

          pkt->hdr.getType());

      assert( 0);

    }

      PktInfo    *pi = getPacketState( pkt);

      pi->pktType = pkt->hdr.type;

    }

  //Visualization

  if (pkt->hdr.info.newSeqNumber == false){

    switch (pkt->hdr.type)

      {

      case REQUEST:

    printf("%03d %f sending to -1 03-DREQ-Fwd sn%08X (SendPacket, DREQ)\n",(int)MyEtherAddr,SimTime(),pkt->hdr.seq_number);

    break;

      case DATA:

    printf("%03d %f sending to -1 14-Data-Fwd sn%08X (SendPacket, Data)\n",(int)MyEtherAddr,SimTime(),pkt->hdr.seq_number);

    break;

      case REPLY:

    printf("%03d %f sending to -1 07-DREP-Fwd sn%08X (SendPacket, DREP)\n",(int)MyEtherAddr,SimTime(),pkt->hdr.seq_number);

    break;

      default:

    break;

      }

  }

  pkt->hdr.info.newSeqNumber = false;

  dbgPrint( ("Send-Packet: ea %3d; sending %s; sn%08X; actHC %2d; rc %2d\n",

         (int) MyEtherAddr, pkt->hdr.getType(), pkt->hdr.seq_number,

         pkt->hdr.actual_hop, pkt->refcount));

  if( m_mac_busy == false)

    {

      m_mac_busy = true;

      m_active_packet = pkt;

      m_active_packet->inc_ref();

      SendToMac( pkt);

    }

  else

    {

      dbgPrint( ("SendPacket: ea %3d; sn%08X; mac busy, delaying\n", (int) MyEtherAddr,

         pkt->hdr.seq_number));

      AddToSendQueue( pkt);

    }

  return;

}



/*

** The inport that is called when the power manager wakes the node up.

** If the configuration file is in use, this must be defined, but it must not

** do anything

*/

template <class PLD>

void SHR<PLD>::from_pm_node_up()

{

  return;

}



template <class PLD>

void SHR<PLD>::recvNewDATA(

  packet_t    *pkt,

  PktInfo    *pi)

{

  // packet at destination

  if( MyEtherAddr == pkt->hdr.dst_addr)

  {

    //Visualization

    printf("%03d %f received from %03d 17-Data-Dest sn%08X\n",(int)MyEtherAddr,SimTime(),(int)pkt->hdr.info.prev_addr,pkt->hdr.seq_number);

    // update stats

    stats->appReceive( pkt->getStatsId(), SimTime() - pkt->hdr.info.send_time,

               pkt->hdr.actual_hop);

    // Send the incoming packet to the application

    pkt->inc_pld_ref();

    to_transport( pkt->pld);

    generatePathTrace( pkt, true);

    

    // send ACK< S, D, s#>

    packet_t    *ackPkt = (packet_t *) createAckPkt( pkt->hdr.src_addr, pkt->hdr.dst_addr,

                             pkt->hdr.seq_number);

    ackPkt->setStatsId( pkt->getStatsId());

    SendPacket( ackPkt);

    

    // goto SendAck (Safe to do so before transmission completes since there

    // is no reason to cancel ACK.)

    startProtocolTimer( pi, pkt, 10 * MyForwardDelay, NULL, "rND0");

    setPacketState( pkt, PSSendAck);

    return;

  }

  

  // packet at intermediate

  // locate destination in cost table

  cache_t::iterator iter = m_seq_cache.find( pkt->hdr.dst_addr);

  if( iter == m_seq_cache.end() || iter->second.noDistance() == true)

   {

      dbgPrint( ("rND: ea %3d; dest %3d; sn%08X; not in cache\n",

         (int) MyEtherAddr, (int) pkt->hdr.dst_addr,

         pkt->hdr.seq_number));

      printf("not in cache...\n");

      return;

    }

  unsigned int    distToDest = iter->second.hopCount();

  FlowInfo        *fInfo = getFlow( pkt);  

  // if DATA< S, D, s#, *, >ld, *, *>

  // if this node is not closer, then drop the packet

  if( pkt->hdr.expected_hop < distToDest)

    {

      //Visualization

      printf("%03d %f received from %03d 13-Data-Recv-FurtherDist sn%08X\n",(int)MyEtherAddr,SimTime(),(int)pkt->hdr.info.prev_addr,pkt->hdr.seq_number);

      printf("%03d %f received from %03d 16-Data-Drop sn%08X\n",(int)MyEtherAddr,SimTime(),(int)pkt->hdr.info.prev_addr,pkt->hdr.seq_number);

      

      dbgPrint( ("rND: ea %3d; dest %3d; sn%08X; %d <= %d\n",

         (int) MyEtherAddr, (int) pkt->hdr.dst_addr, pkt->hdr.seq_number,

         pkt->hdr.expected_hop, distToDest));

      return;

    }

  else if (pkt->hdr.expected_hop == distToDest)

    {

      //Visualization

      printf("%03d %f received from %03d 12-Data-Recv-SameDist sn%08X\n",(int)MyEtherAddr,SimTime(),(int)pkt->hdr.info.prev_addr,pkt->hdr.seq_number);

      printf("%03d %f received from %03d 16-Data-Drop sn%08X\n",(int)MyEtherAddr,SimTime(),(int)pkt->hdr.info.prev_addr,pkt->hdr.seq_number);

      return;

      //fInfo->TDN = (Random(MyForwardDelay/2.0) + (MyForwardDelay/2.0));

    }

  else if(pkt->hdr.expected_hop == (distToDest+1))

    {

      //Visualization

      printf("%03d %f received from %03d 10-Data-Recv-1HopCloser sn%08X\n",(int)MyEtherAddr,SimTime(),(int)pkt->hdr.info.prev_addr,pkt->hdr.seq_number);

      fInfo->TDN = Random(MyForwardDelay/4.0);

    }

  else

    {

      //Visualization

      printf("%03d %f received from %03d 11-Data-Recv->1HopCloser sn%08X\n",(int)MyEtherAddr,SimTime(),(int)pkt->hdr.info.prev_addr,pkt->hdr.seq_number);

      fInfo->TDN = (Random(MyForwardDelay/4.0) + (MyForwardDelay/4.0));

    }



/*

** Forward the packet.

**

** Copy the packet, set the expected hop count, calculate the back off and

** send the packet.

*/



  packet_t    *fwdPkt = (packet_t *) copyPkt( pkt, true);

  

  //If we've won our second election (PW>1) and we won the last election (SQPEW+1==current Seq_NUM)

  //Then calculate a reduced delay



  if (fInfo->previousWinner > 0 && fInfo->SQPEW + 1 == fwdPkt->hdr.seq_number)

    {

      //Visualization

      printf("%03d %f received from %03d 15-Data-PP sn%08X\n",(int)MyEtherAddr,SimTime(),(int)pkt->hdr.info.prev_addr,pkt->hdr.seq_number);

      fInfo->TDN = fInfo->TDN / 625;

      if (fInfo->TDN < 0.00002)

      {

        fInfo->TDN = 0.00002 + Random(0.00004);

      }

    }

  

  dbgPrint( ("rND: ea %3d; dest %3d; sn%08X; expHC %2d; ct %2d; backoff %f\n",

         (int) MyEtherAddr, (int) fwdPkt->hdr.dst_addr,

         fwdPkt->hdr.seq_number, pkt->hdr.expected_hop, distToDest, fInfo->TDN));

  fwdPkt->hdr.info.delay = fInfo->TDN;    // for statistics

  startProtocolTimer( pi, pkt, fInfo->TDN, fwdPkt, "rND1");

  setPacketState( pkt, PSPossible);

  pi->savedAHC = pkt->hdr.actual_hop;

  pi->savedEHC = pkt->hdr.expected_hop;

  pi->maxHop = pkt->hdr.max_hop;

  return;

}



template <class PLD>

void SHR<PLD>::recvNewACK(

  packet_t    *pkt,

  PktInfo    *pi)

{

  setPacketState( pkt, PSIgnore);

  return;

}



template <class PLD>

bool SHR<PLD>::timer_Possible(

    PktID        *id,

    PktInfo    *pi)

{

  if( pi->pktType != DATA)

    {

      printf( "***** timer_Possible: ea %3d; packet type not DATA\n", (int) MyEtherAddr);

      assert( 0);

    }

  packet_t    *pkt = (packet_t *) pi->savedData;

  dbgPrint( ("tP: ea %3d; %3d->%3d; sn%08X @ %f\n", (int) MyEtherAddr,

         (int) pkt->hdr.src_addr, (int) pkt->hdr.dst_addr,

         pkt->hdr.seq_number, SimTime()));

  // It is safe to modify the packet, a copy was made before starting the timer

  pkt->hdr.expected_hop = getHCFromCostTable( pkt->hdr.dst_addr);

  pkt->hdr.info.nextState = PSOwner;

  

  //Set SQPEW (Sequence Number Past Election Won) to the current sequence number

  getFlow(pkt)->SQPEW = pkt->hdr.seq_number; 

  

  //Increment the base number for the election win counter, but keep it at most ??

  getFlow(pkt)->previousWinner ++;

  //Visualization

  printf("%03d %f received from %03d 14-Data-Winner sn%08X (Possible, Timer)\n",(int)MyEtherAddr,SimTime(),(int)pkt->hdr.info.prev_addr,pkt->hdr.seq_number);



  pkt->hdr.info.prev_addr = MyEtherAddr;

  pkt->hdr.info.prev_x = MyX;

  pkt->hdr.info.prev_y = MyY;

  

  SendPacket( pkt);

  

  // start next timer when transmission ends

  // id not retained, so return false

  return false;

}



template <class PLD>

void SHR<PLD>::process_Possible(

                packet_t    *pkt,

                PktInfo    *pi,

                Location    loc,

                packet_t    *outgoingPkt)

{

  switch( pkt->hdr.type)

    {

    case REQUEST:

    case REPLY:

      printf( "***** process_Possible: ea %3d; DREQ/DREP packet\n", (int) MyEtherAddr);

      assert( 0);

    case DATA:

      if( pkt->hdr.expected_hop < pi->savedEHC)

    {

      //Visualization

      printf("%03d %f received from %03d 16-Data-Drop sn%08X (Possible, Data)\n",(int)MyEtherAddr,SimTime(),(int)pkt->hdr.info.prev_addr,pkt->hdr.seq_number);

      

      CancelTimer(pkt);

      setPacketState( pkt, PSIgnore);        // move to state Ignore

      getFlow(pkt)->previousWinner = 0;

    }

      break;

    case ACK:

      //Visualization

      printf("%03d %f received from %03d 16-Data-Drop sn%08X (Possible, Ack)\n",(int)MyEtherAddr,SimTime(),(int)pkt->hdr.info.prev_addr,pkt->hdr.seq_number);

      // cancel timer

      Cancel( pkt, loc, outgoingPkt);

      //***Someone else won an election, reset previous winner counter to 0

      getFlow(pkt)->previousWinner = 0;

      // goto Ignore

      setPacketState( pkt, PSIgnore);

      break;

    default:

      printf( "***** process_Possible: ea %3d; Received invalid packet type 0x%08x\n",

          (int) MyEtherAddr, pkt->hdr.type);

      assert( 0);

      break;

    }

  return;

}



template <class PLD>

bool SHR<PLD>::timer_Owner(

    PktID        *id,

    PktInfo    *pi)

{

    if( pi->pktType != DATA)

    {

    printf( "***** timer_Owner: ea %3d; packet type not DATA\n", (int) MyEtherAddr);

    assert( 0);

    }

    packet_t    *pkt = (packet_t *) pi->savedData;

    dbgPrint( ("tO: ea %3d; %3d->%3d; sn%08X @ %f\n", (int) MyEtherAddr,

           (int) pkt->hdr.src_addr, (int) pkt->hdr.dst_addr,

           pkt->hdr.seq_number, SimTime()));

    // packet passed in is a copy which may be in use by other nodes

    // create a new one that can be modified

    // do not increase the actual hop count

    packet_t    *fwdPkt = (packet_t *) copyPkt( pkt, false);

    fwdPkt->hdr.expected_hop = getHCFromCostTable( pkt->hdr.dst_addr);

    fwdPkt->hdr.info.nextState = PSResend;

    SendPacket( fwdPkt);

    pkt->free();

    return false;

}



template <class PLD>

void SHR<PLD>::process_Owner(

    packet_t    *pkt,

    PktInfo    *pi,

    Location    loc,

    packet_t    *outgoingPkt)

{

    switch( pkt->hdr.type)

    {

    case REQUEST:

    case REPLY:

        printf( "***** process_Owner: DREQ/DREP packet\n");

        assert( 0);

/*

** Receiving a DATA packet while in the Owner state is a bit different than

** other transitions since the timer is not canceled. But, there is a race

** condition. Consider the case when the timer expires, so the node sends

** another DATA packet. The node stays in the Owner state until the packet

** transmission finishes. It is possible for the transmission to be delayed

** because the MAC is receiving a packet. If this incoming packet is supposed

** to move the automaton to the Father state, it should instead cancel the

** outgoing packet and move the automaton to the Ignore state. The timer to

** move the automaton from the Father to Ignore state has already expired.

*/

    case DATA:

      if( pkt->hdr.expected_hop <= getHCFromCostTable( pkt->hdr.dst_addr))

    {

      CancelPacket( outgoingPkt, loc);    // will not cancel timer, only outgoing packet

      if( pi->timerActive == true)

        {

          setPacketState( pkt, PSFather);

        }

      else 

        {

          //Visualization

          printf("%03d %f received from %03d 16-Data-Drop sn%08X (Owner, Data)\n",(int)MyEtherAddr,SimTime(),(int)pkt->hdr.info.prev_addr,pkt->hdr.seq_number);

          setPacketState( pkt, PSIgnore);

        }

    }

      break;

    case ACK:

      Cancel( pkt, loc, outgoingPkt);    // cancel timer or outgoing packet

      setPacketState( pkt, PSIgnore);

      break;

    default:

      printf( "***** process_Owner: ea %3d; Received invalid packet type 0x%08x\n",

          (int) MyEtherAddr, pkt->hdr.type);

      assert( 0);

    }

    return;

}



template <class PLD>

bool SHR<PLD>::timer_Resend(

    PktID       *id,

    PktInfo    *pi)

{

    if( pi->pktType != DATA)

    {

    printf( "***** timer_Resend: ea %3d; packet type not DATA\n", (int) MyEtherAddr);

    assert( 0);

    }

    // ld += 2

   

    packet_t    *pkt = (packet_t *) pi->savedData;

    unsigned int    distToDest = increaseHopCountInCache( id->flow.dest, id->seqNumber);

  

    // if( ld+sts < smh)

    //   send DATA< S, D, s#, sts+1, ld, smh, sdata>

    // pkt->hdr.actual_hop is from the last broadcast packet (i.e. incoming+1)

    if( distToDest + pi->savedAHC-1 <= pi->maxHop)

    {

    packet_t    *fwdPkt = (packet_t *) copyPkt( pkt, false);

    fwdPkt->hdr.expected_hop = distToDest;

    SendPacket( fwdPkt);

    }

    pkt->free();

    setPacketState( *id, PSIgnore);

    return false;

}



template <class PLD>

void SHR<PLD>::process_Resend(

    packet_t    *pkt,

    PktInfo    *pi,

    Location    loc,

    packet_t    *outgoingPkt)

{

    switch( pkt->hdr.type)

    {

    case REQUEST:

    case REPLY:

        printf( "***** process_Resend: ea %3d; Received DREQ/DREP packet\n", (int) MyEtherAddr);

        assert( 0);

        break;

    case DATA:

        if( pkt->hdr.expected_hop <= getHCFromCostTable( pkt->hdr.dst_addr))

        {

        Cancel( pkt, loc, outgoingPkt);

        setPacketState( pkt, PSIgnore);

        }

        break;

    case ACK:

        Cancel( pkt, loc, outgoingPkt);

        setPacketState( pkt, PSIgnore);

        break;

    default:

        printf( "***** process_Resend: ea %3d; Received invalid packet type 0x%08x\n",

            (int) MyEtherAddr, pkt->hdr.type);

        assert( 0);

        break;

    }

    return;

}



template <class PLD>

bool SHR<PLD>::timer_SendAck(

  PktID        *id,

  PktInfo    *pi)

{

  if( pi->pktType != DATA && pi->pktType != ACK)

  {

    printf( "***** timer_SendAck: ea %3d; packet type not DATA or ACK\n", (int) MyEtherAddr);

    assert( 0);

  }

  setPacketState( *id, PSIgnore);

  return false;

}



template <class PLD>

void SHR<PLD>::process_SendAck(

  packet_t    *pkt,

  PktInfo    *,

  Location    loc,

  packet_t    *outgoingPkt)

{

    switch( pkt->hdr.type)

    {

    case DATA:

        if( MyEtherAddr == pkt->hdr.dst_addr)

        {

        stats->appReceive( pkt->getStatsId(),

                   SimTime() - pkt->hdr.info.send_time,

                   pkt->hdr.actual_hop);

        generatePathTrace( pkt, true);

        packet_t    *ackPkt = (packet_t *) createAckPkt( pkt->hdr.src_addr, pkt->hdr.dst_addr,

                                     pkt->hdr.seq_number);

        ackPkt->setStatsId( pkt->getStatsId());

        SendPacket( ackPkt);

        }

        else

        {

        printf( "***** process_SendAck: ea %3d; sn%08X; not at destination\n",

            (int) MyEtherAddr, pkt->hdr.seq_number);

        assert( 0);

        }

        break;

    case ACK:

        break;

    default:

        printf( "***** process_SendAck: ea %3d; Received invalid packet type %s\n",

            (int) MyEtherAddr, pkt->hdr.getType());

        assert( 0);

        break;

    }

    return;

}



template <class PLD>

bool SHR<PLD>::timer_Father(

  PktID        *id,

  PktInfo    *pi)

{

  dbgPrint( ("tF: ea %3d; %3d->%3d; sn%08X @ %f\n", (int) MyEtherAddr,

         (int) id->flow.src, (int) id->flow.dest, id->seqNumber,

         SimTime()));

  setPacketState( *id, PSIgnore);

  return false;

}



template <class PLD>

void SHR<PLD>::process_Father(

    packet_t    *pkt,

    PktInfo    *pi,

    Location    loc,

    packet_t    *outgoingPkt)

{

    switch( pkt->hdr.type)

    {

    case REQUEST:

    case REPLY:

        printf( "***** process_Father: ea %3d; Received DREQ/DREP packet\n", (int) MyEtherAddr);

        assert( 0);

    case DATA:

        if( pkt->hdr.expected_hop < getHCFromCostTable( pkt->hdr.dst_addr))

        {

        Cancel( pkt, loc, outgoingPkt);

        packet_t    *ackPkt = (packet_t *) createAckPkt( pkt->hdr.src_addr, pkt->hdr.dst_addr,

                                     pkt->hdr.seq_number);

        ackPkt->setStatsId( pkt->getStatsId());

        SendPacket( ackPkt);

        getFlow(pkt)->previousWinner = 0;

        setPacketState( pkt, PSIgnore);

        }

        break;

    case ACK:

        Cancel( pkt, loc, outgoingPkt);

        setPacketState( pkt, PSIgnore);

        break;

    default:

        printf( "***** process_Father: ea %3d; Received invalid packet type 0x%08x\n",

            (int) MyEtherAddr, pkt->hdr.type);

        assert( 0);

    }

    return;

}



/*

** The routing layer has received an acknowledgement from the mac layer.

** False indicates that the transmission failed.

*/

template <class PLD>

void SHR<PLD>::from_mac_ack(

  bool        success)

{

  if( m_active_packet->hdr.info.canceled == false)

  {

/*

** If the packet transmission failed, drop the packet.

*/

    if( success == false)

    {

      dbgPrint( ("fma: ea %3d; sn%08X; %s; trans failed\n", (int) MyEtherAddr,

         m_active_packet->hdr.seq_number, m_active_packet->hdr.getType()));

      stats->netSendFail( m_active_packet->getStatsId());

    }



/*

** Packet was sent successfully.

*/

    else

      stats->netSend( m_active_packet->getStatsId(), m_active_packet->hdr.info.delay);



    PktInfo    *pi = getPacketState( m_active_packet);

    double    delay;

    switch( m_active_packet->hdr.type)

    {

/*

** DATA packet sent from the Possible or Owner state require starting a timer

** after the transmission has completed. Do this even if the transmission

** failed.

*/

      case DATA:

    switch( pi->state)

    {

      case PSPossible:

      case PSOwner:

          delay = Random( 0.5 * MyForwardDelay) + (retryMultiplier - 0.25) * MyForwardDelay;

          m_active_packet->inc_ref();        // retain copy of packet

          startProtocolTimer( pi, m_active_packet, delay, m_active_packet, "fma1");

          break;

      case PSFather:

        printf( "***** fma: ea %3d; state Father\n", (int) MyEtherAddr);

        assert( 0);

      default:

        break;

    }

    break;

      case REPLY:

      case REQUEST:

    if( pi->state == PSDelay)        // must be intermediate node

    {

      delay = 10.0 * MyForwardDelay;    // this is part of DREP automaton

      startProtocolTimer( pi, m_active_packet, delay, NULL, "fma2");

    }

    break;

      default:

    break;

    }

    // set the new state for the PktID

    setPacketState( m_active_packet, m_active_packet->hdr.info.nextState);

  }

  else

  {

    dbgPrint( ("fma: ea %3d; sn%08X; %s; canceled\n", (int) MyEtherAddr,

           m_active_packet->hdr.seq_number, m_active_packet->hdr.getType()));

  }



  m_active_packet->free();

  sendNextPkt();

  return;

}



#endif /*_shr_h_*/
Dai então eu tentei fazer include dele no arquivo que tava dando os erros de definição o shr-common.h, ... (eu sou um analfabeto em C/C++, então nem sei se é possivel fazer isso apenas procurei um arquivo que tivesse algo parecido), dai o resultado do make foi o seguinte g++ -Wall -O2 -DVISUAL_ROUTE -DVR_SIZE=30 -c -o sim_dsr.o sim_dsr.cxx g++ -o sim_dsr sim_dsr.o -L../../libraries/visualizer/lib -L../../libraries/parseConfig/lib -L../../libraries/packetList/lib -lpacketList -lvisualizer -lconfigParser -lfl ../../bin/cxx sim_shr.cc error 0001 (../../net/shr-pp.h,1):cannot open include file '../../net/shr-pp.h' make: *** [sim_shr.cxx] Falha de segmentação tai o erro e só o cabeçalho do shr-common.h que eu modifiquei
#ifndef _shr_common_h_
#define _shr_common_h_

#ifdef    DEBUG_PRINT
#define dbgDoPrint    (true)
#define    dbgPrint(x)     printf x
#else    // DEBUG_PRINT
#define    dbgPrint(x)
#define    dbgDoPrint    (false)
#endif    // DEBUG_PRINT

#include "shr-pp.h"

Tipo eu to fazendo alguma besteira ou coisa assim????

Link para o comentário
Compartilhar em outros sites

  • 0

Pela mensagem de erro, ele não tá podendo abrir o arquivo '../../net/shr-pp.h'. Veja se ele tá no mesmo lugar dos seus fontes.

Quanto ao erro do char*, poste a mensagem de erro do compilador.

Pra mim fica difícil te ajudar nesse erro dos includes porque é complicado visualizar os arquivos aqui. Me parece que você tá compilando um projeto grande. Verifica as linkagens também. Qualquer coisa posta ai.

Abraço.

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,3k
    • Posts
      652,4k
×
×
  • Criar Novo...