📄 sim_ssab.cc
字号:
connect pm.to_battery_query, battery.query_in; connect pm.to_battery_power, battery.power_in;/******************************************************************* * These three connect statements are different. All above ones * are between an outport of a subcomponent and an outport of another * subcomponent, while these three are between a port of the sensor * node and a port of a subcomponent. We can view these connections * as mapping from the ports of subcomponents to its own ports, i.e., * to expose the ports of internal components. * Also remember the connect statement is so designed that it can take * only two ports, and than packets always flow through from the first port * to the second port, so when connection two inports, the inport of * the subcomponent must be placed in the second place. *******************************************************************/ connect phy.to_channel, to_channel_packet; connect mob.pos_out, to_channel_pos; connect from_channel, phy.from_channel;/******************************************************************* * This connection lets the power manager notify the network protocol that * the node has become active again. *******************************************************************/ connect pm.from_pm_node_up, net.from_pm_node_up; return;}/******************************************************************* * Once we have the sensor node component ready, we can start to * build the entire simulation, which is named @RoutingSim@. It must * be derived from the simulation engine class @CostSimEng@. * This is the structure of the network. * * @<center><img src=sim_flooding_net.gif></center>@ *******************************************************************/component RoutingSim : public CostSimEng{public: void Start(); void Stop();/******************************************************************* * These are simulation parameters. We don't want configurators of * the simulation to access the parameters of those inter-components. *******************************************************************/ double MaxX, MaxY; int NumNodes; int NumSourceNodes; int NumConnections; int PacketSize; double Interval;/******************************************************************* * Here we declare sense nodes as an array of @SensorNode@, and a * channel component. *******************************************************************/ SensorNode[] nodes; SimpleChannel < mac_packet_t > channel; void Setup();};void RoutingSim :: Start(){}/******************************************************************* * After the simulation is stopped, we will collect some statistics. *******************************************************************/void RoutingSim :: Stop(){ int i,sent,recv,hop,samples; double app_recv; double delay; 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 -- packets sent: %d, received: %d, success rate: %.3f, end-to-end delay: %.3f\n", sent,recv,(double)recv/sent,delay/recv); app_recv=recv; delay=0.0; samples=0; for(hop=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; } printf("NET -- packets sent: %d, received: %d, average hop: %.3f, backoff delay: %.3f\n",sent,recv,hop/app_recv, delay/samples); for(sent=recv=i=0;i<NumNodes;i++) { sent+=nodes[i].mac.SentPackets; recv+=nodes[i].mac.RecvPackets; } printf("MAC -- packets sent: %d, received: %d\n",sent,recv);}/************************************************************************ * 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(); // don't forget to call this function for each sensor node } /********************************************************************* * 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 sim_shr.h. *********************************************************************/ 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. ************************************************************************/ int src,dst; for(i=0;i<NumSourceNodes;i++) { for(j=0;j<NumConnections;j++) { do { src=Random(NumNodes); dst=Random(NumNodes); }while(src==dst); nodes[src].app.Connections.push_back( make_triple(ether_addr_t(dst),Random(PacketSize)+PacketSize/2, Random(Interval)+Interval/2)); } }/********************************************************************* * Initialize the power manager for each node. *********************************************************************/ for(i=0;i<NumNodes;i++) { nodes[i].pm.BeginTime=0.0; nodes[i].pm.FinishTime=StopTime(); nodes[i].pm.failureStats( 0.0); } return;}/************************************************************************ * @<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_routing.cc * g++ -Wall -o sim_routing sim_routing.cxx * * To run the simulation, simply type in: * * sim_routing [StopTime] [NumNodes] [MaxX] [NumSourceNodes] [PacketSize] [Interval] ************************************************************************/int main(int argc, char* argv[]){ RoutingSim sim; sim.StopTime( 1000); sim.Seed = 234; sim.MaxX = 2000; sim.MaxY = 2000; sim.NumNodes = 110; sim.NumConnections = 2; sim.PacketSize = 2000; sim.Interval = 100.0; if(argc >= 2) sim.StopTime( atof(argv[1])); if(argc >= 3) sim.NumNodes = atoi(argv[2]); sim.NumSourceNodes = sim.NumNodes / 10; if(argc >= 4) sim.MaxX = sim.MaxY = atof(argv[3]); if(argc >= 5) sim.NumSourceNodes = atoi(argv[4]); if(argc >= 6) sim.PacketSize = atoi(argv[5]); if(argc >= 7) sim.Interval = atof(argv[6]); #ifdef USING_SSAB printf("Using SSAB\n");#else printf("Not using SSAB\n");#endif printf("StopTime: %.0f, Number of Nodes: %d, Terrain: %.0f by %.0f\n", sim.StopTime(), sim.NumNodes, sim.MaxX, sim.MaxY); printf("Number of Sources: %d, Packet Size: %d, Interval: %f\n", sim.NumSourceNodes, sim.PacketSize, sim.Interval); sim.Setup(); sim.Run(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -