📄 sim_shr.h
字号:
PacketSize = 1000; StopTime( 1000); numNodes = NumNodes = 110; MaxX = MaxY = 2000; NumSourceNodes = 0; ActivePercent = 1.0; // 1.0 == 100% ActiveCycle = 100.0; // in seconds Interval = 20.0; BackOff = SHRBackOff_SSR; ContinuousBackOff = true; OneSink = false; UnidirectionalTraffic = false; SlotWidth = 0.001; TransitionTime = 0.0; TXPower = 0.0280; RouteRepair = 0; dumpStatus = false;#ifdef USE_CONFIG_FILE configStatus = false;#endif // USE_CONFIG_FILE neighborMatrix = NULL; ForwardDelay = 0.1; return;}void RoutingSim::Start(){ return;}void RoutingSim::ClearStats(){ int i; for( i = 0; i < NumNodes; i++) { nodes[ i].app.clearStats(); nodes[ i].net.clearStats(); nodes[ i].mac.clearStats(); nodes[ i].battery.clearStats(); } return;}void RoutingSim::GenerateHELLO( int id){ nodes[ id].net.generateHELLO(); return;}/******************************************************************* * After the simulation is stopped, we will collect some statistics. *******************************************************************/void RoutingSim::Stop(){ int i, j, sent, recv, samples, recv_data, hopCounts[ HCArraySize]; int sentSuboptimal, canceledSuboptimal, canceledPkts; double hop; double app_recv; double delay; int numCollisions; if( dumpStatus == true) { FILE *fp; int len = strlen( dumpFile); char *str = new char[ len + 5 + 1]; strcpy( str, dumpFile); strcpy( str + len, ".stat"); fp = fopen( str, "w"); if( fp == 0) printf( "***** Unable to open %s for writing\n", str); else { char comma = ' '; fprintf( fp, "common {\n NumNodes %d,\n", NumNodes); nodes[0].net.dumpStatic( fp, 0, 2); fprintf( fp, "}\ncostTable {\n"); nodes[0].net.dumpCacheStatic( fp, 0, 2); fprintf( fp, "}\nsenders {"); for( i = 0; i < NumNodes; i++) if( nodes[i].SrcOrDst == true) { fprintf( fp, "%c %d", comma, nodes[i].ID); comma = ','; } fprintf( fp, "}\n"); for( i = 0; i < NumNodes; i++) { char comma; fprintf( fp, "node {\n"); fprintf( fp, " location { %f, %f }\n", nodes[i].mob.getX(), nodes[i].mob.getY()); fprintf( fp, " net {\n"); nodes[i].net.dump( fp, 2, 2); fprintf( fp, " }\n costTable {\n"); nodes[i].net.dumpCostTable( fp, 2, 2); fprintf( fp, " }\n neighbors {"); comma = ' '; for( j = 0; j < NumNodes; j++) { if( j == i) continue; if( neighborMatrix[ i * NumNodes + j] == true) { fprintf( fp, "%c %3d", comma, j); comma = ','; } } fprintf( fp, "}\n}\n"); } } delete [] neighborMatrix; delete [] str; } // Application layer stats for( sent = recv = i = 0, delay = 0.0; i < NumNodes; i++) { sent+=nodes[i].app.SentPackets; recv+=nodes[i].app.RecvPackets; delay+=nodes[i].app.TotalDelay; } printf( "APP -- pkts sent: %10d, rcvd: %10d, success rate: %.3f, " "end-to-end delay: %.3f\n", sent, recv, (double) recv/sent, delay/recv); // Network (SSR) layer stats app_recv=recv; delay=0.0; samples=0; hop=0.0; recv_data=0; sentSuboptimal = 0; canceledPkts = 0; canceledSuboptimal = 0; for( j = HCArraySize-1; j >= 0; j--) hopCounts[j] = 0; for( sent = recv = i = 0; i < NumNodes; i++) { sent+=nodes[i].net.SentPackets; sentSuboptimal += nodes[i].net.SentSuboptimal; canceledPkts += nodes[i].net.CanceledPackets; canceledSuboptimal += nodes[i].net.CanceledSuboptimal; recv+=nodes[i].net.RecvPackets; hop+=nodes[i].net.TotalHop; delay+=nodes[i].net.TotalDelay; samples+=nodes[i].net.TotalSamples; recv_data+=nodes[i].net.RecvDataPackets; for( j = HCArraySize-1; j >= 0; j--) hopCounts[j] += nodes[i].net.HopCounts[j]; } printf( "NET -- pkts sent: %10d, rcvd: %10d, avg hop: %.3f, " "backoff delay: %.3f, suboptimal: %10d, canceled pkts: %10d, " "canceled suboptimal: %10d\n", sent, recv, hop/recv_data, delay/samples, sentSuboptimal, canceledPkts, canceledSuboptimal); printf( "NET -- hop counts ["); for( i = 0; i < HCArraySize; i++) printf( " %4d", hopCounts[ i]); printf( "]\n"); numCollisions = 0; //mwl // MAC layer stats for( sent = recv = i = 0; i < NumNodes; i++) { sent+=nodes[i].mac.SentPackets; recv+=nodes[i].mac.RecvPackets; numCollisions += nodes[i].mac.rrCollisions; } printf( "MAC -- pkts sent: %10d, rcvd: %10d, # collisions %10d\n", sent, recv, numCollisions); // Battery stats { double ie = 0, re = 0; for( i=0; i < NumNodes; i++) { ie += nodes[i].battery.InitialEnergy; re += nodes[i].battery.RemainingEnergy; } printf( "BAT -- Initial: %f, Remaining: %f\n", ie, re); } return;}/************************************************************************ * The simulation has a @Setup()@ function which must be called before * the simulation can be run. The reason we don't do this in the constructor * is that we must assign values to * its parameters after the simulation component has been instantiated. * The @Setup()@ function, which you can rename to anything you like, * first maps component * parameters to corresponding simulation parameters (for instance, assign * the value of the simulation parameter @interval@ to the component parameter * @source.interval@). It then connects pairs of inport and outports. ************************************************************************/void RoutingSim::packetTimes( int source, int dest){ int xmitSize = Random( PacketSize) + PacketSize / 2; double xmitTime = Random( Interval) + Interval/2; nodes[ source].app.Connections.push_back( make_triple( ether_addr_t( dest), xmitSize, xmitTime)); printf( "node %3d->%3d transmissions: %4d @ %f\n", source, dest, xmitSize, xmitTime); return;}void RoutingSim::Setup(){ int i; /************************************************************************ * The size of the sensor node array must be set using @SetSize()@ before * the array can ever be used. ************************************************************************/ nodes.SetSize(NumNodes); for(i=0;i<NumNodes;i++) { nodes[i].MaxX=MaxX; nodes[i].MaxY=MaxY; nodes[i].MyEtherAddr=i; nodes[i].ID=i; nodes[i].Setup( TXPower, ForwardDelay); nodes[i].mob.Setup(); } if( dumpStatus == true) { neighborMatrix = new bool[ NumNodes * NumNodes]; for( i = NumNodes * NumNodes-1; i >= 0; i--) neighborMatrix[ i] = false; }/************************************************************************ * Set the slot width, transition time and backoff type (lambda or slot). * This is done only once since they are static (class wide) for the SHR class. * But, this must be done after @nodes@ has been initialized. The [] operator * is overloaded and throws an exception if nodes hasn't been initialized. ************************************************************************/ nodes[0].net.setSlotWidth( SlotWidth); nodes[0].net.setTransitionTime( TransitionTime); nodes[0].net.setBackOff( BackOff); nodes[0].net.setContinuousBackOff( ContinuousBackOff); nodes[0].net.setRouteRepair( RouteRepair); /********************************************************************* * Pass the pointer to the Visualizer to the protocol (for packet routes) and * mobility (for node location) components. If the Visualizer is to create * the output files, it must have been instantiated before these calls. See * the 'v' argument in main. *********************************************************************/ nodes[0].net.setVisualizer( Visualizer::instantiate()); nodes[0].mob.setVisualizer( Visualizer::instantiate());/************************************************************************ * The channel component needs to know the total number of sensor nodes. * It also needs to know the value of @CSThresh@ since it won't sent packets * to nodes that can't detect them. @RXThresh@ is also needed to produce * the same receive power in those nodes that can just correctly receive packets * when using different propagation models. * * In this example @FreeSpace@ is used. ************************************************************************/ channel.setNumNodes( NumNodes); channel.setDumpPackets( false); channel.setCSThresh( nodes[0].phy.getCSThresh()); channel.setRXThresh( nodes[0].phy.getRXThresh()); channel.useFreeSpace(); channel.setWaveLength( speed_of_light / nodes[0].phy.getFrequency()); channel.setX( MaxX); channel.setY( MaxY); channel.setGridEnabled( false); channel.setMaxTXPower( nodes[0].phy.getTXPower()); /************************************************************************ * The channel component also has a @Setup()@ function which is to * set the size of its outport array. ************************************************************************/ channel.Setup(); for(i=0;i<NumNodes;i++) { connect nodes[i].to_channel_packet,channel.from_phy; connect nodes[i].to_channel_pos,channel.pos_in; connect channel.to_phy[i],nodes[i].from_channel ; }/************************************************************************ * This is to create communication pairs. ************************************************************************/ bool randomNodes; randomNodes = channel.initSources( NumSourceNodes, OneSink); int src, dest = 0, count = (int) (1.5 * NumNodes); if( randomNodes == true && OneSink == true) { dest = Random( NumNodes); nodes[ dest].SrcOrDst = true; } for( i = 0; i < NumSourceNodes; i++) { if( randomNodes == true) { double dx, dy; do { do src = Random( NumNodes); while( nodes[ src].SrcOrDst == true); if( OneSink == false) { do dest = Random( NumNodes); while( nodes[ dest].SrcOrDst == true); } dx = nodes[ src].mob.getX() - nodes[ dest].mob.getX(); dy = nodes[ src].mob.getY() - nodes[ dest].mob.getY(); --count; } while( src == dest || (dx*dx + dy*dy) < MaxX * MaxY / 4 && count > 0); if( count <= 0) { printf( "***** Unable to find enough source nodes.\n"); assert( 0); } } else channel.getSourcePair( src, dest); nodes[ src].SrcOrDst = true; if( OneSink == false) nodes[ dest].SrcOrDst = true; packetTimes( src, dest); if( UnidirectionalTraffic == false) packetTimes( dest, src); } if( ActiveCycle == 0.0) // failures are permanent { double startupTime = 5 * Interval; double steadyStateTime = FinishTimeRatio * StopTime() - startupTime; for( i = 0; i < NumNodes; i++) { nodes[i].pm.BeginTime = 0.0; nodes[i].pm.FinishTime = StopTime(); if( nodes[i].SrcOrDst == false && Random() > ActivePercent) nodes[i].pm.failureStats( startupTime + Random() * steadyStateTime); else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -