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

📄 sim_routing.cc

📁 SensorSimII is the framework of a simulator that I have been working on to study how future sensor n
💻 CC
📖 第 1 页 / 共 2 页
字号:
    phy.ID=ID;/******************************************************************* *  Now we can establish the connections between components.  The connections *  will become much clearer if we look at the diagram. *******************************************************************/    connect app.to_transport, net.from_transport;    connect net.to_transport, app.from_transport;        connect net.to_mac, queue.in;    connect queue.out, mac.from_network;    connect mac.to_network_ack, queue.next;    connect queue.ack, net.from_mac_ack;    connect mac.to_network_data, net.from_mac_data ;    /******************************************************************* *  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 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;}/******************************************************************* *  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;    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, delay: %.3f\n",		sent,recv,(double)recv/sent,delay/recv);    for(sent=recv=i=0;i<NumNodes;i++)    {		sent+=nodes[i].net.SentPackets;		recv+=nodes[i].net.RecvPackets;    }    printf("NET -- packets sent: %d, received: %d\n",sent,recv);    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    }    /************************************************************************ * 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 cann't detect them.  @RXThresh@ is also needed to produce * the same receive power in those nodes that can just correctly recieve 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;/************************************************************************ * 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));		}    }}/************************************************************************ * @<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 = 1234;    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]);     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 + -