Middleware de Aplicações Distribuídas • Resolução dos Exercícios 1.1, 1.2 e 1.4 // Converter.java import java.rmi.*; public interface Converter extends Remote { double eurotoesc(double euro) throws RemoteException; double esctoeuro(double esc) throws RemoteException; int [] testbw(int [] m) throws RemoteException; // 1.4 } // ConverterServer.java import java.rmi.*; import java.rmi.server.*; public class ConverterServer extends RemoteObject implements Converter { public ConverterServer() throws RemoteException { super(); } public double esctoeuro(double esc) throws RemoteException { return (esc/200.482); } public double eurotoesc(double euro) throws RemoteException { return (euro*200.482); } public int[] testbw(int[] b) throws RemoteException { // 1.4 return(b); } } Arquitecturas Paralelas I 13 © João Luís Sobral 2002 Middleware de Aplicações Distribuídas • Resolução dos Exercícios 1.1, 1.2 e 1.4 (continuação) // ConverterServerApp.java import java.io.*; import javax.naming.*; import javax.rmi.PortableRemoteObject; public class ConverterServerApp { public static void main(String args[]) { try { ConverterServer cs = new ConverterServer(); PortableRemoteObject.exportObject(cs); Context ctx = new InitialContext(); ctx.rebind("ConverterServer",cs); // Wait for shutdown BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.println("Type EXIT to shutdown the server"); if ("EXIT".equals(rdr.readLine())) { break; } } ctx.unbind("ConverterServer"); PortableRemoteObject.unexportObject(cs); } catch(Exception e) { e.printStackTrace(); } } } Arquitecturas Paralelas I 14 © João Luís Sobral 2002 Middleware de Aplicações Distribuídas • Resolução dos Exercícios 1.1, 1.2 e 1.4 (continuação) // ConverterClient.java import javax.rmi.PortableRemoteObject; import javax.naming.*; import java.util.*; public class ConverterClient { public static void main(String args[]) { try { double cnti, cntf; Converter c = new ConverterServer(); cnti = new Date().getTime(); for(int i=0; i<1000000; i++) { c.esctoeuro(200); } cntf = new Date().getTime(); // 1.3 System.out.println("Time to local call " +(cntf-cnti)/1000 + "us"); cnti = new Date().getTime(); // Obtain a reference to that remote object Context ctx = new InitialContext(); Converter cs = (Converter) PortableRemoteObject.narrow( ctx.lookup("ConverterServer"), Converter.class); cntf = new Date().getTime(); // 1.3 System.out.println("Time to lookup " + (cntf-cnti)*1000+ "us"); // Display numbers System.out.println("Amount is: " + args[0]); double d1 = Double.valueOf(args[0]).doubleValue(); cnti = new Date().getTime(); // 1.3 cs.esctoeuro(d1); cntf= new Date().getTime(); System.out.println("Time to remote call "+(cntf-cnti)*1000 + "us"); // Invoke remote method and display result System.out.println("The result in EURO is: " + cs.esctoeuro(d1)); System.out.println("The result in ESC is: " + cs.eurotoesc(d1)); // test network b/w // 1.4 for (int i=1; i<10000000; i=i*10) { // up to 4 MB int[] bt = new int[i]; cnti = new Date().getTime(); bt = cs.testbw(bt); cntf = new Date().getTime(); System.out.print("Time to send " +i +" bytes "+(cntf-cnti)+"ms"); System.out.println("(" + 2*i*4/(countf-counti) + " KByte/s)"); } } catch(Exception ex) { ex.printStackTrace(); } } } Arquitecturas Paralelas I 15 © João Luís Sobral 2002