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

📄 ratecontrolledscheduler.java

📁 实现网格环境下资源调度和分配的仿真
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                System.out.println(super.get_name() +".enque(): Warning - " +
                    " packet class = " + type + ", num of classes = " +
                    packets_.length);
                type = 0;
            }

            packets_[type].add(pkt);  // add a packet into its respective class
            if (packets_[type].size() == 1)
            {
                // send an internal event to itself
                double delay = (pkt.getSize() * BITS) / rates_[type];
                Integer obj = (Integer) classType_.get(type);
                super.sim_schedule(super.get_id(),delay,INTERNAL_DEQUEUE,obj);
            }
        }
        catch (Exception e) {
            // ... empty
        }
    }

    /**
     * In this scheduler, the packet returned is always from the head of the
     * queue.
     * @param ev    a Sim_event object
     * @pre ev != null
     * @post $none
     */
    private void internalDequeue(Sim_event ev)
    {
        if (ev == null) {
            return;
        }

        try
        {
            Integer classTypeInt = (Integer) ev.get_data();
            int type = classTypeInt.intValue();

            // check whether the class type is correct or not
            if (type >= packets_.length)
            {
                System.out.println(super.get_name() +
                    ".internalDequeue(): Warning - packet class = " + type +
                    ", num of classes = " + packets_.length);
                type = 0;
            }

            // if no packets in the array, then exit
            if (packets_[type].isEmpty() == true) {
                return;
            }

            // add to final buffer
            Packet pkt = (Packet) packets_[type].remove(0);
            pktList_.add(pkt);

            // send an internal event to itself
            double delay = 0;
            if (pktList_.size() == 1)
            {
                delay = (pkt.getSize() * BITS) / baudRate_;
                super.sim_schedule(super.get_id(), delay, DEQUEUE_PACKET);
            }

            // schedule the next packet
            if (packets_[type].isEmpty() == false)
            {
                Packet nextPkt = (Packet) packets_[type].get(0);

                // rate limit next packet
                delay = (nextPkt.getSize() * BITS) / rates_[type];
                super.sim_schedule(super.get_id(), delay, INTERNAL_DEQUEUE,
                                   classTypeInt);
            }
        }
        catch (Exception e) {
            // .... empty
        }
    }

    /**
     * Dequeues a packet and send it to a router
     * @param ev    a Sim_event object
     * @pre ev != null
     * @post $none
     */
    private synchronized void dequeue(Sim_event ev)
    {
        if (ev == null || pktList_.isEmpty() == true) {
            return;
        }

        try
        {
            Packet pkt = (Packet) pktList_.remove(0);
            super.sim_schedule(routerID_, 0, GridSimTags.SCHEDULER_DEQUE, pkt);
            if (pktList_.isEmpty() == false)
            {
                Packet nextPkt = (Packet) pktList_.get(0);
                double delay = (nextPkt.getSize() * BITS) / baudRate_;
                super.sim_schedule(super.get_id(), delay, DEQUEUE_PACKET);
            }
        }
        catch (Exception e) {
            // ... empty
        }
    }

    /**
     * This method allows you to set different rates for different types of
     * traffic. Traffic of class <tt>n</tt> are assigned a rate of
     * <tt>rates[n]</tt>.
     * The higher the rate of a class, the better the service it receives.
     * NOTE: Each rate must be a positive number.
     *
     * @param rates   a linear array of the rates to be assigned to different
     *                classes of traffic.
     * @pre rates != null
     * @post $none
     */
    public boolean setRates(double[] rates)
    {
        // error checking
        if (rates == null || rates.length != numClasses_) {
            return false;
        }

        // the value of each rate must be a positive number
        for (int i = 0; i < rates.length; i++)
        {
            if (rates[i] <= 0)
            {
                System.out.println(super.get_name() +
                    ".setRates(): Error - the rate must be a positive number.");
                return false;
            }
        }

        this.rates_ = rates;
        return true;
    }

    /**
     * Determines whether the scheduler is currently keeping any packets in
     * its queue(s).
     *
     * @return <tt>true</tt> if no packets are enqueued, <tt>false</tt>
     *         otherwise
     * @pre $none
     * @post $none
     */
    public synchronized boolean isEmpty()
    {
        for (int i = 0; i < numClasses_; i++)
        {
            if (packets_[i].isEmpty() == false) {
                return false;
            }
        }

        return true;
    }

    /**
     * Determines the number of packets that are currently enqueued in this
     * scheduler.
     *
     * @return the number of packets enqueud by this scheduler.
     * @pre $none
     * @post $none
     */
    public synchronized int size()
    {
        int size = 0;
        for (int i = 0; i < numClasses_; i++) {
            size += (packets_[i]).size();
        }

        return size;
    }

    /**
     * Returns the baud rate of the egress port that is using this scheduler.
     * If the baud rate is zero, it means you haven't set it up.
     * @return the baud rate in bits/s
     * @see gridsim.net.PacketScheduler#setBaudRate(double)
     * @pre $none
     * @post $result >= 0
     */
    public double getBaudRate() {
        return baudRate_;
    }

    /**
     * Sets the baud rate that this scheduler will be sending packets at.
     * @param rate the baud rate of this scheduler (in bits/s)
     * @pre rate > 0
     * @post $none
     */
    public boolean setBaudRate(double rate)
    {
        if (rate <= 0) {
            return false;
        }

        baudRate_ = rate;
        return true;
    }

    /**
     * Gets the name of this scheduler.
     * @return the name of this scheduler
     * @pre $none
     * @post $none
     */
    public String getSchedName() {
        return super.get_name();
    }

    /**
     * Gets the ID of this scheduler.
     * @return the ID of this scheduler or <tt>-1</tt> if no ID is found
     * @pre $none
     * @post $none
     */
    public int getSchedID() {
        return super.get_id();
    }

    /**
     * Sets the router ID that hosts this scheduler.
     * @param routerID  the router ID that hosts this scheduler
     * @return <tt>true</tt> if successful or <tt>false</tt> otherwise
     * @pre routerID > 0
     * @post $none
     */
    public boolean setRouterID(int routerID)
    {
        if (routerID <= 0) {
            return false;
        }

        this.routerID_ = routerID;
        return true;
    }

    /**
     * Gets the router ID that hosts this scheduler.
     * @return the router ID or <tt>-1</tt> if no ID is found
     * @pre $none
     * @post $none
     */
    public int getRouterID() {
        return routerID_;
    }

    /**
     * Puts a packet into the queue -- <b>This method is not used</b>
     *
     * @param np    A Packet to be enqued by this scheduler.
     * @return <tt>true</tt> if enqued, <tt>false</tt> otherwise
     * @pre pnp != null
     * @post $none
     */
    public boolean enque(Packet np)
    {
        System.out.println(super.get_name()+".enque(): This method is empty.");
        return false;
    }

    /**
     * The method deque() has to decide which queue is to be
     * served next -- <b>This method is not used</b>
     *
     * @return the packet to be sent out or <tt>null</tt> if empty
     * @pre $none
     * @post $none
     */
    public Packet deque()
    {
        System.out.println(super.get_name()+".deque(): This method is empty.");
        return null;
    }

} // end class

⌨️ 快捷键说明

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