package basededatos;
import java.net.UnknownHostException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.List;
import common.Gui;
import common.Utils;
import interfaces.ServicioDatosInterface;
public class Basededatos {
private static ServicioDatosInterface dataService;
private static int registryPort = 8888;
public static void main(String[] args) throws Exception {
Utils.setCodeBase(interfaces.ServicioAutenticacionInterface.class);
startRegistry(registryPort);
//inicializar servicio de datos
ServicioDatosImpl dataService = new ServicioDatosImpl();
Naming.rebind("rmi://localhost:" + registryPort + "/data", dataService);
System.out.println("Servicio de datos listo.");
gui();
}
//iniciar registro
private static void startRegistry (int registryPort) throws RemoteException {
try {
Registry registry = LocateRegistry.getRegistry(registryPort);
registry.list();
} catch (RemoteException exc) {
System.out.println("No existe un registro valido en el puerto: " + registryPort);
Registry registry = LocateRegistry.createRegistry(registryPort);
System.out.println("Registro creado en el puerto: "+ registryPort);
}
}
private static void stopServices () throws RemoteException {
try {
Naming.unbind("remi://localhost:" + registryPort + "/data");
} catch (Exception e) {
}
}
private static void gui() throws RemoteException, UnknownHostException {
int opt = 0;
do {
opt = Gui.menu("Menu BBDD", new String [] {
"Informacion de la base de datos",
"Listar jugadores registrados",
"Salair(logout)"});
switch (opt) {
case 0: infoBBDD();
break;
case 1: listPlayers();
break;
}
} while (opt != 2);
}
//informacion bbdd
private static String infoBBDD() throws UnknownHostException {
return "rmi://" + java.net.InetAddress.getLocalHost().getHostAddress() + ":" + registryPort + "/" + dataService;
}
//listar jugadores
private static void listPlayers() {
List<String> tmpList;
try {
tmpList = dataService.getRegisteredPlayers();
if (!tmpList.isEmpty()) {
for (String s : tmpList) {
System.out.println(s);
}
} else {
System.out.println("No hay jugadores registrados para mostrar.");
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
}