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

📄 rateexample.java

📁 中間件開發详细说明:清华大学J2EE教程讲义(ppt)-Tsinghua University J2EE tutorial lectures (ppt) [上载源码成为会员下载此源码] [成为VIP会
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Author: Anthony Sulistio
 * Date: March 2006
 * Description: A simple program to demonstrate of how to use GridSim
 *              network extension package.
 *              This example shows how to use a Rate Controlled packet 
 *              scheduler. In addition, this example shows how to read a network
 *              topology from a given file.
 */

import gridsim.*;
import gridsim.net.*;
import java.util.*;
import gridsim.util.NetworkReader;   // for reading a network topology


/**
 * Test Driver class for this example
 */
public class RateExample
{
    /**
     * Creates main() to run this example
     */
    public static void main(String[] args)
    {
        System.out.println("Starting network example ...");

        try
        {
            if (args.length < 1) {
                System.out.println("Usage: java RateExample network.txt");
                return;
            }

            // get the network topology
            String filename = args[0];

            //////////////////////////////////////////
            // First step: Initialize the GridSim package. It should be called
            // before creating any entities. We can't run this example without
            // initializing GridSim first. We will get run-time exception
            // error.
            int num_user = 3;   // number of grid users
            Calendar calendar = Calendar.getInstance();

            // a flag that denotes whether to trace GridSim events or not.
            boolean trace_flag = false;

            // Initialize the GridSim package
            System.out.println("Initializing GridSim package");
            GridSim.init(num_user, calendar, trace_flag);

            //////////////////////////////////////////
            // Second step: Builds the network topology among Routers.

            // use RateControlled packet scheduler. Hence, determine the
            // percentage bandwidth allocation for each Type of Service (ToS).
            // For example, ToS = 0 will get 10%, ToS = 1 will get 35%, etc
            double[] percentage = {10, 35, 55};  // % of total bandwidth
            System.out.println("Reading network from " + filename);
            LinkedList routerList = NetworkReader.createRate(filename, percentage);

            //////////////////////////////////////////
            // Third step: Creates one or more GridResource entities

            double baud_rate = 100000000;  // 100 Mbps
            double propDelay = 10;   // propagation delay in millisecond
            int mtu = 1500;          // max. transmission unit in byte
            int i = 0;

            // link the resources into R5
            Router router = NetworkReader.getRouter("R5", routerList);

            // more resources can be created by
            // setting totalResource to an appropriate value
            int totalResource = 1;
            ArrayList resList = new ArrayList(totalResource);
            for (i = 0; i < totalResource; i++)
            {
                GridResource res = createGridResource("Res_"+i, baud_rate,
                                                      propDelay, mtu);

                // add a resource into a list
                resList.add(res);

                // link a resource to this router object
                linkNetwork(router, res, percentage);
            }

            //////////////////////////////////////////
            // Fourth step: Creates one or more grid user entities

            // number of Gridlets that will be sent to the resource
            int totalGridlet = 5;

            // create users
            ArrayList userList = new ArrayList(num_user);
            ArrayList userNameList = new ArrayList(); // for background traffic
            for (i = 0; i < num_user; i++)
            {
                String name = "User_" + i;

                // if trace_flag is set to "true", then this experiment will
                // create User_i.csv where i = 0 ... (num_user-1)
                NetUser user = new NetUser(name, totalGridlet, baud_rate,
                                           propDelay, mtu, trace_flag);

                // for each user, determines the Type of Service (ToS)
                // for sending objects over the network. The greater ToS is,
                // the more bandwidth allocation
                int ToS = i;
                if (i > percentage.length) {
                    ToS = 0;
                }
                user.setNetServiceLevel(ToS);

                // link a User to a router object
                if (i == 0) {
                    router = NetworkReader.getRouter("R1", routerList);
                }
                else if (i == 1) {
                    router = NetworkReader.getRouter("R2", routerList);
                }
                else {
                    router = NetworkReader.getRouter("R3", routerList);
                }
                linkNetwork(router, user, percentage);

                // add a user into a list
                userList.add(user);
                userNameList.add(name);
            }

            //////////////////////////////////////////
            // Fifth step: Starts the simulation
            GridSim.startGridSimulation();

            //////////////////////////////////////////
            // Final step: Prints the Gridlets when simulation is over

            // also prints the routing table
            router = NetworkReader.getRouter("R3", routerList);
            router.printRoutingTable();
            router = NetworkReader.getRouter("R4", routerList);
            router.printRoutingTable();

            GridletList glList = null;
            for (i = 0; i < userList.size(); i++)
            {
                NetUser obj = (NetUser) userList.get(i);
                glList = obj.getGridletList();
                printGridletList(glList, obj.get_name(), false);
            }

            System.out.println("\nFinish network example ...");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("Unwanted errors happen");
        }
    }

    /**
     * Links a particular entity with a given Router.
     * Since we are using the RateControlled packet scheduler, we need to
     * specify the percentage of bandwith allocation for
     * each Type of Service (ToS).
     *
     * @param router    a Router object
     * @param obj       a GridSim entity to be attached to the Router
     * @param percentage    a percentage of bandwith allocation
     */
    private static void linkNetwork(Router router, GridSimCore obj,
                                    double[] percentage) throws Exception
    {
        if (router == null) {
            System.out.println("Error - router is NULL !!");
            return;
        }

        // get the baud rate of a link
        double baud_rate = obj.getLink().getBaudRate();

        // create the packet scheduler for this link
        PacketScheduler pktObj = new RateControlledScheduler(router.get_name() +
                                 "_to_" + obj.get_name(), percentage.length);

        // for each percentage, determine the allocated baud rate
        double[] rate = new double[percentage.length];
        for (int k = 0; k < rate.length; k++)
        {
            double value = baud_rate * percentage[k] / 100;
            rate[k] = value;

⌨️ 快捷键说明

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