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.List; import java.util.Random; import fr.ntr.scheduler.Scheduler; public class AccessPoint { private List users; private Scheduler scheduler; /** * nombre de slots */ private static int timeSlotNb; /** * Nombre de sous-porteuses */ private static int subCarrierNb; /** * trame */ private 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 FileOutputStream outputDataFile; public AccessPoint(Scheduler scheduler, ResourceBlock[][] frame, List users, int timeSlotNb, int subCarrierNb, double min, double max) { this.min = min; this.max = max; this.users = users; this.scheduler = scheduler; this.frame = frame; this.timeSlotNb = timeSlotNb; this.subCarrierNb = subCarrierNb; } /** * Lancer la simulation * @param duration */ public void startSimulation(int duration) { try{ Files.deleteIfExists(Paths.get("export", this.users.size() + ".csv")); new File("export").mkdir(); this.outputDataFile = new FileOutputStream("export" + File.separator + this.users.size() + ".csv", true); outputDataFile.write("tick;x;y;user;bandwidth;\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 reset(); updateBandwidth(ticks); schedule(ticks); computePacketDelay(ticks); // 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 updateBandwidth(int ticks) { int n = 200; int timeInterval = Math.max(1, random.nextInt(51)); // avoid div by 0 for(User user : users) { // 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 0-50 ms if(ticks % timeInterval == 0){ timeInterval = 50 + random.nextInt(51); n = user.createPackets(n, ticks); } } } private void schedule(int ticks) { scheduler.scheduling(ticks); } private void analyseData(int tick) throws IOException { 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() + ";\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 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.0); } } } private void computePacketDelay(int ticks){ for(int i = 0; i < this.users.size(); i++){ User u = this.users.get(i); if(!u.getPacketsToSend().isEmpty()){ for(Packets p : u.getPacketsToSend()){ } } } } public ResourceBlock[][] getFrame() { return frame; } public static int getTimeSlotNb() { return timeSlotNb; } public static int getSubCarrierNb() { return subCarrierNb; } }