Description: In iFogSim, task offloading involves transferring computational workloads from local devices or fog nodes to more powerful resources, such as fog or cloud servers, to optimize resource usage and reduce processing time. The offloading process typically starts with defining tasks and their characteristics in the application’s workflow, specifying the computational requirements, and determining which tasks can be offloaded based on their size, processing needs, and latency constraints. Once these tasks are identified, the scheduling and resource allocation components in iFogSim decide whether to execute them locally on fog nodes or offload them to the cloud or other fog nodes. Various offloading strategies, such as workload balancing, energy efficiency, or minimizing latency, can influence this decision. After the task is offloaded, the simulation tracks its execution across the resources, managing factors like bandwidth, network latency, and resource availability to ensure that offloading decisions align with the application’s performance requirements. The key goal is to enhance efficiency by utilizing cloud or fog resources in a way that minimizes delays and resource bottlenecks while meeting the application’s needs.
Sample Code
TaskOffload.java:
package rapid;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Pe;
import org.cloudbus.cloudsim.Storage;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.power.PowerHost;
import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple;
import org.cloudbus.cloudsim.sdn.overbooking.BwProvisionerOverbooking;
import org.cloudbus.cloudsim.sdn.overbooking.PeProvisionerOverbooking;
import org.fog.application.AppEdge;
import org.fog.application.AppLoop;
import org.fog.application.Application;
import org.fog.application.selectivity.FractionalSelectivity;
import org.fog.entities.Actuator;
import org.fog.entities.FogBroker;
import org.fog.entities.FogDevice;
import org.fog.entities.FogDeviceCharacteristics;
import org.fog.entities.Sensor;
import org.fog.entities.Tuple;
import org.fog.placement.Controller;
import org.fog.placement.ModuleMapping;
import org.fog.placement.ModulePlacementEdgewards;
import org.fog.placement.ModulePlacementMapping;
import org.fog.placement.ModulePlacementOnlyCloud;
import org.fog.policy.AppModuleAllocationPolicy;
import org.fog.scheduler.StreamOperatorScheduler;
import org.fog.utils.FogLinearPowerModel;
import org.fog.utils.FogUtils;
import org.fog.utils.TimeKeeper;
import org.fog.utils.distribution.DeterministicDistribution;
public class TaskOffload {
static List fogDevices = new ArrayList();
static List sensors = new ArrayList();
static List actuators = new ArrayList();
static int numOfFogColony;
static int numOfFogCell;
static int numOfNode_S;
static int numOfNode_M;
static int numOfWearable;
static int numOfHealthCenter;
private static boolean CLOUD = true;
public TaskOffload(int fogclny, int fogcl, int fognod, int wear, int hcenter) {
setFogColony(fogclny);
setFogCell(fogcl);
setFogNodeStatic(fognod / 2);
setFogNodeMobile(fognod / 2);
setWearable(wear);
setActuator(hcenter);
}
public void process() {
Log.printLine("RAPID Starting of IABO...");
try {
Log.disable();
int num_user = 1; // number of cloud users
Calendar calendar = Calendar.getInstance();
boolean trace_flag = false; // mean trace events
CloudSim.init(num_user, calendar, trace_flag);
String appId = "rapid"; // identifier of the application
FogBroker broker = new FogBroker("broker");
Application application = createApplication1(appId, broker.getId());
application.setUserId(broker.getId());
createFogDevices(broker.getId(), appId);
RapidController rapidcontroller = null;
ModuleMapping moduleMapping = ModuleMapping.createModuleMapping(); // initializing a module mapping
for (FogDevice device : fogDevices) {
if (device.getName().startsWith("fm")) {
moduleMapping.addModuleToDevice("health_data_receiver", device.getName());
moduleMapping.addModuleToDevice("report_analyser", device.getName());
moduleMapping.addModuleToDevice("nomad_tracker", device.getName());
} else if (device.getName().startsWith("hlth-care")) {
moduleMapping.addModuleToDevice("health_caretaker", device.getName());
} else if (device.getName().startsWith("ns") || device.getName().startsWith("nm")) { // names of all Nodes Static and Mobile start with 'ns' and 'nm'
moduleMapping.addModuleToDevice("healthcare_analyser", device.getName()); // fixing 1 instance of the health_analyser module to each nodes
} else if (device.getName().startsWith("fc")) {
moduleMapping.addModuleToDevice("request_receiver", device.getName());
// moduleMapping.addModuleToDevice("patient_locator_dw", device.getName());
// moduleMapping.addModuleToDevice("patient_locator_uw", device.getName());
// moduleMapping.addModuleToDevice("health_locator", device.getName());
} else if (device.getName().startsWith("iot-gateway")) {
moduleMapping.addModuleToDevice("stream_receiver", device.getName());
//moduleMapping.addModuleToDevice("patient_tracker_dw", device.getName());
//moduleMapping.addModuleToDevice("patient_tracker_uw", device.getName());
moduleMapping.addModuleToDevice("alert_center", device.getName());
} else if (device.getName().startsWith("isp-GW")) {
moduleMapping.addModuleToDevice("isp_transport", device.getName());
}
}
if (CLOUD) {
// if the mode of deployment is cloud-based
moduleMapping.addModuleToDevice("patient_tracker_dw", "cloud");
moduleMapping.addModuleToDevice("patient_tracker_uw", "cloud");
moduleMapping.addModuleToDevice("healthcare_analyser", "cloud"); // placing all instances of Object Detector module in the Cloud
}
rapidcontroller = new RapidController("master-controller", fogDevices, sensors,
actuators);
rapidcontroller.submitApplication(application,
(CLOUD) ? (new ModulePlacementMapping(fogDevices, application, moduleMapping))
: (new RapidModulePlacement(fogDevices, sensors, actuators, application, moduleMapping)));
TimeKeeper.getInstance().setSimulationStartTime(Calendar.getInstance().getTimeInMillis());
CloudSim.startSimulation();
CloudSim.stopSimulation();
Log.printLine("Rapid-IABO finished!");
} catch (Exception e) {
e.printStackTrace();
Log.printLine("Unwanted errors happen");
}
}
/**
* Function to create the Health care wearable application in the DDF model.
*
* @param appId unique identifier of the application
* @param userId identifier of the user of the application
* @return Application model
*/
@SuppressWarnings({"serial"})
private Application createApplication1(String appId, int userId) {
Application application1 = Application.createApplication(appId, userId);
/*
* Adding modules (vertices) to the application model (directed graph)
*/
application1.addAppModule("stream_receiver", 100); // iot
application1.addAppModule("health_data_receiver", 100); //fm
application1.addAppModule("isp_transport", 100); //isp
application1.addAppModule("health_caretaker", 100); //hct
application1.addAppModule("request_receiver", 100);//fogcl
application1.addAppModule("healthcare_analyser", 100); //fn,cl
application1.addAppModule("report_analyser", 100); //fm
application1.addAppModule("patient_tracker_dw", 100); //iot
application1.addAppModule("patient_tracker_uw", 100); //iot
application1.addAppModule("nomad_tracker", 100); //fm
application1.addAppModule("alert_center", 100); //iot
/*
* Defining the input-output relationships (represented by selectivity) of the application modules.
*/
application1.addTupleMapping("stream_receiver", "WEARABLE", "STREAM_DATA", new FractionalSelectivity(1.0));
application1.addTupleMapping("health_data_receiver", "STREAM_DATA", "HEALTH_SENSED_DATA", new FractionalSelectivity(0.5));
application1.addTupleMapping("health_data_receiver", "STREAM_DATA", "DATA_REQUEST", new FractionalSelectivity(0.5));
application1.addTupleMapping("request_receiver", "HEALTH_SENSED_DATA", "HEALTH_SENSED_DATA", new FractionalSelectivity(1.0));
application1.addTupleMapping("healthcare_analyser", "HEALTH_SENSED_DATA", "HEALTH_REPORT", new FractionalSelectivity(1.0));
application1.addTupleMapping("isp_transport", "DATA_REQUEST", "SEND_REQUEST", new FractionalSelectivity(1.0));
application1.addTupleMapping("health_caretaker", "SEND_REQUEST", "REPLY_DATA", new FractionalSelectivity(1.0));
application1.addTupleMapping("isp_transport", "REPLY_DATA", "UNIVERSAL_DATA", new FractionalSelectivity(1.0));
application1.addTupleMapping("report_analyser", "UNIVERSAL_DATA", "PATIENT_REPORT", new FractionalSelectivity(0.5));
application1.addTupleMapping("report_analyser", "HEALTH_REPORT", "PATIENT_REPORT", new FractionalSelectivity(0.5));
application1.addTupleMapping("patient_tracker_dw", "PATIENT_REPORT", "PATIENT_PRESENCE", new FractionalSelectivity(1.0));
application1.addTupleMapping("patient_tracker_uw", "PATIENT_PRESENCE", "PATIENT_REACH", new FractionalSelectivity(1.0));
application1.addTupleMapping("nomad_tracker", "PATIENT_REACH", "PATIENT_STATUS", new FractionalSelectivity(0.5));
application1.addTupleMapping("alert_center", "PATIENT_STATUS", "PATIENT_HEALTH_LOCATION", new FractionalSelectivity(1.0));
/*
* Connecting the application modules (vertices) in the application model (directed graph) with edges
*/
//sensor from wearable
application1.addAppEdge("WEARABLE", "stream_receiver", 1000, 2000, "WEARABLE", Tuple.UP, AppEdge.SENSOR);
//from iot-gateway
application1.addAppEdge("stream_receiver", "health_data_receiver", 100, 20000, "STREAM_DATA", Tuple.UP, AppEdge.MODULE);
//from fm to fog node and cloud
application1.addAppEdge("health_data_receiver", "request_receiver", 1000, 20000, "HEALTH_SENSED_DATA", Tuple.DOWN, AppEdge.MODULE); // DOWN to fog nodes
application1.addAppEdge("request_receiver", "healthcare_analyser", 1000, 20000, "HEALTH_SENSED_DATA", Tuple.DOWN, AppEdge.MODULE); // DOWN to fog nodes
//fm to isp
application1.addAppEdge("health_data_receiver", "isp_transport", 100, 20000, "DATA_REQUEST", Tuple.UP, AppEdge.MODULE);
//isp to hct
application1.addAppEdge("isp_transport", "health_caretaker", 100, 20000, "SEND_REQUEST", Tuple.DOWN, AppEdge.MODULE); // down
//hct to isp
application1.addAppEdge("health_caretaker", "isp_transport", 1000, 20000, "REPLY_DATA", Tuple.UP, AppEdge.MODULE); // up
//isp to fm
application1.addAppEdge("isp_transport", "report_analyser", 1000, 20000, "UNIVERSAL_DATA", Tuple.DOWN, AppEdge.MODULE); //
// fm to fm
//from fog node and cloud to fm
application1.addAppEdge("healthcare_analyser", "report_analyser", 500, 20000, "HEALTH_REPORT", Tuple.UP, AppEdge.MODULE); // UP from fog node
//fm to fc
application1.addAppEdge("report_analyser", "patient_tracker_dw", 1000, 1000, "PATIENT_REPORT", Tuple.DOWN, AppEdge.MODULE); //
//patient identification fc to iot
application1.addAppEdge("patient_tracker_dw", "patient_tracker_uw", 1000, 1000, "PATIENT_PRESENCE", Tuple.UP, AppEdge.MODULE); //
// iot to fc
application1.addAppEdge("patient_tracker_uw", "nomad_tracker", 1000, 1000, "PATIENT_REACH", Tuple.UP, AppEdge.MODULE); //
//fm to fc
application1.addAppEdge("nomad_tracker", "alert_center", 1000, 1000, "PATIENT_STATUS", Tuple.DOWN, AppEdge.MODULE); //
//fc to iot
//iot to actuator
application1.addAppEdge("alert_center", "HEALTHCENTER", 1000, 500, "PATIENT_HEALTH_LOCATION", Tuple.DOWN, AppEdge.ACTUATOR);
/*
* Defining application loops (maybe incomplete loops) to monitor the latency of.
* Here, we add two loops for monitoring : Motion Detector -> Object Detector -> Object Tracker and Object Tracker -> PTZ Control
*/
final AppLoop loop1 = new AppLoop(new ArrayList() {
{
add("WEARABLE");
add("stream_receiver");
add("health_data_receiver");
add("isp_transport");
add("health_caretaker");
add("isp_transport");
add("report_analyser");
}
});
final AppLoop loop2 = new AppLoop(new ArrayList() {
{
add("health_data_receiver");
add("request_receiver");
add("healthcare_analyser");
add("report_analyser");
add("patient_tracker_dw");
add("patient_tracker_uw");
add("nomad_tracker");
add("alert_center");
}
});
final AppLoop loop3 = new AppLoop(new ArrayList() {
{
add("alert_center");
add("HEALTHCENTER");
}
});
List loops = new ArrayList() {
{
add(loop1);
add(loop2);
add(loop3);
}
};
application1.setLoops(loops);
return application1;
}
/**
* Creates a vanilla fog device
*
* @param nodeName name of the device to be used in simulation
* @param mips MIPS
* @param ram RAM
* @param upBw uplink bandwidth
* @param downBw downlink bandwidth
* @param level hierarchy level of the device
* @param ratePerMips cost rate per MIPS used
* @param busyPower
* @param idlePower
* @return
*/
private FogDevice createFogDevice(String nodeName, long mips,
int ram, long upBw, long downBw, int level, double ratePerMips, double busyPower, double idlePower) {
List peList = new ArrayList();
// 3. Create PEs and add these into a list.
peList.add(new Pe(0, new PeProvisionerOverbooking(mips))); // need to store Pe id and MIPS Rating
int hostId = FogUtils.generateEntityId();
long storage = 1000000; // host storage
int bw = 10000;
PowerHost host = new PowerHost(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerOverbooking(bw),
storage,
peList,
new StreamOperatorScheduler(peList),
new FogLinearPowerModel(busyPower, idlePower)
);
List hostList = new ArrayList();
hostList.add(host);
String arch = "x86"; // system architecture
String os = "Linux"; // operating system
String vmm = "Xen";
double time_zone = 10.0; // time zone this resource located
double cost = 3.0; // the cost of using processing in this resource
double costPerMem = 0.05; // the cost of using memory in this resource
double costPerStorage = 0.001; // the cost of using storage in this
// resource
double costPerBw = 0.0; // the cost of using bw in this resource
LinkedList storageList = new LinkedList(); // we are not adding SAN
// devices by now
FogDeviceCharacteristics characteristics = new FogDeviceCharacteristics(
arch, os, vmm, host, time_zone, cost, costPerMem,
costPerStorage, costPerBw);
FogDevice fogdevice = null;
try {
fogdevice = new FogDevice(nodeName, characteristics,
new AppModuleAllocationPolicy(hostList), storageList, 10, upBw, downBw, 0, ratePerMips);
} catch (Exception e) {
e.printStackTrace();
}
fogdevice.setLevel(level);
return fogdevice;
}
private FogManager createFogManager(String nodeName, long mips,
int ram, long upBw, long downBw, int level, double ratePerMips, double busyPower, double idlePower) {
List peList = new ArrayList();
// 3. Create PEs and add these into a list.
peList.add(new Pe(0, new PeProvisionerOverbooking(mips))); // need to store Pe id and MIPS Rating
int hostId = FogUtils.generateEntityId();
long storage = 1000000; // host storage
int bw = 10000;
PowerHost host = new PowerHost(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerOverbooking(bw),
storage,
peList,
new StreamOperatorScheduler(peList),
new FogLinearPowerModel(busyPower, idlePower)
);
);
List hostList = new ArrayList();
hostList.add(host);
String arch = "x86"; // system architecture
String os = "Linux"; // operating system
String vmm = "Xen";
double time_zone = 10.0; // time zone this resource located
double cost = 3.0; // the cost of using processing in this resource
double costPerMem = 0.05; // the cost of using memory in this resource
double costPerStorage = 0.001; // the cost of using storage in this
// resource
double costPerBw = 0.0; // the cost of using bw in this resource
LinkedList storageList = new LinkedList(); // we are not adding SAN
// devices by now
FogDeviceCharacteristics characteristics = new FogDeviceCharacteristics(
arch, os, vmm, host, time_zone, cost, costPerMem,
costPerStorage, costPerBw);
FogManager fogdevice = null;
try {
fogdevice = new FogManager(nodeName, characteristics,
new AppModuleAllocationPolicy(hostList), storageList, 10, upBw, downBw, 0, ratePerMips);
} catch (Exception e) {
e.printStackTrace();
}
fogdevice.setLevel(level);
return fogdevice;
}
private FogCell createFogCell(String nodeName, long mips,
int ram, long upBw, long downBw, int level, double ratePerMips, double busyPower, double idlePower) {
List peList = new ArrayList();
// 3. Create PEs and add these into a list.
peList.add(new Pe(0, new PeProvisionerOverbooking(mips))); // need to store Pe id and MIPS Rating
int hostId = FogUtils.generateEntityId();
long storage = 1000000; // host storage
int bw = 10000;
PowerHost host = new PowerHost(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerOverbooking(bw),
storage,
peList,
new StreamOperatorScheduler(peList),
new FogLinearPowerModel(busyPower, idlePower)
);
List hostList = new ArrayList();
hostList.add(host);
String arch = "x86"; // system architecture
String os = "Linux"; // operating system
String vmm = "Xen";
double time_zone = 10.0; // time zone this resource located
double cost = 3.0; // the cost of using processing in this resource
double costPerMem = 0.05; // the cost of using memory in this resource
double costPerStorage = 0.001; // the cost of using storage in this
// resource
double costPerBw = 0.0; // the cost of using bw in this resource
LinkedList storageList = new LinkedList(); // we are not adding SAN
// devices by now
FogDeviceCharacteristics characteristics = new FogDeviceCharacteristics(
arch, os, vmm, host, time_zone, cost, costPerMem,
costPerStorage, costPerBw);
FogCell fogdevice = null;
try {
fogdevice = new FogCell(nodeName, characteristics,
new AppModuleAllocationPolicy(hostList), storageList, 10, upBw, downBw, 0, ratePerMips);
} catch (Exception e) {
e.printStackTrace();
}
fogdevice.setLevel(level);
return fogdevice;
}
private FogNodeS createFogNodes(String nodeName, long mips,
int ram, long upBw, long downBw, int level, double ratePerMips, double busyPower, double idlePower) {
List peList = new ArrayList();
// 3. Create PEs and add these into a list.
peList.add(new Pe(0, new PeProvisionerOverbooking(mips))); // need to store Pe id and MIPS Rating
int hostId = FogUtils.generateEntityId();
long storage = 1000000; // host storage
int bw = 10000;
PowerHost host = new PowerHost(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerOverbooking(bw),
storage,
peList,
new StreamOperatorScheduler(peList),
new FogLinearPowerModel(busyPower, idlePower)
);
List hostList = new ArrayList();
hostList.add(host);
String arch = "x86"; // system architecture
String os = "Linux"; // operating system
String vmm = "Xen";
double time_zone = 10.0; // time zone this resource located
double cost = 3.0; // the cost of using processing in this resource
double costPerMem = 0.05; // the cost of using memory in this resource
double costPerStorage = 0.001; // the cost of using storage in this
// resource
double costPerBw = 0.0; // the cost of using bw in this resource
LinkedList storageList = new LinkedList(); // we are not adding SAN
// devices by now
FogDeviceCharacteristics characteristics = new FogDeviceCharacteristics(
arch, os, vmm, host, time_zone, cost, costPerMem,
costPerStorage, costPerBw);
FogNodeS fogdevice = null;
try {
fogdevice = new FogNodeS(nodeName, characteristics,
new AppModuleAllocationPolicy(hostList), storageList, 10, upBw, downBw, 0, ratePerMips);
} catch (Exception e) {
e.printStackTrace();
}
fogdevice.setLevel(level);
return fogdevice;
}
/**
* Creates the fog devices in the physical topology of the simulation.
*
* @param userId
* @param appId
*/
private void createFogDevices(int userId, String appId) {
FogDevice cloud = createFogDevice("cloud", 44800, 40000, 100, 10000, 0, 0.01, 16 * 103, 16 * 83.25);
cloud.setParentId(-1);
fogDevices.add(cloud);
FogDevice gateway = createFogDevice("isp-GW", 2800, 4000, 10000, 10000, 1, 0.0, 107.339, 83.4333);
gateway.setParentId(cloud.getId());
gateway.setUplinkLatency(100); // latency of connection between proxy server and cloud is 100 ms
fogDevices.add(gateway);
FogDevice careTaker = createFogDevice("hlth-care", 2800, 4000, 100, 10000, 2, 0.0, 107.339, 83.43333);
careTaker.setParentId(gateway.getId());
careTaker.setUplinkLatency(15);
fogDevices.add(careTaker);
for (int i = 0; i < getFogColony(); i++) {
addFogColony(i + "", userId, appId, gateway.getId());
}
private FogManager addFogColony(String id, int userId, String appId, int parentId) {
FogManager fogManager = createFogManager("fm-" + id, 22400, 32000, 10000, 10000, 2, 0.0, 107.339 * 10, 83.4333 * 10);
fogDevices.add(fogManager);
fogManager.setUplinkLatency(2); // latency of connection between router and proxy server is 2 ms
List listofcells = new ArrayList();
for (int i = 0; i < getFogCell(); i++) {
String fogcellId = id + "-" + i;
FogCell fogcell = addFogCell(fogcellId, userId, appId, fogManager.getId());
listofcells.add(new Cells(fogcell.getName(), fogcell));
}
fogManager.setFogCells(listofcells);
fogManager.setParentId(parentId);
return fogManager;
}
private FogCell addFogCell(String id, int userId, String appId, int parentId) {
FogCell fogCell = createFogCell("fc-" + id, 22400, 32000, 10000, 10000, 3, 0.0, 107.339 * 10, 83.4333 * 10);
fogDevices.add(fogCell);
fogCell.setUplinkLatency(2); // latency of connection between fog Cell and Fog Manager is 2 ms
List nodeSlist = new ArrayList();
for (int i = 0; i < getFogNodeStatic(); i++) {
String nodeSId = id + "-" + i;
FogDevice nodeS = addNodeStatic(nodeSId, userId, appId, fogCell.getId());
nodeSlist.add(new Nodes(nodeS.getName(), nodeS));
}
fogCell.setStaticNodes(nodeSlist);
List nodeMlist = new ArrayList();
for (int i = 0; i < getFogNodeMobile(); i++) {
String nodeMId = id + "-" + i;
FogDevice nodeM = addNodeMobile(nodeMId, userId, appId, fogCell.getId());
nodeMlist.add(new Nodes(nodeM.getName(), nodeM));
}
fogCell.setMobileNodes(nodeMlist);
FogDevice iotDevice = addIotDevice(id, userId, appId, fogCell.getId());
fogCell.setIotDevice(new Nodes(iotDevice.getName(), iotDevice));
fogCell.setParentId(parentId);
return fogCell;
}
private FogDevice addNodeStatic(String id, int userId, String appId, int parentId) {
FogDevice nodes = createFogDevice("ns-" + id, 11200, 16000, 10000, 10000, 4, 0.0, 107.339, 83.4333);
fogDevices.add(nodes);
nodes.setUplinkLatency(2); // latency of connection between fog Cell and Fog Nodes is 2 ms
nodes.setParentId(parentId);
return nodes;
}
private FogDevice addNodeMobile(String id, int userId, String appId, int parentId) {
FogDevice nodem = createFogDevice("nm-" + id, 11200, 16000, 10000, 10000, 4, 0.0, 107.339, 83.4333);
fogDevices.add(nodem);
nodem.setUplinkLatency(2); // latency of connection between fog Cell and Fog Nodes is 2 ms
nodem.setParentId(parentId);
return nodem;
}
private FogDevice addIotDevice(String id, int userId, String appId, int parentId) {
FogDevice iotGateway = createFogDevice("iot-gateway" + id, 28000, 40000, 10000, 10000, 4, 0.0, 87.53, 82.44);
fogDevices.add(iotGateway);
iotGateway.setUplinkLatency(2);
for (int i = 0; i < getWearable(); i++) {
Sensor wearable = new Sensor("w-" + id + "-" + i, "WEARABLE", userId, appId, new DeterministicDistribution(5)); // inter-transmission time of wearable (sensor) follows a deterministic distribution
sensors.add(wearable);
wearable.setGatewayDeviceId(iotGateway.getId());
wearable.setLatency(1.0); // latency of connection between wearable (sensor) and the parent iotGateway is 1 ms
}
for (int j = 0; j < getActuator(); j++) {
Actuator healthcenter = new Actuator("hc-" + id + "-" + j, userId, appId, "HEALTHCENTER");
actuators.add(healthcenter);
healthcenter.setGatewayDeviceId(iotGateway.getId());
healthcenter.setLatency(1.0); // latency of connection between healthcenter and the parent iotGateway is 1 ms
}
iotGateway.setParentId(parentId);
return iotGateway;
}
public static void setFogColony(int fcolony) {
numOfFogColony = fcolony;
}
public static void setFogCell(int fcell) {
numOfFogCell = fcell;
}
public static void setFogNodeStatic(int fnodes) {
numOfNode_S = fnodes;
}
public static void setFogNodeMobile(int fnodem) {
numOfNode_M = fnodem;
}
public static void setWearable(int wearable) {
numOfWearable = wearable;
}
public static void setActuator(int actuator) {
numOfHealthCenter = actuator;
}
public static int getFogColony() {
return numOfFogColony;
}
public static int getFogCell() {
return numOfFogCell;
}
public static int getFogNodeStatic() {
return numOfNode_S;
}
public static int getFogNodeMobile() {
return numOfNode_M;
}
public static int getWearable() {
return numOfWearable;
}
public static int getActuator() {
return numOfHealthCenter;
}
FogManager.java: protected void processTupleArrival(SimEvent ev) {
System.out.println(" ProcessTupleArrival FogEvents.TUPLE_ARRIVAL " + "Starts");
Tuple tuple = (Tuple) ev.getData();
if (getName().equals("cloud")) {
updateCloudTraffic();
}
/*if(getName().equals("d-0") && tuple.getTupleType().equals("_SENSOR")){
System.out.println(++numClients);
}*/
System.out.println();
System.out.println("device :" + getName() + " Received tuple id #" + tuple.getCloudletId() + " actual id #" + tuple.getActualTupleId() + " tupleType = " + tuple.getTupleType() + "\t| Source : "
+ CloudSim.getEntityName(ev.getSource()) + "|Dest : " + CloudSim.getEntityName(ev.getDestination()));
Logger.debug(getName(), "Received tuple " + tuple.getCloudletId() + "with tupleType = " + tuple.getTupleType() + "\t| Source : "
+ CloudSim.getEntityName(ev.getSource()) + "|Dest : " + CloudSim.getEntityName(ev.getDestination()));
send(ev.getSource(), CloudSim.getMinTimeBetweenEvents(), FogEvents.TUPLE_ACK);
if (FogUtils.appIdToGeoCoverageMap.containsKey(tuple.getAppId())) {
}
if (tuple.getDirection() == Tuple.ACTUATOR) {
sendTupleToActuator(tuple);
return;
}
if (getHost().getVmList().size() > 0) {
final AppModule operator = (AppModule) getHost().getVmList().get(0);
if (CloudSim.clock() > 0) {
getHost().getVmScheduler().deallocatePesForVm(operator);
getHost().getVmScheduler().allocatePesForVm(operator, new ArrayList() {
protected static final long serialVersionUID = 1L;
{
add((double) getHost().getTotalMips());
}
});
}
}
if (getName().equals("cloud") && tuple.getDestModuleName() == null) {
sendNow(getControllerId(), FogEvents.TUPLE_FINISHED, null);
}
if (appToModulesMap.containsKey(tuple.getAppId())) {
if (appToModulesMap.get(tuple.getAppId()).contains(tuple.getDestModuleName())) {
int vmId = -1;
for (Vm vm : getHost().getVmList()) {
if (((AppModule) vm).getName().equals(tuple.getDestModuleName())) {
vmId = vm.getId();
}
}
if (vmId < 0
|| (tuple.getModuleCopyMap().containsKey(tuple.getDestModuleName())
&& tuple.getModuleCopyMap().get(tuple.getDestModuleName()) != vmId)) {
return;
}
tuple.setVmId(vmId);
//Logger.error(getName(), "Executing tuple for operator " + moduleName);
updateTimingsOnReceipt(tuple);
executeTuple(ev, tuple.getDestModuleName());
} else if (tuple.getDestModuleName() != null) {
if (tuple.getDirection() == Tuple.UP) {
System.out.println("Sending Up tuple type:" + tuple.getTupleType());
sendUp(tuple);
} else if (tuple.getDirection() == Tuple.DOWN) {
System.out.println("Sending Down tuple type:" + tuple.getTupleType());
List cells = getFogCells();
int gatewayId = tuple.getGatewayDeviceId();
if (tuple.getTupleType() == "HEALTH_SENSED_DATA") {
int check = Integer.MAX_VALUE;
FogCell selectedCell = null;
List listofNodes = new ArrayList();
for (Cells cell : cells) {
FogCell fogcell = cell.FogCells;
listofNodes.addAll(fogcell.listOfMobileNodes);
listofNodes.addAll(fogcell.listOfStaticNodes);
if (!fogCellTupleMap.containsKey(fogcell)) {
fogCellTupleMap.put(fogcell, new ArrayList());
}
if (fogCellTupleMap.get(fogcell).size() < check) {
selectedCell = fogcell;
check = fogCellTupleMap.get(fogcell).size();
}
}
//IABO algorithm to be called for scheduling
NodeVm nodeVmResult = IABO(listofNodes, tuple);
System.out.println("Fogcell id #" + selectedCell.getId() + " tuple id #" + tuple.getCloudletId() + " tuple type :" + tuple.getTupleType());
fogCellTupleMap.get(selectedCell).add(tuple);
tuple.setVmId(nodeVmResult.VirtualMachine.getId());
tuple.setFogNodeName(nodeVmResult.FogNode.getName());
System.out.println("sending down tuple type :" + tuple.getTupleType() + "FogNode :" + nodeVmResult.FogNode.getName() + "Fogcell id #" + nodeVmResult.FogNode.getParentId());
sendDown(tuple, nodeVmResult.FogNode.getParentId());
} else {
for (int childId : getChildrenIds()) {
sendDown(tuple, childId);
}
}
}
} else {
sendUp(tuple);
}
} else if (tuple.getDirection() == Tuple.UP) {
sendUp(tuple);
} else if (tuple.getDirection() == Tuple.DOWN) {
for (int childId : getChildrenIds()) {
sendDown(tuple, childId);
}
}
}