/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package socius.state;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.control.Dialogs;
import javafx.stage.Stage;
import socius.autenticacao.AutenticacaoRemota;
import socius.dispositivos.Computador;
import socius.dispositivos.Servidor;
import socius.dispositivos.ServidorRemoto;
import socius.excecoes.ComputadorNaoConectadoException;
import socius.excecoes.FalhaNaConexaoComServidorException;
import socius.excecoes.UsuarioInvalidoException;
import socius.app.ClientBoot;
import socius.excecoes.UsuarioJaLogadoException;
import socius.util.ConfigSistema;
/**
*
* @author Lucas Dillmann <lucas [at] dillmann.com.br>
*/
public class EstadoOffline implements EstadoComputador {
private String estado = "offline";
private Computador computador;
private ServidorRemoto servidor;
public EstadoOffline(Computador computador) {
this.computador = computador;
}
@Override
public void conectar() throws FalhaNaConexaoComServidorException, UsuarioInvalidoException, UsuarioJaLogadoException {
StringBuilder urlRMI = new StringBuilder();
urlRMI.append("rmi://");
urlRMI.append(ConfigSistema.getProperty("cliente.ip_servidor"));
urlRMI.append(":");
urlRMI.append(ConfigSistema.getProperty("cliente.porta_servidor"));
urlRMI.append("/");
urlRMI.append(ConfigSistema.getProperty("cliente.servico_servidor"));
// Tenta conexão com servidor
AutenticacaoRemota auth = null;
boolean atualizaEstado = false;
try {
auth = (AutenticacaoRemota) Naming.lookup(urlRMI.toString());
servidor = auth.getServidor(computador);
if (servidor == null) {
if(computador.getUsuario().getId() == -1)
throw new UsuarioJaLogadoException();
else
throw new UsuarioInvalidoException();
} else {
atualizaEstado = true;
}
} catch (NotBoundException | MalformedURLException | RemoteException ex) {
ex.printStackTrace();
throw new FalhaNaConexaoComServidorException();
} finally {
if (atualizaEstado) {
computador.setEstado(new EstadoOnline(computador, servidor));
}
}
}
@Override
public void desconectar() {
if (Dialogs.showConfirmDialog(new Stage(), "Você não está conectado!.\n deseja Conectar?").equals("YES")) {
ClientBoot redirecionar = new ClientBoot();
redirecionar.telaLogin();
}
}
@Override
public Servidor getServidorConectado() throws ComputadorNaoConectadoException {
throw new ComputadorNaoConectadoException();
}
@Override
public String toString() {
return estado;
}
}