MobileTracker: Ejemplo de implementación con RMI · ... (long time, double x, double y, double z)...

12
Héctor Pérez PROGRAMACION DISTRIBUIDA MobileTracker: Ejemplo de implementación con RMI MobileTracker: Especificación RCSD: J.M. Drake y Héctor Pérez 2 Torre de control Coche A Coche B Telemetría 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 del coche ejecuta el cliente Mobile que obtiene los datos de telemetría propios y los registra en el servidor Follower 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 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) Circuito del Jarama 11/05/2015

Transcript of MobileTracker: Ejemplo de implementación con RMI · ... (long time, double x, double y, double z)...

Héctor Pérez

PROGRAMACION DISTRIBUIDA

MobileTracker:

Ejemplo de implementación

con RMI

MobileTracker: Especificación

RCSD: J.M. Drake y Héctor Pérez

2

Torre de control

Coche A

Coche B

Telemetría

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 del coche ejecuta el cliente Mobile que obtiene los datos de telemetría propios y los registra en el servidor Follower

El computador de la torre de control también contiene el rmiRegistry, en el que se registra el servidor Followercon la clave “JaramaFollower”

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)Circuito del Jarama

11/05/2015

MobileTracker: Diseño

11/05/2015RCSD: J.M. Drake y Héctor Pérez

3

MobileTracker: Ejecución

RCSD: J.M. Drake y Héctor Pérez

4

«active»

mobile:Mobile

putPosition()

«active»

«protected»

buffer:Buffer

follower:Follower

MobileProcessor FollowerProcessor

Ethernet

RMI Middleware RMI Middleware

rmiRegistry

bind()

11/05/2015

Concurrente«active» «active»«protected»

mobile:Mobile buffer:Buffer follower:FollowerputPosition()

Distribuida

MobileTracker: MobilePartition

RCSD: J.M. Drake y Héctor Pérez

5

MobileRMILauncher main()MobilePartition

MobileRMILauncher«main»

+ main(args:String[])

Mobile«active»

+ Mobile(buff:RemoteBuffer)

+ run()

11/05/2015

Se interpretan los argumentosfollowerName= args[0];registryIP=args[1];

Se obtiene el acceso al rmiRegistry

registry = LocateRegistry.getRegistry(RegistryIP);

Se obtiene la referencia remota del servidor

buffer=(BufferRemote) registry.lookup(FollowerName);

Se construye el objeto de negocio

new Mobile(buffer);

MobileTracker: FollowerPartition

RCSD: J.M. Drake y Héctor Pérez

6

FollowerPartition

FollowerRMILauncher«main»

+ main(args:String[])

Follower«active»

+ Follower(buff:RemoteBuffer)

+ run()

Buffer«protected»

+ Buffer()

+ putPosition()

+ getPosition()

+ finalize()

BufferRemote

«remoteInterface»

+ putPosition()

buff

11/05/2015

FollowerRMILauncher main()

Se construyen los objetos de negocio

buffer=new Buffer();

follower=new Follower(buffer);

Se invoca el servicio requeridoSe obtiene el outpustream

Se invoca el servicio requeridoSe obtiene el outpustream

Se crea el rmiRegistry

registry = LocateRegistry.createRegistry(1099);

Se registra en el rmiRegistry el servidor

registry.bind("JaramaFollower", buffer);

Estructura de paquetes

RCSD: J.M. Drake y Héctor Pérez

grant {

permission java.security.AllPermission;

};

grant{

permission java.security.AllPermission;

};

11/05/2015

Código fuente (1/5): Interfaz remota

11/05/2015RCSD: J.M. Drake y Héctor Pérez

8

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;

}

Código fuente (2/5): MobileRMILauncher (cliente)

11/05/2015RCSD: J.M. Drake y Héctor Pérez

9

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]; // Argumento [0] Nombre de registro del servidor

