Condition for Create Number of Application in IFogSim
Description: To create multiple applications in iFogSim, each application is defined with a unique ID and user ID. Modules are added to the application using the addAppModule method, and edges are created between modules to define data flow with addAppEdge. The application topology can be customized with tuple mappings for input-output relationships. These applications are then submitted to the Fog Controller for execution, with the controller managing module placement across fog devices. Each application can have its own set of resources, ensuring independent execution and resource allocation.
/**
* Simulation setup for case study 2 - Intelligent Surveillance
*
* @author Harshit Gupta
*
*/
public class NumberOfApplications {
static List fogDevices = new ArrayList();
static List sensors = new ArrayList();
static List actuators = new ArrayList();
static int numOfAreas = 1;
static int numOfCamerasPerArea = 4;
private static boolean CLOUD = false;
public static void main(String[] args) {
Log.printLine("Starting Add module to fog device...");
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 = "module"; // identifier of the application
FogBroker broker = new FogBroker("broker");
Application application = createApplication(appId, broker.getId());
application.setUserId(broker.getId());
printApplicationDetails(application);
createFogDevices(broker.getId(), appId);
Controller controller = null;
ModuleMapping moduleMapping = ModuleMapping.createModuleMapping(); // initializing a module mapping
for (FogDevice device : fogDevices) {
if (device.getName().startsWith("m")) { // names of all Smart Cameras start with 'm'
moduleMapping.addModuleToDevice("motion_detector", device.getName()); // fixing 1 instance of the Motion Detector module to each Smart Camera
}
}
moduleMapping.addModuleToDevice("user_interface", "cloud"); // fixing instances of User Interface module in the Cloud
if (CLOUD) {
// if the mode of deployment is cloud-based
moduleMapping.addModuleToDevice("object_detector", "cloud"); // placing all instances of Object Detector module in the Cloud
moduleMapping.addModuleToDevice("object_tracker", "cloud"); // placing all instances of Object Tracker module in the Cloud
}
controller = new Controller("master-controller", fogDevices, sensors,
actuators);
controller.submitApplication(application,
(CLOUD) ? (new ModulePlacementMapping(fogDevices, application, moduleMapping))
: (new ModulePlacementEdgewards(fogDevices, sensors, actuators, application, moduleMapping)));
TimeKeeper.getInstance().setSimulationStartTime(Calendar.getInstance().getTimeInMillis());
CloudSim.startSimulation();
CloudSim.stopSimulation();
Log.printLine("simulation finished!");
} catch (Exception e) {
e.printStackTrace();
Log.printLine("Unwanted errors happen");
}
}
/**
* Creates the fog devices in the physical topology of the simulation.
*
* @param userId
* @param appId
*/
private static 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 proxy = createFogDevice("proxy-server", 2800, 4000, 10000, 10000, 1, 0.0, 107.339, 83.4333);
proxy.setParentId(cloud.getId());
proxy.setUplinkLatency(100); // latency of connection between proxy server and cloud is 100 ms
fogDevices.add(proxy);
for (int i = 0; i < numOfAreas; i++) {
addArea(i + "", userId, appId, proxy.getId());
}
}
private static FogDevice addArea(String id, int userId, String appId, int parentId) {
FogDevice router = createFogDevice("d-" + id, 2800, 4000, 10000, 10000, 1, 0.0, 107.339, 83.4333);
fogDevices.add(router);
router.setUplinkLatency(2); // latency of connection between router and proxy server is 2 ms
for (int i = 0; i < numOfCamerasPerArea; i++) {
String mobileId = id + "-" + i;
FogDevice camera = addCamera(mobileId, userId, appId, router.getId()); // adding a smart camera to the physical topology. Smart cameras have been modeled as fog devices as well.
camera.setUplinkLatency(2); // latency of connection between camera and router is 2 ms
fogDevices.add(camera);
}
router.setParentId(parentId);
return router;
}
private static FogDevice addCamera(String id, int userId, String appId, int parentId) {
FogDevice camera = createFogDevice("m-" + id, 500, 1000, 10000, 10000, 3, 0, 87.53, 82.44);
camera.setParentId(parentId);
Sensor sensor = new Sensor("s-" + id, "CAMERA", userId, appId, new DeterministicDistribution(5)); // inter-transmission time of camera (sensor) follows a deterministic distribution
sensors.add(sensor);
Actuator ptz = new Actuator("ptz-" + id, userId, appId, "PTZ_CONTROL");
actuators.add(ptz);
sensor.setGatewayDeviceId(camera.getId());
sensor.setLatency(1.0); // latency of connection between camera (sensor) and the parent Smart Camera is 1 ms
ptz.setGatewayDeviceId(camera.getId());
ptz.setLatency(1.0); // latency of connection between PTZ Control and the parent Smart Camera is 1 ms
return camera;
private static FogDevice createFogDevice(String nodeName, long mips,
int ram, long upBw, long downBw, int level, double ratePerMips, double busyPower, double idlePower) {
List peList = new ArrayList();
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
double costPerBw = 0.0; // the cost of using bw in this resource
LinkedList storageList = new LinkedList(); // we are not adding SAN
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;
@SuppressWarnings({"serial"})
private static Application createApplication(String appId, int userId) {
Application application = Application.createApplication(appId, userId);
application.addAppModule("object_detector", 10);
application.addAppModule("motion_detector", 10);
application.addAppModule("object_tracker", 10);
application.addAppModule("user_interface", 10);
application.addAppEdge("CAMERA", "motion_detector", 1000, 20000, "CAMERA", Tuple.UP, AppEdge.SENSOR);
application.addAppEdge("motion_detector", "object_detector", 2000, 2000, "MOTION_VIDEO_STREAM", Tuple.UP, AppEdge.MODULE);
application.addAppEdge("object_detector", "user_interface", 500, 2000, "DETECTED_OBJECT", Tuple.UP, AppEdge.MODULE);
application.addAppEdge("object_detector", "object_tracker", 1000, 100, "OBJECT_LOCATION", Tuple.UP, AppEdge.MODULE);
application.addAppEdge("object_tracker", "PTZ_CONTROL", 100, 28, 100, "PTZ_PARAMS", Tuple.DOWN, AppEdge.ACTUATOR);
application.addTupleMapping("motion_detector", "CAMERA", "MOTION_VIDEO_STREAM", new FractionalSelectivity(1.0));
application.addTupleMapping("object_detector", "MOTION_VIDEO_STREAM", "OBJECT_LOCATION", new FractionalSelectivity(1.0));
application.addTupleMapping("object_detector", "MOTION_VIDEO_STREAM", "DETECTED_OBJECT", new FractionalSelectivity(0.05));
final AppLoop loop1 = new AppLoop(new ArrayList() {
{
add("motion_detector");
add("object_detector");
add("object_tracker");
}
});
final AppLoop loop2 = new AppLoop(new ArrayList() {
{
add("object_tracker");
add("PTZ_CONTROL");
}
});
List loops = new ArrayList() {
{
add(loop1);
add(loop2);
}
};
application.setLoops(loops);
return application;
private static void printApplicationDetails(Application application) {
System.out.println("Application Modules:");
System.out.println("\nApplication Edges:");
for (AppEdge edge : application.getEdges()) {
String source = edge.getSource();
String destination = edge.getDestination();
String dataType = edge.getTupleType();
int tupleCount = edge.getEdgeType();
String direction = edge.getDirection() == Tuple.UP ? "Up" : "Down";
String edgeType = edge.getEdgeType() == AppEdge.SENSOR ? "Sensor"
: edge.getEdgeType() == AppEdge.MODULE ? "Module"
: edge.getEdgeType() == AppEdge.ACTUATOR ? "Actuator" : "Unknown";
System.out.println("Edge from " + source + " to " + destination
+ " [Type: " + edgeType + ", Data: " + dataType
+ ", Tuple Count: " + tupleCount
+ ", Direction: " + direction + "]");
}
System.out.println("\nApplication Loops:");
for (AppLoop loop : application.getLoops()) {
System.out.println("Loop: " + loop.getModules());
}
}