Created
February 2, 2014 03:26
-
-
Save goldem64/8762635 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
**todo listo | |
** para compilar | |
javac *.java | |
rmic ConexionBanco | |
solo falta provarlo D: con suerte todo funcione bien | |
Clase banco | |
esta clase debe contener la lista de cuentas y una conexión remota y los cajeros y los usuarios que acceden a los cajeros | |
--lista | |
Clase conexión remota | |
*Esta clase es un hilo que accede a las cuentas remotas | |
**me comentan que se puede hacer con rmi por lo que intentare acerlo | |
**agregar | |
***Consulta remota | |
***Interfaz Consulta remota | |
--lista | |
Clase cajero este accederá a las cuentas locales y remotas | |
* creo que esta método es synchronized ya que solo son dos cajero por banco | |
--lista | |
clase cuenta | |
esta es la clase que debe asegurar la integridad, además de contar con las operaciones | |
--lista | |
clase Usuario | |
no estoy seguro si se trate de una clase o no, por que estos acceden | |
los cajeros y los cajeros a los as cuentas | |
** lo usare como clase para usarlo como hilos que representen varios | |
** usuarios | |
--lista |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Unicaribe | |
* Ingenieria en telematica | |
* Sistemas operativos distribuidos y de tiempo real | |
* Israel Navarrete Alvarado | |
*/ | |
import java.rmi.*; | |
import java.rmi.registry.*; | |
import java.util.Scanner;// | |
public class Banco{ | |
private static InterfazConexionBanco servidor; | |
private static Registry registro; | |
private static String direccionServidor = "192.168.1.71";// | |
private static String puertoServidor = "3232";// | |
private static void conectarseAlServidor() { | |
try { | |
registro = LocateRegistry.getRegistry(direccionServidor,(new Integer(puertoServidor)).intValue()); | |
servidor = (InterfazConexionBanco) (registro.lookup("ConexionBanco")); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} catch (NotBoundException e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
} | |
public static void main (String args[]) { | |
final int TOCU =10; | |
Cuenta[] cuenLoc = new Cuenta[TOCU]; | |
for (int i = 0; i < TOCU; i++) { | |
cuenLoc[i]= new Cuenta(100); | |
} | |
try { | |
ConexionBanco con = new ConexionBanco(cuenLoc,3232); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
try { | |
Thread.sleep(2000); | |
} catch (InterruptedException ie) { | |
ie.printStackTrace(); | |
} | |
conectarseAlServidor(); | |
Cajero cajero = new Cajero(cuenLoc,servidor); | |
int id=1,tipo,monto=0,local; | |
Scanner in = new Scanner(System.in); | |
while (id > 0){ | |
System.out.println("Tipo de operacion local(1) o remota(2)"); | |
local=in.nextInt(); | |
System.out.println("Id cuenta del 0 a "+TOCU); | |
id=in.nextInt(); | |
System.out.println("operacion rertiro(1), deposito(2), Consulta(3) "); | |
tipo=in.nextInt(); | |
if (tipo==1||tipo==2){ | |
System.out.println("monto para la operacion"); | |
monto=in.nextInt(); | |
} | |
if ((local>0&&local<3)&&(id>-1&&id<TOCU+1)&&(tipo>0&&tipo<4)&&(monto>0)) | |
cajero.realizaOp(id,tipo,monto,local); | |
else | |
System.out.println("verifica tus datos"); | |
System.out.println("para terminar -1"); | |
id=in.nextInt(); | |
} | |
in.close(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Unicaribe | |
* Ingenieria en telematica | |
* Sistemas operativos distribuidos y de tiempo real | |
* Israel Navarrete Alvarado | |
*/ | |
import java.rmi.*; | |
import java.rmi.registry.*; | |
public class Cajero { | |
private Cuenta[] cuenLoc; | |
private InterfazConexionBanco conexion; | |
public Cajero (Cuenta[] cuentas,InterfazConexionBanco servidor){ | |
this.cuenLoc=cuentas; | |
this.conexion=servidor; | |
} | |
public synchronized void realizaOp (int idCuen,//]Identificador cuenta | |
int trans,//Tipo de transacción | |
int monto,//monto de la tansaccion | |
int local){// si el una operacion local | |
if (local==1){ | |
System.out.println("Operacion Local"); | |
System.out.println("Id cuenta "+ idCuen); | |
System.out.println("Tipo de operacion"); | |
switch (trans) { | |
case 1: | |
System.out.println("Retiro"); | |
if (!cuenLoc[idCuen].retirar(monto)) | |
System.out.println("Saldo insuficuente"); | |
else | |
System.out.println("Retiro exitoso "+monto); | |
System.out.println("Saldo "+cuenLoc[idCuen].consultarSaldo()); | |
break; | |
case 2: | |
System.out.println("Deposito"); | |
cuenLoc[idCuen].depositar(monto); | |
System.out.println("Deposito exitoso "+monto); | |
System.out.println("Saldo "+cuenLoc[idCuen].consultarSaldo()); | |
break; | |
case 3: | |
System.out.println("Consulta"); | |
System.out.println("Saldo: "+cuenLoc[idCuen].consultarSaldo()); | |
break; | |
} | |
}else{ | |
System.out.println("Operacion remota"); | |
System.out.println("Id cuenta "+ idCuen); | |
System.out.println("Tipo de operacion"); | |
switch (trans) { | |
case 1: | |
System.out.println("Retiro"); | |
try { | |
if (conexion.realizaOp(idCuen,trans,monto+10)==-1) | |
System.out.println("Monto insufuciente"); | |
else | |
System.out.println("Retiro exitoso"); | |
} catch (RemoteException re) { | |
re.printStackTrace(); | |
} | |
break; | |
case 2: | |
System.out.println("Deposito"); | |
try { | |
conexion.realizaOp(idCuen,trans,monto-10); | |
} catch (RemoteException re) { | |
re.printStackTrace(); | |
} | |
break; | |
case 3: | |
System.out.println("Consulta"); | |
try { | |
System.out.println("Saldo: "+conexion.realizaOp(idCuen,trans,monto)); | |
} catch (RemoteException re) { | |
re.printStackTrace(); | |
} | |
break; | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Unicaribe | |
* Ingenieria en telematica | |
* Sistemas operativos distribuidos y de tiempo real | |
* Israel Navarrete Alvarado | |
*/ | |
import java.net.InetAddress; | |
import java.rmi.*; | |
import java.rmi.registry.*; | |
import java.rmi.server.*; | |
public class ConexionBanco extends UnicastRemoteObject implements InterfazConexionBanco { | |
private Cuenta[] cuenLoc; | |
private int estePuerto; | |
private String estaIP; | |
private Registry registro; | |
public ConexionBanco(Cuenta[] cuenLoc,int port ) throws RemoteException { | |
this.cuenLoc = cuenLoc; | |
try { | |
// obtener la direccion de este host. | |
estaIP = (InetAddress.getLocalHost()).toString(); | |
System.out.println(estaIP); | |
} catch (Exception e) { | |
throw new RemoteException("No se puede obtener la direccion IP."); | |
} | |
this.estePuerto = port; // asignar el puerto que se registra | |
try { | |
// crear el registro y ligar el nombre y objeto. | |
registro = LocateRegistry.createRegistry(estePuerto); | |
registro.rebind("ConexionBanco", this); | |
} catch (RemoteException e) { | |
throw e; | |
} | |
} | |
public synchronized int realizaOp(int idCuen,int trans,int monto){ | |
int val=0; | |
System.out.println("Operacion remota"); | |
System.out.println("Id cuenta "+ idCuen); | |
System.out.println("Tipo de operacion"); | |
switch (trans) { | |
case 1: | |
System.out.println("Retiro"); | |
if (!cuenLoc[idCuen].retirar(monto)){ | |
val=-1; | |
System.out.println("Saldo insuficuente"); | |
}else{ | |
System.out.println("Retiro exitoso "+monto); | |
} | |
System.out.println("Saldo "+cuenLoc[idCuen].consultarSaldo()); | |
break; | |
case 2: | |
System.out.println("Deposito"); | |
cuenLoc[idCuen].depositar(monto); | |
System.out.println("Deposito exitoso "+monto); | |
System.out.println("Saldo "+cuenLoc[idCuen].consultarSaldo()); | |
break; | |
case 3: | |
System.out.println("Cosulta de saldo"); | |
val=cuenLoc[idCuen].consultarSaldo(); | |
System.out.println("Saldo "+cuenLoc[idCuen].consultarSaldo()); | |
break; | |
} | |
return val; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Unicaribe | |
* Ingenieria en telematica | |
* Sistemas operativos distribuidos y de tiempo real | |
* Israel Navarrete Alvarado | |
*/ | |
public class Cuenta { | |
private int saldo; | |
public Cuenta (int saldo){ | |
//Se define un saldo inicial | |
this.saldo=saldo; | |
} | |
public synchronized boolean retirar (int monto) { | |
//Este metodo retira un cantidad del saldo, retoruna un TRUE si la operacion es exitosa | |
boolean flag; | |
if(monto<=saldo){ | |
saldo=saldo-monto; | |
flag=true; | |
}else{ | |
flag=false; | |
} | |
return flag; | |
} | |
public synchronized void depositar (int monto ) { | |
//Este agrega una monto a la cuenta | |
saldo=saldo+monto; | |
} | |
public synchronized int consultarSaldo () { | |
//retorna el saldo de la cuenta | |
return saldo; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Unicaribe | |
* Ingenieria en telematica | |
* Sistemas operativos distribuidos y de tiempo real | |
* Israel Navarrete Alvarado | |
*/ | |
import java.rmi.*; | |
public interface InterfazConexionBanco extends Remote{ | |
//Este es el metodo que implementará el servidor | |
int realizaOp(int idCuen,//Identificador cuenta | |
int trans,//Tipo de transacción | |
int monto)//moonto de la tanzacion | |
throws RemoteException; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Unicaribe | |
* Ingenieria en telematica | |
* Sistemas operativos distribuidos y de tiempo real | |
* Israel Navarrete Alvarado | |
*/ | |
public class Usuario extends Thread{ | |
private Cajero[] cajero; | |
private boolean end = true; | |
public Usuario (Cajero[] cajero){ | |
this.cajero=cajero; | |
} | |
public void end (){ | |
end = true; | |
} | |
public void run() { | |
while (end){ | |
cajero[(int)Math.random()*2].realizaOp((int)Math.random()*10,//Cuenta | |
(int)Math.random()*2+1 ,//Transacion | |
(int)Math.random()*10 ,//monto | |
(int)Math.random()*1);//local remota | |
try {wait();}catch (InterruptedException e) { } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gg