viernes, 29 de noviembre de 2013

Lexuz, un sistema distribuido

Un Sistema Distribuido es un sistema de computadoras en interconexión, que se comportan como una sola a pesar de estar compuestas por varios elementos. Al estar todas ellas en una red, el usuario puede consultar información sin importar si ésta se originó en otra computadora, ya que todas pueden tener acceso a la misma información y a los mismos procesos.
Este es el objetivo de los sistemas distribuidos, el establecer una comunicación Cliente-Servidor y el compartimiento de recursos de manera eficiente, segura y rápida. Todo esto se logra mediante el uso de mensajes entre las partes.
Lexuz es distribuido porque se compone de un servidor, en el cual se encuentra la base de datos y el programa ejecutándose, y de varios clientes, desde donde se puede accesar al sistema sin necesidad de tener la base de datos o el programa ejecutándose en la maquina cliente.
Esto se logra ya que se comunican utilizando la IP del servidor, se conectan a este y pueden obtener información generada por otra maquina cliente y ejecutar procesos.

miércoles, 20 de noviembre de 2013

Juego Distribuido

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ServidorTCP {
 private static final int INACTIVO = 0;
 private static final int JUGANDO = 1;
 private static final int GANO = 2;
 private static final int PERDIO = 3;
 private static final int SOLICITANDO_JUEGO_NUEVO = 4;
 private static final int nroPuerto = 8080;
 private static boolean SocketDisponible = true;
 private static int conexionesRealizadas = 0;
 private static Socket con;
 private static int estadoJuego = INACTIVO;
 private static char letra = '?';
 private static String palabra = "?";
 private static String palabraEnProgreso = "?";
 private static int nroIntentos = 6;
 static private final String[] palabrasAhorcado = {"reingenieria", "cubeta","tunelizacion", "protocolo", "puertos", "conexion", "broadcasting", "direccion","internet", "router", "switch", "wifi", "estandar", "socket", "transporte","enlace", "capas", "arquitectura", "cliente", "servidor", "proxy", "firewall","redes", "LAN", "WAN", "MAN", "hub", "concentrador", "datagrama", "puente","fibra", "TCP", "UDP", "mascara", "gateway", "servidor", "DNS", "cliente","conmutacion", "circuito", "satelite", "coaxial", "microondas", "señal","ingrarrojos", "token", "anillo", "bus", "control", "flujo", "congestion","enrutamiento", "aplicacion", "correo", "peertopeer", "reingenieria", "cubeta","tunelizacion", "protocolo", "puertos", "conexion", "broadcasting", "direccion","internet", "router", "switch", "wifi", "estandar", "socket", "transporte","enlace", "capas", "arquitectura", "cliente", "servidor", "proxy", "firewall","redes", "LAN", "WAN", "MAN", "hub", "concentrador", "datagrama", "puente","fibra", "TCP", "UDP", "mascara", "gateway", "servidor", "DNS", "cliente","conmutacion", "circuito", "satelite", "coaxial", "microondas", "señal","ingrarrojos", "token", "anillo", "bus", "control", "flujo", "congestion","enrutamiento", "aplicacion", "correo", "peertopeer"};
 private static int nroMensaje = 0;
 public static void main(String[] args) throws IOException {
     ServerSocket socketDeServicio = null;
  try {
      socketDeServicio = new ServerSocket(nroPuerto);
   BufferedReader entrada;
   DataOutputStream salida;
   while (true) {
      try {
          if (SocketDisponible) {
          //EL SOCKET ESTA DISPONIBLE, POR LO TANTO NO SE ESTA EN JUEGO
       if (estadoJuego == INACTIVO) {
           System.out.println("\nEsperando cliente...");
        con = socketDeServicio.accept();
        System.out.println("Conexion aceptada...\n");
        salida = new DataOutputStream(con.getOutputStream());
        entrada = new BufferedReader(new InputStreamReader(con.getInputStream()));
        conexionesRealizadas++;
        System.out.println("Servidor : Conexion aceptada a Cliente " +conexionesRealizadas);
        /*leer primer mensaje, peticion de inicio de juego desde el cliente.*/
        leerMensaje(entrada.readLine());
        // imprimirEntrada();
        procesarMensaje();
        salida.writeBytes(responderMensaje());
        // imprimirSalida();
        }} else {
         entrada = new BufferedReader(new InputStreamReader(con.getInputStream()));
         salida = new DataOutputStream(con.getOutputStream());
         //EL SOCKET ESTA OCUPADO, POR LO TANTO SE ESTA EN JUEGO
         if (estadoJuego == JUGANDO) {
         leerMensaje(entrada.readLine());
         // imprimirEntrada();
         procesarMensaje();
         salida.writeBytes(responderMensaje());
         // imprimirSalida();
         if (estadoJuego == GANO || estadoJuego == PERDIO) {
         estadoJuego = INACTIVO;SocketDisponible = true;
         System.out.println("Juego numero " + conexionesRealizadas + "Terminado.");
         }}}
       } catch (java.net.SocketException e) {
        System.out.println("Termino abrupto de la comunicacion con el cliente.");
     estadoJuego = INACTIVO;
     SocketDisponible = true;
     System.out.println("Juego numero " + conexionesRealizadas + " Terminado.");}}
   } catch (IOException BindException) {
       System.out.println("La maquina virtual de java ya esta ocupando el socket"+ "en ese puerto, intente iniciar el servicio con otro puerto");}}
   
private static void leerMensaje(String mensaje) {
            StringTokenizer stk = new StringTokenizer(mensaje, "#");
   while (stk.hasMoreTokens()) {
       estadoJuego = Integer.valueOf(stk.nextToken());
    nroIntentos = Integer.valueOf(stk.nextToken());
    letra = stk.nextToken().toUpperCase().charAt(0);
    palabraEnProgreso = stk.nextToken().toUpperCase();
    nroMensaje = Integer.valueOf(stk.nextToken());}
   nroMensaje++;}
  
private static void procesarMensaje() {
            if (estadoJuego == SOLICITANDO_JUEGO_NUEVO) {
       setSocketDisponible(false);
    setEstadoJuego(JUGANDO);
    setNroIntentos(6);
    setLetra('?');
    setPalabra(escojerPalabraJuegoNuevo());
    setPalabraEnProgreso();
   } else {
     if (estadoJuego == JUGANDO) {
         if (huboAcierto()) {
       reemplazarLetra();
       if (ganoJuego()) {estadoJuego = GANO;
       System.out.println("Servidor : Cliente ha ganado el juego");}
             else {System.out.println("Servidor : Cliente ha acertado la palabra");}}
         else {
      nroIntentos--;
      System.out.println("Servidor : Se le ha disminuido un intento al cliente por no haber acertado");
      if (nroIntentos == 0) {
       estadoJuego = PERDIO;
       System.out.println("Servidor : Cliente ha perdido el juego");}}}
      else {
        try {
         System.out.println("Servidor : cerrando conexion...");
      con.shutdownOutput();
      SocketDisponible = true;
      System.out.println("Servidor : Conexion finalizada.");
     } catch (IOException ex) {
       Logger.getLogger(ServidorTCP.class.getName()).log(Level.SEVERE, null, ex);}}}
 }
private static String responderMensaje() {
            String a = estadoJuego + "#" + nroIntentos + "#" + letra + "#" + palabraEnProgreso + "#" +nroMensaje + "\n";
   return a;}
  
public static void setSocketDisponible(boolean SocketDisponible) {
           ServidorTCP.SocketDisponible = SocketDisponible;
  }
 
public static void setConexionesRealizadas(int conexionesRealizadas) {
          ServidorTCP.conexionesRealizadas = conexionesRealizadas;}
   
public static void setEstadoJuego(int estadoJuego) {
          ServidorTCP.estadoJuego = estadoJuego;}
   
public static void setLetra(char letra) {
          ServidorTCP.letra = letra;}
   
public static void setNroIntentos(int nroIntentos) {
          ServidorTCP.nroIntentos = nroIntentos;}
   
public static void setPalabra(String palabra) {
          ServidorTCP.palabra = palabra;}
   
public static void setPalabraEnProgreso(String palabraEnProgreso) {
           ServidorTCP.palabraEnProgreso = palabraEnProgreso;}
    
private static String escojerPalabraJuegoNuevo() {
          return palabrasAhorcado[(int) (Math.random() * palabrasAhorcado.length)];}
   
private static void setPalabraEnProgreso() {
          String p = "";
    for (int i = 0; i < palabra.length(); i++) {
        p += "_";}
     palabraEnProgreso = p;}
    
private static boolean huboAcierto() {
         boolean tuvoAcierto = true;
   //PRIMERO DEBEMOS COMPROBAR QUE LA LETRA NO SE REPITA CON LO QUE YA TENEMOS COMPLETADO ACTUALMENTE
   tuvoAcierto = !seRepite(letra, palabraEnProgreso) && esParteDeLaPalabra(letra, palabra);
   return tuvoAcierto;}
  
private static boolean seRepite(char l, String enProgreso) {
         boolean repite = false;
   char[] prog = enProgreso.toCharArray();
   for (int i = 0; i < prog.length; i++) {
       if (l == prog[i]) {repite = true;}}
      return repite;}
  
private static boolean esParteDeLaPalabra(char letra, String palabra) {
         boolean esParte = false;
   char[] pa = palabra.toUpperCase().toCharArray();
   for (int i = 0; i < pa.length; i++) {
       if (letra == pa[i]) {esParte = true;}}
  return esParte;}
 
private static void reemplazarLetra() {
         String[] enProg = palabraEnProgreso.split("");
   String[] pal = palabra.split("");
   String reemplazada = "";
   for (int i = 0; i < pal.length; i++) {
       if (String.valueOf(letra).equalsIgnoreCase(pal[i])) {
        enProg[i] = String.valueOf(letra);}
       reemplazada += enProg[i];}
   palabraEnProgreso = reemplazada;}
  
private static boolean ganoJuego() {
         if (palabraEnProgreso.equalsIgnoreCase(palabra)) {
   return true;}
   else {return false;}}
  
private static String mostrarEstado() {
    if (estadoJuego == 0) {return "INACTIVO";}
 else {if (estadoJuego == 1) {
    return "JUGANDO";}
 else {if (estadoJuego == 2) {
     return "GANO";}
    else {if (estadoJuego == 3) {
     return "PERDIO";}
 else {if (estadoJuego == 4) {
     return "SOLICITANDO_JUEGO_NUEVO";}
 else {
     return "JUEGO_TERMINADO";}}}}}}
 
private static void imprimirEntrada() {
    String a = estadoJuego + "#" + nroIntentos + "#" + letra + "#" + palabraEnProgreso + "#" +nroMensaje;
 System.out.println("\nLeído por Servidor: " + a + "\n" + mostrarEstado());}

private static void imprimirSalida() {
    String a = estadoJuego + "#" + nroIntentos + "#" + letra + "#" + palabraEnProgreso + "#" +nroMensaje;
 System.out.println("\nEnviado por Servidor: " + a + "\n" + mostrarEstado());}

}

Juego Ahorcado Distribuido

Interfaz del Ahorcado:

public interface ClienteAhorcadoint{
 
 public  void enviarMensaje();
 public  String crearMensajeRespuesta();
 public  int getEstadoJuego();
 public  char getLetra();
 public  int getNroIntentos();
 public  String getPalabraEnProgreso();
 public  void setEstadoJuego(int estadoJuego);
 public  void setLetra(char letra);
 public  void leerMensaje(String mensaje);
 public  void imprimirMensajeEnPantalla();
 public String getPalabraActualGuionBajo();
 public  String mostrarEstado();
 public  void imprimirEntrada();
 public  void imprimirSalida();
}
 

Implementación de la Interfaz del Ahorcado:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.StringTokenizer;

public class ClienteAhorc implements ClienteAhorcadoint {
private static final int INACTIVO = 0;
private static final int JUGANDO = 1;
private static final int GANO = 2;
private static final int PERDIO = 3;
private static final int SolicJuegoNuevo = 4;
private static final int JuegoTerminado = 5;
private static final int puerto = 10028;
private static BufferedReader in = null;
private static DataOutputStream out = null;
private static Socket conexion;
private static int edoJuego = INACTIVO;
private static int intentRest = 6;
private static char letra = '*';
private static String palabraEnProgreso = "*";
private static Integer numMensaje = 0;
public static void main(String[] args) {
try {
     ClienteAhorc Ahcd = new ClienteAhorc();
     conexion = new Socket("192.168.15.132", 10028);
  BufferedReader entradaUsuario = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Iniciando Conexion con servidor...");
  in = new BufferedReader(new InputStreamReader(conexion.getInputStream()));
  out = new DataOutputStream(conexion.getOutputStream());
  System.out.println("Conexion realizada...");
  boolean jugando = true;
  while (jugando == true) {
      if (edoJuego == INACTIVO) {
       Ahcd. setEstadoJuego(SolicJuegoNuevo);
    Ahcd.enviarMensaje();
    } else {
       Ahcd.leerMensaje(in.readLine());
       if (edoJuego == JUGANDO) {
     Ahcd.imprimirMensajeEnPantalla();
     System.out.println("Ingrese una letra: ");
     Ahcd.setLetra(entradaUsuario.readLine().charAt(0));
     Ahcd.enviarMensaje();
     } else {
        if (edoJuego == GANO) {
        Ahcd. setEstadoJuego(JuegoTerminado);
        Ahcd.imprimirMensajeEnPantalla();
        jugando = false;
        System.out.println("¡Felicidades! Ganaste en el juego de Ahorcado");
        }
        if (edoJuego == PERDIO) {
         Ahcd. setEstadoJuego(JuegoTerminado);
         Ahcd.imprimirMensajeEnPantalla();
         jugando = false;
         System.out.println("Lastima, no te quedan mas intentos.\nHas perdido");
        }
      System.out.println("Juego terminado");
       }
    }
   }
 }catch (IOException ex) {
    System.out.println("Error de Entrada/Salida");
    System.out.println("No fue posible realizar la conexion, posiblemente el servidor este inactivo.");
    }
}

public  void enviarMensaje() {
  ClienteAhorc Ahcd = new ClienteAhorc();
  try {
     if (edoJuego == SolicJuegoNuevo) {
      out.writeBytes(Ahcd.crearMensajeRespuesta());
  } else {
   out.writeBytes(Ahcd.crearMensajeRespuesta());
   }
 }catch (IOException iOException) {
     System.out.println("Error al enviar el mensaje");
  }
}
 
public  String crearMensajeRespuesta() {
    return edoJuego + "#" + intentRest + "#" + letra + "#" + palabraEnProgreso + "#" +numMensaje + "\n";
 }

public  int getEstadoJuego() {
    return edoJuego;
 }

public  char getLetra() {
    return letra;
 }

public  int getNroIntentos() {
    return intentRest;
 }
public  String getPalabraEnProgreso() {
    return palabraEnProgreso;
 }
public  void  setEstadoJuego(int edoJuego) {
    ClienteAhorc.edoJuego = edoJuego;}
public  void setLetra(char letra) {
    ClienteAhorc.letra = letra;
 }
public  void leerMensaje(String mensaje) {
  StringTokenizer stk = new StringTokenizer(mensaje, "#");
  while (stk.hasMoreTokens()) {
      edoJuego = Integer.valueOf(stk.nextToken());
   intentRest = Integer.valueOf(stk.nextToken());
   letra = stk.nextToken().charAt(0);
   palabraEnProgreso = stk.nextToken();
   numMensaje = Integer.valueOf(stk.nextToken());
   }
 }
  
public  void imprimirMensajeEnPantalla() {
    ClienteAhorc Ahcd = new ClienteAhorc();
 System.out.println("^^^^^^^^^^^ Ahorcado ^^^^^^^^^^^");
 imprimirAhorcado(intentRest);
 System.out.println("\nPalabra actual: " + Ahcd.getPalabraActualGuionBajo());
 System.out.println("Intentos restantes: " + intentRest);
 }
public  String getPalabraActualGuionBajo() {
    String[] a = palabraEnProgreso.split("");
 String impr = "";
 for (int i = 0; i < a.length; i++) {
     impr += a[i] + " ";}
 return impr;
 }

public  String mostrarEstado() {
    if (edoJuego == 0) {
     return "INACTIVO";}
    else {
     if (edoJuego == 1) {
  return "JUGANDO";}
 else {
     if (edoJuego == 2) {
  return "GANO";}
 else {
     if (edoJuego == 3) {
  return "PERDIO";}
 else {
     if (edoJuego == 4) {
  return "SolicJuegoNuevo";}
 else {
     return "JuegoTerminado";}}}}}}

public  void imprimirEntrada() {
    ClienteAhorc Ahcd = new ClienteAhorc();
    String a = edoJuego + "#" + intentRest + "#" + letra + "#" + palabraEnProgreso +"#" + numMensaje;
 System.out.println("Leido por el cliente: " + a + "\n" + Ahcd.mostrarEstado());}

public  void imprimirSalida() {
    ClienteAhorc Ahcd = new ClienteAhorc();
    String a = edoJuego + "#" + intentRest + "#" + letra + "#" + palabraEnProgreso +"#" + numMensaje;
 System.out.println("Enviado por el cliente: " + a + "\n" + Ahcd.mostrarEstado());}
public  void imprimirAhorcado(int intentRest) {
    System.out.println(" ________________");
 if (intentRest < 1) {
  System.out.println("|   |");
  System.out.println("|   |");
  System.out.println("|   O");
  System.out.println("|  \\|/");
  System.out.println("|   |");
  System.out.println("|  /|\\");
  System.out.println("|");
  System.out.println("|");
  System.out.println("|");
  }
 else {
     if (intentRest < 2) {
  System.out.println("|   |");
  System.out.println("|   |");
  System.out.println("|   O");
  System.out.println("|  \\|/");
  System.out.println("|   |");
  System.out.println("|  /|");
  System.out.println("|");
  System.out.println("|");
  System.out.println("|");
   }
 else {
      if (intentRest < 3) {
  System.out.println("|   |");
  System.out.println("|   |");
  System.out.println("|   O");
  System.out.println("|  \\|/");
  System.out.println("|   |");
  System.out.println("|   |");
  System.out.println("|");
  System.out.println("|");
  System.out.println("|");
   }
 else {
      if (intentRest < 4) {
  System.out.println("|   |");
  System.out.println("|   |");
  System.out.println("|   O");
  System.out.println("|  \\|");
  System.out.println("|   |");
  System.out.println("|   |");
  System.out.println("|");
  System.out.println("|");
  System.out.println("|");
   }
 else {
      if (intentRest < 5) {
  System.out.println("|  |");
  System.out.println("|  |");
  System.out.println("|  O");
  System.out.println("|  |");
  System.out.println("|  |");
  System.out.println("|  |");
  System.out.println("|");
  System.out.println("|");
  System.out.println("|");
   }
 else {
      if (intentRest < 6) {
  System.out.println("|  |");
  System.out.println("|  |");
  System.out.println("|  O");
  System.out.println("|");
  System.out.println("|");
  System.out.println("|");
  System.out.println("|");
  System.out.println("|");
  System.out.println("|");
   }
 else {
      if (intentRest < 7) {
  System.out.println("|  |");
  System.out.println("|  |");
  System.out.println("|");
  System.out.println("|");
  System.out.println("|");
  System.out.println("|");
  System.out.println("|");
  System.out.println("|");
  System.out.println("|");
   }}}}}}}
 System.out.print("----------------------------");}
}