All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cloudsimplus.cloudlets.network.NetworkCloudlet Maven / Gradle / Ivy

Go to download

CloudSim Plus: A modern, highly extensible and easier-to-use Java 8+ Framework for Modeling and Simulation of Cloud Computing Infrastructures and Services

The newest version!
/*
 * Title:        CloudSim Toolkit
 * Description:  CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 *
 * Copyright (c) 2009-2012, The University of Melbourne, Australia
 */
package org.cloudsimplus.cloudlets.network;

import lombok.NonNull;
import lombok.experimental.Accessors;
import org.cloudsimplus.cloudlets.CloudletSimple;
import org.cloudsimplus.vms.Vm;
import org.cloudsimplus.vms.network.NetworkVm;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * NetworkCloudlet to support simulation of complex applications.
 * Each application is compounded of one or more {@link CloudletTask}s
 * for performing different kinds of processing.
 *
 * 

* Please refer to following publication for more details: *

*

* * @author Saurabh Kumar Garg * @author Manoel Campos da Silva Filho * @since CloudSim Toolkit 1.0 * * TODO Check how to implement the NULL pattern for this class. */ @Accessors public class NetworkCloudlet extends CloudletSimple { /** * The index of the active running task or -1 if no task has started yet. */ private int currentTaskNum; /** @see #getTasks() */ private final List tasks; /** * Creates a NetworkCloudlet with no priority and file size and output size equal to 1. * * @param length the length or size (in MI) of this cloudlet to be executed in a VM (check out {@link #setLength(long)}) * @param pesNumber the number of PEs this Cloudlet requires */ public NetworkCloudlet(final long length, final int pesNumber) { this(-1, length, pesNumber); } /** * Creates a NetworkCloudlet with no priority and file size and output size equal to 1. * * @param id the unique ID of this cloudlet * @param length the length or size (in MI) of this cloudlet to be executed in a VM (check out {@link #setLength(long)}) * @param pesNumber the pes number */ public NetworkCloudlet(final int id, final long length, final int pesNumber) { super(id, length, pesNumber); this.currentTaskNum = -1; this.tasks = new ArrayList<>(); } public double getNumberOfTasks() { return tasks.size(); } /** * @return a read-only list of Cloudlet's tasks. */ public List getTasks() { return Collections.unmodifiableList(tasks); } /** * Checks if some Cloudlet Task has started yet. * * @return true if some task has started, false otherwise */ public boolean isTasksStarted() { return currentTaskNum > -1; } /** * Change the current task to the next one in order * to start executing it, if the current task is finished. * * @param nextTaskStartTime the time that the next task will start * @return true if the current task finished and the next one was started, false otherwise */ public boolean startNextTaskIfCurrentIsFinished(final double nextTaskStartTime){ return getNextTaskIfCurrentIfFinished() .map(task -> startTask(task, nextTaskStartTime)) .isPresent(); } private static CloudletTask startTask(final CloudletTask task, double time) { task.setStartTime(time); return task; } /** * Gets an {@link Optional} containing the current task * or an {@link Optional#empty()} if there is no current task yet. * @return */ public Optional getCurrentTask() { if (currentTaskNum < 0 || currentTaskNum >= tasks.size()) { return Optional.empty(); } return Optional.of(tasks.get(currentTaskNum)); } /** * Gets an {@link Optional} containing the next task in the list if the current task is finished. * * @return the next task if the current one is finished; * otherwise an {@link Optional#empty()} if the current task is already the last one, * or it is not finished yet. */ private Optional getNextTaskIfCurrentIfFinished(){ if(getCurrentTask().filter(CloudletTask::isActive).isPresent()) { return Optional.empty(); } if(this.currentTaskNum <= tasks.size()-1) { this.currentTaskNum++; } return getCurrentTask(); } @Override public boolean isFinished() { final boolean allTasksFinished = tasks.stream().allMatch(CloudletTask::isFinished); return super.isFinished() && allTasksFinished; } /** * {@inheritDoc} *

The length of a NetworkCloudlet is the * length sum of all its {@link CloudletExecutionTask}'s.

* @return the length sum of all {@link CloudletExecutionTask}'s */ @Override public long getLength() { return getTasks().stream() .filter(CloudletTask::isExecutionTask) .map(task -> (CloudletExecutionTask)task) .mapToLong(CloudletExecutionTask::getLength) .sum(); } /** * Adds a task to the {@link #getTasks() task list} * and links the task to the NetworkCloudlet. * * @param task Task to be added * @return the NetworkCloudlet instance */ public NetworkCloudlet addTask(@NonNull final CloudletTask task) { task.setCloudlet(this); tasks.add(task); return this; } @Override public NetworkVm getVm() { return (NetworkVm)super.getVm(); } @Override public NetworkCloudlet setVm(final Vm vm) { if(vm == Vm.NULL) { setVm(NetworkVm.NULL); return this; } if(vm instanceof NetworkVm) { super.setVm(vm); return this; } throw new IllegalArgumentException("NetworkCloudlet can just be executed by a NetworkVm"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy