diff --git a/build.gradle b/build.gradle index c64edef..b0052ff 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ plugins { } run { - args = ["1000", "250"] + args = ["10000", "17"] } application { diff --git a/plot/main.py b/plot/main.py new file mode 100644 index 0000000..5f91b25 --- /dev/null +++ b/plot/main.py @@ -0,0 +1,82 @@ +import os +import matplotlib.pyplot as plt +import numpy as np +import pandas as pd + +nb_files = os.listdir(".." + os.sep + "export") + +size = len(nb_files) + + +def mean_mkn() -> np.ndarray: + averages_mkn = np.empty((size, 2)) + nb = 0 + for i in nb_files: + data = pd.read_csv(".." + os.sep + "export" + os.sep + i, delimiter=';').to_numpy() + rb = data[:, 4] + + total = 0.0 + for x in rb: + total = total + x + average = total / len(rb) + nb_users = i.split(".")[0] + averages_mkn[nb, 0] = int(nb_users) + averages_mkn[nb, 1] = average + nb += 1 + return averages_mkn + + +def rb_available() -> np.ndarray: + available = np.zeros((size, 2)) + nb = 0 + for i in nb_files: + data = pd.read_csv(".." + os.sep + "export" + os.sep + i, delimiter=';').to_numpy() + nb_users = i.split(".")[0] + available[nb, 0] = int(nb_users) + available[nb, 1] = (data.shape[0] / (200 * 10000)) * 100 + nb += 1 + return available + + +def delay() -> np.ndarray: + delays = np.zeros((size, 2)) + nb = 0 + for i in nb_files: + data = pd.read_csv(".." + os.sep + "export" + os.sep + i, delimiter=';').to_numpy() + nb_users = i.split(".")[0] + d = data[:, 5] + for x in d: + delays[nb, 0] = int(nb_users) + delays[nb, 1] = float(x) + nb += 1 + return delays + + +averages = mean_mkn() +available = rb_available() +delays = delay() +delays.sort(axis=0) +# Data for plotting +averages.sort(axis=0) + +fig, ax = plt.subplots() +ax.scatter(averages[:, 0], averages[:, 1]) + +ax.set(xlabel='number of users', ylabel='Efficacité spectrale', title='Efficacité spectrale') +ax.grid() + +# fig.savefig("test.png") +plt.show() + +fig, ax = plt.subplots() +ax.scatter(available[:, 0], available[:, 1]) +ax.set(xlabel='number of users', ylabel='RB utilisés', title='Pourcentage de RB utilisés') +ax.grid() +plt.show() + + +fig, ax = plt.subplots() +ax.scatter(delays[:, 0], delays[:, 1]) +ax.set(xlabel='number of users', ylabel='delays(ms)', title='Delay') +ax.grid() +plt.show() \ No newline at end of file diff --git a/src/main/java/fr/ntr/AccessPoint.java b/src/main/java/fr/ntr/AccessPoint.java index aa98c6d..e2c8d50 100644 --- a/src/main/java/fr/ntr/AccessPoint.java +++ b/src/main/java/fr/ntr/AccessPoint.java @@ -3,25 +3,34 @@ package fr.ntr; import java.io.File; 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; -import fr.ntr.Reuse.Reuse1; -import fr.ntr.Reuse.Reuse3; import fr.ntr.scheduler.Scheduler; public class AccessPoint { + /** + * nombre de slots + */ + private static int timeSlotNb; + /** + * Nombre de sous-porteuses + */ + private static int subCarrierNb; private final Cell cell1; private final Cell cell2; - private List users; private FileOutputStream outputDataFile; + private final List cellList; + public AccessPoint(Cell cell1, Cell cell2){ this.cell1 = cell1; this.cell2 = cell2; + cellList = new ArrayList<>(); + cellList.add(cell1); + cellList.add(cell2); } /** @@ -29,41 +38,30 @@ public class AccessPoint { * @param duration */ public void startSimulation(int duration) { - /* - try{ - Files.deleteIfExists(Paths.get("export", this.users.size() + ".csv")); - new File("export").mkdir(); + /*try{ this.outputDataFile = new FileOutputStream("export" + File.separator + this.users.size() + ".csv", true); - outputDataFile.write("tick;x;y;user;bandwidth;\n".getBytes()); + outputDataFile.write("tick;x;y;user;bandwidth;delay;\n".getBytes()); } catch(IOException e) { System.err.println(e.getClass().getSimpleName() + " : " + e.getMessage()); System.exit(1); return; - } - */ + } */ for (int ticks = 0; ticks < duration; ++ticks) { // Simulation cell1.reset(); cell2.reset(); - List cellList = null; - cellList.add(cell1); - cellList.add(cell2); cell1.updateBandwidth(ticks); cell2.updateBandwidth(ticks); - cell1.schedule(); - cell2.schedule(); - - + cell1.schedule(ticks); + cell2.schedule(ticks); //simulation des interférences computeInterference(); - // traite les données et les enregistre dans un fichier - - /* try { + try { analyseData(ticks); } catch (IOException e) { System.out.println("Can't export data"); - }*/ + } } } @@ -77,62 +75,49 @@ public class AccessPoint { //interférences si les deux cellules parlent au même UE sur le même time slot User user1 = frameCell1[k][l].getUser(); User user2 = frameCell2[k][l].getUser(); - if (user1 == user2 && (user1 != null || user2 != null)) { + if ((user1 != null || user2 != null) && user1 == user2) { double bandwidth1 = frameCell1[k][l].getBandwidth(); double bandwidth2 = frameCell2[k][l].getBandwidth(); //User proche if (user1.getDistance() < 200d) { frameCell1[k][l].getUser().getBandwidthTable()[k][l] = bandwidth1 / 2; frameCell2[k][l].getUser().getBandwidthTable()[k][l] = bandwidth2 / 2; - //System.out.println("Interference Cell1 /2 ="+frameCell1[k][l].getUser().getBandwidthTable()[k][l]); - //System.out.println("Interference Cell2 /2 ="+frameCell2[k][l].getUser().getBandwidthTable()[k][l]); } //User loin else { frameCell1[k][l].getUser().getBandwidthTable()[k][l] = bandwidth1 / 4; frameCell2[k][l].getUser().getBandwidthTable()[k][l] = bandwidth2 / 4; - //System.out.println("Interference Cell1 /4 ="+frameCell1[k][l].getUser().getBandwidthTable()[k][l]); - //System.out.println("Interference Cell2 /4 ="+frameCell2[k][l].getUser().getBandwidthTable()[k][l]); } } } - - // traite les données et les enregistre dans un fichier - - /* try { - analyseData(ticks); - } catch (IOException e) { - System.out.println("Can't export data"); - }*/ - } } -/* private void analyseData(int tick) throws IOException { - double delayAverage = 0.0; - int nbPacketsSent = 0; - for(User u: this.users){ - List packets = u.getPacketsSent(); - nbPacketsSent += packets.size(); - for (Packets p: packets){ - delayAverage += p.getDurationSending(); + /*double delayAverage = 0.0; + int nbPacketsSent = 0; + for(User u: this.users){ + List packets = u.getPacketsSent(); + nbPacketsSent += packets.size(); + for (Packets p: packets){ + delayAverage += p.getDurationSending(); + } + } + delayAverage = delayAverage/nbPacketsSent; + for(int i = 0; i < frame.length; i++) { + for(int j = 0; j < frame[i].length; j++) { + ResourceBlock ur = frame[i][j]; + if(ur.getUser() != null) { + String data = (tick + ";" + i + ";" + j + ";" + this.users.indexOf(ur.getUser()) + ";" + ur.getBandwidth() + ";" + delayAverage +";\n"); + try{ + outputDataFile.write(data.getBytes()); + }catch(IOException e){ + System.err.println("Cannot write the data in the output file"); + System.exit(1); + return; + } } } - /*delayAverage = delayAverage/nbPacketsSent; - for(int i = 0; i < frame.length; i++) { - for(int j = 0; j < frame[i].length; j++) { - ResourceBlock ur = frame[i][j]; - if(ur.getUser() != null) { - String data = (tick + ";" + i + ";" + j + ";" + this.users.indexOf(ur.getUser()) + ";" + ur.getBandwidth() + ";" + delayAverage +";\n"); - try{ - outputDataFile.write(data.getBytes()); - }catch(IOException e){ - System.err.println("Cannot write the data in the output file"); - System.exit(1); - return; - } - } - } - }*/ + } */ + } } diff --git a/src/main/java/fr/ntr/Cell.java b/src/main/java/fr/ntr/Cell.java index b7227ad..925ae3c 100644 --- a/src/main/java/fr/ntr/Cell.java +++ b/src/main/java/fr/ntr/Cell.java @@ -23,44 +23,34 @@ public class Cell { /** * Trame */ - private ResourceBlock[][] frame; + private final ResourceBlock[][] frame; /** * Reste pour la prochaine source */ private double leftForNextSource; - /** - * Portée minimum et maximum de l'antenne - */ - private final double min, max; - private Random random = new Random(); + private final Random random = new Random(); - public Cell(Scheduler scheduler, ResourceBlock[][] frame, List users, int timeSlotNb, int subCarrierNb, double min, double max) { - this.min = min; - this.max = max; + public Cell(Scheduler scheduler, ResourceBlock[][] frame, List users) { this.users = users; this.scheduler = scheduler; this.frame = frame; - this.timeSlotNb = timeSlotNb; - this.subCarrierNb = subCarrierNb; } + public void updateBandwidth(int ticks) { - int n = 200; - int timeInterval = 50 + random.nextInt(51); + int m = 200; for(User user : users) { + // On régénère les sources toutes les 50-100 ms + user.createPackets(m, ticks); // On régénère le tableau de débits toutes les 50 ms if(ticks % 50 == 0){ user.generateBandwidth(); } - // On régénère les sources toutes les 50-100 ms - if(ticks % timeInterval == 0){ - timeInterval = 50 + random.nextInt(51); - n = user.createPackets(n, ticks); - } } } + public void reset() { // TODO insert new UR for(int i = 0; i < timeSlotNb; i++) { @@ -70,8 +60,8 @@ public class Cell { } } - public void schedule() { - scheduler.scheduling(); + public void schedule(int tick) { + scheduler.scheduling(tick); } public ResourceBlock[][] getFrame() { @@ -85,4 +75,12 @@ public class Cell { public static int getSubCarrierNb() { return subCarrierNb; } + + public static void setTimeSlotNb(int timeSlotNb) { + Cell.timeSlotNb = timeSlotNb; + } + + public static void setSubCarrierNb(int subCarrierNb) { + Cell.subCarrierNb = subCarrierNb; + } } \ No newline at end of file diff --git a/src/main/java/fr/ntr/Main.java b/src/main/java/fr/ntr/Main.java index e5eae1a..bb490c2 100644 --- a/src/main/java/fr/ntr/Main.java +++ b/src/main/java/fr/ntr/Main.java @@ -4,20 +4,25 @@ import fr.ntr.scheduler.MaxSNR; import fr.ntr.scheduler.RoundRobin; import fr.ntr.scheduler.Scheduler; +import java.io.File; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.Objects; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; public class Main { public static void main(String[] args) { - if(args.length == 2) { + if(args.length == 2) { int numberOfTicks; // Nombre de ticks de la simulation -> durée de la simulation int maximumLoad; // Nombre maximal d'utilisateurs dans le système //TODO make this cleanly - boolean reuse3=false;//reuse 1 par def sinon reuse 3 - int nbcell = 2; //nombre de cellule que l'on va utilise + boolean reuse3=false; //reuse 1 par def sinon reuse 3 + int nbcell = 2; //nombre de cellules que l'on va utilise try { @@ -31,29 +36,36 @@ public class Main { } int timeSlotNb = 2; int subCarrierNb = 100; - for(int i = 2; i < maximumLoad; i += 2) { - List users = generateUsers(i, timeSlotNb, subCarrierNb); + File folder = new File("export"); + if(!folder.exists() && !folder.mkdir()) { + System.err.println("Cannot create export folder"); + System.exit(1); - //reuse choise - ResourceBlock[][] frame; - if(reuse3){ - frame = new ResourceBlock[timeSlotNb][subCarrierNb/nbcell]; - } else { - frame = new ResourceBlock[timeSlotNb][subCarrierNb]; - } - - //TODO : changes schedulers - Scheduler schedulerCell1 = new MaxSNR(frame, users); - Scheduler schedulerCell2 = new MaxSNR(frame, users); - //TODO : modify cells parameters ? - - Cell cell1 = new Cell(schedulerCell1, frame, users, timeSlotNb, subCarrierNb, 0, 50); - Cell cell2 = new Cell(schedulerCell2, frame, users, timeSlotNb, subCarrierNb, 0, 50); - AccessPoint accessPoint = new AccessPoint(cell1, cell2); - accessPoint.startSimulation(numberOfTicks); } - } - else { + Arrays.stream(Objects.requireNonNull(new File("export").listFiles())) + .filter(File::isFile) + .forEach(f -> { + if(!f.delete()) { + System.err.println("Cannot remove file " + f.getAbsolutePath()); + } + }); + Cell.setTimeSlotNb(timeSlotNb); + Cell.setSubCarrierNb(subCarrierNb); + try(ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())) { + for(int i = 2; i <= maximumLoad; i += 2) { + List users = generateUsers(i, timeSlotNb, subCarrierNb); + ResourceBlock[][] frame = new ResourceBlock[timeSlotNb][subCarrierNb]; + //TODO : changes schedulers + Scheduler schedulerCell1 = new MaxSNR(frame, users); + Scheduler schedulerCell2 = new MaxSNR(frame, users); + + Cell cell1 = new Cell(schedulerCell1, frame, users); + Cell cell2 = new Cell(schedulerCell2, frame, users); + AccessPoint accessPoint = new AccessPoint(cell1, cell2); + executor.submit(() -> accessPoint.startSimulation(numberOfTicks)); + } + } + } else { System.err.println("Please give launch arguments"); System.err.println("gradle run --args=\" \""); System.exit(1); diff --git a/src/main/java/fr/ntr/Packets.java b/src/main/java/fr/ntr/Packets.java index 01e1602..f6c5a74 100644 --- a/src/main/java/fr/ntr/Packets.java +++ b/src/main/java/fr/ntr/Packets.java @@ -1,16 +1,21 @@ package fr.ntr; public class Packets { - static final int packetSize = 100; + static final int PACKET_SIZE = 100; private int startTimeSending; private int endTimeSending; private int durationSending; - private double bitsNumberRemaining; + private int bitsNumberRemaining; public Packets(int startTimeSending){ + this.bitsNumberRemaining = PACKET_SIZE; this.startTimeSending = startTimeSending; } + public double getBitsNumberRemaining() { + return bitsNumberRemaining; + } + public int getStartTimeSending() { return startTimeSending; } @@ -24,17 +29,20 @@ public class Packets { } public void setEndTimeSending(int endTimeSending) { - this.endTimeSending = endTimeSending; + this.endTimeSending = endTimeSending+1; } - public void setDurationSending(){ - int durationSending = endTimeSending - startTimeSending; - this.durationSending = durationSending; + public void setDurationSending(int ticks){ + this.durationSending = ticks - startTimeSending; } public int getDurationSending() { return durationSending; } + public void decreaseBitsNumberRemaining(int d) { + this.bitsNumberRemaining -= d; + } + } \ No newline at end of file diff --git a/src/main/java/fr/ntr/User.java b/src/main/java/fr/ntr/User.java index 8c786e9..d80be5d 100644 --- a/src/main/java/fr/ntr/User.java +++ b/src/main/java/fr/ntr/User.java @@ -11,6 +11,9 @@ public class User { private final List packetsToSend; private final List packetsSent; private int leftForNextSource; + private int timeInterval = 1; + + private int mbis; private final Random random = new Random(); @@ -23,46 +26,38 @@ public class User { } public void generateBandwidth() { - for(int y = 0; y < bandwidthTable[0].length; y++) { - double random = this.random.nextDouble(); - for(int x = 0; x < bandwidthTable.length; x++) { - double h = 1 * Math.sqrt(-2 * Math.log(1 - random)); - double gain = h * Math.pow(10, random * 1/10) * Math.pow(1 / this.distance, 3.5); - double spectralEfficiency = (20 * gain) / (15000 * (3.9*Math.pow(10, -21))); - double mkn = Math.log(1 + spectralEfficiency) / Math.log(2); - this.bandwidthTable[x][y] = mkn; - } + double rand = this.random.nextDouble(); + double h = 1 * Math.sqrt(-2 * Math.log(1 - rand)); + double gain = h * h * Math.pow(10, rand * 1/10) * Math.pow(1 / this.distance, 3.5); + double spectralEfficiency = (20 * gain) / (15000 * (3.9*Math.pow(10, -21))); + double mkn = Math.log(1 + spectralEfficiency) / Math.log(2); + this.bandwidthTable[0][y] = mkn; + this.bandwidthTable[1][y] = mkn; } } /** * - * @param m un truc + * @param m * @param ticks qui va définir le temps auquel a été créé un paquet - * @return mbis */ - public int createPackets(int m, int ticks) { - Random random = new Random(); - // On tire un nombre entre 0 et 2 * m - int mbis = m == 0 ? 0 : random.nextInt(2 * (m + this.leftForNextSource)); - // On calcule le nombre de paquets qu'on peut transmettre - int nbPacketsToSend = mbis / Packets.packetSize; - // On conserve le nombre de bits restants pour la prochaine génération - this.leftForNextSource = mbis % Packets.packetSize; - // On crée les paquets - for(int i = 0; i < nbPacketsToSend; i++){ - this.packetsToSend.add(new Packets(ticks)); - } - return mbis; - } + public void createPackets(int m, int ticks) { + timeInterval--; + if(timeInterval == 0) { + timeInterval = random.nextInt(50, 101); + // On tire un nombre entre 0 et 2 * m + mbis = random.nextInt(1, 2 * m + 1) ; - /** - * Calcul délais de transmission des paquets - */ - public void computeTimeSending() { - for(Packets p : packetsToSend){ - p.setDurationSending(); + } + // On calcule le nombre de paquets qu'on peut transmettre + int bitsToSend = random.nextInt(2 * mbis + 1) + this.leftForNextSource; + int nbPacketsToSend = bitsToSend / Packets.PACKET_SIZE; + // On conserve le nombre de bits restants pour la prochaine génération + this.leftForNextSource = bitsToSend % Packets.PACKET_SIZE; + // On crée les paquets + for(int i = 0; i < nbPacketsToSend; i++) { + this.packetsToSend.add(new Packets(ticks)); } } diff --git a/src/main/java/fr/ntr/scheduler/MaxSNR.java b/src/main/java/fr/ntr/scheduler/MaxSNR.java index 0f4f10a..ae5090c 100644 --- a/src/main/java/fr/ntr/scheduler/MaxSNR.java +++ b/src/main/java/fr/ntr/scheduler/MaxSNR.java @@ -15,33 +15,28 @@ public class MaxSNR extends Scheduler { } @Override - public void scheduling() { + public void scheduling(int ticks) { User userMax; for(int ts = 0; ts < 2; ts++){ for(int sp = 0; sp < 100; sp++){ - userMax = selectionUtilisateur(sp, ts, users); - if (userMax.getPacketsToSend().size() != 0) { - userMax.getPacketsSent().add(userMax.getPacketsToSend().get(0)); - userMax.getPacketsToSend().remove(userMax.getPacketsToSend().get(0)); - frame[ts][sp].setUser(userMax); - frame[ts][sp].setBandwidth(userMax.getBandwidthTable()[ts][sp]); - } + userMax = userSelection(ts, sp); + allocateRessource(userMax, frame, ts, sp, ticks); } } } /** + * Sélectionne le prochain utilisateur en fonction du contexte * @param ts Time slot (int) * @param sp Sous porteuse(int) - * @param Users (List) - * Return + * @return the user with the best bandwidth */ - private User selectionUtilisateur(int sp, int ts, List Users) { - double MaxSnr = 0.0; + private User userSelection(int ts, int sp) { + double maxSnr = 0.0; User userMax = null; - for(User u: Users){ - if (MaxSnr < u.getBandwidthTable()[ts][sp]){ - MaxSnr = u.getBandwidthTable()[ts][sp]; + for(User u: users){ + if (maxSnr < u.getBandwidthTable()[ts][sp]) { + maxSnr = u.getBandwidthTable()[ts][sp]; userMax = u; } } diff --git a/src/main/java/fr/ntr/scheduler/ProportionalFair.java b/src/main/java/fr/ntr/scheduler/ProportionalFair.java index b11e5b4..6865746 100644 --- a/src/main/java/fr/ntr/scheduler/ProportionalFair.java +++ b/src/main/java/fr/ntr/scheduler/ProportionalFair.java @@ -1,5 +1,6 @@ package fr.ntr.scheduler; +import java.util.ArrayList; import java.util.List; import fr.ntr.ResourceBlock; @@ -7,29 +8,52 @@ import fr.ntr.User; public class ProportionalFair extends Scheduler { - private final String name; + private final List users; + private final ResourceBlock[][] frame; - public ProportionalFair(String name, ResourceBlock[][] frame, List users) { - this.name = name; + public ProportionalFair(ResourceBlock[][] frame, List users) { this.frame = frame; this.users = users; } - @Override - public void scheduling() { - } - - private void selectionUtilisateur(int Ts, int Sp, List Users) { - - } - @Override - protected void TraitementDonnees() { - // TODO Auto-generated method stub - + @Override + public void scheduling(int ticks) { + ArrayList averageBandwiths = new ArrayList(); + for(User u : users){ + double avg = 0d; + double[][] bandwidthTable = u.getBandwidthTable(); + for (double[] doubles : bandwidthTable) { + for (double aDouble : doubles) { + avg += aDouble; + } + } + avg = avg / (bandwidthTable.length * bandwidthTable[0].length); + averageBandwiths.add(avg); } + for(int ts = 0; ts < 2; ts++){ + for(int sp = 0; sp < 100; sp++){ + User selectedUser = userSelection(ts, sp, users, averageBandwiths); + allocateRessource(selectedUser, frame, ts, sp, ticks); + } + } + } - + private User userSelection(int ts, int sp, List users, List averageBandwiths) { + double PF = 0.0; + User selectedUser = null; + for(int i = 0; i < users.size(); i++){ + User u = users.get(i); + double mkn = u.getBandwidthTable()[ts][sp]; + double averageMkn = averageBandwiths.get(i); + double pf = mkn / averageMkn; + if (PF < pf){ + PF = pf; + selectedUser = u; + } + } + return selectedUser; + } } diff --git a/src/main/java/fr/ntr/scheduler/RoundRobin.java b/src/main/java/fr/ntr/scheduler/RoundRobin.java index fa4ee9a..71c9fd4 100644 --- a/src/main/java/fr/ntr/scheduler/RoundRobin.java +++ b/src/main/java/fr/ntr/scheduler/RoundRobin.java @@ -1,6 +1,7 @@ package fr.ntr.scheduler; import java.util.List; import java.util.Random; +import java.util.stream.Collectors; import fr.ntr.Cell; import fr.ntr.ResourceBlock; @@ -9,13 +10,10 @@ import fr.ntr.User; public class RoundRobin extends Scheduler { - private int index; - private final List users; private final ResourceBlock[][] frame; - public RoundRobin(int index, ResourceBlock[][] frame, List users) { - this.index = index; + public RoundRobin(ResourceBlock[][] frame, List users) { this.frame = frame; this.users = users; } @@ -26,43 +24,30 @@ public class RoundRobin extends Scheduler { * Return */ @Override - public void scheduling() { - //selection aleatoire du premier utilisateur - Random random = new Random(); - index = random.nextInt(users.size()); + public void scheduling(int ticks) { + List userCopy = users.stream().filter(u -> !u.getPacketsToSend().isEmpty()).collect(Collectors.toList()); //Pour chaque time slot et sous porteuses - for (int ts = 0; ts < Cell.getTimeSlotNb(); ts++) { + loop: for (int ts = 0; ts < Cell.getTimeSlotNb(); ts++) { for(int sp = 0; sp < Cell.getSubCarrierNb(); sp++) { - if (users.get(index).getPacketsToSend().size() != 0) { - // on enlève le packet transmis de la liste - // TODO Verify sub the packet send need Set packet - 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 - User user = UserSelection(ts, sp); - frame[ts][sp].setUser(user); - frame[ts][sp].setBandwidth(user.getBandwidthTable()[ts][sp]); + if(userCopy.isEmpty()) { + break loop; } - + User userSelected = userSelection(userCopy); + allocateRessource(userSelected, frame, ts, sp, ticks); + if(!userSelected.getPacketsToSend().isEmpty()) + userCopy.add(userSelected); } } } /** - * @param ts Time slot (int) - * @param sp Sous porteuse(int) + * Entry Time slot (int), Sous porteuse(int), and users ( List) * Return the user in function of TS and SP selected + * + * @return the next user which have something to send */ - private User UserSelection(int ts, int sp) { - //compte le nombre de bloc attribue - for (int i = 0; i < ts; i++){ - for (int j = 0; j < sp; j++) { - index++; - } - } - //on retourne l'utilisateur - return users.get(index%(users.size() - 1)); - + private User userSelection(List users) { + return users.remove(0); } } diff --git a/src/main/java/fr/ntr/scheduler/Scheduler.java b/src/main/java/fr/ntr/scheduler/Scheduler.java index e21432d..b2dc953 100644 --- a/src/main/java/fr/ntr/scheduler/Scheduler.java +++ b/src/main/java/fr/ntr/scheduler/Scheduler.java @@ -1,5 +1,9 @@ package fr.ntr.scheduler; +import fr.ntr.Packets; +import fr.ntr.ResourceBlock; +import fr.ntr.User; + public abstract class Scheduler { /** @@ -7,8 +11,24 @@ public abstract class Scheduler { * Rempli la trame avec les utilisateurs * Return */ - public abstract void scheduling(); - protected void TraitementDonnees() { + public abstract void scheduling(int ticks); + protected void allocateRessource(User userMax, ResourceBlock[][] frame, int ts, int sp, int ticks) { + if (userMax != null && !userMax.getPacketsToSend().isEmpty()) { + Packets p = userMax.getPacketsToSend().get(0); + if(p.getBitsNumberRemaining() > 0){ + p.decreaseBitsNumberRemaining((int) userMax.getBandwidthTable()[ts][sp]); + } else { + if(ticks == 0){ + p.setDurationSending(1); + }else { + p.setDurationSending(ticks); + } + userMax.getPacketsSent().add(p); + userMax.getPacketsToSend().remove(p); + } + frame[ts][sp].setUser(userMax); + frame[ts][sp].setBandwidth(userMax.getBandwidthTable()[ts][sp]); + } } }