This repository has been archived on 2023-08-28. You can view files and clone it, but cannot push or open issues or pull requests.
ntr-interferences/src/main/java/fr/ntr/AccessPoint.java

152 lines
4.4 KiB
Java
Raw Normal View History

2023-03-03 10:38:35 +01:00
package fr.ntr;
2023-03-24 09:05:44 +01:00
import java.io.File;
2023-03-17 10:55:45 +01:00
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
2023-03-03 11:28:16 +01:00
import java.util.List;
import java.util.Random;
2023-03-03 11:28:16 +01:00
import fr.ntr.scheduler.Scheduler;
2023-03-03 10:38:35 +01:00
public class AccessPoint {
2023-03-10 10:50:46 +01:00
private List<User> users;
2023-03-03 11:28:16 +01:00
private Scheduler scheduler;
2023-03-03 11:47:53 +01:00
/**
* nombre de slots
*/
private static int timeSlotNb;
/**
* Nombre de sous-porteuses
2023-03-03 11:47:53 +01:00
*/
private static int subCarrierNb;
/**
* trame
*/
private ResourceBlock[][] frame;
/**
* Reste pour la prochaine source
2023-03-03 11:47:53 +01:00
*/
private double leftForNextSource;
/**
* Portée minimum et maximum de l'antenne
*/
private final double min, max;
2023-03-03 11:28:16 +01:00
2023-03-17 11:38:31 +01:00
private Random random = new Random();
private FileOutputStream outputDataFile;
2023-03-03 11:28:16 +01:00
2023-03-17 11:11:26 +01:00
public AccessPoint(Scheduler scheduler, ResourceBlock[][] frame, List<User> users, int timeSlotNb, int subCarrierNb, double min, double max) {
this.min = min;
this.max = max;
2023-03-17 11:11:26 +01:00
this.users = users;
2023-03-03 11:28:16 +01:00
this.scheduler = scheduler;
this.frame = frame;
this.timeSlotNb = timeSlotNb;
this.subCarrierNb = subCarrierNb;
2023-03-03 11:28:16 +01:00
}
/**
* Lancer la simulation
* @param duration
*/
public void startSimulation(int duration) {
try{
2023-03-24 09:05:44 +01:00
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);
2023-03-28 09:35:26 +02:00
outputDataFile.write("tick;x;y;user;bandwidth;delay;\n".getBytes());
2023-03-24 09:05:44 +01:00
} catch(IOException e) {
System.err.println(e.getClass().getSimpleName() + " : " + e.getMessage());
System.exit(1);
return;
}
2023-03-10 08:30:28 +01:00
for (int ticks = 0; ticks < duration; ++ticks) {
// Simulation
reset();
updateBandwidth(ticks);
2023-03-24 12:12:18 +01:00
schedule(ticks);
// traite les données et les enregistre dans un fichier
2023-03-17 10:55:45 +01:00
try {
analyseData(ticks);
2023-03-17 10:55:45 +01:00
} catch (IOException e) {
System.out.println("Can't export data");
}
}
2023-03-24 12:12:18 +01:00
2023-03-03 11:28:16 +01:00
}
private void updateBandwidth(int ticks) {
int n = 200;
2023-03-24 11:51:08 +01:00
int timeInterval = Math.max(1, random.nextInt(51)); // avoid div by 0
for(User user : users) {
2023-03-17 10:29:36 +01:00
// On régénère le tableau de débits toutes les 50 ms
if(ticks % 50 == 0){
user.generateBandwidth();
}
2023-03-24 11:51:08 +01:00
// On régénère les sources toutes les 0-50 ms
if(ticks % timeInterval == 0){
timeInterval = Math.max(1, random.nextInt(51));
n = user.createPackets(n, ticks);
}
}
}
2023-03-24 12:12:18 +01:00
private void schedule(int ticks) {
scheduler.scheduling(ticks);
}
private void analyseData(int tick) throws IOException {
2023-03-28 09:15:30 +02:00
double delayAverage = 0.0;
int nbPacketsSent = 0;
2023-03-28 08:32:26 +02:00
for(User u: this.users){
List<Packets> packets = u.getPacketsSent();
2023-03-28 09:15:30 +02:00
nbPacketsSent += packets.size();
2023-03-28 08:32:26 +02:00
for (Packets p: packets){
2023-03-28 09:15:30 +02:00
delayAverage += p.getDurationSending();
2023-03-28 08:32:26 +02:00
}
}
2023-03-28 09:15:30 +02:00
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) {
2023-03-28 09:15:30 +02:00
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;
}
2023-03-17 11:04:32 +01:00
}
}
}
2023-03-03 11:28:16 +01:00
}
2023-03-10 08:30:28 +01:00
private void reset() {
2023-03-17 12:00:01 +01:00
// TODO insert new UR
2023-03-17 12:08:19 +01:00
for(int i = 0; i < timeSlotNb; i++) {
for(int j = 0; j < subCarrierNb; j++) {
frame[i][j] = new ResourceBlock(0.0);
2023-03-17 12:08:19 +01:00
}
}
2023-03-03 11:28:16 +01:00
}
2023-03-03 12:12:18 +01:00
2023-03-10 08:30:28 +01:00
public ResourceBlock[][] getFrame() {
return frame;
}
2023-03-10 11:38:52 +01:00
public static int getTimeSlotNb() {
return timeSlotNb;
}
public static int getSubCarrierNb() {
return subCarrierNb;
2023-03-10 08:30:28 +01:00
}
2023-03-03 10:38:35 +01:00
}