CREATING USERS

Typically many users are created before the start of a simulation, but the example code below shows how to create just one user.

try 
{
       user = new ExampleUser("User_1"1,
       baud_rate, propDelay, mtu);
       //set a replica catalogue, if not set the TopReplica RC will be used
       user.setReplicaCatalogue("Res_1");
catch (Exception e2) {
       e2.printStackTrace();
}


Coding the user

During the simulation every user can perform different tasks. These tasks must be defined in the body of each user. Below you can see an example user, which transfers one file, makes one replica, deletes the replica and retrieves the attribute (all the information) of a file.

import gridsim.GridSim;
import gridsim.datagrid.DataGridUser;
import gridsim.datagrid.File;
import gridsim.datagrid.FileAttribute;
import gridsim.net.SimpleLink;


public class ExampleUser extends DataGridUser {
 
    ExampleUser(String name, int totalGridlet, double baud_rate, double delay,
            int MTUthrows Exception {
        super(name, new SimpleLink(name + "_link", baud_rate, delay, MTU));

    }
    
    /**
     * The core method that handles communications among GridSim entities.
     */
    public void body() {
        
        experiment()// experiment
        shutdownUserEntity();
        terminateIOEntities();
    }
   
   
/**
     * This experiment demonstrates the usage of common commands, available to the user.
     *  - getting a file from a resource
     *  - replicating a file to a resource
     *  - deleting a replica from a resource
     *  - getting an attribute from a resource
     */

    private void experiment(){
        //------------getting a file
        //Step 1 - get the full name of a file
        String name = getFullFilename("testFile1");
        File f = null;
        int location;
        if(name!=null){
            location = this.getReplicaLocation(name);
            if(location!=-1){
                f = this.getFile(name, location);
                System.out.println("user:"+this.get_name()+":-Transfer of file "+name+" succesful");
            }
        }
        //------------replicate a file to Res_0
        replicateFile(f, GridSim.getEntityId("Res_0"));
        //------------delete a replica from resource Res_0
        deleteFile(name, GridSim.getEntityId("Res_0"));
        //------------get attribute of a file
        FileAttribute attr = this.getFileAttribute(name);
    }
}