📄 sim_ashr.cc
字号:
{ 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); app_recv=recv; delay=0.0; samples=0; hop=0.0; recv_data=0; for( j = HCArraySize-1; j >= 0; j--) hopCounts[j] = 0; for( int i = NumT2Slots-1; i >= 0; i--) { T2Colls[i] = T2PktsSent[i] = 0; T2PktsRcvd[0][i] = T2PktsRcvd[1][i] = 0; } for( int i = NumT3Slots-1; i >= 0; i--) { T3Colls[i] = T3PktsSent[i] = 0; T3PktsRcvd[0][i] = T3PktsRcvd[1][i] = 0; } dropPkts = T1PktsSent = 0; T1Colls = IgnColls = 0; T1PktsRcvd[0] = T1PktsRcvd[1] = 0; T4PktsRcvd[0] = T4PktsRcvd[1] = 0; CanceledPktsRcvd[0] = CanceledPktsRcvd[1] = 0; BadAddrPktsRcvd[0] = BadAddrPktsRcvd[1] = 0; WrongHopPktsRcvd[0] = WrongHopPktsRcvd[1] = 0; for( sent = recv = i = 0; i < NumNodes; i++) { sent += nodes[i].net.SentPackets; 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; dropPkts += nodes[i].net.DropPkts; T1PktsSent += nodes[i].net.T1PktsSent; T1Colls += nodes[i].net.T1Collisions; IgnColls += nodes[i].net.IgnoreCollisions; t3AbnormalPktsSent += nodes[i].net.T3AbnormalPktsSent; T1PktsRcvd[0] += nodes[i].net.T1PktsRcvd[0]; T1PktsRcvd[1] += nodes[i].net.T1PktsRcvd[1]; T4PktsRcvd[0] += nodes[i].net.T4PktsRcvd[0]; T4PktsRcvd[1] += nodes[i].net.T4PktsRcvd[1]; CanceledPktsRcvd[0] += nodes[i].net.CanceledPktsRcvd[0]; CanceledPktsRcvd[1] += nodes[i].net.CanceledPktsRcvd[1]; BadAddrPktsRcvd[0] += nodes[i].net.BadAddrPktsRcvd[0]; BadAddrPktsRcvd[1] += nodes[i].net.BadAddrPktsRcvd[1]; WrongHopPktsRcvd[0] += nodes[i].net.WrongHopPktsRcvd[0]; WrongHopPktsRcvd[1] += nodes[i].net.WrongHopPktsRcvd[1]; for( j = HCArraySize-1; j >= 0; j--) hopCounts[j] += nodes[i].net.HopCounts[j]; for( int j = NumT2Slots-1; j >= 0; j--) { T2PktsSent[j] += nodes[i].net.T2PktsSent[j]; T2Colls[j] += nodes[i].net.T2Collisions[j]; T2PktsRcvd[0][j] += nodes[i].net.T2PktsRcvd[0][j]; T2PktsRcvd[1][j] += nodes[i].net.T2PktsRcvd[1][j]; } for( int j = NumT3Slots-1; j >= 0; j--) { T3PktsSent[j] += nodes[i].net.T3PktsSent[j]; T3Colls[j] += nodes[i].net.T3Collisions[j]; T3PktsRcvd[0][j] += nodes[i].net.T3PktsRcvd[0][j]; T3PktsRcvd[1][j] += nodes[i].net.T3PktsRcvd[1][j]; } } printf( "NET -- pkts sent: %10d, rcvd: %10d, avg hop: %.3f, " "backoff delay: %.3f\n", sent, recv, hop/recv_data, delay/samples); printf( "NET -- hop counts ["); for( i = 0; i < HCArraySize; i++) printf( " %4d", hopCounts[ i]); printf( "]\nNET -- T2 Sent ["); for( i = 0; i < NumT2Slots; i++) printf( " %4d", T2PktsSent[ i]); printf( "]\nNET -- T3 Sent ["); for( i = 0; i < NumT3Slots; i++) printf( " %4d", T3PktsSent[ i]); printf( "]\nNET -- T1 Sent %4d; Dropped Packets %4d; T3 Abnormal %4d\n" "NET -- T2 Collisions [", T1PktsSent, dropPkts, t3AbnormalPktsSent); for( i = 0; i < NumT2Slots; i++) printf( " %4d", T2Colls[ i]); printf( "]\nNET -- T3 Collisions ["); for( i = 0; i < NumT3Slots; i++) printf( " %4d", T3Colls[ i]); printf( "]\nNET -- T1 Collisions %4d; Ignored Collisions %4d\n", T1Colls, IgnColls); numCollisions = 0; //mwl for( sent = recv = i = 0; i < NumNodes; i++) { sent += nodes[i].mac.SentPackets; recv += nodes[i].mac.RecvPackets; numCollisions += nodes[i].mac.rrCollisions; } printf( "NET -- T1 Rcvd [ %4d %4d]\n", T1PktsRcvd[0], T1PktsRcvd[1]); for( j = 0; j < 2; j++) { printf( "NET -- T2 Rcvd[%s] [", nodes[0].net.pktRcvdName[j]); for( i = 0; i < NumT2Slots; i++) printf( " %4d", T2PktsRcvd[j][i]); printf( "]\n"); } for( j = 0; j < 2; j++) { printf( "NET -- T3 Rcvd[%s] [", nodes[0].net.pktRcvdName[j]); for( i = 0; i < NumT3Slots; i++) printf( " %4d", T3PktsRcvd[j][i]); printf( "]\n"); } printf( "NET -- T4 Rcvd [ %4d %4d]\n", T4PktsRcvd[0], T4PktsRcvd[1]); printf( "NET -- Cancel[ %4d %4d]; Bad[ %4d %4d]; WrongHop[ %4d %4d]\n", CanceledPktsRcvd[0], CanceledPktsRcvd[1], BadAddrPktsRcvd[0], BadAddrPktsRcvd[1], WrongHopPktsRcvd[0], WrongHopPktsRcvd[1]); printf( "MAC -- pkts sent: %10d, rcvd: %10d, # collisions %10d\n", sent, recv, numCollisions); { 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); }//mwl for( i = 0; i < NumNodes; i++)//mwl printf( "ea %3d: 4 hc %2d; 69 hc %2d\n", i, nodes[i].net.mwlGetHC( 4), nodes[i].net.mwlGetHC( 69)); 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 :: Setup(){ int i,j; /************************************************************************ * 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); // don't forget to call this function for each sensor node } /************************************************************************ * Set the slot width, transition time. * This is done only once since they are static (class wide) for the SSRv02 * 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.setNumT2Slots( NumT2Slots); nodes[0].net.setNumT3Slots( NumT3Slots); /************************************************************************ * 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.NumNodes=NumNodes; channel.DumpPackets=false; channel.CSThresh=nodes[0].phy.CSThresh; channel.RXThresh=nodes[0].phy.RXThresh; channel.PropagationModel=channel.FreeSpace; channel.WaveLength=speed_of_light/nodes[0].phy.Frequency; channel.X=MaxX; channel.Y=MaxY; channel.GridEnabled=false; channel.MaxTXPower=nodes[0].phy.TXPower; /************************************************************************ * 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. ************************************************************************/ int src,dst; for(i=0;i<NumSourceNodes;i++) { for(j=0;j<NumConnections;j++) { double dx,dy; do { src=Random(NumNodes); dst=Random(NumNodes); dx=nodes[src].mob.InitX-nodes[dst].mob.InitX; dy=nodes[src].mob.InitY-nodes[dst].mob.InitY; } while( src == dst || (dx*dx+dy*dy) < MaxX * MaxY / 4); nodes[src].SrcOrDst=true; nodes[dst].SrcOrDst=true; nodes[src].app.Connections.push_back( make_triple(ether_addr_t(dst),Random(PacketSize)+PacketSize/2, Random(Interval)+Interval/2)); nodes[dst].app.Connections.push_back( make_triple(ether_addr_t(src),Random(PacketSize)+PacketSize/2, Random(Interval)+Interval/2)); } } for(i=0;i<NumNodes;i++) { nodes[i].pm.BeginTime=0.0; nodes[i].pm.FinishTime=StopTime; nodes[i].pm.ActiveCycle=100.0; nodes[i].pm.ActivePercent=ActivePercent; if(nodes[i].SrcOrDst) { nodes[i].pm.ActivePercent=1.0; } //nodes[i].mob.InitX=(i/20)*MaxX/20; //nodes[i].mob.InitY=(i%20)*MaxY/20; } /*nodes[260].app.Connections.push_back(make_triple(ether_addr_t(273),1000,5.34)); nodes[29].app.Connections.push_back(make_triple(ether_addr_t(235),1000,1.0203)); */}/************************************************************************ * @<h2>Running the Simulation</h2>@ * To run the simulation, first we need to create a * simulation object from the simulation component class. * Several default simulation parameters must be determined. * @StopTime@ denotes the ending time of the simulation. * @Seed@ is the initial seed of the random number generator used * by the simulation. * * To compile the program, enter: * * ../../bin/cxx sim_rr.cc * g++ -Wall -o sim_rr sim_rr.cxx * * To run the simulation, simply type in: * * sim_rr <flags> * mwl ************************************************************************/int main( int argc, char *argv[]){ RoutingSim sim; int seed; bool doDecrement, proceed = true; const char *UnsupportedOption = "***** Unsupported option %s *****\n"; seed = (int) time( 0); for( argv++; argc > 1; argc--, argv++) { if( *argv[0] != '-') { printf( UnsupportedOption, *argv); proceed = false; continue; } doDecrement = true; switch( (*argv)[1]) { case '2': // number of slots in T2 sim.NumT2Slots = atoi( argv[1]); if( sim.NumT2Slots > MaxT2Slots) { printf( "Simulation supports only %d slots in T2.\n", MaxT2Slots); proceed = false; } break; case '3': // number of slots in T3 sim.NumT3Slots = atoi( argv[1]); if( sim.NumT3Slots > MaxT3Slots) { printf( "Simulation supports only %d slots in T3.\n", MaxT3Slots); proceed = false; } break; case 'a': // percent active sim.ActivePercent = atof( argv[1]); if( sim.ActivePercent > 1.0) { printf( "***** Warning: Nodes active more than 100%% of the time *****\n"); proceed = false; } break; case 'e': // simulation end time sim.StopTime = atof( argv[1]); break; case 'i': // interval sim.Interval = atof( argv[1]); break; case 'n': // number of nodes sim.NumNodes = atoi( argv[1]); break; case 'p': // antenna transmission power in watts sim.TXPower = atof( argv[1]); break; case 'r': // random number seed seed = atoi( argv[1]); break; case 's': // number of sources sim.NumSourceNodes = atoi( argv[1]); break; case 't': // transition timer sim.TransitionTime = atof( argv[1]); break; case 'w': // slot width sim.SlotWidth = atof( argv[1]); break; case 'x': // x (and y) dimension sim.MaxX = sim.MaxY = atof( argv[1]); break; default: doDecrement = false; proceed = false; printf( UnsupportedOption, *argv); break; } if( doDecrement == true) { argv++; argc--; } } if( proceed == false) return 1; if( sim.NumSourceNodes == 0) sim.NumSourceNodes = sim.NumNodes / 10; sim.Seed = seed;/*** Display parameters that define the simulation.*/ printf( "Fault Tolerant SSR:\t");#ifdef RR_ACK printf( "RR_ACK defined\n");#else //RR_ACK printf( "RR_ACK not defined\n");#endif //RR_ACK printf( "Random number seed:\t%d\nStopTime:\t\t%.0f\n" "Number of Nodes:\t%d\nPercent Active:\t\t%5.2f\n" "Terrain:\t\t%.0f by %.0f\nNumber of Sources:\t%d\n" "Packet Size:\t\t%d\nInterval:\t\t%f\nAntenna Power:\t\t%f\n" "Slot Width:\t\t%f\nTransition Time:\t%f\n# slots in T2\t\t%d\n" "# slots in T3\t\t%d\n", seed, sim.StopTime, sim.NumNodes, sim.ActivePercent * 100.0, sim.MaxX, sim.MaxY, sim.NumSourceNodes, sim.PacketSize, sim.Interval, sim.TXPower, sim.SlotWidth, sim.TransitionTime, sim.NumT2Slots, sim.NumT3Slots); sim.Setup(); sim.Run(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -