diff --git a/main/src/main/java/sd/CruzamentoServer.java b/main/src/main/java/sd/CruzamentoServer.java new file mode 100644 index 0000000..1d84a00 --- /dev/null +++ b/main/src/main/java/sd/CruzamentoServer.java @@ -0,0 +1,34 @@ +package sd; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; + +public class CruzamentoServer { + public static void main(String[] args) { + // ... Inicializa Semáforos (Threads) ... + // ... Inicializa as Estruturas de Dados ... + + try (ServerSocket serverSocket = new ServerSocket(portaDoCruzamento)) { + while (true) { + Socket clienteSocket = serverSocket.accept(); + // Cria uma Thread de atendimento para lidar com o Veículo/Cliente + new Thread(new AtendenteVeiculo(clienteSocket)).start(); + } + } catch (IOException e) { /* ... */ } + } + + // Método chamado pelo AtendenteVeiculo para gerenciar o tráfego + public synchronized boolean tentarPassar(Veiculo veiculo, String direcao) { + // 1. Veículo entra na fila da direção + // 2. Verifica o estado do semáforo da direção: + Semaforo semaforo = getSemaforo(direcao); + semaforo.esperarPeloVerde(); // O Veículo fica bloqueado se for vermelho + + // 3. Após o verde: + // - Remove da fila + // - Permite a passagem (envia resposta de volta ao Veículo cliente) + // 4. Envia estatística de passagem ao Simulador Principal (Cliente TCP) + return true; + } +} diff --git a/main/src/main/java/sd/Semaforo.java b/main/src/main/java/sd/Semaforo.java new file mode 100644 index 0000000..8f1d8ad --- /dev/null +++ b/main/src/main/java/sd/Semaforo.java @@ -0,0 +1,47 @@ +package sd; + +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; + +public class Semaforo extends Thread { + // ... atributos ... + private final Lock semaforoLock; // Para sincronizar acesso ao estado + private final Condition verdeCondition; // Para Veículos esperarem pelo verde + + public Semaforo(...) { + this.semaforoLock = new ReentrantLock(); + this.verdeCondition = semaforoLock.newCondition(); + } + + @Override + public void run() { + while (true) { + // Ciclo de tempo (ajustável para controle) + estado = Estado.VERMELHO; + // Notificar o Cruzamento sobre o estado + try { + Thread.sleep(tempoVermelho); + estado = Estado.VERDE; + // Ao ficar VERDE, notifica as threads Veículo que estão esperando + semaforoLock.lock(); + try { + verdeCondition.signalAll(); + } finally { + semaforoLock.unlock(); + } + Thread.sleep(tempoVerde); + } catch (InterruptedException e) { /* ... */ } + } + } + // Método para a thread Veículo esperar + public void esperarPeloVerde() throws InterruptedException { + semaforoLock.lock(); + try { + if (estado == Estado.VERMELHO) { + verdeCondition.await(); + } + } finally { + semaforoLock.unlock(); + } + } +} \ No newline at end of file diff --git a/main/src/main/java/sd/Veiculo.java b/main/src/main/java/sd/Veiculo.java new file mode 100644 index 0000000..2a55ec9 --- /dev/null +++ b/main/src/main/java/sd/Veiculo.java @@ -0,0 +1,35 @@ +package sd; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.net.Socket; + +public class Veiculo implements Runnable { + // ... + private String proximoCruzamentoIP; + private int proximoCruzamentoPorta; + + public void run() { + // Simular o movimento na rua (Thread.sleep(t)) + + // 1. Tenta se conectar ao próximo Cruzamento + try (Socket socket = new Socket(proximoCruzamentoIP, proximoCruzamentoPorta); + ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); + ObjectInputStream in = new ObjectInputStream(socket.getInputStream())) { + + // Envia o objeto Veículo com a solicitação de passagem + out.writeObject(this); + + // 2. BLOQUEIA a Thread, esperando a resposta do Servidor/Cruzamento + String permissao = (String) in.readObject(); + + if ("OK_PASSAR".equals(permissao)) { + // Simular tempo de travessia do cruzamento (pequeno Thread.sleep()) + // Atualiza a rota (próximo nó) + } + + } catch (IOException | ClassNotFoundException e) { /* ... */ } + // ... continua o loop da rota até a Saída (S) ... + } +} diff --git a/main/target/classes/sd/CruzamentoServer.class b/main/target/classes/sd/CruzamentoServer.class new file mode 100644 index 0000000..f1925fd Binary files /dev/null and b/main/target/classes/sd/CruzamentoServer.class differ diff --git a/main/target/classes/sd/Semaforo.class b/main/target/classes/sd/Semaforo.class new file mode 100644 index 0000000..e275fc3 Binary files /dev/null and b/main/target/classes/sd/Semaforo.class differ diff --git a/main/target/classes/sd/Veiculo.class b/main/target/classes/sd/Veiculo.class new file mode 100644 index 0000000..3414695 Binary files /dev/null and b/main/target/classes/sd/Veiculo.class differ