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

📄 mm1.cc

📁 无限传感器网络的模拟环境
💻 CC
📖 第 1 页 / 共 2 页
字号:
    return;}/************************************************************************ * When asked for the next packet, give the first one in the queue. If the * queue is empty, change the value of @m_busy@ to indicate the server * is now free so that the next arriving packet will * directly go through without staying in the queue first. ************************************************************************/template < class DATATYPE >void FIFO <DATATYPE> :: next(){    if (m_queue.size()>0)    {        out(m_queue.front());        m_queue.pop_front();    }     else    {        m_busy=false;    }    return;}/************************************************************************ * @<h2>Server</h2>@ * @Sever@ is a template component too. It simulates the * service for each incoming packet. Ports are similar to those of * the @FIFO@ component, except that the @next@ port is an * outport, which is to be connected with the @next@ port of the * @FIFO@ component. The timer @wait@ is used to schedule the event representing * the completion of the service. ************************************************************************/template <class DATATYPE>component Server : public TypeII{public:	virtual ~Server() {}    double service_time;  // the average serice time/************************************************************************ * Packets arrive at the inport @in@, after a random service time, depart * from @out@. Accompanying the departure of an event, a signal must also be * sent out through the outport @next@ to indicate that the server is now * ready to receive the next packet. ************************************************************************/    inport inline void in(DATATYPE&);    outport void out(DATATYPE&);    outport void next();    inport inline void depart(trigger_t&);    Timer<trigger_t> wait;    Server();private:    DATATYPE m_packet; // };/************************************************************************ * The outport @to_component@ of the timer component must be connected to * the @depart()@ inport. ************************************************************************/template <class DATATYPE>Server<DATATYPE>::Server(){    connect wait.to_component,depart;}/************************************************************************ * When a packet comes, schedule the service completion event. ************************************************************************/template <class DATATYPE>void Server<DATATYPE> :: in(DATATYPE& packet){    m_packet=packet;    wait.Set(SimTime()+Exponential(service_time));    return;}/************************************************************************ * When it is time for the packet to depart (the service completion event arrives),  * write it to the outport @out@, and at the same time send a trigger signal to the * outport @next@. ************************************************************************/template <class DATATYPE>void Server <DATATYPE> :: depart(trigger_t&){    out(m_packet);    next();    return;}/************************************************************************ * @<h2>Sink</h2>@ * In the @Sink@ component, we collect the time that each packet spent in * the @FIFO@ queue and the server. It only has one inport and no timer. * What is new here is the @Stop()@ function, which * is called when the simulation reaches the preset end time. ************************************************************************/component Sink : public TypeII{public:    inport inline void in(packet_t&);    void Start()    {        m_total=0.0;        m_number=0;    }    Sink() {}    void Stop()    {        printf("Average packet delay is: %f (%d packets) \n",	       m_total/m_number,m_number);    }private:    double m_total;    int m_number;    packet_t m_packet;};void Sink::in(packet_t &packet){    m_packet=packet;    m_packet.departure_time=SimTime();    m_total+=m_packet.departure_time-m_packet.arrival_time;    m_number++;    return;}/************************************************************************ * @<h2>Constructing the Simulation</h2>@ * The simulation component is derived from the @CostSimEng@ class.  Components  * are instantiated as public members. ************************************************************************/component MM1 : public CostSimEng{public:    void Setup();/************************************************************************ * Several simulation parameters.   ************************************************************************/    double interval;    int queue_length;    double service_time;/************************************************************************ * Components are instantiated as public members. ************************************************************************/private:    Source source;    FIFO <packet_t> fifo;    Server <packet_t> server;    Sink sink;};/************************************************************************ * 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 give the simulation an opportunity to assign values to * its parameters after the simulation component has been instantiated.  *  The @Setup()@ function, which you can rename, 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 MM1::Setup(){    source.interval=interval;    fifo.queue_length=queue_length;    server.service_time=service_time;    connect source.out,fifo.in;    connect fifo.out,server.in;    connect server.next,fifo.next;    connect server.out,sink.in;}/************************************************************************ * @<h2>Running the Simulation</h2>@ * To run the M/M/1 simulation, first we need to create an M/M/1 * simulation object. 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 run the simulation, simply type in: * * mm1-cost [stop time] [random seed] *  ************************************************************************/int main(int argc, char* argv[]){    MM1 mm1;    mm1.interval=1;    mm1.queue_length=100;    mm1.service_time=0.5;    mm1.StopTime( 1000000.0);    mm1.Seed=10;    if (argc>=2)      mm1.StopTime( atof(argv[1]));    if (argc>=3)      mm1.Seed=atoi(argv[2]);    mm1.Setup(); // must be called first    mm1.Run(); // run the simulation    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -