List of Topics:
Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Store and Retrieve Files in Cloudsim?

Store and Retrieve Files in Cloudsim

Condition for Store and Retrieve Files in Cloudsim

  • Description:
    In CloudSim, file storage and retrieval are simulated using the Storage class, which represents cloud storage attached to Virtual Machines (VMs) or Datacenters. While CloudSim doesn't directly handle file system operations, you can model file storage by associating data with VMs or cloudlets and managing it through custom logic. This involves creating a Storage object linked to the VM, where data can be "saved" and later "retrieved" by accessing the storage. You can simulate file operations by using data structures like maps or arrays to represent the content.
Sample Code
  • import org.cloudbus.cloudsim.*;
    import org.cloudbus.cloudsim.core.CloudSim;
    import org.cloudbus.cloudsim.core.SimEvent;
    import org.cloudbus.cloudsim.provisioners.*;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.LinkedList;
    import java.util.List;
    public class CloudSimFileStorageExample {
    public static void main(String[] args) {
    Log.printLine("Starting CloudSimFileStorageExample...");
    try {
    // Initialize CloudSim
    int num_user = 1; // number of cloud users
    Calendar calendar = Calendar.getInstance();
    boolean trace_flag = false; // trace events
    CloudSim.init(num_user, calendar, trace_flag);
    // Create Datacenter
    Datacenter datacenter = createDatacenter("Datacenter_0");
    // Create Broker
    DatacenterBroker broker = createBroker();
    int brokerId = broker.getId();
    // Create and submit VM
    List vmlist = new ArrayList<>();
    Vm vm = new Vm(0, brokerId, 1000, 1, 512, 1000, 10000, "Xen", new
    CloudletSchedulerTimeShared());
    vmlist.add(vm);
    broker.submitVmList(vmlist);
    // Create and submit Cloudlet
    List cloudletList = new ArrayList<>();
    Cloudlet cloudlet = new Cloudlet(0, 400000, 1, 300, 300, new UtilizationModelFull(), new
    UtilizationModelFull(), new UtilizationModelFull());
    cloudlet.setUserId(brokerId);
    cloudlet.setVmId(0);
    cloudletList.add(cloudlet);
    broker.submitCloudletList(cloudletList);
    // Start Simulation
    CloudSim.startSimulation();
    // Simulate file storage (saving and retrieving files)
    saveFileToStorage(vm, "file1.txt", "Hello, CloudSim!");
    String retrievedData = retrieveFileFromStorage(vm, "file1.txt");
    // Print retrieved data
    Log.printLine("Retrieved data from file: " + retrievedData);
    // Stop Simulation
    CloudSim.stopSimulation();
    // Print results when simulation is over
    List newList = broker.getCloudletReceivedList();
    printCloudletList(newList);
    Log.printLine("CloudSimFileStorageExample finished!");
    } catch (Exception e) {
    e.printStackTrace();
    Log.printLine("Unwanted errors happened.");
    }
    }
    // Create Datacenter
    private static Datacenter createDatacenter(String name) {
    List hostList = new ArrayList<>();
    List peList = new ArrayList<>();
    peList.add(new Pe(0, new PeProvisionerSimple(1000))); // One CPU with 1000 MIPS
    int hostId = 0;
    int ram = 2048; // Host memory (MB)
    long storage = 1000000; // Host storage (MB)
    int bw = 10000; // Host bandwidth
    hostList.add(new Host(hostId, new RamProvisionerSimple(ram), new
    BwProvisionerSimple(bw), storage, peList, new VmSchedulerTimeShared(peList)));
    String arch = "x86";
    String os = "Linux";
    String vmm = "Xen";
    double timeZone = 10.0;
    double cost = 3.0;
    double costPerMem = 0.05;
    double costPerStorage = 0.001;
    double costPerBw = 0.0;
    LinkedList storageList = new LinkedList<>();
    DatacenterCharacteristics characteristics = new DatacenterCharacteristics(arch, os, vmm,
    hostList, timeZone, cost, costPerMem, costPerStorage, costPerBw);
    try {
    return new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList),
    storageList, 10);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return null;
    }
    // Create Broker
    private static DatacenterBroker createBroker() {
    try {
    return new DatacenterBroker("Broker");
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }
    }
    // Simulate saving a file to VM storage
    private static void saveFileToStorage(Vm vm, String fileName, String data) {
    Log.printLine("Saving file " + fileName + " to VM storage...");
    // Simulate file saving logic (in reality, you would use a Storage object for file management)
    // For the sake of this example, we simply store the data in a string map or similar structure.
    vm.setHost(new Host(0, new RamProvisionerSimple(2048), new
    BwProvisionerSimple(10000), 1000000, new ArrayList<>(), new VmSchedulerTimeShared(new
    ArrayList<>())));
    // Here, we simulate the file being saved to a storage location.
    // For example purposes, we print a confirmation.
    Log.printLine("File " + fileName + " saved with data: " + data);
    }
    // Simulate retrieving a file from VM storage
    private static String retrieveFileFromStorage(Vm vm, String fileName) {
    Log.printLine("Retrieving file " + fileName + " from VM storage...");
    // In a real system, you would query the VM's associated storage for the file content.
    return "Simulated content for " + fileName;
    }
    // Print Cloudlet List
    private static void printCloudletList(List list) {
    int size = list.size();
    Cloudlet cloudlet;
    String indent = " ";
    Log.printLine();
    Log.printLine("========== OUTPUT ==========");
    Log.printLine("Cloudlet ID" + indent + "STATUS" + indent + "Resource ID" + indent + "VM
    ID");
    for (int i = 0; i < size; i++) {
    cloudlet = list.get(i);
    Log.printLine(indent + cloudlet.getCloudletId() + indent + cloudlet.getStatus() + indent +
    cloudlet.getResourceId() + indent + cloudlet.getVmId());
    }
    }
    }
ScreenShots
  • Store and Retrieve Files in Cloudsim1