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

📄 sim_routing.cc

📁 无限传感器网络的模拟环境
💻 CC
📖 第 1 页 / 共 2 页
字号:
 *  These three lines are commented out.  They are used when  *  the net component is directly connected to the mac component *  without going through the queue. *******************************************************************/    //connect mac.to_network_data, net.from_mac_data ;    //connect mac.to_network_ack, net.from_mac_ack;    //connect net.to_mac, mac.from_network;        connect mac.to_phy, phy.from_mac;    connect phy.to_mac, mac.from_phy;    connect phy.to_power_switch, pm.switch_state;    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 that 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;    SrcOrDst=false;}/******************************************************************* *  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;    double ActivePercent;/******************************************************************* *  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,recv_data;    double delay;    double hop;    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, delay: %.3f\n",	   sent,recv,(double)recv/sent,delay/recv);    recv_data=recv;    hop=0.0;    for(sent=recv=i=0;i<NumNodes;i++)    {	sent+=nodes[i].net.SentPackets;	recv+=nodes[i].net.RecvPackets;	hop+=nodes[i].net.TotalHop;    }    printf("%f %d\n", hop, recv_data);    printf("NET -- packets sent: %d, received: %d, average hop: %.3f\n",sent,recv,hop/recv_data);    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++)	{	    double dx,dy;	    do	    {		src=Random(NumNodes);		dst=Random(NumNodes);		dx=nodes[src].mob.getX() - nodes[dst].mob.getX();		dy=nodes[src].mob.getY() - nodes[dst].mob.getY();	    }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();      if( nodes[i].SrcOrDst == true)	nodes[i].pm.failureStats( 0.0);      else	nodes[i].pm.failureStats( (double) 100.0 * i/ NumNodes, 100.0,				  ActivePercent);    }    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] [ActivePercent] [PacketSize] [Interval] ************************************************************************/int main(int argc, char* argv[]){    RoutingSim sim;    sim.StopTime( 1000);    sim.Seed = 2345;    sim.MaxX = 2000;    sim.MaxY = 2000;    sim.NumNodes = 110;    sim.NumConnections = 1;    sim.PacketSize = 1000;    sim.Interval = 20.0;    sim.ActivePercent = 1.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.ActivePercent = atof(argv[5]);    if(argc >= 7) sim.PacketSize = atoi(argv[6]);    if(argc >= 8) sim.Interval = atof(argv[7]);     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 + -