mirror of
https://github.com/davidalves04/Trabalho-Pratico-SD.git
synced 2025-12-08 20:43:32 +00:00
Criação de classes
This commit is contained in:
34
main/src/main/java/sd/CruzamentoServer.java
Normal file
34
main/src/main/java/sd/CruzamentoServer.java
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
47
main/src/main/java/sd/Semaforo.java
Normal file
47
main/src/main/java/sd/Semaforo.java
Normal file
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
35
main/src/main/java/sd/Veiculo.java
Normal file
35
main/src/main/java/sd/Veiculo.java
Normal file
@@ -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) ...
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
main/target/classes/sd/CruzamentoServer.class
Normal file
BIN
main/target/classes/sd/CruzamentoServer.class
Normal file
Binary file not shown.
BIN
main/target/classes/sd/Semaforo.class
Normal file
BIN
main/target/classes/sd/Semaforo.class
Normal file
Binary file not shown.
BIN
main/target/classes/sd/Veiculo.class
Normal file
BIN
main/target/classes/sd/Veiculo.class
Normal file
Binary file not shown.
Reference in New Issue
Block a user