From 631ad01bfcfecee663d053ba4a4d26ffbe7cb437 Mon Sep 17 00:00:00 2001 From: Remi Boure Date: Fri, 17 Mar 2023 20:31:42 +0100 Subject: [PATCH] Delete the file if it exists before starting simulation --- build.gradle | 2 +- src/main/java/fr/ntr/AccessPoint.java | 41 +++++++++++-------- src/main/java/fr/ntr/Main.java | 4 +- .../java/fr/ntr/scheduler/RoundRobin.java | 4 +- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/build.gradle b/build.gradle index 079c0fe..cb590a9 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ plugins { } run { - args = ["5000", "2"] + args = ["1000", "20"] } application { diff --git a/src/main/java/fr/ntr/AccessPoint.java b/src/main/java/fr/ntr/AccessPoint.java index ff482dc..02b2031 100644 --- a/src/main/java/fr/ntr/AccessPoint.java +++ b/src/main/java/fr/ntr/AccessPoint.java @@ -3,6 +3,8 @@ package fr.ntr; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.Random; @@ -35,6 +37,8 @@ public class AccessPoint { private Random random = new Random(); + private FileOutputStream outputDataFile; + public AccessPoint(Scheduler scheduler, ResourceBlock[][] frame, List users, int timeSlotNb, int subCarrierNb, double min, double max) { this.min = min; @@ -50,7 +54,16 @@ public class AccessPoint { * Lancer la simulation * @param duration */ - public void startSimulation(int duration, int nbUsers) { + public void startSimulation(int duration) { + try{ + Files.deleteIfExists(Paths.get("data.csv")); + this.outputDataFile = new FileOutputStream("data.csv", true); + + }catch(IOException e){ + System.err.println("Cannot create the output file"); + System.exit(1); + return; + } for (int ticks = 0; ticks < duration; ++ticks) { // Simulation reset(); @@ -101,34 +114,30 @@ public class AccessPoint { } private void analyseData() throws IOException { - try(FileOutputStream file = new FileOutputStream("data.csv", true)) { - for(int i = 0; i < frame.length; i++) { - for(int j = 0; j < frame[i].length; j++) { - ResourceBlock ur = frame[i][j]; - String data = (i + ";" + j + ";" + this.users.indexOf(ur.getUser()) + ";" + ur.getBandwidth() + ";\n"); - file.write(data.getBytes()); + for(int i = 0; i < frame.length; i++) { + for(int j = 0; j < frame[i].length; j++) { + ResourceBlock ur = frame[i][j]; + String data = (i + ";" + j + ";" + this.users.indexOf(ur.getUser()) + ";" + ur.getBandwidth() + ";\n"); + try{ + outputDataFile.write(data.getBytes()); + }catch(IOException e){ + System.err.println("Cannot write the data in the output file"); + System.exit(1); + return; } } } } - private void plotData() { - - } - private void reset() { // TODO insert new UR for(int i = 0; i < timeSlotNb; i++) { for(int j = 0; j < subCarrierNb; j++) { - frame[i][j] = new ResourceBlock(0); + frame[i][j] = new ResourceBlock(0.0); } } } - public int getFrameSize() { - return this.timeSlotNb * this.subCarrierNb; - } - public ResourceBlock[][] getFrame() { return frame; } diff --git a/src/main/java/fr/ntr/Main.java b/src/main/java/fr/ntr/Main.java index 70ae2e5..5f2551f 100644 --- a/src/main/java/fr/ntr/Main.java +++ b/src/main/java/fr/ntr/Main.java @@ -24,11 +24,11 @@ public class Main { } int timeSlotNb = 2; int subCarrierNb = 100; - List users = generateUsers(20, timeSlotNb, subCarrierNb); + List users = generateUsers(maximumLoad, timeSlotNb, subCarrierNb); ResourceBlock[][] frame = new ResourceBlock[timeSlotNb][subCarrierNb]; Scheduler scheduler = new RoundRobin("round robin", 0, frame, users); AccessPoint accessPoint = new AccessPoint(scheduler, frame, users, timeSlotNb, subCarrierNb, 0, 50); - accessPoint.startSimulation(numberOfTicks, maximumLoad); + accessPoint.startSimulation(numberOfTicks); } else { System.err.println("Please give launch arguments"); diff --git a/src/main/java/fr/ntr/scheduler/RoundRobin.java b/src/main/java/fr/ntr/scheduler/RoundRobin.java index 55ef36e..4566fbc 100644 --- a/src/main/java/fr/ntr/scheduler/RoundRobin.java +++ b/src/main/java/fr/ntr/scheduler/RoundRobin.java @@ -42,7 +42,9 @@ public class RoundRobin extends Scheduler { users.get(index).getPacketsSent().add(users.get(index).getPacketsToSend().get(0)); users.get(index).getPacketsToSend().remove(users.get(index).getPacketsToSend().get(0)); //on ajoute l'utilisateur a la frame - frame[Ts][Sp].setUser(UserSelection(Ts, Sp, users)); + User user = UserSelection(Ts, Sp, users); + frame[Ts][Sp].setUser(user); + frame[Ts][Sp].setBandwidth(user.getBandwidthTable()[Ts][Sp]); } }