?? netex03.java
字號:
/*
* Author: Anthony Sulistio
* Date: November 2004
* Description: A simple program to demonstrate of how to use GridSim
* network extension package.
* This example shows how to create user and resource
* entities connected via a network topology, using link
* and router.
* In addition, background traffic functionality is explained
* in this example.
*/
import gridsim.*;
import gridsim.net.*;
import java.util.*;
import gridsim.util.TrafficGenerator; // for background traffic
import eduni.simjava.distributions.*; // for background traffic
/**
* Test Driver class for this example
*/
public class NetEx03
{
/**
* Creates main() to run this example
*/
public static void main(String[] args)
{
System.out.println("Starting network example ...");
try
{
//////////////////////////////////////////
// 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 = 2; // number of grid users
Calendar calendar = Calendar.getInstance();
// a flag that denotes whether to trace GridSim events or not.
boolean trace_flag = true;
// Initialize the GridSim package
System.out.println("Initializing GridSim package");
GridSim.init(num_user, calendar, trace_flag);
//////////////////////////////////////////
// Second step: Creates one or more GridResource entities
double baud_rate = 1000; // bits/sec
double propDelay = 10; // propagation delay in millisecond
int mtu = 1500; // max. transmission unit in byte
int i = 0;
// 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);
}
//////////////////////////////////////////
// Third 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);
// add a user into a list
userList.add(user);
userNameList.add(name);
}
//////////////////////////////////////////
// Fourth step: Builds the network topology among entities.
// In this example, the topology is:
// user(s) --1Mb/s-- r1 --10Mb/s-- r2 --1Mb/s-- GridResource(s)
// create the routers.
// If trace_flag is set to "true", then this experiment will create
// the following files (apart from sim_trace and sim_report):
// - router1_report.csv
// - router2_report.csv
Router r1 = new RIPRouter("router1", trace_flag); // router 1
Router r2 = new RIPRouter("router2", trace_flag); // router 2
// generates some background traffic using SimJava2 distribution
// package. NOTE: if you set the values to be too high, then
// the simulation might finish longer
TrafficGenerator tg = new TrafficGenerator(
new Sim_uniform_obj("freq",1,3), // num of packets
new Sim_uniform_obj("inter_arrival_time",10,20) );
// connect all user entities with r1 with 1Mb/s connection
// For each host, specify which PacketScheduler entity to use.
NetUser obj = null;
for (i = 0; i < userList.size(); i++)
{
// A First In First Out Scheduler is being used here.
// SCFQScheduler can be used for more fairness
FIFOScheduler userSched = new FIFOScheduler("NetUserSched_"+i);
obj = (NetUser) userList.get(i);
r1.attachHost(obj, userSched);
// for even user number
if (i % 2 == 0)
{
// for each time, sends junk packet(s) to all entities
tg.setPattern(TrafficGenerator.SEND_ALL);
// sends junk packet(s) to resource and user entities
obj.setBackgroundTraffic(tg, userNameList);
}
else // for odd user number
{
// for each time, sends junk packet(s) to only one entity
tg.setPattern(TrafficGenerator.SEND_ONE_ONLY);
// sends junk packet(s) to resource entities only
obj.setBackgroundTraffic(tg);
}
}
// connect all resource entities with r2 with 1Mb/s connection
// For each host, specify which PacketScheduler entity to use.
GridResource resObj = null;
for (i = 0; i < resList.size(); i++)
{
FIFOScheduler resSched = new FIFOScheduler("GridResSched_"+i);
resObj = (GridResource) resList.get(i);
r2.attachHost(resObj, resSched);
}
// then connect r1 to r2 with 10Mb/s connection
// For each host, specify which PacketScheduler entity to use.
baud_rate = 10000;
Link link = new SimpleLink("r1_r2_link", baud_rate, propDelay, mtu);
FIFOScheduler r1Sched = new FIFOScheduler("r1_Sched");
FIFOScheduler r2Sched = new FIFOScheduler("r2_Sched");
// attach r2 to r1
r1.attachRouter(r2, link, r1Sched, r2Sched);
//////////////////////////////////////////
// Fifth step: Starts the simulation
GridSim.startGridSimulation();
//////////////////////////////////////////
// Final step: Prints the Gridlets when simulation is over
// also prints the routing table
r1.printRoutingTable();
r2.printRoutingTable();
GridletList glList = null;
for (i = 0; i < userList.size(); i++)
{
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");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -