mirror of
https://github.com/davidalves04/Trabalho-Pratico-SD.git
synced 2025-12-08 20:43:32 +00:00
Compare commits
13 Commits
12-impleme
...
17-create-
| Author | SHA1 | Date | |
|---|---|---|---|
| 06f079ce5b | |||
| 72893f87ae | |||
| 6b94d727e2 | |||
| 84cba39597 | |||
| 5dc1b40c88 | |||
| 3117bdf332 | |||
| 1140c3ca48 | |||
|
|
484cba1eee | ||
|
|
0e5526c3f6 | ||
|
|
cf88db4297 | ||
|
|
0960a7a141 | ||
|
|
3b4f968a59 | ||
|
|
4710c96450 |
382
main/src/main/java/sd/ExitNodeProcess.java
Normal file
382
main/src/main/java/sd/ExitNodeProcess.java
Normal file
@@ -0,0 +1,382 @@
|
|||||||
|
package sd;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import sd.config.SimulationConfig;
|
||||||
|
import sd.coordinator.SocketClient;
|
||||||
|
import sd.model.Message;
|
||||||
|
import sd.model.MessageType;
|
||||||
|
import sd.model.Vehicle;
|
||||||
|
import sd.model.VehicleType;
|
||||||
|
import sd.protocol.MessageProtocol;
|
||||||
|
import sd.protocol.SocketConnection;
|
||||||
|
import sd.serialization.SerializationException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Processo responsável pelo nó de saída do sistema de simulação de tráfego distribuído.
|
||||||
|
*
|
||||||
|
* Este processo representa o ponto final ("S") onde os veículos completam as suas rotas.
|
||||||
|
* As suas principais responsabilidades são:
|
||||||
|
* - Receber veículos que terminam a sua rota vindos das interseções
|
||||||
|
* - Calcular e agregar estatísticas finais dos veículos
|
||||||
|
* - Enviar estatísticas periódicas para o dashboard
|
||||||
|
* - Gerar relatórios finais ao terminar a simulação
|
||||||
|
*/
|
||||||
|
public class ExitNodeProcess {
|
||||||
|
|
||||||
|
private final SimulationConfig config;
|
||||||
|
private ServerSocket serverSocket;
|
||||||
|
private final ExecutorService connectionHandlerPool;
|
||||||
|
|
||||||
|
/** Flag para controlar a execução do processo (volatile para visibilidade entre threads) */
|
||||||
|
private volatile boolean running;
|
||||||
|
|
||||||
|
/** Counter de veículos que completaram a rota */
|
||||||
|
private int totalVehiclesReceived;
|
||||||
|
|
||||||
|
/** Soma dos tempos no sistema de todos os veículos */
|
||||||
|
private double totalSystemTime;
|
||||||
|
|
||||||
|
/** Soma dos tempos de espera de todos os veículos */
|
||||||
|
private double totalWaitingTime;
|
||||||
|
|
||||||
|
/** Soma dos tempos de travessia de todos os veículos */
|
||||||
|
private double totalCrossingTime;
|
||||||
|
|
||||||
|
/** Contagem de veículos por tipo */
|
||||||
|
private final Map<VehicleType, Integer> vehicleTypeCount;
|
||||||
|
|
||||||
|
/** Tempo total de espera acumulado por tipo de veículo */
|
||||||
|
private final Map<VehicleType, Double> vehicleTypeWaitTime;
|
||||||
|
|
||||||
|
/** Socket para comunicação com o dashboard */
|
||||||
|
private SocketClient dashboardClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Método para iniciar o processo
|
||||||
|
*
|
||||||
|
* @param args Argumentos da linha de comandos. Se fornecido, args[0] deve ser
|
||||||
|
* o caminho para um ficheiro de configuração personalizado.
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("=".repeat(60));
|
||||||
|
System.out.println("EXIT NODE PROCESS");
|
||||||
|
System.out.println("=".repeat(60));
|
||||||
|
|
||||||
|
try {
|
||||||
|
String configFile = args.length > 0 ? args[0] : "src/main/resources/simulation.properties";
|
||||||
|
System.out.println("Loading configuration from: " + configFile);
|
||||||
|
|
||||||
|
SimulationConfig config = new SimulationConfig(configFile);
|
||||||
|
ExitNodeProcess exitNode = new ExitNodeProcess(config);
|
||||||
|
|
||||||
|
System.out.println("\n" + "=".repeat(60));
|
||||||
|
exitNode.initialize();
|
||||||
|
|
||||||
|
System.out.println("\n" + "=".repeat(60));
|
||||||
|
exitNode.start();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Failed to start exit node: " + e.getMessage());
|
||||||
|
System.exit(1);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Exit node error: " + e.getMessage());
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constrói um novo processo de nó de saída.
|
||||||
|
*
|
||||||
|
* Inicializa todas as estruturas de dados necessárias para recolher estatísticas
|
||||||
|
* e configura o pool de threads para processar as ligações concorrentes.
|
||||||
|
*
|
||||||
|
* @param config Configuração da simulação contendo portas e endereços dos serviços
|
||||||
|
*/
|
||||||
|
public ExitNodeProcess(SimulationConfig config) {
|
||||||
|
this.config = config;
|
||||||
|
this.connectionHandlerPool = Executors.newCachedThreadPool();
|
||||||
|
this.running = false;
|
||||||
|
|
||||||
|
this.totalVehiclesReceived = 0;
|
||||||
|
this.totalSystemTime = 0.0;
|
||||||
|
this.totalWaitingTime = 0.0;
|
||||||
|
this.totalCrossingTime = 0.0;
|
||||||
|
this.vehicleTypeCount = new HashMap<>();
|
||||||
|
this.vehicleTypeWaitTime = new HashMap<>();
|
||||||
|
|
||||||
|
// Inicializa os counters para cada tipo de veículo
|
||||||
|
for (VehicleType type : VehicleType.values()) {
|
||||||
|
vehicleTypeCount.put(type, 0);
|
||||||
|
vehicleTypeWaitTime.put(type, 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Exit node initialized");
|
||||||
|
System.out.println(" - Exit port: " + config.getExitPort());
|
||||||
|
System.out.println(" - Dashboard: " + config.getDashboardHost() + ":" + config.getDashboardPort());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inicializa o processo de ligação ao dashboard.
|
||||||
|
*
|
||||||
|
* Tenta conectar-se ao dashboard. Se a ligação falhar, o processo
|
||||||
|
* continua a funcionar normalmente, mas sem enviar estatísticas.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void initialize() {
|
||||||
|
System.out.println("Connecting to dashboard...");
|
||||||
|
|
||||||
|
try {
|
||||||
|
String host = config.getDashboardHost();
|
||||||
|
int port = config.getDashboardPort();
|
||||||
|
|
||||||
|
dashboardClient = new SocketClient("Dashboard", host, port);
|
||||||
|
dashboardClient.connect();
|
||||||
|
|
||||||
|
System.out.println("Successfully connected to dashboard");
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("WARNING: Failed to connect to dashboard: " + e.getMessage());
|
||||||
|
System.err.println("Exit node will continue without dashboard connection");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inicia o socket e começa a aceitar ligações.
|
||||||
|
*
|
||||||
|
* Este é o loop principal do processo que:
|
||||||
|
* 1. Cria um socket na porta definida
|
||||||
|
* 2. Aguarda pelas ligações das interseções
|
||||||
|
* 3. Delega cada ligação a uma thread da pool para processamento assíncrono
|
||||||
|
*
|
||||||
|
* @throws IOException Se o socket não puder ser criado ou houver erro na aceitação
|
||||||
|
*/
|
||||||
|
public void start() throws IOException {
|
||||||
|
int port = config.getExitPort();
|
||||||
|
serverSocket = new ServerSocket(port);
|
||||||
|
running = true;
|
||||||
|
|
||||||
|
System.out.println("Exit node started on port " + port);
|
||||||
|
System.out.println("Waiting for vehicles...\n");
|
||||||
|
|
||||||
|
while (running) {
|
||||||
|
try {
|
||||||
|
Socket clientSocket = serverSocket.accept();
|
||||||
|
connectionHandlerPool.submit(() -> handleIncomingConnection(clientSocket));
|
||||||
|
} catch (IOException e) {
|
||||||
|
if (running) {
|
||||||
|
System.err.println("Error accepting connection: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Processa uma ligação recebida de uma interseção.
|
||||||
|
*
|
||||||
|
* Mantém a ligação aberta e processa continuamente mensagens do tipo
|
||||||
|
* VEHICLE_TRANSFER. Cada mensagem representa um veículo que chegou ao nó de saída.
|
||||||
|
*
|
||||||
|
* @param clientSocket Socket da ligação estabelecida com a interseção
|
||||||
|
*/
|
||||||
|
private void handleIncomingConnection(Socket clientSocket) {
|
||||||
|
try (SocketConnection connection = new SocketConnection(clientSocket)) {
|
||||||
|
|
||||||
|
System.out.println("New connection accepted from " +
|
||||||
|
clientSocket.getInetAddress().getHostAddress());
|
||||||
|
|
||||||
|
while (running && connection.isConnected()) {
|
||||||
|
try {
|
||||||
|
MessageProtocol message = connection.receiveMessage();
|
||||||
|
|
||||||
|
if (message.getType() == MessageType.VEHICLE_TRANSFER) {
|
||||||
|
Vehicle vehicle = (Vehicle) message.getPayload();
|
||||||
|
processExitingVehicle(vehicle);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
System.err.println("Unknown message type received: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
if (running) {
|
||||||
|
System.err.println("Connection error: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Processa um veículo que chegou ao nó de saída.
|
||||||
|
*
|
||||||
|
* Método sincronizado para garantir thread-safety ao atualizar as estatísticas.
|
||||||
|
* Calcula as métricas finais do veículo e atualiza:
|
||||||
|
* - Counters globais;
|
||||||
|
* - Estatísticas por tipo de veículo;
|
||||||
|
* - Faz update ao dashboard a cada 10 veículos.
|
||||||
|
*
|
||||||
|
* @param vehicle Veículo que completou a sua rota
|
||||||
|
*/
|
||||||
|
private synchronized void processExitingVehicle(Vehicle vehicle) {
|
||||||
|
totalVehiclesReceived++;
|
||||||
|
|
||||||
|
double systemTime = vehicle.getTotalTravelTime(getCurrentTime());
|
||||||
|
double waitTime = vehicle.getTotalWaitingTime();
|
||||||
|
double crossingTime = vehicle.getTotalCrossingTime();
|
||||||
|
|
||||||
|
totalSystemTime += systemTime;
|
||||||
|
totalWaitingTime += waitTime;
|
||||||
|
totalCrossingTime += crossingTime;
|
||||||
|
|
||||||
|
VehicleType type = vehicle.getType();
|
||||||
|
vehicleTypeCount.put(type, vehicleTypeCount.get(type) + 1);
|
||||||
|
vehicleTypeWaitTime.put(type, vehicleTypeWaitTime.get(type) + waitTime);
|
||||||
|
|
||||||
|
System.out.printf("[Exit] Vehicle %s completed (type=%s, system_time=%.2fs, wait=%.2fs)%n",
|
||||||
|
vehicle.getId(), vehicle.getType(), systemTime, waitTime);
|
||||||
|
|
||||||
|
if (totalVehiclesReceived % 10 == 0) {
|
||||||
|
sendStatsToDashboard();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtém o tempo atual da simulação em segundos.
|
||||||
|
*
|
||||||
|
* @return Tempo atual em segundos desde "epoch"
|
||||||
|
*
|
||||||
|
* "Epoch" é um ponto de referência temporal Unix (1 de janeiro de 1970).
|
||||||
|
* Este método retorna os segundos decorridos desde esse momento.
|
||||||
|
*/
|
||||||
|
private double getCurrentTime() {
|
||||||
|
return System.currentTimeMillis() / 1000.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Envia as estatísticas para o dashboard.
|
||||||
|
*
|
||||||
|
* Prepara e envia uma mensagem STATS_UPDATE com:
|
||||||
|
* - O total de veículos processados;
|
||||||
|
* - A média dos tempos (sistema, espera, travessia);
|
||||||
|
* - As contagens e médias por cada tipo de veículo.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private void sendStatsToDashboard() {
|
||||||
|
if (dashboardClient == null || !dashboardClient.isConnected()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Map<String, Object> stats = new HashMap<>();
|
||||||
|
stats.put("totalVehicles", totalVehiclesReceived);
|
||||||
|
stats.put("avgSystemTime", totalVehiclesReceived > 0 ? totalSystemTime / totalVehiclesReceived : 0.0);
|
||||||
|
stats.put("avgWaitingTime", totalVehiclesReceived > 0 ? totalWaitingTime / totalVehiclesReceived : 0.0);
|
||||||
|
stats.put("avgCrossingTime", totalVehiclesReceived > 0 ? totalCrossingTime / totalVehiclesReceived : 0.0);
|
||||||
|
|
||||||
|
Map<String, Integer> typeCounts = new HashMap<>();
|
||||||
|
Map<String, Double> typeAvgWait = new HashMap<>();
|
||||||
|
for (VehicleType type : VehicleType.values()) {
|
||||||
|
int count = vehicleTypeCount.get(type);
|
||||||
|
typeCounts.put(type.name(), count);
|
||||||
|
if (count > 0) {
|
||||||
|
typeAvgWait.put(type.name(), vehicleTypeWaitTime.get(type) / count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stats.put("vehicleTypeCounts", typeCounts);
|
||||||
|
stats.put("vehicleTypeAvgWait", typeAvgWait);
|
||||||
|
|
||||||
|
Message message = new Message(MessageType.STATS_UPDATE, "ExitNode", "Dashboard", stats);
|
||||||
|
dashboardClient.send(message);
|
||||||
|
|
||||||
|
System.out.printf("[Exit] Sent stats to dashboard (total=%d, avg_wait=%.2fs)%n",
|
||||||
|
totalVehiclesReceived, totalWaitingTime / totalVehiclesReceived);
|
||||||
|
|
||||||
|
} catch (SerializationException | IOException e) {
|
||||||
|
System.err.println("Failed to send stats to dashboard: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Termina o processo
|
||||||
|
*
|
||||||
|
* Executa a seguinte sequência:
|
||||||
|
* Imprime as estatísticas finais no terminal;
|
||||||
|
* Envia a última atualização de estatísticas ao dashboard;
|
||||||
|
* Fecha o socket;
|
||||||
|
* Aguarda pela finalização das threads;
|
||||||
|
* Fecha a ligação com o dashboard;
|
||||||
|
*/
|
||||||
|
public void shutdown() {
|
||||||
|
System.out.println("\n[Exit] Shutting down...");
|
||||||
|
running = false;
|
||||||
|
|
||||||
|
printFinalStatistics();
|
||||||
|
|
||||||
|
sendStatsToDashboard();
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (serverSocket != null && !serverSocket.isClosed()) {
|
||||||
|
serverSocket.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error closing server socket: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
connectionHandlerPool.shutdown();
|
||||||
|
try {
|
||||||
|
if (!connectionHandlerPool.awaitTermination(5, TimeUnit.SECONDS)) {
|
||||||
|
connectionHandlerPool.shutdownNow();
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
connectionHandlerPool.shutdownNow();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dashboardClient != null) {
|
||||||
|
dashboardClient.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("[Exit] Shutdown complete.");
|
||||||
|
System.out.println("=".repeat(60));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Imprime as estatísticas finais detalhadas no terminal
|
||||||
|
*
|
||||||
|
* Gera um relatório com:
|
||||||
|
* Total de veículos que completaram a rota;
|
||||||
|
* Médias de tempo no sistema, espera e travessia;
|
||||||
|
* Distribuição e médias pelo tipo de veículo (BIKE, LIGHT, HEAVY);
|
||||||
|
*
|
||||||
|
* Este método é chamado durante o shutdown para fornecer um resumo
|
||||||
|
* da simulação antes de terminar o processo.
|
||||||
|
*/
|
||||||
|
private void printFinalStatistics() {
|
||||||
|
System.out.println("\n=== EXIT NODE STATISTICS ===");
|
||||||
|
System.out.printf("Total Vehicles Completed: %d%n", totalVehiclesReceived);
|
||||||
|
|
||||||
|
if (totalVehiclesReceived > 0) {
|
||||||
|
System.out.printf("%nAVERAGE METRICS:%n");
|
||||||
|
System.out.printf(" System Time: %.2f seconds%n", totalSystemTime / totalVehiclesReceived);
|
||||||
|
System.out.printf(" Waiting Time: %.2f seconds%n", totalWaitingTime / totalVehiclesReceived);
|
||||||
|
System.out.printf(" Crossing Time: %.2f seconds%n", totalCrossingTime / totalVehiclesReceived);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nVEHICLE TYPE DISTRIBUTION:");
|
||||||
|
for (VehicleType type : VehicleType.values()) {
|
||||||
|
int count = vehicleTypeCount.get(type);
|
||||||
|
if (count > 0) {
|
||||||
|
double percentage = (count * 100.0) / totalVehiclesReceived;
|
||||||
|
double avgWait = vehicleTypeWaitTime.get(type) / count;
|
||||||
|
System.out.printf(" %s: %d (%.1f%%), Avg Wait: %.2fs%n",
|
||||||
|
type, count, percentage, avgWait);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -8,19 +8,22 @@ import java.util.Map;
|
|||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
import sd.config.SimulationConfig;
|
import sd.config.SimulationConfig;
|
||||||
|
import sd.engine.TrafficLightThread;
|
||||||
import sd.model.Intersection;
|
import sd.model.Intersection;
|
||||||
import sd.model.MessageType;
|
import sd.model.MessageType;
|
||||||
import sd.model.TrafficLight;
|
import sd.model.TrafficLight;
|
||||||
import sd.model.TrafficLightState;
|
|
||||||
import sd.model.Vehicle;
|
import sd.model.Vehicle;
|
||||||
import sd.protocol.MessageProtocol;
|
import sd.protocol.MessageProtocol;
|
||||||
import sd.protocol.SocketConnection;
|
import sd.protocol.SocketConnection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main class for an Intersection Process in the distributed traffic simulation.
|
* Main class for an Intersection Process in the distributed traffic simulation.
|
||||||
* * Each IntersectionProcess runs as an independent Java application (JVM instance)
|
* * Each IntersectionProcess runs as an independent Java application (JVM
|
||||||
|
* instance)
|
||||||
* representing one of the five intersections (Cr1-Cr5) in the network.
|
* representing one of the five intersections (Cr1-Cr5) in the network.
|
||||||
*/
|
*/
|
||||||
public class IntersectionProcess {
|
public class IntersectionProcess {
|
||||||
@@ -39,8 +42,22 @@ public class IntersectionProcess {
|
|||||||
|
|
||||||
private final ExecutorService trafficLightPool;
|
private final ExecutorService trafficLightPool;
|
||||||
|
|
||||||
private volatile boolean running; //Quando uma thread escreve um valor volatile, todas as outras
|
private volatile boolean running; // Quando uma thread escreve um valor volatile, todas as outras
|
||||||
//threads veem a mudança imediatamente.
|
// threads veem a mudança imediatamente.
|
||||||
|
|
||||||
|
// Traffic Light Coordination
|
||||||
|
/**
|
||||||
|
* Lock to ensure mutual exclusion between traffic lights.
|
||||||
|
* Only one traffic light can be green at any given time within this
|
||||||
|
* intersection.
|
||||||
|
*/
|
||||||
|
private final Lock trafficCoordinationLock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tracks which direction currently has the green light.
|
||||||
|
* null means no direction is currently green (all are red).
|
||||||
|
*/
|
||||||
|
private volatile String currentGreenDirection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new IntersectionProcess.
|
* Constructs a new IntersectionProcess.
|
||||||
@@ -57,6 +74,8 @@ public class IntersectionProcess {
|
|||||||
this.connectionHandlerPool = Executors.newCachedThreadPool();
|
this.connectionHandlerPool = Executors.newCachedThreadPool();
|
||||||
this.trafficLightPool = Executors.newFixedThreadPool(4); // Max 4 directions
|
this.trafficLightPool = Executors.newFixedThreadPool(4); // Max 4 directions
|
||||||
this.running = false;
|
this.running = false;
|
||||||
|
this.trafficCoordinationLock = new ReentrantLock();
|
||||||
|
this.currentGreenDirection = null;
|
||||||
|
|
||||||
System.out.println("=".repeat(60));
|
System.out.println("=".repeat(60));
|
||||||
System.out.println("INTERSECTION PROCESS: " + intersectionId);
|
System.out.println("INTERSECTION PROCESS: " + intersectionId);
|
||||||
@@ -70,13 +89,12 @@ public class IntersectionProcess {
|
|||||||
|
|
||||||
configureRouting();
|
configureRouting();
|
||||||
|
|
||||||
startTrafficLights();
|
|
||||||
|
|
||||||
System.out.println("[" + intersectionId + "] Initialization complete.");
|
System.out.println("[" + intersectionId + "] Initialization complete.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates traffic lights for this intersection based on its physical connections.
|
* Creates traffic lights for this intersection based on its physical
|
||||||
|
* connections.
|
||||||
* Each intersection has different number and directions of traffic lights
|
* Each intersection has different number and directions of traffic lights
|
||||||
* according to the network topology.
|
* according to the network topology.
|
||||||
*/
|
*/
|
||||||
@@ -86,19 +104,19 @@ public class IntersectionProcess {
|
|||||||
String[] directions = new String[0];
|
String[] directions = new String[0];
|
||||||
switch (intersectionId) {
|
switch (intersectionId) {
|
||||||
case "Cr1":
|
case "Cr1":
|
||||||
directions = new String[]{"East", "South"};
|
directions = new String[] { "East", "South" };
|
||||||
break;
|
break;
|
||||||
case "Cr2":
|
case "Cr2":
|
||||||
directions = new String[]{"West", "East", "South"};
|
directions = new String[] { "West", "East", "South" };
|
||||||
break;
|
break;
|
||||||
case "Cr3":
|
case "Cr3":
|
||||||
directions = new String[]{"West", "South"};
|
directions = new String[] { "West", "South" };
|
||||||
break;
|
break;
|
||||||
case "Cr4":
|
case "Cr4":
|
||||||
directions = new String[]{"East"};
|
directions = new String[] { "East" };
|
||||||
break;
|
break;
|
||||||
case "Cr5":
|
case "Cr5":
|
||||||
directions = new String[]{"East"};
|
directions = new String[] { "East" };
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,15 +125,14 @@ public class IntersectionProcess {
|
|||||||
double redTime = config.getTrafficLightRedTime(intersectionId, direction);
|
double redTime = config.getTrafficLightRedTime(intersectionId, direction);
|
||||||
|
|
||||||
TrafficLight light = new TrafficLight(
|
TrafficLight light = new TrafficLight(
|
||||||
intersectionId + "-" + direction,
|
intersectionId + "-" + direction,
|
||||||
direction,
|
direction,
|
||||||
greenTime,
|
greenTime,
|
||||||
redTime
|
redTime);
|
||||||
);
|
|
||||||
|
|
||||||
intersection.addTrafficLight(light);
|
intersection.addTrafficLight(light);
|
||||||
System.out.println(" Created traffic light: " + direction +
|
System.out.println(" Created traffic light: " + direction +
|
||||||
" (Green: " + greenTime + "s, Red: " + redTime + "s)");
|
" (Green: " + greenTime + "s, Red: " + redTime + "s)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,6 +171,29 @@ public class IntersectionProcess {
|
|||||||
System.out.println(" Routing configured.");
|
System.out.println(" Routing configured.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Requests permission for a traffic light to turn green.
|
||||||
|
* Blocks until permission is granted (no other light is green).
|
||||||
|
*
|
||||||
|
* @param direction The direction requesting green light
|
||||||
|
*/
|
||||||
|
public void requestGreenLight(String direction) {
|
||||||
|
trafficCoordinationLock.lock();
|
||||||
|
currentGreenDirection = direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Releases the green light permission, allowing another light to turn green.
|
||||||
|
*
|
||||||
|
* @param direction The direction releasing green light
|
||||||
|
*/
|
||||||
|
public void releaseGreenLight(String direction) {
|
||||||
|
if (direction.equals(currentGreenDirection)) {
|
||||||
|
currentGreenDirection = null;
|
||||||
|
trafficCoordinationLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts all traffic light threads.
|
* Starts all traffic light threads.
|
||||||
*/
|
*/
|
||||||
@@ -161,107 +201,21 @@ public class IntersectionProcess {
|
|||||||
System.out.println("\n[" + intersectionId + "] Starting traffic light threads...");
|
System.out.println("\n[" + intersectionId + "] Starting traffic light threads...");
|
||||||
|
|
||||||
for (TrafficLight light : intersection.getTrafficLights()) {
|
for (TrafficLight light : intersection.getTrafficLights()) {
|
||||||
trafficLightPool.submit(() -> runTrafficLightCycle(light));
|
|
||||||
|
TrafficLightThread lightTask = new TrafficLightThread(light, this, config);
|
||||||
|
|
||||||
|
trafficLightPool.submit(lightTask);
|
||||||
|
|
||||||
System.out.println(" Started thread for: " + light.getDirection());
|
System.out.println(" Started thread for: " + light.getDirection());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The main loop for a traffic light thread.
|
|
||||||
* Continuously cycles between GREEN and RED states.
|
|
||||||
*
|
|
||||||
* @param light The traffic light to control.
|
|
||||||
*/
|
|
||||||
private void runTrafficLightCycle(TrafficLight light) {
|
|
||||||
System.out.println("[" + light.getId() + "] Traffic light thread started.");
|
|
||||||
|
|
||||||
while (running) {
|
|
||||||
try {
|
|
||||||
// Green state
|
|
||||||
light.changeState(TrafficLightState.GREEN);
|
|
||||||
System.out.println("[" + light.getId() + "] State: GREEN");
|
|
||||||
|
|
||||||
// Process vehicles while green
|
|
||||||
processGreenLight(light);
|
|
||||||
|
|
||||||
// Wait for green duration
|
|
||||||
Thread.sleep((long) (light.getGreenTime() * 1000));
|
|
||||||
|
|
||||||
// RED state
|
|
||||||
light.changeState(TrafficLightState.RED);
|
|
||||||
System.out.println("[" + light.getId() + "] State: RED");
|
|
||||||
|
|
||||||
// Wait for red duration
|
|
||||||
Thread.sleep((long) (light.getRedTime() * 1000));
|
|
||||||
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
System.out.println("[" + light.getId() + "] Traffic light thread interrupted.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("[" + light.getId() + "] Traffic light thread stopped.");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Processes vehicles when a traffic light is GREEN.
|
|
||||||
* Dequeues vehicles and sends them to their next destination.
|
|
||||||
*
|
|
||||||
* @param light The traffic light that is currently green.
|
|
||||||
*/
|
|
||||||
private void processGreenLight(TrafficLight light) {
|
|
||||||
while (light.getState() == TrafficLightState.GREEN && light.getQueueSize() > 0) {
|
|
||||||
Vehicle vehicle = light.removeVehicle();
|
|
||||||
|
|
||||||
if (vehicle != null) {
|
|
||||||
// Get crossing time based on vehicle type
|
|
||||||
double crossingTime = getCrossingTimeForVehicle(vehicle);
|
|
||||||
|
|
||||||
// Simulate crossing time
|
|
||||||
try {
|
|
||||||
Thread.sleep((long) (crossingTime * 1000));
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update vehicle statistics
|
|
||||||
vehicle.addCrossingTime(crossingTime);
|
|
||||||
|
|
||||||
// Update intersection statistics
|
|
||||||
intersection.incrementVehiclesSent();
|
|
||||||
|
|
||||||
// Send vehicle to next destination
|
|
||||||
sendVehicleToNextDestination(vehicle);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the crossing time for a vehicle based on its type.
|
|
||||||
*
|
|
||||||
* @param vehicle The vehicle.
|
|
||||||
* @return The crossing time in seconds.
|
|
||||||
*/
|
|
||||||
private double getCrossingTimeForVehicle(Vehicle vehicle) {
|
|
||||||
switch (vehicle.getType()) {
|
|
||||||
case BIKE:
|
|
||||||
return config.getBikeVehicleCrossingTime();
|
|
||||||
case LIGHT:
|
|
||||||
return config.getLightVehicleCrossingTime();
|
|
||||||
case HEAVY:
|
|
||||||
return config.getHeavyVehicleCrossingTime();
|
|
||||||
default:
|
|
||||||
return config.getLightVehicleCrossingTime();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a vehicle to its next destination via socket connection.
|
* Sends a vehicle to its next destination via socket connection.
|
||||||
*
|
*
|
||||||
* @param vehicle The vehicle that has crossed this intersection.
|
* @param vehicle The vehicle that has crossed this intersection.
|
||||||
*/
|
*/
|
||||||
private void sendVehicleToNextDestination(Vehicle vehicle) {
|
public void sendVehicleToNextDestination(Vehicle vehicle) {
|
||||||
String nextDestination = vehicle.getCurrentDestination();
|
String nextDestination = vehicle.getCurrentDestination();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -270,21 +224,20 @@ public class IntersectionProcess {
|
|||||||
|
|
||||||
// Create and send message
|
// Create and send message
|
||||||
MessageProtocol message = new VehicleTransferMessage(
|
MessageProtocol message = new VehicleTransferMessage(
|
||||||
intersectionId,
|
intersectionId,
|
||||||
nextDestination,
|
nextDestination,
|
||||||
vehicle
|
vehicle);
|
||||||
);
|
|
||||||
|
|
||||||
connection.sendMessage(message);
|
connection.sendMessage(message);
|
||||||
|
|
||||||
System.out.println("[" + intersectionId + "] Sent vehicle " + vehicle.getId() +
|
System.out.println("[" + intersectionId + "] Sent vehicle " + vehicle.getId() +
|
||||||
" to " + nextDestination);
|
" to " + nextDestination);
|
||||||
|
|
||||||
// Note: vehicle route is advanced when it arrives at the next intersection
|
// Note: vehicle route is advanced when it arrives at the next intersection
|
||||||
|
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
System.err.println("[" + intersectionId + "] Failed to send vehicle " +
|
System.err.println("[" + intersectionId + "] Failed to send vehicle " +
|
||||||
vehicle.getId() + " to " + nextDestination + ": " + e.getMessage());
|
vehicle.getId() + " to " + nextDestination + ": " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,7 +246,7 @@ public class IntersectionProcess {
|
|||||||
*
|
*
|
||||||
* @param destinationId The ID of the destination node.
|
* @param destinationId The ID of the destination node.
|
||||||
* @return The SocketConnection to that destination.
|
* @return The SocketConnection to that destination.
|
||||||
* @throws IOException If connection cannot be established.
|
* @throws IOException If connection cannot be established.
|
||||||
* @throws InterruptedException If connection attempt is interrupted.
|
* @throws InterruptedException If connection attempt is interrupted.
|
||||||
*/
|
*/
|
||||||
private synchronized SocketConnection getOrCreateConnection(String destinationId)
|
private synchronized SocketConnection getOrCreateConnection(String destinationId)
|
||||||
@@ -304,7 +257,7 @@ public class IntersectionProcess {
|
|||||||
int port = getPortForDestination(destinationId);
|
int port = getPortForDestination(destinationId);
|
||||||
|
|
||||||
System.out.println("[" + intersectionId + "] Creating connection to " +
|
System.out.println("[" + intersectionId + "] Creating connection to " +
|
||||||
destinationId + " at " + host + ":" + port);
|
destinationId + " at " + host + ":" + port);
|
||||||
|
|
||||||
SocketConnection connection = new SocketConnection(host, port);
|
SocketConnection connection = new SocketConnection(host, port);
|
||||||
outgoingConnections.put(destinationId, connection);
|
outgoingConnections.put(destinationId, connection);
|
||||||
@@ -353,6 +306,10 @@ public class IntersectionProcess {
|
|||||||
running = true;
|
running = true;
|
||||||
|
|
||||||
System.out.println("\n[" + intersectionId + "] Server started on port " + port);
|
System.out.println("\n[" + intersectionId + "] Server started on port " + port);
|
||||||
|
|
||||||
|
// Start traffic light threads when running is true
|
||||||
|
startTrafficLights();
|
||||||
|
|
||||||
System.out.println("[" + intersectionId + "] Waiting for incoming connections...\n");
|
System.out.println("[" + intersectionId + "] Waiting for incoming connections...\n");
|
||||||
|
|
||||||
// Main accept loop
|
// Main accept loop
|
||||||
@@ -360,14 +317,34 @@ public class IntersectionProcess {
|
|||||||
try {
|
try {
|
||||||
Socket clientSocket = serverSocket.accept();
|
Socket clientSocket = serverSocket.accept();
|
||||||
|
|
||||||
|
System.out.println("[" + intersectionId + "] New connection accepted from " +
|
||||||
|
clientSocket.getInetAddress().getHostAddress());
|
||||||
|
|
||||||
|
// Check running flag again before handling
|
||||||
|
if (!running) {
|
||||||
|
clientSocket.close();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// **Set timeout before submitting to handler**
|
||||||
|
try {
|
||||||
|
clientSocket.setSoTimeout(1000);
|
||||||
|
} catch (java.net.SocketException e) {
|
||||||
|
System.err.println("[" + intersectionId + "] Failed to set timeout: " + e.getMessage());
|
||||||
|
clientSocket.close();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Handle each connection in a separate thread
|
// Handle each connection in a separate thread
|
||||||
connectionHandlerPool.submit(() -> handleIncomingConnection(clientSocket));
|
connectionHandlerPool.submit(() -> handleIncomingConnection(clientSocket));
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (running) {
|
// Expected when serverSocket.close() is called during shutdown
|
||||||
System.err.println("[" + intersectionId + "] Error accepting connection: " +
|
if (!running) {
|
||||||
e.getMessage());
|
break; // Normal shutdown
|
||||||
}
|
}
|
||||||
|
System.err.println("[" + intersectionId + "] Error accepting connection: " +
|
||||||
|
e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -379,10 +356,18 @@ public class IntersectionProcess {
|
|||||||
* @param clientSocket The accepted socket connection.
|
* @param clientSocket The accepted socket connection.
|
||||||
*/
|
*/
|
||||||
private void handleIncomingConnection(Socket clientSocket) {
|
private void handleIncomingConnection(Socket clientSocket) {
|
||||||
|
try {
|
||||||
|
clientSocket.setSoTimeout(1000); // 1 second timeout
|
||||||
|
|
||||||
|
} catch (java.net.SocketException e) {
|
||||||
|
System.err.println("[" + intersectionId + "] Failed to set socket timeout: " + e.getMessage());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try (SocketConnection connection = new SocketConnection(clientSocket)) {
|
try (SocketConnection connection = new SocketConnection(clientSocket)) {
|
||||||
|
|
||||||
System.out.println("[" + intersectionId + "] New connection accepted from " +
|
System.out.println("[" + intersectionId + "] New connection accepted from " +
|
||||||
clientSocket.getInetAddress().getHostAddress());
|
clientSocket.getInetAddress().getHostAddress());
|
||||||
|
|
||||||
// Continuously receive messages while connection is active
|
// Continuously receive messages while connection is active
|
||||||
while (running && connection.isConnected()) {
|
while (running && connection.isConnected()) {
|
||||||
@@ -393,15 +378,22 @@ public class IntersectionProcess {
|
|||||||
Vehicle vehicle = (Vehicle) message.getPayload();
|
Vehicle vehicle = (Vehicle) message.getPayload();
|
||||||
|
|
||||||
System.out.println("[" + intersectionId + "] Received vehicle: " +
|
System.out.println("[" + intersectionId + "] Received vehicle: " +
|
||||||
vehicle.getId() + " from " + message.getSourceNode());
|
vehicle.getId() + " from " + message.getSourceNode());
|
||||||
|
|
||||||
// Add vehicle to appropriate queue
|
// Add vehicle to appropriate queue
|
||||||
intersection.receiveVehicle(vehicle);
|
intersection.receiveVehicle(vehicle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} catch (java.net.SocketTimeoutException e) {
|
||||||
|
// Timeout - check running flag and continue
|
||||||
|
if (!running) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Continue waiting for next message
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
System.err.println("[" + intersectionId + "] Unknown message type received: " +
|
System.err.println("[" + intersectionId + "] Unknown message type received: " +
|
||||||
e.getMessage());
|
e.getMessage());
|
||||||
|
break; // Invalid message, close connection
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -409,6 +401,7 @@ public class IntersectionProcess {
|
|||||||
if (running) {
|
if (running) {
|
||||||
System.err.println("[" + intersectionId + "] Connection error: " + e.getMessage());
|
System.err.println("[" + intersectionId + "] Connection error: " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
// Expected during shutdown
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -417,47 +410,67 @@ public class IntersectionProcess {
|
|||||||
* Shuts down all threads and closes all connections.
|
* Shuts down all threads and closes all connections.
|
||||||
*/
|
*/
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
|
// Check if already shutdown
|
||||||
|
if (!running) {
|
||||||
|
return; // Already shutdown, do nothing
|
||||||
|
}
|
||||||
|
|
||||||
System.out.println("\n[" + intersectionId + "] Shutting down...");
|
System.out.println("\n[" + intersectionId + "] Shutting down...");
|
||||||
running = false;
|
running = false;
|
||||||
|
|
||||||
// Close server socket
|
// 1. Close ServerSocket first
|
||||||
try {
|
if (serverSocket != null && !serverSocket.isClosed()) {
|
||||||
if (serverSocket != null && !serverSocket.isClosed()) {
|
try {
|
||||||
serverSocket.close();
|
serverSocket.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// Expected
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
|
||||||
System.err.println("[" + intersectionId + "] Error closing server socket: " +
|
|
||||||
e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shutdown thread pools
|
// 2. Shutdown thread pools with force
|
||||||
trafficLightPool.shutdown();
|
if (trafficLightPool != null && !trafficLightPool.isShutdown()) {
|
||||||
connectionHandlerPool.shutdown();
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (!trafficLightPool.awaitTermination(5, TimeUnit.SECONDS)) {
|
|
||||||
trafficLightPool.shutdownNow();
|
|
||||||
}
|
|
||||||
if (!connectionHandlerPool.awaitTermination(5, TimeUnit.SECONDS)) {
|
|
||||||
connectionHandlerPool.shutdownNow();
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
trafficLightPool.shutdownNow();
|
trafficLightPool.shutdownNow();
|
||||||
|
}
|
||||||
|
if (connectionHandlerPool != null && !connectionHandlerPool.isShutdown()) {
|
||||||
connectionHandlerPool.shutdownNow();
|
connectionHandlerPool.shutdownNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close all outgoing connections
|
// 3. Wait briefly for termination (don't block forever)
|
||||||
for (Map.Entry<String, SocketConnection> entry : outgoingConnections.entrySet()) {
|
try {
|
||||||
try {
|
if (trafficLightPool != null) {
|
||||||
entry.getValue().close();
|
trafficLightPool.awaitTermination(1, TimeUnit.SECONDS);
|
||||||
} catch (IOException e) {
|
|
||||||
System.err.println("[" + intersectionId + "] Error closing connection to " +
|
|
||||||
entry.getKey() + ": " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
if (connectionHandlerPool != null) {
|
||||||
|
connectionHandlerPool.awaitTermination(1, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Close outgoing connections
|
||||||
|
synchronized (outgoingConnections) {
|
||||||
|
for (SocketConnection conn : outgoingConnections.values()) {
|
||||||
|
try {
|
||||||
|
conn.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
outgoingConnections.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("[" + intersectionId + "] Shutdown complete.");
|
System.out.println("[" + intersectionId + "] Shutdown complete.");
|
||||||
System.out.println("=".repeat(60));
|
System.out.println("============================================================\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the Intersection object managed by this process.
|
||||||
|
* Useful for testing and monitoring.
|
||||||
|
*
|
||||||
|
* @return The Intersection object.
|
||||||
|
*/
|
||||||
|
public Intersection getIntersection() {
|
||||||
|
return intersection;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Inner class for Vehicle Transfer Messages ---
|
// --- Inner class for Vehicle Transfer Messages ---
|
||||||
|
|||||||
110
main/src/main/java/sd/dashboard/DashboardClientHandler.java
Normal file
110
main/src/main/java/sd/dashboard/DashboardClientHandler.java
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
package sd.dashboard;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
import sd.model.MessageType;
|
||||||
|
import sd.protocol.MessageProtocol;
|
||||||
|
import sd.protocol.SocketConnection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Processes statistics messages from a single client connection.
|
||||||
|
* Runs in a separate thread per client.
|
||||||
|
*/
|
||||||
|
public class DashboardClientHandler implements Runnable {
|
||||||
|
|
||||||
|
private final Socket clientSocket;
|
||||||
|
private final DashboardStatistics statistics;
|
||||||
|
|
||||||
|
public DashboardClientHandler(Socket clientSocket, DashboardStatistics statistics) {
|
||||||
|
this.clientSocket = clientSocket;
|
||||||
|
this.statistics = statistics;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
String clientInfo = clientSocket.getInetAddress().getHostAddress() + ":" + clientSocket.getPort();
|
||||||
|
|
||||||
|
try (SocketConnection connection = new SocketConnection(clientSocket)) {
|
||||||
|
System.out.println("[Handler] Started handling client: " + clientInfo);
|
||||||
|
|
||||||
|
while (!Thread.currentThread().isInterrupted()) {
|
||||||
|
try {
|
||||||
|
MessageProtocol message = connection.receiveMessage();
|
||||||
|
|
||||||
|
if (message == null) {
|
||||||
|
System.out.println("[Handler] Client disconnected: " + clientInfo);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
processMessage(message);
|
||||||
|
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
System.err.println("[Handler] Unknown message class from " + clientInfo + ": " + e.getMessage());
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("[Handler] Connection error with " + clientInfo + ": " + e.getMessage());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("[Handler] Error initializing connection with " + clientInfo + ": " + e.getMessage());
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (!clientSocket.isClosed()) {
|
||||||
|
clientSocket.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("[Handler] Error closing socket for " + clientInfo + ": " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processMessage(MessageProtocol message) {
|
||||||
|
if (message.getType() != MessageType.STATS_UPDATE) {
|
||||||
|
System.out.println("[Handler] Ignoring non-statistics message type: " + message.getType());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String senderId = message.getSourceNode();
|
||||||
|
Object payload = message.getPayload();
|
||||||
|
|
||||||
|
System.out.println("[Handler] Received STATS_UPDATE from: " + senderId);
|
||||||
|
|
||||||
|
if (payload instanceof StatsUpdatePayload stats) {
|
||||||
|
updateStatistics(senderId, stats);
|
||||||
|
} else {
|
||||||
|
System.err.println("[Handler] Unknown payload type: " +
|
||||||
|
(payload != null ? payload.getClass().getName() : "null"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateStatistics(String senderId, StatsUpdatePayload stats) {
|
||||||
|
if (stats.getTotalVehiclesGenerated() >= 0) {
|
||||||
|
statistics.updateVehiclesGenerated(stats.getTotalVehiclesGenerated());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stats.getTotalVehiclesCompleted() >= 0) {
|
||||||
|
statistics.updateVehiclesCompleted(stats.getTotalVehiclesCompleted());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stats.getTotalSystemTime() >= 0) {
|
||||||
|
statistics.addSystemTime(stats.getTotalSystemTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stats.getTotalWaitingTime() >= 0) {
|
||||||
|
statistics.addWaitingTime(stats.getTotalWaitingTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (senderId.startsWith("Cr") || senderId.startsWith("E")) {
|
||||||
|
statistics.updateIntersectionStats(
|
||||||
|
senderId,
|
||||||
|
stats.getIntersectionArrivals(),
|
||||||
|
stats.getIntersectionDepartures(),
|
||||||
|
stats.getIntersectionQueueSize()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("[Handler] Successfully updated statistics from: " + senderId);
|
||||||
|
}
|
||||||
|
}
|
||||||
148
main/src/main/java/sd/dashboard/DashboardServer.java
Normal file
148
main/src/main/java/sd/dashboard/DashboardServer.java
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
package sd.dashboard;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
import sd.config.SimulationConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Aggregates and displays real-time statistics from all simulation processes.
|
||||||
|
* Uses a thread pool to handle concurrent client connections.
|
||||||
|
*/
|
||||||
|
public class DashboardServer {
|
||||||
|
|
||||||
|
private final int port;
|
||||||
|
private final DashboardStatistics statistics;
|
||||||
|
private final ExecutorService clientHandlerPool;
|
||||||
|
private final AtomicBoolean running;
|
||||||
|
private ServerSocket serverSocket;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("=".repeat(60));
|
||||||
|
System.out.println("DASHBOARD SERVER - DISTRIBUTED TRAFFIC SIMULATION");
|
||||||
|
System.out.println("=".repeat(60));
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Load configuration
|
||||||
|
String configFile = args.length > 0 ? args[0] : "src/main/resources/simulation.properties";
|
||||||
|
System.out.println("Loading configuration from: " + configFile);
|
||||||
|
|
||||||
|
SimulationConfig config = new SimulationConfig(configFile);
|
||||||
|
DashboardServer server = new DashboardServer(config);
|
||||||
|
|
||||||
|
// Start the server
|
||||||
|
System.out.println("\n" + "=".repeat(60));
|
||||||
|
server.start();
|
||||||
|
|
||||||
|
// Keep running until interrupted
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||||
|
System.out.println("\n\nShutdown signal received...");
|
||||||
|
server.stop();
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Display statistics periodically
|
||||||
|
server.displayLoop();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Failed to start Dashboard Server: " + e.getMessage());
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public DashboardServer(SimulationConfig config) {
|
||||||
|
this.port = config.getDashboardPort();
|
||||||
|
this.statistics = new DashboardStatistics();
|
||||||
|
this.clientHandlerPool = Executors.newFixedThreadPool(10);
|
||||||
|
this.running = new AtomicBoolean(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() throws IOException {
|
||||||
|
if (running.get()) {
|
||||||
|
System.out.println("Dashboard Server is already running.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
serverSocket = new ServerSocket(port);
|
||||||
|
running.set(true);
|
||||||
|
|
||||||
|
System.out.println("Dashboard Server started on port " + port);
|
||||||
|
System.out.println("Waiting for statistics updates from simulation processes...");
|
||||||
|
System.out.println("=".repeat(60));
|
||||||
|
|
||||||
|
Thread acceptThread = new Thread(this::acceptConnections, "DashboardServer-Accept");
|
||||||
|
acceptThread.setDaemon(false);
|
||||||
|
acceptThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void acceptConnections() {
|
||||||
|
while (running.get()) {
|
||||||
|
try {
|
||||||
|
Socket clientSocket = serverSocket.accept();
|
||||||
|
System.out.println("[Connection] New client connected: " +
|
||||||
|
clientSocket.getInetAddress().getHostAddress() + ":" + clientSocket.getPort());
|
||||||
|
|
||||||
|
clientHandlerPool.execute(new DashboardClientHandler(clientSocket, statistics));
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
if (running.get()) {
|
||||||
|
System.err.println("[Error] Failed to accept client connection: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("BusyWait")
|
||||||
|
private void displayLoop() {
|
||||||
|
final long DISPLAY_INTERVAL_MS = 5000;
|
||||||
|
|
||||||
|
while (running.get()) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(DISPLAY_INTERVAL_MS);
|
||||||
|
displayStatistics();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void displayStatistics() {
|
||||||
|
System.out.println("\n" + "=".repeat(60));
|
||||||
|
System.out.println("REAL-TIME SIMULATION STATISTICS");
|
||||||
|
System.out.println("=".repeat(60));
|
||||||
|
statistics.display();
|
||||||
|
System.out.println("=".repeat(60));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
if (!running.get()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nStopping Dashboard Server...");
|
||||||
|
running.set(false);
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (serverSocket != null && !serverSocket.isClosed()) {
|
||||||
|
serverSocket.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error closing server socket: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
clientHandlerPool.shutdownNow();
|
||||||
|
System.out.println("Dashboard Server stopped.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public DashboardStatistics getStatistics() {
|
||||||
|
return statistics;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isRunning() {
|
||||||
|
return running.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
214
main/src/main/java/sd/dashboard/DashboardStatistics.java
Normal file
214
main/src/main/java/sd/dashboard/DashboardStatistics.java
Normal file
@@ -0,0 +1,214 @@
|
|||||||
|
package sd.dashboard;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
|
import sd.model.VehicleType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thread-safe storage for aggregated simulation statistics.
|
||||||
|
* Uses atomic types and concurrent collections for lock-free updates.
|
||||||
|
*/
|
||||||
|
public class DashboardStatistics {
|
||||||
|
|
||||||
|
private final AtomicInteger totalVehiclesGenerated;
|
||||||
|
private final AtomicInteger totalVehiclesCompleted;
|
||||||
|
private final AtomicLong totalSystemTime;
|
||||||
|
private final AtomicLong totalWaitingTime;
|
||||||
|
|
||||||
|
private final Map<String, IntersectionStats> intersectionStats;
|
||||||
|
private final Map<VehicleType, AtomicInteger> vehicleTypeCount;
|
||||||
|
private final Map<VehicleType, AtomicLong> vehicleTypeWaitTime;
|
||||||
|
|
||||||
|
private volatile long lastUpdateTime;
|
||||||
|
|
||||||
|
public DashboardStatistics() {
|
||||||
|
this.totalVehiclesGenerated = new AtomicInteger(0);
|
||||||
|
this.totalVehiclesCompleted = new AtomicInteger(0);
|
||||||
|
this.totalSystemTime = new AtomicLong(0);
|
||||||
|
this.totalWaitingTime = new AtomicLong(0);
|
||||||
|
|
||||||
|
this.intersectionStats = new ConcurrentHashMap<>();
|
||||||
|
this.vehicleTypeCount = new ConcurrentHashMap<>();
|
||||||
|
this.vehicleTypeWaitTime = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
for (VehicleType type : VehicleType.values()) {
|
||||||
|
vehicleTypeCount.put(type, new AtomicInteger(0));
|
||||||
|
vehicleTypeWaitTime.put(type, new AtomicLong(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.lastUpdateTime = System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateVehiclesGenerated(int count) {
|
||||||
|
totalVehiclesGenerated.set(count);
|
||||||
|
updateTimestamp();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void incrementVehiclesGenerated() {
|
||||||
|
totalVehiclesGenerated.incrementAndGet();
|
||||||
|
updateTimestamp();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateVehiclesCompleted(int count) {
|
||||||
|
totalVehiclesCompleted.set(count);
|
||||||
|
updateTimestamp();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void incrementVehiclesCompleted() {
|
||||||
|
totalVehiclesCompleted.incrementAndGet();
|
||||||
|
updateTimestamp();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addSystemTime(long timeMs) {
|
||||||
|
totalSystemTime.addAndGet(timeMs);
|
||||||
|
updateTimestamp();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addWaitingTime(long timeMs) {
|
||||||
|
totalWaitingTime.addAndGet(timeMs);
|
||||||
|
updateTimestamp();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateVehicleTypeStats(VehicleType type, int count, long waitTimeMs) {
|
||||||
|
vehicleTypeCount.get(type).set(count);
|
||||||
|
vehicleTypeWaitTime.get(type).set(waitTimeMs);
|
||||||
|
updateTimestamp();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void incrementVehicleType(VehicleType type) {
|
||||||
|
vehicleTypeCount.get(type).incrementAndGet();
|
||||||
|
updateTimestamp();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateIntersectionStats(String intersectionId, int arrivals,
|
||||||
|
int departures, int currentQueueSize) {
|
||||||
|
intersectionStats.compute(intersectionId, (id, stats) -> {
|
||||||
|
if (stats == null) {
|
||||||
|
stats = new IntersectionStats(intersectionId);
|
||||||
|
}
|
||||||
|
stats.updateStats(arrivals, departures, currentQueueSize);
|
||||||
|
return stats;
|
||||||
|
});
|
||||||
|
updateTimestamp();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateTimestamp() {
|
||||||
|
lastUpdateTime = System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTotalVehiclesGenerated() {
|
||||||
|
return totalVehiclesGenerated.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTotalVehiclesCompleted() {
|
||||||
|
return totalVehiclesCompleted.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getAverageSystemTime() {
|
||||||
|
int completed = totalVehiclesCompleted.get();
|
||||||
|
if (completed == 0) return 0.0;
|
||||||
|
return (double) totalSystemTime.get() / completed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getAverageWaitingTime() {
|
||||||
|
int completed = totalVehiclesCompleted.get();
|
||||||
|
if (completed == 0) return 0.0;
|
||||||
|
return (double) totalWaitingTime.get() / completed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getVehicleTypeCount(VehicleType type) {
|
||||||
|
return vehicleTypeCount.get(type).get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getAverageWaitingTimeByType(VehicleType type) {
|
||||||
|
int count = vehicleTypeCount.get(type).get();
|
||||||
|
if (count == 0) return 0.0;
|
||||||
|
return (double) vehicleTypeWaitTime.get(type).get() / count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IntersectionStats getIntersectionStats(String intersectionId) {
|
||||||
|
return intersectionStats.get(intersectionId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, IntersectionStats> getAllIntersectionStats() {
|
||||||
|
return new HashMap<>(intersectionStats);
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getLastUpdateTime() {
|
||||||
|
return lastUpdateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void display() {
|
||||||
|
System.out.println("\n--- GLOBAL STATISTICS ---");
|
||||||
|
System.out.printf("Total Vehicles Generated: %d%n", getTotalVehiclesGenerated());
|
||||||
|
System.out.printf("Total Vehicles Completed: %d%n", getTotalVehiclesCompleted());
|
||||||
|
System.out.printf("Vehicles In Transit: %d%n",
|
||||||
|
getTotalVehiclesGenerated() - getTotalVehiclesCompleted());
|
||||||
|
System.out.printf("Average System Time: %.2f ms%n", getAverageSystemTime());
|
||||||
|
System.out.printf("Average Waiting Time: %.2f ms%n", getAverageWaitingTime());
|
||||||
|
|
||||||
|
System.out.println("\n--- VEHICLE TYPE STATISTICS ---");
|
||||||
|
for (VehicleType type : VehicleType.values()) {
|
||||||
|
int count = getVehicleTypeCount(type);
|
||||||
|
double avgWait = getAverageWaitingTimeByType(type);
|
||||||
|
System.out.printf("%s: %d vehicles, avg wait: %.2f ms%n",
|
||||||
|
type, count, avgWait);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\n--- INTERSECTION STATISTICS ---");
|
||||||
|
if (intersectionStats.isEmpty()) {
|
||||||
|
System.out.println("(No data received yet)");
|
||||||
|
} else {
|
||||||
|
for (IntersectionStats stats : intersectionStats.values()) {
|
||||||
|
stats.display();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.printf("%nLast Update: %tT%n", lastUpdateTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class IntersectionStats {
|
||||||
|
private final String intersectionId;
|
||||||
|
private final AtomicInteger totalArrivals;
|
||||||
|
private final AtomicInteger totalDepartures;
|
||||||
|
private final AtomicInteger currentQueueSize;
|
||||||
|
|
||||||
|
public IntersectionStats(String intersectionId) {
|
||||||
|
this.intersectionId = intersectionId;
|
||||||
|
this.totalArrivals = new AtomicInteger(0);
|
||||||
|
this.totalDepartures = new AtomicInteger(0);
|
||||||
|
this.currentQueueSize = new AtomicInteger(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateStats(int arrivals, int departures, int queueSize) {
|
||||||
|
this.totalArrivals.set(arrivals);
|
||||||
|
this.totalDepartures.set(departures);
|
||||||
|
this.currentQueueSize.set(queueSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIntersectionId() {
|
||||||
|
return intersectionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTotalArrivals() {
|
||||||
|
return totalArrivals.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTotalDepartures() {
|
||||||
|
return totalDepartures.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCurrentQueueSize() {
|
||||||
|
return currentQueueSize.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void display() {
|
||||||
|
System.out.printf("%s: Arrivals=%d, Departures=%d, Queue=%d%n",
|
||||||
|
intersectionId, getTotalArrivals(), getTotalDepartures(), getCurrentQueueSize());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
48
main/src/main/java/sd/dashboard/StatsMessage.java
Normal file
48
main/src/main/java/sd/dashboard/StatsMessage.java
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package sd.dashboard;
|
||||||
|
|
||||||
|
import sd.model.MessageType;
|
||||||
|
import sd.protocol.MessageProtocol;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Message wrapper for sending statistics to the dashboard.
|
||||||
|
*/
|
||||||
|
public class StatsMessage implements MessageProtocol {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private final String sourceNode;
|
||||||
|
private final String destinationNode;
|
||||||
|
private final StatsUpdatePayload payload;
|
||||||
|
|
||||||
|
public StatsMessage(String sourceNode, StatsUpdatePayload payload) {
|
||||||
|
this.sourceNode = sourceNode;
|
||||||
|
this.destinationNode = "DashboardServer";
|
||||||
|
this.payload = payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MessageType getType() {
|
||||||
|
return MessageType.STATS_UPDATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getPayload() {
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSourceNode() {
|
||||||
|
return sourceNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDestinationNode() {
|
||||||
|
return destinationNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("StatsMessage[from=%s, to=%s, payload=%s]",
|
||||||
|
sourceNode, destinationNode, payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
121
main/src/main/java/sd/dashboard/StatsUpdatePayload.java
Normal file
121
main/src/main/java/sd/dashboard/StatsUpdatePayload.java
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
package sd.dashboard;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import sd.model.VehicleType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data transfer object for statistics updates to the dashboard.
|
||||||
|
* Use -1 for fields not being updated in this message.
|
||||||
|
*/
|
||||||
|
public class StatsUpdatePayload implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private int totalVehiclesGenerated = -1;
|
||||||
|
private int totalVehiclesCompleted = -1;
|
||||||
|
private long totalSystemTime = -1;
|
||||||
|
private long totalWaitingTime = -1;
|
||||||
|
|
||||||
|
private int intersectionArrivals = 0;
|
||||||
|
private int intersectionDepartures = 0;
|
||||||
|
private int intersectionQueueSize = 0;
|
||||||
|
|
||||||
|
private Map<VehicleType, Integer> vehicleTypeCounts;
|
||||||
|
private Map<VehicleType, Long> vehicleTypeWaitTimes;
|
||||||
|
|
||||||
|
public StatsUpdatePayload() {
|
||||||
|
this.vehicleTypeCounts = new HashMap<>();
|
||||||
|
this.vehicleTypeWaitTimes = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTotalVehiclesGenerated() {
|
||||||
|
return totalVehiclesGenerated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTotalVehiclesCompleted() {
|
||||||
|
return totalVehiclesCompleted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getTotalSystemTime() {
|
||||||
|
return totalSystemTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getTotalWaitingTime() {
|
||||||
|
return totalWaitingTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIntersectionArrivals() {
|
||||||
|
return intersectionArrivals;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIntersectionDepartures() {
|
||||||
|
return intersectionDepartures;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIntersectionQueueSize() {
|
||||||
|
return intersectionQueueSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<VehicleType, Integer> getVehicleTypeCounts() {
|
||||||
|
return vehicleTypeCounts;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<VehicleType, Long> getVehicleTypeWaitTimes() {
|
||||||
|
return vehicleTypeWaitTimes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StatsUpdatePayload setTotalVehiclesGenerated(int totalVehiclesGenerated) {
|
||||||
|
this.totalVehiclesGenerated = totalVehiclesGenerated;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StatsUpdatePayload setTotalVehiclesCompleted(int totalVehiclesCompleted) {
|
||||||
|
this.totalVehiclesCompleted = totalVehiclesCompleted;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StatsUpdatePayload setTotalSystemTime(long totalSystemTime) {
|
||||||
|
this.totalSystemTime = totalSystemTime;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StatsUpdatePayload setTotalWaitingTime(long totalWaitingTime) {
|
||||||
|
this.totalWaitingTime = totalWaitingTime;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StatsUpdatePayload setIntersectionArrivals(int intersectionArrivals) {
|
||||||
|
this.intersectionArrivals = intersectionArrivals;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StatsUpdatePayload setIntersectionDepartures(int intersectionDepartures) {
|
||||||
|
this.intersectionDepartures = intersectionDepartures;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StatsUpdatePayload setIntersectionQueueSize(int intersectionQueueSize) {
|
||||||
|
this.intersectionQueueSize = intersectionQueueSize;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StatsUpdatePayload setVehicleTypeCounts(Map<VehicleType, Integer> vehicleTypeCounts) {
|
||||||
|
this.vehicleTypeCounts = vehicleTypeCounts;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StatsUpdatePayload setVehicleTypeWaitTimes(Map<VehicleType, Long> vehicleTypeWaitTimes) {
|
||||||
|
this.vehicleTypeWaitTimes = vehicleTypeWaitTimes;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("StatsUpdatePayload[generated=%d, completed=%d, arrivals=%d, departures=%d, queueSize=%d]",
|
||||||
|
totalVehiclesGenerated, totalVehiclesCompleted, intersectionArrivals,
|
||||||
|
intersectionDepartures, intersectionQueueSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
115
main/src/main/java/sd/engine/TrafficLightThread.java
Normal file
115
main/src/main/java/sd/engine/TrafficLightThread.java
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
package sd.engine;
|
||||||
|
|
||||||
|
import sd.IntersectionProcess;
|
||||||
|
import sd.config.SimulationConfig;
|
||||||
|
import sd.model.TrafficLight;
|
||||||
|
import sd.model.TrafficLightState;
|
||||||
|
import sd.model.Vehicle;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements the control logic for a single TrafficLight
|
||||||
|
* as a Runnable task that runs in its own Thread.
|
||||||
|
*/
|
||||||
|
public class TrafficLightThread implements Runnable {
|
||||||
|
|
||||||
|
private final TrafficLight light;
|
||||||
|
private final IntersectionProcess process;
|
||||||
|
private final SimulationConfig config;
|
||||||
|
private volatile boolean running;
|
||||||
|
|
||||||
|
// Store the thread reference for proper interruption
|
||||||
|
private Thread currentThread;
|
||||||
|
|
||||||
|
public TrafficLightThread(TrafficLight light, IntersectionProcess process, SimulationConfig config) {
|
||||||
|
this.light = light;
|
||||||
|
this.process = process;
|
||||||
|
this.config = config;
|
||||||
|
this.running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
this.currentThread = Thread.currentThread();
|
||||||
|
this.running = true;
|
||||||
|
System.out.println("[" + light.getId() + "] Traffic light thread started.");
|
||||||
|
|
||||||
|
try {
|
||||||
|
while (running && !Thread.currentThread().isInterrupted()) {
|
||||||
|
|
||||||
|
// Request permission to turn green (blocks until granted)
|
||||||
|
process.requestGreenLight(light.getDirection());
|
||||||
|
|
||||||
|
try {
|
||||||
|
// --- GREEN Phase ---
|
||||||
|
light.changeState(TrafficLightState.GREEN);
|
||||||
|
System.out.println("[" + light.getId() + "] State: GREEN");
|
||||||
|
|
||||||
|
processGreenLightQueue();
|
||||||
|
|
||||||
|
if (!running || Thread.currentThread().isInterrupted()) break;
|
||||||
|
|
||||||
|
// Wait for green duration
|
||||||
|
Thread.sleep((long) (light.getGreenTime() * 1000));
|
||||||
|
|
||||||
|
if (!running || Thread.currentThread().isInterrupted()) break;
|
||||||
|
|
||||||
|
// --- RED Phase ---
|
||||||
|
light.changeState(TrafficLightState.RED);
|
||||||
|
System.out.println("[" + light.getId() + "] State: RED");
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
// Always release the green light permission
|
||||||
|
process.releaseGreenLight(light.getDirection());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for red duration
|
||||||
|
Thread.sleep((long) (light.getRedTime() * 1000));
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
System.out.println("[" + light.getId() + "] Traffic light thread interrupted.");
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
} finally {
|
||||||
|
this.running = false;
|
||||||
|
System.out.println("[" + light.getId() + "] Traffic light thread stopped.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processGreenLightQueue() throws InterruptedException {
|
||||||
|
while (running && !Thread.currentThread().isInterrupted()
|
||||||
|
&& light.getState() == TrafficLightState.GREEN
|
||||||
|
&& light.getQueueSize() > 0) {
|
||||||
|
|
||||||
|
Vehicle vehicle = light.removeVehicle();
|
||||||
|
|
||||||
|
if (vehicle != null) {
|
||||||
|
double crossingTime = getCrossingTimeForVehicle(vehicle);
|
||||||
|
|
||||||
|
Thread.sleep((long) (crossingTime * 1000));
|
||||||
|
|
||||||
|
vehicle.addCrossingTime(crossingTime);
|
||||||
|
process.getIntersection().incrementVehiclesSent();
|
||||||
|
process.sendVehicleToNextDestination(vehicle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private double getCrossingTimeForVehicle(Vehicle vehicle) {
|
||||||
|
return switch (vehicle.getType()) {
|
||||||
|
case BIKE -> config.getBikeVehicleCrossingTime();
|
||||||
|
case LIGHT -> config.getLightVehicleCrossingTime();
|
||||||
|
case HEAVY -> config.getHeavyVehicleCrossingTime();
|
||||||
|
default -> config.getLightVehicleCrossingTime();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Requests the thread to stop gracefully.
|
||||||
|
* Sets the running flag and interrupts the thread to unblock any sleep() calls.
|
||||||
|
*/
|
||||||
|
public void shutdown() {
|
||||||
|
this.running = false;
|
||||||
|
if (currentThread != null && currentThread.isAlive()) {
|
||||||
|
currentThread.interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectOutputStream;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@@ -19,6 +19,7 @@ import sd.IntersectionProcess;
|
|||||||
import sd.model.MessageType;
|
import sd.model.MessageType;
|
||||||
import sd.model.Vehicle;
|
import sd.model.Vehicle;
|
||||||
import sd.model.VehicleType;
|
import sd.model.VehicleType;
|
||||||
|
import sd.protocol.SocketConnection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for IntersectionProcess - covers initialization, traffic lights,
|
* Tests for IntersectionProcess - covers initialization, traffic lights,
|
||||||
@@ -96,11 +97,17 @@ public class IntersectionProcessTest {
|
|||||||
Files.writeString(configFile, configContent);
|
Files.writeString(configFile, configContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
// cleanup after tests
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
public void tearDown() {
|
public void tearDown() {
|
||||||
if (intersectionProcess != null) {
|
if (intersectionProcess != null) {
|
||||||
intersectionProcess.shutdown();
|
try {
|
||||||
|
// Only shutdown if still running
|
||||||
|
intersectionProcess.shutdown();
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Error in tearDown: " + e.getMessage());
|
||||||
|
} finally {
|
||||||
|
intersectionProcess = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,7 +194,7 @@ public class IntersectionProcessTest {
|
|||||||
intersectionProcess = new IntersectionProcess("Cr1", configFile.toString());
|
intersectionProcess = new IntersectionProcess("Cr1", configFile.toString());
|
||||||
intersectionProcess.initialize();
|
intersectionProcess.initialize();
|
||||||
|
|
||||||
// start server in seperate thread
|
// start server in separate thread
|
||||||
Thread serverThread = new Thread(() -> {
|
Thread serverThread = new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
intersectionProcess.start();
|
intersectionProcess.start();
|
||||||
@@ -197,13 +204,22 @@ public class IntersectionProcessTest {
|
|||||||
});
|
});
|
||||||
serverThread.start();
|
serverThread.start();
|
||||||
|
|
||||||
Thread.sleep(500); // wait for server to start
|
// Wait for server to actually start with retries
|
||||||
|
boolean serverReady = false;
|
||||||
// try connecting to check if its running
|
for (int i = 0; i < 20; i++) {
|
||||||
try (Socket clientSocket = new Socket("localhost", 18001)) {
|
Thread.sleep(100);
|
||||||
assertTrue(clientSocket.isConnected());
|
try (Socket testSocket = new Socket()) {
|
||||||
|
testSocket.connect(new java.net.InetSocketAddress("localhost", 18001), 500);
|
||||||
|
serverReady = true;
|
||||||
|
break;
|
||||||
|
} catch (IOException e) {
|
||||||
|
// Server not ready yet, continue waiting
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assertTrue(serverReady, "Server should start and bind to port 18001");
|
||||||
|
|
||||||
|
// Shutdown immediately after confirming server is running
|
||||||
intersectionProcess.shutdown();
|
intersectionProcess.shutdown();
|
||||||
serverThread.join(2000);
|
serverThread.join(2000);
|
||||||
}
|
}
|
||||||
@@ -219,11 +235,17 @@ public class IntersectionProcessTest {
|
|||||||
cr2.initialize();
|
cr2.initialize();
|
||||||
|
|
||||||
Thread thread1 = new Thread(() -> {
|
Thread thread1 = new Thread(() -> {
|
||||||
try { cr1.start(); } catch (IOException e) { }
|
try {
|
||||||
|
cr1.start();
|
||||||
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Thread thread2 = new Thread(() -> {
|
Thread thread2 = new Thread(() -> {
|
||||||
try { cr2.start(); } catch (IOException e) { }
|
try {
|
||||||
|
cr2.start();
|
||||||
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
thread1.start();
|
thread1.start();
|
||||||
@@ -233,7 +255,7 @@ public class IntersectionProcessTest {
|
|||||||
|
|
||||||
// check both are running
|
// check both are running
|
||||||
try (Socket socket1 = new Socket("localhost", 18001);
|
try (Socket socket1 = new Socket("localhost", 18001);
|
||||||
Socket socket2 = new Socket("localhost", 18002)) {
|
Socket socket2 = new Socket("localhost", 18002)) {
|
||||||
assertTrue(socket1.isConnected());
|
assertTrue(socket1.isConnected());
|
||||||
assertTrue(socket2.isConnected());
|
assertTrue(socket2.isConnected());
|
||||||
}
|
}
|
||||||
@@ -256,29 +278,31 @@ public class IntersectionProcessTest {
|
|||||||
Thread serverThread = new Thread(() -> {
|
Thread serverThread = new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
intersectionProcess.start();
|
intersectionProcess.start();
|
||||||
} catch (IOException e) { }
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
});
|
});
|
||||||
serverThread.start();
|
serverThread.start();
|
||||||
|
|
||||||
Thread.sleep(500);
|
Thread.sleep(500);
|
||||||
|
|
||||||
// create test vehicle
|
try {
|
||||||
java.util.List<String> route = Arrays.asList("Cr2", "Cr3", "S");
|
// create test vehicle - FIXED: use 4-parameter constructor
|
||||||
Vehicle vehicle = new Vehicle("V001", VehicleType.LIGHT, 0.0, route);
|
java.util.List<String> route = Arrays.asList("Cr2", "Cr3", "S");
|
||||||
|
Vehicle vehicle = new Vehicle("V001", VehicleType.LIGHT, 0.0, route);
|
||||||
|
|
||||||
// send vehicle from Cr1 to Cr2
|
// send vehicle from Cr1 to Cr2 - FIXED: use SocketConnection
|
||||||
try (Socket socket = new Socket("localhost", 18002)) {
|
try (Socket socket = new Socket("localhost", 18002);
|
||||||
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
|
SocketConnection conn = new SocketConnection(socket)) {
|
||||||
|
|
||||||
TestVehicleMessage message = new TestVehicleMessage("Cr1", "Cr2", vehicle);
|
TestVehicleMessage message = new TestVehicleMessage("Cr1", "Cr2", vehicle);
|
||||||
out.writeObject(message);
|
conn.sendMessage(message);
|
||||||
out.flush();
|
|
||||||
|
|
||||||
Thread.sleep(1000); // wait for procesing
|
Thread.sleep(1000); // wait for processing
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
intersectionProcess.shutdown();
|
||||||
|
serverThread.join(2000);
|
||||||
}
|
}
|
||||||
|
|
||||||
intersectionProcess.shutdown();
|
|
||||||
serverThread.join(2000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// routing config tests
|
// routing config tests
|
||||||
@@ -312,7 +336,8 @@ public class IntersectionProcessTest {
|
|||||||
Thread serverThread = new Thread(() -> {
|
Thread serverThread = new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
intersectionProcess.start();
|
intersectionProcess.start();
|
||||||
} catch (IOException e) { }
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
});
|
});
|
||||||
serverThread.start();
|
serverThread.start();
|
||||||
|
|
||||||
@@ -330,30 +355,35 @@ public class IntersectionProcessTest {
|
|||||||
intersectionProcess = new IntersectionProcess("Cr1", configFile.toString());
|
intersectionProcess = new IntersectionProcess("Cr1", configFile.toString());
|
||||||
intersectionProcess.initialize();
|
intersectionProcess.initialize();
|
||||||
|
|
||||||
|
// Start server in separate thread
|
||||||
Thread serverThread = new Thread(() -> {
|
Thread serverThread = new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
intersectionProcess.start();
|
intersectionProcess.start();
|
||||||
} catch (IOException e) { }
|
} catch (IOException e) {
|
||||||
|
// Expected on shutdown
|
||||||
|
}
|
||||||
});
|
});
|
||||||
serverThread.start();
|
serverThread.start();
|
||||||
|
|
||||||
|
// Wait for server to start
|
||||||
Thread.sleep(500);
|
Thread.sleep(500);
|
||||||
|
|
||||||
// verify server running
|
// Shutdown
|
||||||
try (Socket socket = new Socket("localhost", 18001)) {
|
|
||||||
assertTrue(socket.isConnected());
|
|
||||||
}
|
|
||||||
|
|
||||||
intersectionProcess.shutdown();
|
intersectionProcess.shutdown();
|
||||||
serverThread.join(2000);
|
serverThread.join(2000);
|
||||||
|
|
||||||
// after shutdown conection should fail
|
// Give shutdown time to complete
|
||||||
Thread.sleep(500);
|
Thread.sleep(200);
|
||||||
Exception exception = assertThrows(IOException.class, () -> {
|
|
||||||
Socket socket = new Socket("localhost", 18001);
|
// Verify we cannot connect (server socket is closed)
|
||||||
socket.close();
|
boolean connectionFailed = false;
|
||||||
});
|
try (Socket testSocket = new Socket()) {
|
||||||
assertNotNull(exception);
|
testSocket.connect(new InetSocketAddress("localhost", 18001), 500);
|
||||||
|
} catch (IOException e) {
|
||||||
|
connectionFailed = true; // Expected - server should be closed
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(connectionFailed, "Server socket should be closed after shutdown");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -365,7 +395,8 @@ public class IntersectionProcessTest {
|
|||||||
Thread serverThread = new Thread(() -> {
|
Thread serverThread = new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
intersectionProcess.start();
|
intersectionProcess.start();
|
||||||
} catch (IOException e) { }
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
});
|
});
|
||||||
serverThread.start();
|
serverThread.start();
|
||||||
|
|
||||||
@@ -388,45 +419,68 @@ public class IntersectionProcessTest {
|
|||||||
@Test
|
@Test
|
||||||
@Timeout(15)
|
@Timeout(15)
|
||||||
public void testIntegration_TwoIntersectionsVehicleTransfer() throws IOException, InterruptedException {
|
public void testIntegration_TwoIntersectionsVehicleTransfer() throws IOException, InterruptedException {
|
||||||
// setup 2 intersections
|
IntersectionProcess cr1 = null;
|
||||||
IntersectionProcess cr1 = new IntersectionProcess("Cr1", configFile.toString());
|
IntersectionProcess cr2 = null;
|
||||||
IntersectionProcess cr2 = new IntersectionProcess("Cr2", configFile.toString());
|
Thread thread1 = null;
|
||||||
|
Thread thread2 = null;
|
||||||
|
|
||||||
cr1.initialize();
|
try {
|
||||||
cr2.initialize();
|
// setup 2 intersections
|
||||||
|
cr1 = new IntersectionProcess("Cr1", configFile.toString());
|
||||||
|
cr2 = new IntersectionProcess("Cr2", configFile.toString());
|
||||||
|
|
||||||
// start both
|
cr1.initialize();
|
||||||
Thread thread1 = new Thread(() -> {
|
cr2.initialize();
|
||||||
try { cr1.start(); } catch (IOException e) { }
|
|
||||||
});
|
|
||||||
|
|
||||||
Thread thread2 = new Thread(() -> {
|
// start both
|
||||||
try { cr2.start(); } catch (IOException e) { }
|
final IntersectionProcess cr1Final = cr1;
|
||||||
});
|
thread1 = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
cr1Final.start();
|
||||||
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
thread1.start();
|
final IntersectionProcess cr2Final = cr2;
|
||||||
thread2.start();
|
thread2 = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
cr2Final.start();
|
||||||
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Thread.sleep(1000); // wait for servers
|
thread1.start();
|
||||||
|
thread2.start();
|
||||||
|
|
||||||
// send vehicle to Cr1 that goes to Cr2
|
Thread.sleep(1000); // wait for servers
|
||||||
java.util.List<String> route = Arrays.asList("Cr1", "Cr2", "S");
|
|
||||||
Vehicle vehicle = new Vehicle("V001", VehicleType.LIGHT, 0.0, route);
|
|
||||||
|
|
||||||
try (Socket socket = new Socket("localhost", 18001)) {
|
// send vehicle to Cr1 that goes to Cr2 - FIXED: use 4-parameter constructor
|
||||||
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
|
java.util.List<String> route = Arrays.asList("Cr1", "Cr2", "S");
|
||||||
|
Vehicle vehicle = new Vehicle("V001", VehicleType.LIGHT, 0.0, route);
|
||||||
|
|
||||||
TestVehicleMessage message = new TestVehicleMessage("Entry", "Cr1", vehicle);
|
// FIXED: use SocketConnection
|
||||||
out.writeObject(message);
|
try (Socket socket = new Socket("localhost", 18001);
|
||||||
out.flush();
|
SocketConnection conn = new SocketConnection(socket)) {
|
||||||
|
|
||||||
Thread.sleep(2000); // time for processing
|
TestVehicleMessage message = new TestVehicleMessage("Entry", "Cr1", vehicle);
|
||||||
|
conn.sendMessage(message);
|
||||||
|
|
||||||
|
Thread.sleep(2000); // time for processing
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (cr1 != null) {
|
||||||
|
cr1.shutdown();
|
||||||
|
}
|
||||||
|
if (cr2 != null) {
|
||||||
|
cr2.shutdown();
|
||||||
|
}
|
||||||
|
if (thread1 != null) {
|
||||||
|
thread1.join(2000);
|
||||||
|
}
|
||||||
|
if (thread2 != null) {
|
||||||
|
thread2.join(2000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cr1.shutdown();
|
|
||||||
cr2.shutdown();
|
|
||||||
thread1.join(2000);
|
|
||||||
thread2.join(2000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
327
main/src/test/java/sd/ExitNodeProcessTest.java
Normal file
327
main/src/test/java/sd/ExitNodeProcessTest.java
Normal file
@@ -0,0 +1,327 @@
|
|||||||
|
package sd;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.Timeout;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
|
import sd.config.SimulationConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Testes unitários para a classe ExitNodeProcess.
|
||||||
|
*
|
||||||
|
* Esta classe de testes verifica:
|
||||||
|
* - Construção e inicialização do processo
|
||||||
|
* - Criação e aceitação de conexões do servidor socket
|
||||||
|
* - Gestão do ciclo de vida (start/shutdown)
|
||||||
|
* - Processamento concorrente de múltiplas conexões
|
||||||
|
* - Impressão de estatísticas finais
|
||||||
|
*
|
||||||
|
* Os testes utilizam configurações temporárias e portas dedicadas (19001)
|
||||||
|
* para evitar conflitos com outros testes ou processos em execução.
|
||||||
|
*/
|
||||||
|
public class ExitNodeProcessTest {
|
||||||
|
|
||||||
|
@TempDir
|
||||||
|
Path tempDir;
|
||||||
|
|
||||||
|
private Path configFile;
|
||||||
|
private ExitNodeProcess exitNodeProcess;
|
||||||
|
private Thread exitNodeThread;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configura o ambiente de teste antes de cada teste.
|
||||||
|
* Cria um ficheiro de configuração temporário com as definições necessárias.
|
||||||
|
*/
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() throws IOException {
|
||||||
|
configFile = tempDir.resolve("test-simulation.properties");
|
||||||
|
|
||||||
|
String configContent = """
|
||||||
|
# Test Exit Node Configuration
|
||||||
|
|
||||||
|
# Exit Configuration
|
||||||
|
exit.host=localhost
|
||||||
|
exit.port=19001
|
||||||
|
|
||||||
|
# Dashboard Configuration (will not be running in tests)
|
||||||
|
dashboard.host=localhost
|
||||||
|
dashboard.port=19000
|
||||||
|
|
||||||
|
# Vehicle Crossing Times
|
||||||
|
vehicle.bike.crossingTime=2.0
|
||||||
|
vehicle.light.crossingTime=3.0
|
||||||
|
vehicle.heavy.crossingTime=5.0
|
||||||
|
|
||||||
|
# Simulation Duration
|
||||||
|
simulation.duration=60.0
|
||||||
|
""";
|
||||||
|
|
||||||
|
Files.writeString(configFile, configContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Limpa os recursos após cada teste.
|
||||||
|
* Garante que o processo e threads são terminados corretamente.
|
||||||
|
*/
|
||||||
|
@AfterEach
|
||||||
|
public void tearDown() {
|
||||||
|
if (exitNodeProcess != null) {
|
||||||
|
exitNodeProcess.shutdown();
|
||||||
|
}
|
||||||
|
if (exitNodeThread != null && exitNodeThread.isAlive()) {
|
||||||
|
exitNodeThread.interrupt();
|
||||||
|
try {
|
||||||
|
exitNodeThread.join(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Testa a construção bem-sucedida do ExitNodeProcess com configuração válida.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testConstructor_Success() throws IOException {
|
||||||
|
SimulationConfig config = new SimulationConfig(configFile.toString());
|
||||||
|
exitNodeProcess = new ExitNodeProcess(config);
|
||||||
|
assertNotNull(exitNodeProcess);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Testa que uma exceção é lançada quando a configuração é inválida.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testConstructor_InvalidConfig() {
|
||||||
|
Exception exception = assertThrows(IOException.class, () -> {
|
||||||
|
new SimulationConfig("non-existent-config.properties");
|
||||||
|
});
|
||||||
|
assertNotNull(exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Testa a inicialização sem dashboard disponível.
|
||||||
|
* Verifica que o processo continua a funcionar mesmo sem conexão ao dashboard.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testInitialize_WithoutDashboard() throws IOException {
|
||||||
|
SimulationConfig config = new SimulationConfig(configFile.toString());
|
||||||
|
exitNodeProcess = new ExitNodeProcess(config);
|
||||||
|
assertDoesNotThrow(() -> exitNodeProcess.initialize());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Testa que o servidor socket é criado corretamente na porta configurada.
|
||||||
|
* Verifica que é possível estabelecer uma conexão ao socket do servidor.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
@Timeout(value = 3, unit = TimeUnit.SECONDS)
|
||||||
|
public void testStart_ServerSocketCreated() throws IOException {
|
||||||
|
SimulationConfig config = new SimulationConfig(configFile.toString());
|
||||||
|
exitNodeProcess = new ExitNodeProcess(config);
|
||||||
|
exitNodeProcess.initialize();
|
||||||
|
|
||||||
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
|
exitNodeThread = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
latch.countDown();
|
||||||
|
exitNodeProcess.start();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// expected when shutdown
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
exitNodeThread.start();
|
||||||
|
|
||||||
|
try {
|
||||||
|
assertTrue(latch.await(2, TimeUnit.SECONDS), "Exit node should start within timeout");
|
||||||
|
Thread.sleep(100);
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
try (Socket testSocket = new Socket("localhost", 19001)) {
|
||||||
|
assertTrue(testSocket.isConnected());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Testa que o servidor aceita conexões de clientes.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
@Timeout(value = 3, unit = TimeUnit.SECONDS)
|
||||||
|
public void testStart_AcceptsConnection() throws IOException, InterruptedException {
|
||||||
|
SimulationConfig config = new SimulationConfig(configFile.toString());
|
||||||
|
exitNodeProcess = new ExitNodeProcess(config);
|
||||||
|
exitNodeProcess.initialize();
|
||||||
|
|
||||||
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
|
exitNodeThread = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
latch.countDown();
|
||||||
|
exitNodeProcess.start();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
exitNodeThread.start();
|
||||||
|
|
||||||
|
assertTrue(latch.await(2, TimeUnit.SECONDS));
|
||||||
|
Thread.sleep(200);
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
try (Socket socket = new Socket("localhost", 19001)) {
|
||||||
|
assertTrue(socket.isConnected());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Testa múltiplas inicializações e encerramentos do processo.
|
||||||
|
* Verifica que o processo pode ser iniciado e parado múltiplas vezes,
|
||||||
|
* permitindo reutilização da porta.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
@Timeout(value = 3, unit = TimeUnit.SECONDS)
|
||||||
|
public void testMultipleStartStop() throws IOException, InterruptedException {
|
||||||
|
SimulationConfig config = new SimulationConfig(configFile.toString());
|
||||||
|
exitNodeProcess = new ExitNodeProcess(config);
|
||||||
|
exitNodeProcess.initialize();
|
||||||
|
|
||||||
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
|
exitNodeThread = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
latch.countDown();
|
||||||
|
exitNodeProcess.start();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
exitNodeThread.start();
|
||||||
|
assertTrue(latch.await(2, TimeUnit.SECONDS));
|
||||||
|
Thread.sleep(100);
|
||||||
|
|
||||||
|
exitNodeProcess.shutdown();
|
||||||
|
Thread.sleep(100);
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
SimulationConfig config2 = new SimulationConfig(configFile.toString());
|
||||||
|
ExitNodeProcess exitNode2 = new ExitNodeProcess(config2);
|
||||||
|
exitNode2.initialize();
|
||||||
|
exitNode2.shutdown();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Testa que o shutdown fecha corretamente o servidor socket.
|
||||||
|
* Após o shutdown, novas conexões ao socket devem falhar.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
@Timeout(value = 3, unit = TimeUnit.SECONDS)
|
||||||
|
public void testShutdown_ClosesServerSocket() throws IOException, InterruptedException {
|
||||||
|
SimulationConfig config = new SimulationConfig(configFile.toString());
|
||||||
|
exitNodeProcess = new ExitNodeProcess(config);
|
||||||
|
exitNodeProcess.initialize();
|
||||||
|
|
||||||
|
CountDownLatch startLatch = new CountDownLatch(1);
|
||||||
|
|
||||||
|
exitNodeThread = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
startLatch.countDown();
|
||||||
|
exitNodeProcess.start();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
exitNodeThread.start();
|
||||||
|
assertTrue(startLatch.await(2, TimeUnit.SECONDS));
|
||||||
|
Thread.sleep(200);
|
||||||
|
|
||||||
|
exitNodeProcess.shutdown();
|
||||||
|
Thread.sleep(200);
|
||||||
|
|
||||||
|
assertThrows(IOException.class, () -> {
|
||||||
|
Socket socket = new Socket("localhost", 19001);
|
||||||
|
socket.close();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Testa que as estatísticas finais são impressas corretamente durante o shutdown.
|
||||||
|
* Verifica que o método não lança exceções mesmo sem dados processados.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testPrintFinalStatistics() throws IOException {
|
||||||
|
SimulationConfig config = new SimulationConfig(configFile.toString());
|
||||||
|
exitNodeProcess = new ExitNodeProcess(config);
|
||||||
|
exitNodeProcess.initialize();
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> exitNodeProcess.shutdown());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Testa o processamento de múltiplas conexões concorrentes.
|
||||||
|
* Verifica que o servidor consegue lidar com vários clientes simultaneamente
|
||||||
|
* usando o pool de threads.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
@Timeout(value = 3, unit = TimeUnit.SECONDS)
|
||||||
|
public void testMultipleConcurrentConnections() throws IOException, InterruptedException {
|
||||||
|
SimulationConfig config = new SimulationConfig(configFile.toString());
|
||||||
|
exitNodeProcess = new ExitNodeProcess(config);
|
||||||
|
exitNodeProcess.initialize();
|
||||||
|
|
||||||
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
|
exitNodeThread = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
latch.countDown();
|
||||||
|
exitNodeProcess.start();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
exitNodeThread.start();
|
||||||
|
assertTrue(latch.await(2, TimeUnit.SECONDS));
|
||||||
|
Thread.sleep(200);
|
||||||
|
|
||||||
|
Thread[] clients = new Thread[3];
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
clients[i] = new Thread(() -> {
|
||||||
|
try (Socket socket = new Socket("localhost", 19001)) {
|
||||||
|
assertTrue(socket.isConnected());
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (IOException | InterruptedException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
});
|
||||||
|
clients[i].start();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Thread client : clients) {
|
||||||
|
client.join(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
206
main/src/test/java/sd/TrafficLightCoordinationTest.java
Normal file
206
main/src/test/java/sd/TrafficLightCoordinationTest.java
Normal file
@@ -0,0 +1,206 @@
|
|||||||
|
package sd;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import sd.model.TrafficLight;
|
||||||
|
import sd.model.TrafficLightState;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class to verify traffic light coordination within an intersection.
|
||||||
|
* Ensures that only ONE traffic light can be GREEN at any given time.
|
||||||
|
*/
|
||||||
|
public class TrafficLightCoordinationTest {
|
||||||
|
|
||||||
|
private IntersectionProcess intersectionProcess;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() throws IOException {
|
||||||
|
// Create an intersection with multiple traffic lights
|
||||||
|
intersectionProcess = new IntersectionProcess("Cr2", "src/main/resources/simulation.properties");
|
||||||
|
intersectionProcess.initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void tearDown() throws InterruptedException {
|
||||||
|
if (intersectionProcess != null) {
|
||||||
|
intersectionProcess.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that verifies mutual exclusion between traffic lights.
|
||||||
|
* Monitors all traffic lights for 10 seconds and ensures that
|
||||||
|
* at most ONE light is GREEN at any point in time.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testOnlyOneGreenLightAtATime() throws InterruptedException {
|
||||||
|
System.out.println("\n=== Testing Traffic Light Mutual Exclusion ===");
|
||||||
|
|
||||||
|
// Start the intersection
|
||||||
|
Thread intersectionThread = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
intersectionProcess.start();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
intersectionThread.start();
|
||||||
|
|
||||||
|
// Monitor traffic lights for violations
|
||||||
|
AtomicInteger maxGreenSimultaneously = new AtomicInteger(0);
|
||||||
|
AtomicInteger violationCount = new AtomicInteger(0);
|
||||||
|
List<String> violations = new ArrayList<>();
|
||||||
|
|
||||||
|
// Monitor for 10 seconds
|
||||||
|
long endTime = System.currentTimeMillis() + 10000;
|
||||||
|
|
||||||
|
while (System.currentTimeMillis() < endTime) {
|
||||||
|
int greenCount = 0;
|
||||||
|
StringBuilder currentState = new StringBuilder("States: ");
|
||||||
|
|
||||||
|
for (TrafficLight light : intersectionProcess.getIntersection().getTrafficLights()) {
|
||||||
|
TrafficLightState state = light.getState();
|
||||||
|
currentState.append(light.getDirection()).append("=").append(state).append(" ");
|
||||||
|
|
||||||
|
if (state == TrafficLightState.GREEN) {
|
||||||
|
greenCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update maximum simultaneous green lights
|
||||||
|
if (greenCount > maxGreenSimultaneously.get()) {
|
||||||
|
maxGreenSimultaneously.set(greenCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for violations (more than one green)
|
||||||
|
if (greenCount > 1) {
|
||||||
|
violationCount.incrementAndGet();
|
||||||
|
String violation = String.format("[VIOLATION] %d lights GREEN simultaneously: %s",
|
||||||
|
greenCount, currentState.toString());
|
||||||
|
violations.add(violation);
|
||||||
|
System.err.println(violation);
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread.sleep(50); // Check every 50ms
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\n=== Test Results ===");
|
||||||
|
System.out.println("Maximum simultaneous GREEN lights: " + maxGreenSimultaneously.get());
|
||||||
|
System.out.println("Total violations detected: " + violationCount.get());
|
||||||
|
|
||||||
|
if (!violations.isEmpty()) {
|
||||||
|
System.err.println("\nViolation details:");
|
||||||
|
violations.forEach(System.err::println);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assert that we never had more than one green light
|
||||||
|
assertEquals(0, violationCount.get(),
|
||||||
|
"Traffic light coordination violated! Multiple lights were GREEN simultaneously.");
|
||||||
|
assertTrue(maxGreenSimultaneously.get() <= 1,
|
||||||
|
"At most ONE light should be GREEN at any time. Found: " + maxGreenSimultaneously.get());
|
||||||
|
|
||||||
|
System.out.println("\nTraffic light coordination working correctly!");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that verifies all traffic lights get a chance to be GREEN.
|
||||||
|
* Ensures fairness in the coordination mechanism.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testAllLightsGetGreenTime() throws InterruptedException {
|
||||||
|
System.out.println("\n=== Testing Traffic Light Fairness ===");
|
||||||
|
|
||||||
|
// Start the intersection
|
||||||
|
Thread intersectionThread = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
intersectionProcess.start();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
intersectionThread.start();
|
||||||
|
|
||||||
|
// Track which lights have been green
|
||||||
|
List<TrafficLight> lights = intersectionProcess.getIntersection().getTrafficLights();
|
||||||
|
boolean[] hasBeenGreen = new boolean[lights.size()];
|
||||||
|
|
||||||
|
// Monitor for 15 seconds (enough time for all lights to cycle)
|
||||||
|
long endTime = System.currentTimeMillis() + 15000;
|
||||||
|
|
||||||
|
while (System.currentTimeMillis() < endTime) {
|
||||||
|
for (int i = 0; i < lights.size(); i++) {
|
||||||
|
if (lights.get(i).getState() == TrafficLightState.GREEN) {
|
||||||
|
hasBeenGreen[i] = true;
|
||||||
|
System.out.println("✓ " + lights.get(i).getDirection() + " has been GREEN");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Thread.sleep(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if all lights got green time
|
||||||
|
int greenCount = 0;
|
||||||
|
System.out.println("\n=== Fairness Results ===");
|
||||||
|
for (int i = 0; i < lights.size(); i++) {
|
||||||
|
String status = hasBeenGreen[i] ? "✓ YES" : "✗ NO";
|
||||||
|
System.out.println(lights.get(i).getDirection() + " got GREEN time: " + status);
|
||||||
|
if (hasBeenGreen[i]) greenCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(greenCount > 0, "At least one light should have been GREEN during the test");
|
||||||
|
System.out.println("\n" + greenCount + "/" + lights.size() + " lights were GREEN during test period");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that verifies the state transitions are consistent.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testStateTransitionsAreConsistent() throws InterruptedException {
|
||||||
|
System.out.println("\n=== Testing State Transition Consistency ===");
|
||||||
|
|
||||||
|
Thread intersectionThread = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
intersectionProcess.start();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
intersectionThread.start();
|
||||||
|
|
||||||
|
List<TrafficLight> lights = intersectionProcess.getIntersection().getTrafficLights();
|
||||||
|
TrafficLightState[] previousStates = new TrafficLightState[lights.size()];
|
||||||
|
|
||||||
|
// Initialize previous states
|
||||||
|
for (int i = 0; i < lights.size(); i++) {
|
||||||
|
previousStates[i] = lights.get(i).getState();
|
||||||
|
}
|
||||||
|
|
||||||
|
int transitionCount = 0;
|
||||||
|
long endTime = System.currentTimeMillis() + 8000;
|
||||||
|
|
||||||
|
while (System.currentTimeMillis() < endTime) {
|
||||||
|
for (int i = 0; i < lights.size(); i++) {
|
||||||
|
TrafficLightState currentState = lights.get(i).getState();
|
||||||
|
|
||||||
|
if (currentState != previousStates[i]) {
|
||||||
|
transitionCount++;
|
||||||
|
System.out.println(lights.get(i).getDirection() + " transitioned: " +
|
||||||
|
previousStates[i] + " → " + currentState);
|
||||||
|
previousStates[i] = currentState;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Thread.sleep(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nTotal state transitions observed: " + transitionCount);
|
||||||
|
assertTrue(transitionCount > 0, "There should be state transitions during the test period");
|
||||||
|
}
|
||||||
|
}
|
||||||
164
main/src/test/java/sd/dashboard/DashboardTest.java
Normal file
164
main/src/test/java/sd/dashboard/DashboardTest.java
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
package sd.dashboard;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import sd.config.SimulationConfig;
|
||||||
|
import sd.model.VehicleType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for Dashboard Server components.
|
||||||
|
*/
|
||||||
|
class DashboardTest {
|
||||||
|
|
||||||
|
private DashboardStatistics statistics;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
statistics = new DashboardStatistics();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void tearDown() {
|
||||||
|
statistics = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testInitialStatistics() {
|
||||||
|
assertEquals(0, statistics.getTotalVehiclesGenerated(),
|
||||||
|
"Initial vehicles generated should be 0");
|
||||||
|
assertEquals(0, statistics.getTotalVehiclesCompleted(),
|
||||||
|
"Initial vehicles completed should be 0");
|
||||||
|
assertEquals(0.0, statistics.getAverageSystemTime(),
|
||||||
|
"Initial average system time should be 0.0");
|
||||||
|
assertEquals(0.0, statistics.getAverageWaitingTime(),
|
||||||
|
"Initial average waiting time should be 0.0");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testVehicleCounters() {
|
||||||
|
statistics.incrementVehiclesGenerated();
|
||||||
|
assertEquals(1, statistics.getTotalVehiclesGenerated());
|
||||||
|
|
||||||
|
statistics.updateVehiclesGenerated(10);
|
||||||
|
assertEquals(10, statistics.getTotalVehiclesGenerated());
|
||||||
|
|
||||||
|
statistics.incrementVehiclesCompleted();
|
||||||
|
assertEquals(1, statistics.getTotalVehiclesCompleted());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAverageCalculations() {
|
||||||
|
// Add 3 completed vehicles with known times
|
||||||
|
statistics.updateVehiclesCompleted(3);
|
||||||
|
statistics.addSystemTime(3000); // 3000ms total
|
||||||
|
statistics.addWaitingTime(1500); // 1500ms total
|
||||||
|
|
||||||
|
assertEquals(1000.0, statistics.getAverageSystemTime(), 0.01,
|
||||||
|
"Average system time should be 1000ms");
|
||||||
|
assertEquals(500.0, statistics.getAverageWaitingTime(), 0.01,
|
||||||
|
"Average waiting time should be 500ms");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testVehicleTypeStatistics() {
|
||||||
|
statistics.incrementVehicleType(VehicleType.LIGHT);
|
||||||
|
statistics.incrementVehicleType(VehicleType.LIGHT);
|
||||||
|
statistics.incrementVehicleType(VehicleType.HEAVY);
|
||||||
|
|
||||||
|
assertEquals(2, statistics.getVehicleTypeCount(VehicleType.LIGHT));
|
||||||
|
assertEquals(1, statistics.getVehicleTypeCount(VehicleType.HEAVY));
|
||||||
|
assertEquals(0, statistics.getVehicleTypeCount(VehicleType.BIKE));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIntersectionStatistics() {
|
||||||
|
statistics.updateIntersectionStats("Cr1", 10, 8, 2);
|
||||||
|
|
||||||
|
DashboardStatistics.IntersectionStats stats =
|
||||||
|
statistics.getIntersectionStats("Cr1");
|
||||||
|
|
||||||
|
assertNotNull(stats, "Intersection stats should not be null");
|
||||||
|
assertEquals("Cr1", stats.getIntersectionId());
|
||||||
|
assertEquals(10, stats.getTotalArrivals());
|
||||||
|
assertEquals(8, stats.getTotalDepartures());
|
||||||
|
assertEquals(2, stats.getCurrentQueueSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMultipleIntersections() {
|
||||||
|
statistics.updateIntersectionStats("Cr1", 10, 8, 2);
|
||||||
|
statistics.updateIntersectionStats("Cr2", 15, 12, 3);
|
||||||
|
statistics.updateIntersectionStats("Cr3", 5, 5, 0);
|
||||||
|
|
||||||
|
assertEquals(3, statistics.getAllIntersectionStats().size(),
|
||||||
|
"Should have 3 intersections");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testStatsUpdatePayload() {
|
||||||
|
StatsUpdatePayload payload = new StatsUpdatePayload()
|
||||||
|
.setTotalVehiclesGenerated(50)
|
||||||
|
.setTotalVehiclesCompleted(20)
|
||||||
|
.setIntersectionArrivals(30)
|
||||||
|
.setIntersectionDepartures(25)
|
||||||
|
.setIntersectionQueueSize(5);
|
||||||
|
|
||||||
|
assertEquals(50, payload.getTotalVehiclesGenerated());
|
||||||
|
assertEquals(20, payload.getTotalVehiclesCompleted());
|
||||||
|
assertEquals(30, payload.getIntersectionArrivals());
|
||||||
|
assertEquals(25, payload.getIntersectionDepartures());
|
||||||
|
assertEquals(5, payload.getIntersectionQueueSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testStatsMessage() {
|
||||||
|
StatsUpdatePayload payload = new StatsUpdatePayload()
|
||||||
|
.setIntersectionArrivals(10);
|
||||||
|
|
||||||
|
StatsMessage message = new StatsMessage("Cr1", payload);
|
||||||
|
|
||||||
|
assertEquals("Cr1", message.getSourceNode());
|
||||||
|
assertEquals("DashboardServer", message.getDestinationNode());
|
||||||
|
assertEquals(sd.model.MessageType.STATS_UPDATE, message.getType());
|
||||||
|
assertNotNull(message.getPayload());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testThreadSafety() throws InterruptedException {
|
||||||
|
// Test concurrent updates
|
||||||
|
Thread t1 = new Thread(() -> {
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
statistics.incrementVehiclesGenerated();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Thread t2 = new Thread(() -> {
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
statistics.incrementVehiclesGenerated();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
t1.start();
|
||||||
|
t2.start();
|
||||||
|
t1.join();
|
||||||
|
t2.join();
|
||||||
|
|
||||||
|
assertEquals(200, statistics.getTotalVehiclesGenerated(),
|
||||||
|
"Concurrent increments should total 200");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDashboardServerCreation() throws Exception {
|
||||||
|
SimulationConfig config = new SimulationConfig("simulation.properties");
|
||||||
|
DashboardServer server = new DashboardServer(config);
|
||||||
|
|
||||||
|
assertNotNull(server, "Server should be created successfully");
|
||||||
|
assertNotNull(server.getStatistics(), "Statistics should be initialized");
|
||||||
|
assertFalse(server.isRunning(), "Server should not be running initially");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user