personal branch

This commit is contained in:
Leandro Afonso
2025-10-14 02:20:24 +01:00
parent c86d303d5b
commit 651dc754b8
11 changed files with 182 additions and 123 deletions

View File

@@ -1,34 +0,0 @@
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;
}
}

View File

@@ -0,0 +1,7 @@
package sd;
public class Entry {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

View File

@@ -1,7 +0,0 @@
package sd;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

View File

@@ -1,47 +0,0 @@
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();
}
}
}

View File

@@ -1,35 +0,0 @@
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) ...
}
}