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

📄 shr.h

📁 大名鼎鼎的传感器网络仿真实验室平台SENSE
💻 H
📖 第 1 页 / 共 4 页
字号:
  newPkt->hdr.src_addr = MyEtherAddr;  newPkt->hdr.pre_addr = MyEtherAddr;  newPkt->hdr.dst_addr = dst;  newPkt->hdr.cur_addr = MyEtherAddr;  newPkt->hdr.newSeqNumber = true;  return newPkt;}template <class PLD>void *SHR<PLD>::createReplyPkt(  ether_addr_t	src,  unsigned int	expectedHop){  packet_t	*newPkt = (packet_t *) createBasicPkt();  newPkt->hdr.type = REPLY;  newPkt->hdr.size = REPLY_SIZE;  newPkt->hdr.expected_hop = expectedHop;  newPkt->hdr.max_hop = expectedHop + AdditionalHop;  newPkt->hdr.src_addr = MyEtherAddr;  newPkt->hdr.dst_addr = src;  newPkt->hdr.cur_addr = MyEtherAddr;  newPkt->hdr.pre_addr = MyEtherAddr;  newPkt->hdr.newSeqNumber = true;  return newPkt;}template <class PLD>void *SHR<PLD>::createAckPkt(  ether_addr_t	src_addr,  ether_addr_t	cur_addr,  ether_addr_t	pre_addr,  unsigned int	seq_number,  simtime_t	delay,  bool		fromDest){  packet_t	*newPkt = (packet_t *) createBasicPkt();  newPkt->hdr.type = ACK;  newPkt->hdr.ackType = fromDest == true ? AT_Destination : AT_Intermediate;  newPkt->hdr.size = ACK_SIZE;  newPkt->hdr.src_addr = src_addr;  newPkt->hdr.cur_addr = cur_addr;  newPkt->hdr.pre_addr = pre_addr;  newPkt->hdr.seq_number = seq_number;  newPkt->hdr.delay = delay;  newPkt->hdr.max_hop = 0;  return newPkt;}//mwl: transfer changes here to SSRv02template <class PLD>void *SHR<PLD>::createHelloPkt(  ether_addr_t	destAddr,  int		hopCount){  packet_t	*newPkt = (packet_t *) createBasicPkt();  newPkt->hdr.type = HELLO;  newPkt->hdr.size = HELLO_SIZE;  newPkt->hdr.expected_hop = 1;  newPkt->hdr.src_addr = MyEtherAddr;  newPkt->hdr.dst_addr = destAddr;  newPkt->hdr.cur_addr = MyEtherAddr;  newPkt->hdr.pre_addr = MyEtherAddr;  newPkt->hdr.helloData = hopCount;  newPkt->hdr.delay = Random( ForwardDelay);  newPkt->hdr.newSeqNumber = true;  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		*fp,  int		indent,  int		offset) const{  indent += offset;  makeiS;  pIndent;  fprintf( fp, "maxCounter %d\n", maxCounter);  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;    case Changing:      fprintf( fp, "currentHC %d, state Changing, pendingHC %d, updateCtr %d",	       currentHC, pendingHC, updateCtr);      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, "backOff %s, continuous %s, routeRepair %s,\n",	   backOff == SHRBackOff_SSR ? "SSR" :	   backOff == SHRBackOff_Incorrect ? "Incorrect" : "SHR",	   continuousBackOff == true ? "true" : "false",	   RouteRepairEnabled == true ? "true" : "false");  if( continuousBackOff == false)  {    pIndent;    fprintf( fp, "slotWidth %f, transitionTime %f,\n", slotWidth, transitionTime);  }  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{//  cache_t::const_iterator	iter;  indent += offset;  makeiS;  pIndent;  fprintf( fp, "addr %d, seqNumber %d, ForwardDelay %f,\n",	   (int) MyEtherAddr, m_seq_number, ForwardDelay);  pIndent;  fprintf( fp, "RXThresh %f, AckWindow %f, MaxResend %d, TimeToLive %d,\n",	   RXThresh, AckWindow, MaxResend, TimeToLive);  pIndent;  fprintf( fp, "AdditionalHop %d, TotalDelay %f, TotalSamples %d, "	   "TotalHop %d,\n", AdditionalHop, TotalDelay, TotalSamples,TotalHop);  pIndent;  fprintf( fp, "SentPackets %d, RecvPackets %d, RecvUniPackets %d, "	   "RecvDataPackets %d,\n", SentPackets, RecvPackets, RecvUniPackets,	   RecvDataPackets);  pIndent;  fprintf( fp, "SentSuboptimal %d, CanceledPackets %d, CanceledSuboptimal %d\n",	   SentSuboptimal, CanceledPackets, CanceledSuboptimal);  return;}template <class PLD>void SHR<PLD>::dumpCostTable(  FILE		*fp,  int		indent,  int		offset) const{  cache_t::const_iterator	iter;  indent += offset;  makeiS;//  pIndent;  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>void SHR<PLD>::setSlotWidth(  double	sw){  slotWidth = sw;  return;}template <class PLD>double SHR<PLD>::getSlotWidth(){  return slotWidth;}template <class PLD>void SHR<PLD>::setTransitionTime(  double	tt){  transitionTime = tt;  return;}template <class PLD>double SHR<PLD>::getTransitionTime(){  return transitionTime;}template <class PLD>void SHR<PLD>::setBackOff(  SHRBackOff	bot){  backOff = bot;  return;}template <class PLD>SHRBackOff SHR<PLD>::getBackOff(){  return backOff;}template <class PLD>void SHR<PLD>::setContinuousBackOff(  bool		flag){  continuousBackOff = flag;  return;}template <class PLD>bool SHR<PLD>::getContinuousBackOff(){  return continuousBackOff;}template <class PLD>void SHR<PLD>::setRouteRepair(  int	numSteps){  if( numSteps == 0)    RouteRepairEnabled = false;  else  {    RouteRepairEnabled = true;    seq_number_t::setCounter( numSteps);  }  return;}template <class PLD>SHR<PLD>::SHR(){  connect ackTimer.to_component, AckTimer;  connect pktDelayTimer.to_component, PktDelayTimer;  connect transitionTimer.to_component, TransitionTimer;  connect helloTimer.to_component, HelloTimer;  return;}template <class PLD>SHR<PLD>::~SHR(){  return;}template <class PLD>void SHR<PLD>::Start() {  if( doubleMsgPrinted == false)  {    printf( "Timeout multiplier: %d\n", MWLTimeoutMultiplier);#ifdef	MWL_DOUBLE_DELAY    printf( "Doubling Lambda on retries\n");#else	// MWL_DOUBLE_DELAY    printf( "Lambda constant on retries\n");#endif	// MWL_DOUBLE_DELAY    doubleMsgPrinted = true;  }  m_mac_busy=false;  m_seq_number = (int) MyEtherAddr << 16;  m_seq_cache.insert(make_pair(MyEtherAddr,seq_number_t()));  SentPackets=RecvPackets=RecvUniPackets=0l;  SentSuboptimal = 0;  CanceledPackets = 0;  CanceledSuboptimal = 0;  TotalDelay=0.0;  TotalHop=0;  TotalSamples=0;  RecvDataPackets=0;  for( int i = HCArraySize-1; i >= 0; i--)    HopCounts[i] = 0;}template <class PLD>void SHR<PLD>::clearStats(){  SentPackets = 0;  SentSuboptimal = 0;  CanceledPackets = 0;  CanceledSuboptimal = 0;  RecvPackets = 0;  RecvUniPackets = 0;  RecvDataPackets = 0;  TotalDelay = 0;  TotalSamples = 0;  TotalHop = 0;  for( int i = HCArraySize-1; i >= 0; i--)    HopCounts[ i] = 0;  return;}template <class PLD>void SHR<PLD>::Stop() {  //printf("%s: sent %d, recv %d\n",GetName(),SentPackets,RecvPackets);  //printf("%s: %f \n", GetName(), (double)RecvPackets/RecvUniPackets);  return;}template <class PLD>void SHR<PLD>::from_transport(  payload_t	&pld,  ether_addr_t	&dst,  unsigned int	size){  if( size > 1510)    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);  Printf((DumpPackets, "%d SHR: %d->%d *%s\n", (int) MyEtherAddr,	  (int) MyEtherAddr, (int) dst, pkt->dump().c_str()));  shrPrint( ("ft: %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.  if( iter == m_seq_cache.end())  {    // the destination is not in the active node table    // must send a REQUEST packet first;    packet_t	*rp = (packet_t *) createRequestPkt( dst);    Printf( (DumpPackets, "%d SHR: %d->%d %s\n", (int) MyEtherAddr,	    (int) MyEtherAddr, (int) dst, rp->dump().c_str()));    SendPacket( rp);    m_data_buffer.push_back( pkt);  }  // 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();    pkt->hdr.max_hop = pkt->hdr.expected_hop + AdditionalHop;    pkt->hdr.delay = 0;		// send without delay    SendPacket( pkt);  }}template <class PLD>void SHR<PLD>::AddToSendQueue(  packet_t	*pkt){  packet_queue_t::iterator	iter = m_send_queue.begin();  while( iter != m_send_queue.end() && (*iter)->hdr.delay <= pkt->hdr.delay)    iter++;  m_send_queue.insert( iter, pkt);  return;}/*** Sending a packet from the network (SHR) layer to the MAC layer consists of** calling four methods in order:** 1. SendPacket: Packets without a delay are forwarded to AssignSeqNumber.**    Packets with a delay are set aside until the delay has elapsed. The timer**    routine, PktDelayTimer, forwards the packet to AssignSeqNumber. Because**    this method may reorder the packets, sequence numbers should not be**    assigned yet.** 2. AssignSeqNumber: The packets can no longer be reordered. 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.** 3. SendToMac: If the simulation does not include a transition time, forward**    the packet to to_mac. Otherwise, start the transition timer and let its**    routine, TransitionTimer, forward the packet to to_mac** 4. to_mac: The outport that connects the network and MAC layers. This**    function should always be called with a delay of 0.*//*** 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){  if( pkt->hdr.delay == 0.0)    AssignSeqNumber( pkt);  else  {    shrPrint( ( "SPT: ea %3d; 0x%08X; sn%08X; %f + %f seconds\n",	    (int) MyEtherAddr, (int) pkt, pkt->hdr.seq_number, SimTime(),	    pkt->hdr.delay));    // add packet to delay list    m_delay_list.push_back( pkt);    // schedule event    pkt->hdr.pktDelayIndex = pktDelayTimer.Set( pkt, SimTime() + pkt->hdr.delay);  }  return;}/*** This method is called when the delay for a packet has expired.** Remove the packet from the list and send it to the MAC layer.*/template <class PLD>void SHR<PLD>::PktDelayTimer(  packet_t	*pkt,  unsigned int){  shrPrint( ( "PDT: ea %3d; 0x%08X; sn%08X; %f\n", (int) MyEtherAddr,	      (unsigned int) pkt, pkt->hdr.seq_number, SimTime()));  m_delay_list.remove( pkt);  AssignSeqNumber( pkt);  return;}/*** High level method to send a packet to the MAC layer.** Assign a new sequence number if one is needed.** If the MAC is idle, send the packet. Otherwise, put the packet on the send** queue.*/template <class PLD>void SHR<PLD>::AssignSeqNumber(  packet_t	*pkt){  pkt->hdr.canceled = false;  if( pkt->hdr.newSeqNumber == true && pkt->hdr.src_addr == MyEtherAddr)  {    m_seq_number++;    m_seq_cache[ MyEtherAddr].check( m_seq_number);    pkt->hdr.seq_number = m_seq_number;#ifdef VISUAL_ROUTE    // this printf is used by the compareHC and checkHC utilities

⌨️ 快捷键说明

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