public interface InterfaceCliente extends java.rmi.Remote{ public void fim (String msg)throws java.rmi.RemoteException; } import java.rmi.*; import java.io.*; import java.net.MalformedURLException; public class Cliente extends java.rmi.server.UnicastRemoteObject implements InterfaceCliente{ public Cliente() throws java.rmi.RemoteException{ super(); } public void fim (String msg)throws java.rmi.RemoteException { System.out.println(msg); } public static void main(String[] args) { try { Cliente C = new Cliente (); InterfaceServidor S = (InterfaceServidor) Naming.lookup ("Clube"); double d; S.subscribe(C); while (true){ System.out.println("Quer doar um valor?"); d = Ler.umDouble(); S.donativo(d); System.out.println(" Valor resultante" + S.consulta() ); } } catch (RemoteException r){ System.out.println("Exception in server"+r.getMessage()); } catch (java.net.MalformedURLException u){ System.out.println("Exception in server - URL" ); } catch (NotBoundException b){ System.out.println("Exception in registry - Not bound" ); } } } public interface InterfaceServidor extends java.rmi.Remote { public void donativo (double valor) throws java.rmi.RemoteException; public double consulta () throws java.rmi.RemoteException; public void subscribe ( InterfaceCliente c ) throws java.rmi.RemoteException; } import java.rmi.*; import java.io.*; import java.util.*; class Servidor extends java.rmi.server.UnicastRemoteObject implements InterfaceServidor{ private static double valorAtingir, donativos; private static Vector socios; public Servidor() throws java.rmi.RemoteException{ super(); valorAtingir = 10000; donativos = 0; socios = new Vector(); } // remote methods public void donativo (double valor) throws java.rmi.RemoteException { donativos = donativos + valor; if (donativos >= valorAtingir) for (int i=0; i < socios.size(); i++){ InterfaceCliente c = (InterfaceCliente) socios.elementAt(i); } } } c.fim("Bem hajam, já chega"); } public double consulta () throws java.rmi.RemoteException { return donativos; } public void subscribe ( InterfaceCliente c ) throws java.rmi.RemoteException { socios.addElement(c); } public static void main(String[] args) { //System.setSecurityManager(new SecurityManager()); try { Servidor S = new Servidor (); Naming.rebind ("Clube",S); } catch (RemoteException r){ System.out.println("Exception in server"+r.getMessage()); } catch (java.net.MalformedURLException u){ System.out.println("Exception in server - URL" ); } public class Ler{ public static String umaString (){ String s = ""; try{ BufferedReader in = new BufferedReader ( new InputStreamReader (System.in), 1); s= in.readLine(); } catch (IOException e){ System.out.println("Erro ao ler fluxo de entrada."); } return s; } public static int umInt(){ while(true){ try{ return Integer.valueOf(umaString().trim()).intValue(); } catch(Exception e){ System.out.println("Não é um inteiro válido!!!"); } } } } public static double umDouble(){ while(true){ try{ return Double.valueOf(umaString().trim()).doubleValue(); } catch(Exception e){ System.out.println("Não é um double válido!!!"); } } }