String RegistryIP = args[1]; // Argumento [1] IP del registro RMI (puerto estándar 1099)

Registry registry = null; // Referencia el registro donde buscar al servidor

BufferRemote buffer = null; // 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

}

}

Código fuente (3/5): Mobile (cliente)

11/05/2015RCSD: J.M. Drake y Héctor Pérez

10

package mobilePartitionRMI;

import java.rmi.RemoteException;

import java.util.LinkedList;

import common.BufferRemote;

public class Mobile extends Thread { // La clase es activa

BufferRemote buffer; // 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();}

. . .

}

}

Código fuente (4/5): FollowerRMILauncher (servidor)

11/05/2015RCSD: J.M. Drake y Héctor Pérez

11

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");

}

}

Código fuente (5/5): Buffer (servidor)

RCSD: J.M. Drake y Héctor Pérez

12

package followerPartition;

import java.rmi.*;

import java.util.LinkedList;

import common.*;

public class Buffer extends UnicastRemoteObject implements BufferRemote {

protected Buffer() throws RemoteException { // Constructor

super();

}

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) {}

}

}

11/05/2015

MobileTracker: Gestor de seguridad

! Debemos configurar las propiedades de la JVM de acuerdo alas políticas de seguridad establecidas

! En caso necesario, también debería activarse el gestor deseguridad en el código

11/05/2015RCSD: José M. Drake y Héctor Pérez

13

MobileTracker: Variaciones (1/4)

¿Cómo implementar el ejemplo si Follower se

ejecutara en otro nodo (cliente)?

11/05/2015RCSD: José M. Drake y Héctor Pérez

14

«active» «active»«protected»mobile:Mobile buffer:Buffer follower:Follower

putPosition()getPosition()

finalize()

MobilePartition BufferPartition FollowerPartition

Variaciones: Follower ejecutado en otro nodo (1/3)

11/05/2015RCSD: J.M. Drake y Héctor Pérez

15

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;

}

Variaciones: Follower ejecutado en otro nodo (2/3)

11/05/2015RCSD: J.M. Drake y Héctor Pérez

16

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;

}

}

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 invocacionesremotas

Nueva clase principal para el servidor de Buffer

" BufferRMILauncher

Nueva clase principal para el cliente Follower

" FollowerRMILauncher

11/05/2015RCSD: J.M. Drake y Héctor Pérez

17

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?

11/05/2015RCSD: José M. Drake y Héctor Pérez

18

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?

11/05/2015RCSD: José M. Drake y Héctor Pérez

19

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?

11/05/2015RCSD: José M. Drake y Héctor Pérez

20

Variaciones: Buffer exportado explícitamente (1/3)

11/05/2015RCSD: J.M. Drake y Héctor Pérez

21

FollowerPartition

Se invoca el servicio requeridoSe obtiene el outpustream

FollowerRMILauncher main()

FollowerRMILauncher«main»

+ main(args:String[])

Follower«active»

+ Follower(buff:RemoteBuffer)

+ run()

Buffer«protected»

+ Buffer()

+ putPosition()

+ getPosition()

+ finalize()

BufferRemote

«remoteInterface»

+ putPosition()

buff

Se invoca el servicio requeridoSe obtiene el outpustream

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);

Se crea el rmiRegistry

registry = LocateRegistry.createRegistry(1099);

Se registra en el rmiRegistry el servidor

registry.bind("JaramaFollower", bufferRemote);

Variaciones: Buffer exportado explícitamente (2/3)

11/05/2015RCSD: J.M. Drake y Héctor Pérez

22

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");

}

}

Variaciones: Buffer exportado explícitamente (3/3)

RCSD: J.M. Drake y Héctor Pérez

23

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) {}

}

}

11/05/2015

Bibliografía

Java Oracle Documentation

" Única fuente de información actualizada

Java RMI by William Grosso, O'Reilly, 2001

11/05/2015RCSD: José M. Drake y Héctor Pérez

24