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-17 20:31:42 +01:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Paths;
|
2023-03-03 11:28:16 +01:00
|
|
|
import java.util.List;
|
2023-03-06 17:48:22 +01:00
|
|
|
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;
|
|
|
|
/**
|
2023-03-10 08:19:07 +01:00
|
|
|
* Nombre de sous-porteuses
|
2023-03-03 11:47:53 +01:00
|
|
|
*/
|
|
|
|
private static int subCarrierNb;
|
|
|
|
/**
|
|
|
|
* trame
|
|
|
|
*/
|
|
|
|
private ResourceBlock[][] frame;
|
|
|
|
/**
|
2023-03-10 08:19:07 +01:00
|
|
|
* Reste pour la prochaine source
|
2023-03-03 11:47:53 +01:00
|
|
|
*/
|
|
|
|
private double leftForNextSource;
|
2023-03-06 17:48:22 +01:00
|
|
|
/**
|
2023-03-10 08:19:07 +01:00
|
|
|
* Portée minimum et maximum de l'antenne
|
2023-03-06 17:48:22 +01:00
|
|
|
*/
|
|
|
|
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();
|
|
|
|
|
2023-03-17 20:31:42 +01:00
|
|
|
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) {
|
2023-03-06 17:48:22 +01:00
|
|
|
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;
|
2023-03-17 10:45:55 +01:00
|
|
|
this.frame = frame;
|
|
|
|
this.timeSlotNb = timeSlotNb;
|
|
|
|
this.subCarrierNb = subCarrierNb;
|
2023-03-03 11:28:16 +01:00
|
|
|
}
|
|
|
|
|
2023-03-06 17:48:22 +01:00
|
|
|
/**
|
|
|
|
* Lancer la simulation
|
|
|
|
* @param duration
|
|
|
|
*/
|
2023-03-17 20:31:42 +01:00
|
|
|
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-24 08:33:43 +01:00
|
|
|
outputDataFile.write("tick;x;y;user;bandwidth;\n".getBytes());
|
2023-03-24 09:05:44 +01:00
|
|
|
} catch(IOException e) {
|
|
|
|
System.err.println(e.getClass().getSimpleName() + " : " + e.getMessage());
|
2023-03-17 20:31:42 +01:00
|
|
|
System.exit(1);
|
|
|
|
return;
|
|
|
|
}
|
2023-03-10 08:30:28 +01:00
|
|
|
for (int ticks = 0; ticks < duration; ++ticks) {
|
2023-03-06 17:48:22 +01:00
|
|
|
// Simulation
|
|
|
|
reset();
|
2023-03-17 09:43:32 +01:00
|
|
|
updateBandwidth(ticks);
|
2023-03-24 12:12:18 +01:00
|
|
|
schedule(ticks);
|
|
|
|
computePacketDelay(ticks);
|
2023-03-06 17:48:22 +01:00
|
|
|
// traite les données et les enregistre dans un fichier
|
2023-03-17 10:55:45 +01:00
|
|
|
try {
|
2023-03-24 08:19:41 +01:00
|
|
|
analyseData(ticks);
|
2023-03-17 10:55:45 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
System.out.println("Can't export data");
|
|
|
|
}
|
2023-03-06 17:48:22 +01:00
|
|
|
}
|
2023-03-24 12:12:18 +01:00
|
|
|
|
2023-03-03 11:28:16 +01:00
|
|
|
}
|
|
|
|
|
2023-03-17 09:43:32 +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
|
2023-03-17 09:43:32 +01:00
|
|
|
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
|
2023-03-17 09:43:32 +01:00
|
|
|
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
|
2023-03-17 09:43:32 +01:00
|
|
|
if(ticks % timeInterval == 0){
|
2023-03-17 10:29:36 +01:00
|
|
|
timeInterval = 50 + random.nextInt(51);
|
2023-03-17 11:36:27 +01:00
|
|
|
n = user.createPackets(n, ticks);
|
2023-03-17 09:43:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 12:12:18 +01:00
|
|
|
private void schedule(int ticks) {
|
|
|
|
scheduler.scheduling(ticks);
|
2023-03-06 17:48:22 +01:00
|
|
|
}
|
|
|
|
|
2023-03-24 08:19:41 +01:00
|
|
|
private void analyseData(int tick) throws IOException {
|
2023-03-17 20:31:42 +01:00
|
|
|
for(int i = 0; i < frame.length; i++) {
|
|
|
|
for(int j = 0; j < frame[i].length; j++) {
|
|
|
|
ResourceBlock ur = frame[i][j];
|
2023-03-24 08:19:41 +01:00
|
|
|
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++) {
|
2023-03-17 20:31:42 +01:00
|
|
|
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-24 12:12:18 +01:00
|
|
|
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()){
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|