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

135 lines
3.8 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;
2023-03-03 11:28:16 +01:00
import java.util.List;
import fr.ntr.scheduler.Scheduler;
2023-03-03 10:38:35 +01:00
public class AccessPoint {
private final List<User> users;
private final 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 final ResourceBlock[][] frame;
private FileOutputStream outputDataFile;
2023-03-03 11:28:16 +01:00
public AccessPoint(Scheduler scheduler, ResourceBlock[][] frame, List<User> users) {
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;
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
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) {
2023-03-30 09:33:28 +02:00
int m = 200;
for(User user : users) {
2023-03-30 09:33:28 +02:00
// On régénère les sources toutes les 50-100 ms
user.createPackets(m, ticks);
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 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: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
}
public static void setTimeSlotNb(int timeSlotNb) {
AccessPoint.timeSlotNb = timeSlotNb;
}
public static void setSubCarrierNb(int subCarrierNb) {
AccessPoint.subCarrierNb = subCarrierNb;
}
2023-03-03 10:38:35 +01:00
}