Download PROGRAMACION DISTRIBUIDA MobileTracker: Ejemplo de
Document related concepts
no text concepts found
Transcript
PROGRAMACION DISTRIBUIDA MobileTracker: Ejemplo de implementación con RMI Héctor Pérez 2 RCSD: J.M. Drake y Héctor Pérez 11/05/2015 MobileTracker: Especificación El computador de la torre de control ejecuta el servicio Follower que registra los datos telemétricos que le envían los clientes Mobile El computador de la torre de control también contiene el rmiRegistry, en el que se registra el servidor Follower con la clave “JaramaFollower” El computador del coche ejecuta el cliente Mobile que obtiene los datos de telemetría propios y los registra en el servidor Follower Torre de control Telemetría Coche A Coche B Circuito del Jarama El computador del coche conoce el IP del computador de la torre de control y la clave con la que se registra en el rmiRegistry. (La recibe como argumento en el lanzamiento) 3 RCSD: J.M. Drake y Héctor Pérez 11/05/2015 MobileTracker: Diseño 4 RCSD: J.M. Drake y Héctor Pérez MobileTracker: Ejecución Concurrente «active» putPosition() mobile:Mobile «protected» MobileProcessor «active» «active» buffer:Buffer follower:Follower FollowerProcessor putPosition() «protected» buffer:Buffer mobile:Mobile «active» bind() Distribuida follower:Follower rmiRegistry RMI Middleware RMI Middleware Ethernet 11/05/2015 5 RCSD: J.M. Drake y Héctor Pérez 11/05/2015 MobileTracker: MobilePartition MobileRMILauncher main() MobilePartition Se interpretan los argumentos followerName= args[0]; registryIP=args[1]; «main» MobileRMILauncher + main(args:String[]) Se obtiene el acceso al rmiRegistry registry = LocateRegistry.getRegistry(RegistryIP); Se obtiene la referencia remota del servidor «active» Mobile buffer=(BufferRemote) registry.lookup(FollowerName); + Mobile(buff:RemoteBuffer) + run() Se construye el objeto de negocio new Mobile(buffer); 6 RCSD: J.M. Drake y Héctor Pérez 11/05/2015 MobileTracker: FollowerPartition FollowerPartition «main» FollowerRMILauncher FollowerRMILauncher main() + main(args:String[]) Se construyen los objetos de negocio buffer=new Buffer(); follower=new Follower(buffer); «remoteInterface» BufferRemote «active» Follower + putPosition() + Follower(buff:RemoteBuffer) + run() Se Se obtiene el el outpustream invoca servicio requerido Se crea el rmiRegistry registry = LocateRegistry.createRegistry(1099); «protected» Buffer Se Se obtiene outpustream invoca el servicio requerido Se registra en elelrmiRegistry el servidor registry.bind("JaramaFollower", buffer); + Buffer() + putPosition() + getPosition() + finalize() buff RCSD: J.M. Drake y Héctor Pérez 11/05/2015 Estructura de paquetes grant { permission java.security.AllPermission; }; grant{ permission java.security.AllPermission; }; 8 RCSD: J.M. Drake y Héctor Pérez 11/05/2015 Código fuente (1/5): Interfaz remota package common; import java.rmi.Remote; // Interfaz remota que define los métodos del servidor que son accesibles por RMI public interface BufferRemote extends Remote { // Método del servidor Follower accesible remotamente boolean putPosition (long time, double x, double y, double z) throws java.rmi.RemoteException; } 9 RCSD: J.M. Drake y Héctor Pérez 11/05/2015 Código fuente (2/5): MobileRMILauncher (cliente) package mobilePartitionRMI; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import common.BufferRemote; public class MobileRMILauncher { public static void main(String[] args) { String FollowerName = args[0]; String RegistryIP = args[1]; Registry registry = null; BufferRemote buffer = null; // // // // Argumento [0] Nombre de registro del servidor Argumento [1] IP del registro RMI (puerto estándar 1099) Referencia el registro donde buscar al servidor Referencia remota (stub) del servidor try { // Se localiza el Registry en el computador que se ha pasado como argumento registry = LocateRegistry.getRegistry(RegistryIP); } catch (RemoteException re){ System.out.println("No se ha podido localizar el rmiRegistry local"); System.exit(-1);} try { // Se obtiene del Registry la referencia remota al servidor en base a la clave FollowerName buffer = (BufferRemote) registry.lookup(FollowerName); } catch(Exception e){ System.out.println("El Follower de nombre "+FollowerName+" no está registrado"); System.exit(-1);} new Mobile(buffer); // Se construye el objeto Mobile al que se pasa la referencia remota como parámetro } } 10 RCSD: J.M. Drake y Héctor Pérez Código fuente (3/5): Mobile (cliente) package mobilePartitionRMI; import java.rmi.RemoteException; import java.util.LinkedList; import common.BufferRemote; public class Mobile extends Thread { BufferRemote buffer; // La clase es activa // Referencia remota al servidor public Mobile(BufferRemote buff){ // Se le pasa la referencia remota en el constructor this.buffer = buff; start(); } . . . public void run(){ // Actividad del móvil . . . try { // Interfaz remota if (buffer.putPosition(pos.time,pos.x,pos.y,pos.z)) { pendingPos.removeFirst(); } else {break;} } catch (RemoteException re){ // Tratar el posible error en el acceso remoto System.out.println("Se ha producido una RemoteException en Mobile"); turnOff();} . . . } } 11/05/2015 11 RCSD: J.M. Drake y Héctor Pérez 11/05/2015 Código fuente (4/5): FollowerRMILauncher (servidor) package followerPartition; import java.rmi.*; public class FollowerRMILauncher { public static void main(String[] args) { Buffer buffer = null; // Se declaran los objetos de negocio Follower follower = null; try { buffer = new Buffer(); // Se exporta automáticamente follower = new Follower(buffer); } catch(RemoteException re1){. . .} // Se crea el rmiRegistry en el computador local (uso del puerto estándar 1099) Registry registry = null; try { registry = LocateRegistry.createRegistry(1099); } catch(RemoteException re2){. . . } try { // Se registra la ref. remota con el nombre "JaramaFollower” registry.bind("JaramaFollower", buffer); } catch (Exception e){. . .} System.out.println("EL SERVIDOR JARAMAFOLLOWER ESTÁ INSTALADO"); } } 12 RCSD: J.M. Drake y Héctor Pérez 11/05/2015 Código fuente (5/5): Buffer (servidor) package followerPartition; import java.rmi.*; import java.util.LinkedList; import common.*; public class Buffer extends UnicastRemoteObject implements BufferRemote { protected Buffer() throws RemoteException { super(); } // Constructor private static final long serialVersionUID = 1L; . . . // Declaración del método remoto synchronized public boolean putPosition(long time, double x, double y, double z) {. . .} // Declaración de métodos no remotos synchronized public Position[] getPosition() {. . .} // Método que finaliza el servidor protected void finalize(){ try { UnicastRemoteObject.unexportObject(this, true); } catch (NoSuchObjectException e) {} } } 13 RCSD: José M. Drake y Héctor Pérez 11/05/2015 MobileTracker: Gestor de seguridad ! Debemos configurar las propiedades de la JVM de acuerdo a las políticas de seguridad establecidas ! En caso necesario, también debería activarse el gestor de seguridad en el código 14 RCSD: José M. Drake y Héctor Pérez 11/05/2015 MobileTracker: Variaciones (1/4) ¿Cómo implementar el ejemplo si Follower se ejecutara en otro nodo (cliente)? «active» mobile:Mobile putPosition() «protected» getPosition() buffer:Buffer finalize() MobilePartition BufferPartition «active» follower:Follower FollowerPartition 15 RCSD: J.M. Drake y Héctor Pérez 11/05/2015 Variaciones: Follower ejecutado en otro nodo (1/3) package common; import java.rmi.Remote; // Interfaz remota que define los métodos del servidor que son accesibles por RMI public interface BufferRemote extends Remote { // Método del servidor Follower accesible remotamente boolean putPosition (long time, double x, double y, double z) throws java.rmi.RemoteException; public Position[] getPosition() throws java.rmi.RemoteException; void finalize() throws java.rmi.RemoteException; } 16 RCSD: J.M. Drake y Héctor Pérez Variaciones: Follower ejecutado en otro nodo (2/3) package common; import java.io.Serializable; public class Position implements Serializable { public long time; public double x; public double y; public double z; // Constructor public Position(long t,double x,double y,double z){ this.time=t; this.x=x; this.y=y; this.z=z; } } 11/05/2015 17 RCSD: J.M. Drake y Héctor Pérez 11/05/2015 Variaciones: Follower ejecutado en otro nodo (3/3) Modificaciones en la clase Follower " Invocaciones de los métodos remotos a través del stub " Manejo de excepciones en las nuevas invocaciones remotas Nueva clase principal para el servidor de Buffer " BufferRMILauncher Nueva clase principal para el cliente Follower " FollowerRMILauncher 18 RCSD: José M. Drake y Héctor Pérez 11/05/2015 MobileTracker: Variaciones (2/4) ¿Cómo implementar el ejemplo si Follower se ejecutara en otro nodo (cliente)? ¿Y si fuera necesario restringir el acceso a un conjunto de coches concreto? 19 RCSD: José M. Drake y Héctor Pérez 11/05/2015 MobileTracker: Variaciones (3/4) ¿Cómo implementar el ejemplo si Follower se ejecutara en otro nodo (cliente)? ¿Y si fuera necesario restringir el acceso a un conjunto de coches concreto? ¿Cambiaría la implementación si tuviéramos que añadir otro servidor RMI en el nodo? 20 RCSD: José M. Drake y Héctor Pérez 11/05/2015 MobileTracker: Variaciones (4/4) ¿Cómo implementar el ejemplo si Follower se ejecutara en otro nodo (cliente)? ¿Y si fuera necesario restringir el acceso a un conjunto de coches concreto? ¿Cambiaría la implementación si tuviéramos que añadir otro servidor RMI en el nodo? ¿Cómo exportar de forma explícita un objeto remoto? 21 RCSD: J.M. Drake y Héctor Pérez 11/05/2015 Variaciones: Buffer exportado explícitamente (1/3) FollowerPartition FollowerRMILauncher main() «main» FollowerRMILauncher Se construyen los objetos de negocio buffer=new Buffer(); follower=new Follower(buffer); Se registra el objeto remoto en el RMI bufferRemote =(BufferRemote) UnicastRemoteObject.exportObject(buffer,0); obtiene el el outpustream invoca servicio requerido Se creaSeelSe rmiRegistry registry = LocateRegistry.createRegistry(1099); Se Se obtiene outpustream invoca el servicio requerido Se registra en elelrmiRegistry el servidor registry.bind("JaramaFollower", bufferRemote); + main(args:String[]) «remoteInterface» BufferRemote «active» Follower + putPosition() + Follower(buff:RemoteBuffer) + run() «protected» Buffer buff + Buffer() + putPosition() + getPosition() + finalize() 22 RCSD: J.M. Drake y Héctor Pérez Variaciones: Buffer exportado explícitamente (2/3) package followerPartition; import java.rmi.*; import java.rmi.server.UnicastRemoteObject; import common.*; public class FollowerRMILauncher { public static void main(String[] args) { Buffer buffer = new Buffer(); // Se crea los objetos de negocio Follower follower = new Follower(buffer); // Se crea la ref. remota registrando el objeto Buffer en el RMI BufferRemote bufferRemote = null; try { bufferRemote =(BufferRemote)UnicastRemoteObject.exportObject(buffer,0); } catch(RemoteException re){. . .} // Se crea el rmiRegistry en el computador local (haciendo uso del puerto estándar 1099) Registry registry = null; try { registry = LocateRegistry.createRegistry(1099); } catch(RemoteException re){. . . } // Se registra la ref. remota con el nombre "JaramaFollower” try { registry.bind("JaramaFollower", bufferRemote); } catch (Exception e){. . .} System.out.println("EL SERVIDOR JARAMAFOLLOWER ESTÁ INSTALADO"); } } 11/05/2015 23 RCSD: J.M. Drake y Héctor Pérez 11/05/2015 Variaciones: Buffer exportado explícitamente (3/3) package followerPartition; import java.rmi.NoSuchObjectException; import java.rmi.server.UnicastRemoteObject; import java.util.LinkedList; import common.*; public class Buffer implements BufferRemote { // Clase del servidor que ofrece la interfaz remota . . . // Declaración del método remoto synchronized public boolean putPosition(long time, double x, double y, double z) {. . .} // Declaración de métodos no remotos synchronized public Position[] getPosition() {. . .} // Método que finaliza el servidor protected void finalize(){ try { UnicastRemoteObject.unexportObject(this, true); } catch (NoSuchObjectException e) {} } } 24 RCSD: José M. Drake y Héctor Pérez Bibliografía Java Oracle Documentation " Única fuente de información actualizada Java RMI by William Grosso, O'Reilly, 2001 11/05/2015