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

141 lines
3.8 KiB
Java
Raw Normal View History

2023-03-03 10:38:35 +01:00
package fr.ntr;
2023-03-17 10:55:45 +01:00
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
2023-03-03 12:12:18 +01:00
import java.util.ArrayList;
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{
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;
}
2023-03-10 08:30:28 +01:00
for (int ticks = 0; ticks < duration; ++ticks) {
// Simulation
reset();
updateBandwidth(ticks);
schedule();
// 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-03 11:28:16 +01:00
}
private void updateBandwidth(int ticks) {
int n = 200;
2023-03-17 10:29:36 +01:00
int timeInterval = 50 + random.nextInt(51);
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-17 10:29:36 +01:00
// On régénère les sources toutes les 50-100 ms
if(ticks % timeInterval == 0){
2023-03-17 10:29:36 +01:00
timeInterval = 50 + random.nextInt(51);
n = user.createPackets(n, ticks);
}
}
}
2023-03-10 08:30:28 +01:00
private void schedule() {
2023-03-17 09:48:38 +01:00
scheduler.scheduling();
}
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;
}
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
}