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
2023-03-31 11:01:00 +02:00

140 lines
4.0 KiB
Java

package fr.ntr;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import fr.ntr.scheduler.Scheduler;
public class AccessPoint {
private final List<User> users;
private final Scheduler scheduler;
/**
* nombre de slots
*/
private static int timeSlotNb;
/**
* Nombre de sous-porteuses
*/
private static int subCarrierNb;
/**
* trame
*/
private final ResourceBlock[][] frame;
private FileOutputStream outputDataFile;
public AccessPoint(Scheduler scheduler, ResourceBlock[][] frame, List<User> users) {
this.users = users;
this.scheduler = scheduler;
this.frame = frame;
}
/**
* Lancer la simulation
* @param duration
*/
public void startSimulation(int duration) {
try{
this.outputDataFile = new FileOutputStream("export" + File.separator + this.users.size() + ".csv", true);
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
reset();
updateBandwidth(ticks);
schedule(ticks);
// traite les données et les enregistre dans un fichier
try {
analyseData(ticks);
} catch (IOException e) {
System.out.println("Can't export data");
}
}
int totalBits = 0;
for(User u: this.users){
totalBits += u.totalbits;
}
System.out.println("total bits / nb users = "+ (((float)totalBits)/this.users.size()/10_000));
}
private void updateBandwidth(int ticks) {
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();
}
}
}
private void schedule(int ticks) {
scheduler.scheduling(ticks);
}
private void analyseData(int tick) throws IOException {
double delayAverage = 0.0;
int nbPacketsSent = 0;
for(User u: this.users){
List<Packets> 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;
}
}
}
}
}
private void reset() {
for(int i = 0; i < timeSlotNb; i++) {
for(int j = 0; j < subCarrierNb; j++) {
frame[i][j] = new ResourceBlock(0.0);
}
}
}
public ResourceBlock[][] getFrame() {
return frame;
}
public static int getTimeSlotNb() {
return timeSlotNb;
}
public static int getSubCarrierNb() {
return subCarrierNb;
}
public static void setTimeSlotNb(int timeSlotNb) {
AccessPoint.timeSlotNb = timeSlotNb;
}
public static void setSubCarrierNb(int subCarrierNb) {
AccessPoint.subCarrierNb = subCarrierNb;
}
}