|
From: <bro...@us...> - 2008-02-22 04:36:38
|
Revision: 126
http://gridsim.svn.sourceforge.net/gridsim/?rev=126&view=rev
Author: brobergj
Date: 2008-02-21 20:36:44 -0800 (Thu, 21 Feb 2008)
Log Message:
-----------
*Initial upload of flow networking example
Added Paths:
-----------
branches/gridsim4.0-branch2/examples/FlowNetEx01/
branches/gridsim4.0-branch2/examples/FlowNetEx01/FlowNetEx01.java
branches/gridsim4.0-branch2/examples/FlowNetEx01/FlowNetUser.java
branches/gridsim4.0-branch2/examples/FlowNetEx01/FlowTest.java
branches/gridsim4.0-branch2/examples/FlowNetEx01/README.txt
Added: branches/gridsim4.0-branch2/examples/FlowNetEx01/FlowNetEx01.java
===================================================================
--- branches/gridsim4.0-branch2/examples/FlowNetEx01/FlowNetEx01.java (rev 0)
+++ branches/gridsim4.0-branch2/examples/FlowNetEx01/FlowNetEx01.java 2008-02-22 04:36:44 UTC (rev 126)
@@ -0,0 +1,125 @@
+package FlowNetEx01;
+
+/*
+ * 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 two GridSim entities and
+ * connect them via a link. NetUser entity sends messages to
+ * Test entity and Test entity sends back these messages.
+ */
+
+import gridsim.*;
+import gridsim.net.*;
+import gridsim.net.flow.*;
+
+import java.util.*;
+
+
+
+/**
+ * Test Driver class for this example
+ */
+public class FlowNetEx01
+{
+ /**
+ * 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 = 1; // number of grid users
+ Calendar calendar = Calendar.getInstance();
+ boolean trace_flag = false; // mean trace GridSim events
+
+ // Initialize the GridSim package without any statistical
+ // functionalities. Hence, no GridSim_stat.txt file is created.
+ System.out.println("Initializing GridSim package");
+ GridSim.init(num_user, calendar, trace_flag);
+ GridSim.initNetworkType(GridSimTags.NET_FLOW_LEVEL);
+
+ // 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 FlowRouter("router1", trace_flag); // router 1
+ Router r2 = new FlowRouter("router2", trace_flag); // router 2
+
+ String sender1 = "user1";
+ String receipient1 = "test1";
+ //String sender2 = "user2";
+ //String receipient2 = "test2";
+
+ // this entity is the sender
+ FlowNetUser user1 = new FlowNetUser(sender1, receipient1);
+ //NetUser user2 = new NetUser(sender2, receipient2);
+
+ FlowTest test1 = new FlowTest(receipient1, sender1);
+ //Test test2 = new Test(receipient2, sender2);
+
+ FIFOScheduler userSched1 = new FIFOScheduler("NetUserSched_0");
+ r1.attachHost(user1, userSched1);
+
+ //FIFOScheduler userSched2 = new FIFOScheduler("NetUserSched_1",Integer.MAX_VALUE);
+ //r1.attachHost(user2, userSched2);
+
+ FIFOScheduler userSched3 = new FIFOScheduler("NetUserSched_2");
+ r2.attachHost(test1, userSched3);
+
+ //FIFOScheduler userSched4 = new FIFOScheduler("NetUserSched_3",Integer.MAX_VALUE);
+ //r2.attachHost(test2, userSched4);
+
+ //////////////////////////////////////////
+ // Second step: Creates a physical link
+ double baud_rate = 1572864; // bits/sec [1.5Mb/s]
+ double propDelay = 300; // propagation delay in millisecond
+ int mtu = Integer.MAX_VALUE;; // max. transmission unit in byte
+
+ Link link = new FlowLink("r1_r2_link", baud_rate, propDelay, mtu);
+ FIFOScheduler r1Sched = new FIFOScheduler("r1_Sched");
+ FIFOScheduler r2Sched = new FIFOScheduler("r2_Sched");
+
+ r1.attachRouter(r2, link, r1Sched, r2Sched);
+
+ // OR ...
+ // use a default value
+ // Link link = new SimpleLink("link");
+
+ //////////////////////////////////////////
+ // Third step: Creates one or more entities.
+ // This can be users or resources. In this example,
+ // we create user's entities only.
+
+
+
+ //////////////////////////////////////////
+ // Final step: Starts the simulation
+ GridSim.startGridSimulation();
+
+ System.out.println("\nFinish network example ...");
+ }
+ catch (Exception e)
+ {
+
+ e.printStackTrace();
+ System.err.print(e.toString());
+ System.out.println("Unwanted errors happen");
+ }
+ }
+
+} // end class
+
Added: branches/gridsim4.0-branch2/examples/FlowNetEx01/FlowNetUser.java
===================================================================
--- branches/gridsim4.0-branch2/examples/FlowNetEx01/FlowNetUser.java (rev 0)
+++ branches/gridsim4.0-branch2/examples/FlowNetEx01/FlowNetUser.java 2008-02-22 04:36:44 UTC (rev 126)
@@ -0,0 +1,153 @@
+package FlowNetEx01;
+
+/*
+ * 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 two GridSim entities and
+ * connect them via a link. NetUser entity sends messages to
+ * Test entity and Test entity sends back these messages.
+ */
+
+import gridsim.*;
+import gridsim.net.*;
+import gridsim.net.flow.*;
+import eduni.simjava.*;
+import java.util.*;
+
+
+/**
+ * This class basically sends one or more messages to the other
+ * entity over a link. Then, it waits for an ack.
+ * Finally, before finishing the simulation, it pings the other
+ * entity.
+ */
+public class FlowNetUser extends GridSim
+{
+ private int myID_; // my entity ID
+ private String name_; // my entity name
+ private String destName_; // destination name
+ private int destID_; // destination id
+
+ /** Custom tag that denotes sending a message */
+ public static final int SEND_MSG = 1;
+ public static final int ACK_MSG = 2;
+
+
+ /**
+ * Creates a new NetUser object
+ * @param name this entity name
+ * @param destName the destination entity's name
+ * @param link the physical link that connects this entity to destName
+ * @throws Exception This happens when name is null or haven't
+ * initialized GridSim.
+ */
+ public FlowNetUser(String name, String destName, Link link) throws Exception
+ {
+ super(name, link);
+
+ // get this entity name from Sim_entity
+ this.name_ = super.get_name();
+
+ // get this entity ID from Sim_entity
+ this.myID_ = super.get_id();
+
+ // get the destination entity name
+ destName_ = destName;
+ }
+
+ public FlowNetUser(String name, String destName) throws Exception
+ {
+ // 10 485 760 bits = 10Mb
+ super(name, new FlowLink(name+"_link",10485760,450,Integer.MAX_VALUE));
+
+ // get this entity name from Sim_entity
+ this.name_ = super.get_name();
+
+ // get this entity ID from Sim_entity
+ this.myID_ = super.get_id();
+
+ // get the destination entity name
+ destName_ = destName;
+ }
+
+ /**
+ * The core method that handles communications among GridSim entities.
+ */
+ public void body()
+ {
+ int packetSize = 5242880; // packet size in bytes [5MB]
+ int size = 2; // number of packets sent
+ int i = 0;
+
+ // get the destination entity ID
+ this.destID_ = GridSim.getEntityId(destName_);
+
+ // sends messages over the other side of the link
+ for (i = 0; i < size; i++)
+ {
+ String msg = "Message_" + i;
+ IO_data data = new IO_data(msg, packetSize, destID_);
+ System.out.println(name_ + ".body(): Sending " + msg +
+ ", at time = " + GridSim.clock() );
+
+ // sends through Output buffer of this entity
+ super.send(super.output, GridSimTags.SCHEDULE_NOW,
+ GridSimTags.FLOW_SUBMIT, data);
+
+ super.sim_pause(10);
+ }
+
+ ////////////////////////////////////////////////////////
+ // get the ack back
+ Object obj = null;
+ for (i = 0; i < size; i++)
+ {
+ // waiting for incoming event in the Input buffer
+ obj = super.receiveEventObject();
+ System.out.println(name_ + ".body(): Receives Ack for " + obj);
+ }
+
+
+
+ super.sim_pause(20);
+ ////////////////////////////////////////////////////////
+ // ping functionality
+ //InfoPacket pkt = null;
+
+ // There are 2 ways to ping an entity:
+ // a. non-blocking call, i.e.
+ //super.ping(destID_, size); // (i) ping
+ //super.gridSimHold(10); // (ii) do something else
+ //pkt = super.getPingResult(); // (iii) get the result back
+
+ // b. blocking call, i.e. ping and wait for a result
+ //System.out.println(name_ + ".body(): Sending ping,at time = " + GridSim.clock() );
+ //pkt = super.pingBlockingCall(destID_, 1500);
+
+ // print the result
+ //System.out.println("\n-------- " + name_ + " ----------------");
+ //System.out.println(pkt);
+ //System.out.println("-------- " + name_ + " ----------------\n");
+
+ ////////////////////////////////////////////////////////
+ // sends back denoting end of simulation
+
+ //super.gridSimHold(3000);
+
+
+ super.send(destID_, GridSimTags.SCHEDULE_NOW,
+ GridSimTags.END_OF_SIMULATION);
+
+ ////////////////////////////////////////////////////////
+ // shut down I/O ports
+ shutdownUserEntity();
+ terminateIOEntities();
+
+ System.out.println(this.name_ + ":%%%% Exiting body() at time " +
+ GridSim.clock() );
+ }
+
+} // end class
+
Added: branches/gridsim4.0-branch2/examples/FlowNetEx01/FlowTest.java
===================================================================
--- branches/gridsim4.0-branch2/examples/FlowNetEx01/FlowTest.java (rev 0)
+++ branches/gridsim4.0-branch2/examples/FlowNetEx01/FlowTest.java 2008-02-22 04:36:44 UTC (rev 126)
@@ -0,0 +1,173 @@
+package FlowNetEx01;
+
+/*
+ * 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 two GridSim entities and
+ * connect them via a link. NetUser entity sends messages to
+ * Test entity and Test entity sends back these messages.
+ */
+
+import java.util.*;
+import gridsim.*;
+import gridsim.net.*;
+import gridsim.net.flow.*;
+import gridsim.util.SimReport;
+import eduni.simjava.*;
+
+
+/**
+ * This class handles incoming requests and sends back an ack.
+ * In addition, this class logs every activities.
+ */
+public class FlowTest extends GridSim
+{
+ private int myID_; // my entity ID
+ private String name_; // my entity name
+ private String destName_; // destination name
+ private int destID_; // destination id
+ private SimReport report_; // logs every activity
+
+ /**
+ * Creates a new NetUser object
+ * @param name this entity name
+ * @param destName the destination entity's name
+ * @param link the physical link that connects this entity to destName
+ * @throws Exception This happens when name is null or haven't
+ * initialized GridSim.
+ */
+ public FlowTest(String name, String destName, Link link) throws Exception
+ {
+ super(name, link);
+
+ // get this entity name from Sim_entity
+ this.name_ = super.get_name();
+
+ // get this entity ID from Sim_entity
+ this.myID_ = super.get_id();
+
+ // get the destination entity name
+ this.destName_ = destName;
+
+ // logs every activity. It will automatically create name.csv file
+ report_ = new SimReport(name);
+ report_.write("Creates " + name);
+ }
+
+ public FlowTest(String name, String destName) throws Exception
+ {
+ super(name, new FlowLink(name+"_link",10485760,250, Integer.MAX_VALUE));
+
+ // get this entity name from Sim_entity
+ this.name_ = super.get_name();
+
+ // get this entity ID from Sim_entity
+ this.myID_ = super.get_id();
+
+ // get the destination entity name
+ this.destName_ = destName;
+
+
+ // logs every activity. It will automatically create name.csv file
+ report_ = new SimReport(name);
+ report_.write("Creates " + name);
+ }
+
+ /**
+ * The core method that handles communications among GridSim entities.
+ */
+ public void body()
+ {
+ // get the destination entity ID
+ this.destID_ = GridSim.getEntityId(destName_);
+
+ int packetSize = 1500; // packet size in bytes
+ Sim_event ev = new Sim_event(); // an event
+
+ // a loop waiting for incoming events
+ while ( Sim_system.running() )
+ {
+ // get the next event from the Input buffer
+ super.sim_get_next(ev);
+
+ // if an event denotes end of simulation
+ if (ev.get_tag() == GridSimTags.END_OF_SIMULATION)
+ {
+ System.out.println();
+ write(super.get_name() + ".body(): exiting ...");
+ break;
+ }
+
+ // if an event denotes another event type
+ else if (ev.get_tag() == GridSimTags.FLOW_SUBMIT)
+ {
+ System.out.println();
+ write(super.get_name() + ".body(): receive " +
+ ev.get_data() + ", at time = " + GridSim.clock());
+
+ // sends back an ack
+ IO_data data = new IO_data(ev.get_data(), packetSize, destID_);
+ write(name_ + ".body(): Sending back " +
+ ev.get_data() + ", at time = " + GridSim.clock() );
+
+ // sends through Output buffer of this entity
+ super.send(super.output, GridSimTags.SCHEDULE_NOW,
+ GridSimTags.FLOW_RETURN , data);
+ }
+
+ // handle a ping requests. You need to write the below code
+ // for every class that extends from GridSim or GridSimCore.
+ // Otherwise, the ping functionality is not working.
+ else if (ev.get_tag() == GridSimTags.INFOPKT_SUBMIT)
+ {
+ processPingRequest(ev);
+ }
+ }
+
+ ////////////////////////////////////////////////////////
+ // shut down I/O ports
+ shutdownUserEntity();
+ terminateIOEntities();
+
+ // don't forget to close the file
+ if (report_ != null) {
+ report_.finalWrite();
+ }
+
+ System.out.println(this.name_ + ":%%%% Exiting body() at time " +
+ GridSim.clock() );
+ }
+
+ /**
+ * Handles ping request
+ * @param ev a Sim_event object
+ */
+ private void processPingRequest(Sim_event ev)
+ {
+ InfoPacket pkt = (InfoPacket) ev.get_data();
+ pkt.setTag(GridSimTags.INFOPKT_RETURN);
+ pkt.setDestID( pkt.getSrcID() );
+
+ // sends back to the sender
+ super.send(super.output, GridSimTags.SCHEDULE_NOW,
+ GridSimTags.INFOPKT_RETURN,
+ new IO_data(pkt,pkt.getSize(),pkt.getSrcID()) );
+ }
+
+ /**
+ * Prints out the given message into stdout.
+ * In addition, writes it into a file.
+ * @param msg a message
+ */
+ private void write(String msg)
+ {
+ System.out.println(msg);
+ if (report_ != null) {
+ report_.write(msg);
+ }
+ }
+
+} // end class
+
Added: branches/gridsim4.0-branch2/examples/FlowNetEx01/README.txt
===================================================================
--- branches/gridsim4.0-branch2/examples/FlowNetEx01/README.txt (rev 0)
+++ branches/gridsim4.0-branch2/examples/FlowNetEx01/README.txt 2008-02-22 04:36:44 UTC (rev 126)
@@ -0,0 +1,53 @@
+
+/**
+ * Author: James Broberg
+ * Date: February 2008
+ */
+
+
+Welcome to the Example of how to use GridSim flow network extension.
+To compile the example source code:
+ In Unix/Linux: javac -classpath $GRIDSIM/jars/gridsim.jar:. NetEx01.java
+ In Windows: javac -classpath %GRIDSIM%\jars\gridsim.jar;. NetEx01.java
+
+where $GRIDSIM or %GRIDSIM% is the location of the gridsimtoolkit package.
+
+
+To run the class file:
+ In Unix/Linux: java -classpath $GRIDSIM/jars/gridsim.jar:. FlowNetEx01 > file.txt
+ In Windows: java -classpath %GRIDSIM%\jars\gridsim.jar;. FlowNetEx01 > file.txt
+
+
+The above command means run the program and output the results into a file
+named "file.txt" rather than into screen or standard output.
+To prevent from overwriting an existing file, I renamed "file.txt" into
+"output.txt"
+NOTE: When you open "output.txt" file, it tells you that this example creates
+ two entities: "user" and "test". Both entities exchanging messages.
+
+
+When running the example file, it will produce the following files:
+
+ tracefile -> created by SimJava 1.2, now being replaced by sim_trace.
+ NOTE: GridSim 2.1 uses SimJava 1.2
+ GridSim 2.2 onwards use SimJava2
+
+ sim_trace -> created by the SimJava2 package (lower-level) to trace every
+ events (performed by SimJava and GridSim) during the simulation.
+ We don't need to worry about this file. Not to important for our
+ example.
+
+ sim_report -> created by the SimJava2 package (lower-level) of GridSim.
+ This is a simulation report that contains general information about
+ running this experiment. We don't need to worry about this file.
+ Not to important for our example.
+
+ test.csv -> created by Test.java to record every incoming activities.
+ The format of this file is:
+ simulation_time, ...
+
+ where ... means other descriptive information.
+
+NOTE: sim_trace, sim_report and test.csv will be overwritten if running
+ a new experiment.
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|