Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Cap 4
Cap 4
%!
'(
! "
%
#! $
&
%
1'
%16
2%
%
1/../
-
3 % /.0
2%
..02 %
%
8 9:08
1
!
; 1 %<51 =/1+
?(
3 % 450
2%
%
1/../
7
!
"
>1499:
>
?(
>
Modelo RPC
Stubs de cliente y servidor
Visin en Java es RMI
>
En Unix:
En Java:
Sockets.
CTJ (Communicating Threads for Java)
?(
>
Modelo General
SOLICITUD
CLIENTE
SERVIDOR
RESPUESTA
NUCLEO
NUCLEO
RED
?(
>
#
>
"
%
%
B
@ A $
%
&
%;
CB
P
U
E
R
T
O
CLIENTE
SERVIDOR
TCP/UDP
?(
>
App B
App A
Port xxx
Port yyy
App C
Port zzz
PROTOCOLO TCP/UDP
Port kkk
P
U
E
R
T
O
CLIENTE
SERVIDOR
Solicitud de conexin
CLIENTE
?(
P
U
E
R
T
O
P
U
E
R
T
O
SERVIDOR
PUERTO
>
D
A
%
7
%;
% %
7
%
%%
Accept()
Socket()
Socket creado y activo
?(
>
ServerSocket
"
%
%
&
"
% %
Socket
'%B
%
L.accept();
)%
%
%B
%
?(
C%
>
Socket
%
%%
%
%
"
%
Socket (direccin_IP, puerto);
?(
>
4
/
<
E
5
G
?(
7
2
& ServerSocket
& Socket
InputStream
%
%%
OutputStream
%%
F
%
>
&
%# B
import java.io.*;
import java.net.*;
public class SocketServidor
{
public static final int port = 8080;
public static void main (String [] args)
throws IOException
{
ServerSocket GenSocket = new ServerSocket(port);
System.out.println("El servidor esta escuchando
en: "+ GenSocket);
try{//espera solicitud conexion desde cliente...
Socket enchufe = GenSocket.accept();
} finally { System.out.println("se cierra la
conexin...");
GenSocket.close();
}
}//main
}//SocketServidor
4
/
<
E
5
?(
7
2
& Socket
InputStream
%%
OutputStream
%%
F
%
>
!
import java.io.*;
import java.net.*;
public class SocketCliente
{
public static void main (String [] args)
throws IOException
{
//llamando ordenador despacho de al lado...
InetAddress dir_IP =
InetAddress.getByName("150.214.72.96");
Socket enchufe = new Socket (dir_IP, 8080);
System.out.println("Datos socket creado:
"+enchufe);
}//main
}//SocketCliente
"
A
%
!@
%
% %
socket
D 1
& socket
socket
getInputStream()
getOutputStream()
% %
&%
!
(3
&
SocketChannel
?(
7
%&
F
% %
>
>
#$
"
'
()***+
%&
%
%
8
8
%
%
">
%
"%
3
read-only
%
% %
%
&
! >
3 F
%
B
7
"% #
%
%
8!$
?(
>
2%
A
>
% "%
3
7
%
?(
%rendezvous
>
Emisor Listo
(synchronized).
D%
3
%
%
% 3
%
Canal
;
&
Receptor Listo
>
,
send (B, mensaje)
Proceso A
rendezvous
Proceso B
receive (A, mensaje)
tiempo
?(
>
&
%
7;
!@
run()
&
&
%
%
%
Canal de Salida
Canal de Entrada
#-
",Process
>
",
7;
#
@
read >write
&
%
%
@
%
;
#
;$
H7 $ 3
%
H7
?(
>
import csp.lang.*;
import csp.lang.Process.*;
import csp.lang.Integer.*
class Producer implements Process{
ChannelOutput_of_Integer channel;
Integer object;
public Producer (ChannelOutput_of_Integer out){
channel = out;
object = new Integer();
}
public void run(){
object.value=100;
channel.write(object);
}
}? (
>
import csp.lang.*;
import csp.lang.Process.*;
import csp.lang.Integer.*
class Consumer implements Process{
ChannelInput_of_Integer channel;
Integer object;
public Consumer (ChannelInput_of_Integer out){
channel = in;
object = new Integer();
}
public void run(){
channel.read(object);
System.out.println(object);
}
}? (
>
import csp.lang.*;
import csp.lang.Process;
import csp.lang.Integer;
public class P_C_Main{
public static void main (String [] args){
Channel channel = new Channel();
Process modo = new Parallel(new Process[]
new Producer(channel),
new Consumer(channel));
modo.run();
}//main
}// P_C_Main
?(
>
' % (Channel_of_Boolean)+read/write
'> (Channel_of_Byte)+read/write
" (Channel_of_Char)+read/write
% (Channel_of_Double)+read/write
+%
(Channel_of_Float)+read/write
(Channel_of_Integer)+read/write
)
(Channel_of_Long)+read/write
" (Channel_of_Short)+read/write
7
(Channel_of_Reference)+read/write
& (Channel_of_Object)+read/write
( >(Channel_of_Any)+read/write
?(
>
#
Sequential (Process[] procesos)
Parallel (Process[] procesos)
PriParallel (Process[] procesos)
Con guardas:
Alternative
PriAlternative
?(
>
.
/
01
csp.io.linkdrivers.TCPIP ( UDP)
%
public TCPIP(remotehost, remoteport, TCPIP.tipo)
REALTIME
ASYNCHRONOUS
?(
% %
% %
>
import
import
import
Import
csp.lang.*;
csp.lang.Process;
csp.lang.Integer;
csp.io.lindrivers.TCPIP;
import
import
import
Import
csp.lang.*;
csp.lang.Process;
csp.lang.Integer;
csp.io.lindrivers.TCPIP;
En Java:
Objetos remotos. Serializacin
Paquete java.rmi
Automatizacin parcial: interfaz-compilador rmic
?(
>
%
%
func()
'
8
4 7
/
< KL
func(void)
{//codigo
.
}
%%
%
%
%
% %
%MN
7 H
C
'%B
%
7
%%
&
%
?(
>
%
%
%
callrpc
+
llamada
normal
rpcreturn
%%
RED
H%
return
H%
.
/
1
7
7
%
C
7
7O
rpcgen # P
?(
7
%
3
7
D$F
rmic #
7;
$
>
5
Especificacin Formal
Interface
Cliente
Ejecutable
C
O
M
P
I
L
A
R
?(
Servidor
Ejecutable
Precompilador
rpcgen o rmic
Resguardo
Stub
Ficheros
compartidos
Resguardo
Skeleton
Cliente
Representacin
de datos
Servidor
>
C
O
M
P
I
L
A
R
Generador
de
Resguardos
Especificacin Formal
Interface
Interfaz del
servidor+asa
create, read
delete, write
Read(f,d)
CONECTOR
SERVIDOR
CLIENTE
Envo de asa
>
%
7
7;
.x
7"
/*rand.x*/
program RAND_PROG{
version RAND_VER{
void inicia_aleatorio(long)=1;
double obtener_aleat(void)=2;
}=1;
}=0x3111111;
*
C
% 7"
>
%;
?(
$rpcgen rand.x
%
CB
%
Q
' %
xdr.h
>
32 3
2 $
Objeto
Cliente
Objeto
Servidor
JVM
JVM
?(
>
32
RPC
RMI
?(
>
'
Un objeto remoto es aqul que se ejecuta en una JVM diferente,
situada potencialmente en un host distinto.
RMI es la accin de invocar a un mtodo de la interfaz de un objeto
remoto.
//ejemplo de llamada a mtodo local
int dato;
dato = Suma (x,y);
//ejemplo de llamada a mtodo remoto (no completo)
IEjemploRMI1 ORemoto =
(IEjemploRMI1)Naming.lookup(//sargo:2005/EjemploRMI1);
ORemoto.Suma(x,y);
7!
32
Cliente
Servidor
Stub
Skeleton
Referencia
Remota
Referencia
Remota
Transporte
Transporte
?(
java.rmi.Naming
public static void bind(String name, Remote obj)
public static String[] list(String name)
public static Remote lookup(String name)
public static void rebind(String name, Remote obj)
public static void unbind(String name)
?(
>
%N
public interfaz
extends Remote
{signaturas mtodos}
implements
resultado
ref.metodo(param)
Implementacin
de la interfaz
Cliente
new
referencia a interfaz
6
lookup
?(
SERVIDOR
3"
"
bind rebind
>
implements
Definir Interfaz Remota
Implementar Interfaz
Remota
$ rmic .class
Skeleton
Stub
JVM
JVM
Implementar Servidor
Implementar Cliente
87
>
2 : 8 & 3:
6# 387=
?(
>
87
9:32 )< 2
2 6#7 >6
?(
>
2 : 2
2 6#7 >6
6# 387=/
3? :3
un RMI
87
9:32 @< 4
Fichero de
implementacin
de Interfaz
compilado
(Imp.class)
Compilador de RMI
$ rmic imp
#A / B
#:6
Fichero de
stub
(Imp_Stub.class)
Fichero de
skeleton
(Imp_Skel.class)
$ rmic EjemploRMI1
Es necesario que en la mquina remota donde se aloje el servidor se siten tambin
los ficheros de stub y skeleton.
Con todo ello disponible, se lanza el servidor llamando a JVM del host remoto.
En nuestro caso, el servidor remoto se activ en hercules.uca.es sobre el puerto
1099.
$ java EjemploRMI1 &
?(
>
87
!@
Naming.bind(Servidor",ORemoto);
para registrar el objeto remoto creado requiere que el servidor
de nombres est activo. El nombre de ese servidor es
rmiregistry
Dicho servidor se activa con start rmiregistry en Win32
Dicho servidor se activa con rmiregistry & en Unix.
Se puede indicar como parmetro adicional el puerto que
escucha.
Si no se indica, por defecto es el puerto 1099.
El parmetro puede ser el nombre de un host como en
Naming.bind(//sargo.uca.es:2005/Servidor",ORemoto);
?(
>
87
9:32 D<
6#
?(
>
2 :
6#
",Serializable
%;
1
%
H%
&
%
&
%&
Serializable #
%;
?(
%
% &
&
%&
%;
@
%
7
% $
>
7;
,
import java.io.*;
import java.util.*;
public class cuentabanca
implements Serializable
{ int ccc; float saldo; String Titular;
public cuentabanca(int codigo, float deposito, String
nombre)
{ccc = codigo; saldo = deposito; Titular = nombre;}
public String toString()
{return("codigo = "+ccc+" saldo= "+saldo+" titular=
"+Titular);}
}
",
import java.io.*;
import java.rmi.*;
public interface intefaz
extends Remote
{
boolean deposito(int codigo, float cantidad)
throws RemoteException;
boolean reintegro(int codigo, float cantidad)
RemoteException;
throws
-policytool
%
System.setSecurityManager(new RMISecurityManager());
&
%
>
)%
% @
& SecurityManager
setSecurityManager (clase System)
RMISecurityManager
&
% %
Policy
%
Policy.getPolicy()
Policy.setPolicy()
7&
7"
/
?(
"
%
%
3
3
7
% policytool
>
(&
% %
3
%
%> % &
%
7"
.policy
/* AUTOMATICALLY GENERATED ON Fri May 28
19:23:13 CEST 2004*/
/* DO NOT EDIT */
grant {
permission java.net.SocketPermission
"*:1024-65535", "connect,accept, listen,
accept, connect, listen, resolve";
permission java.net.SocketPermission "*:80",
"connect, accept";
permission java.security.AllPermission;
};
(
%
&
&
% %
3
Java Djava.security=fichero.policy
servidor|cliente
& RMISecurityManager > &
SetSecurityManager
% &
Microsoft DCOM
Jini de Sun
CORBA de OMG
EJB
?(
>
:3 7
3!
7 $
>
Elementos de CORBA
OMA (Object Management Architecture): es la
especificacin de la interoperabilidad entre
objetos
Core Object Model (Core Object Model):
establece los concepto bsicos sobre un objeto,
interfaz, operacin.
OMA Reference Architecture: define la
infraestructura de servicios y los mecanismos
subyacentes que permiten la interaccin entre
objetos.
ORB (Object Request Broker): es el canal de
comunicacin a travs del cual los objetos se
solicitan servicios con independencia de la
ubicacin fsica. Existen varias
(JavaIDL)
? implementaciones.
(
>
>
APPLICATION
INTERFACES
Bussines
Healthcare
Finance
Telecom
DOMAIN
INTERFACES
Compound
Object Linking
Help
MOF
COMMON
FACILITIES
SERVICIOS A
OBJETOS
Security
Time
Properties
Externalization
Cliente
ORB
?(
>
Invocacin Esttica
Servidor
Cliente
metodo(param)
rebObjeto.metodo(param)
IDL
Skeleton
IDL
Stub
marshalling
unmarshalling
ORB Core
Interface dependiente del ORB
Stub para cada tipo de objeto
Object
Adapter
>
JavaIDL
Tecnologa que integra CORBA en el lenguaje
Java
En java, $idltojava fichero.idl
A partir de J2SE v1.4 es $idlj
Genera el stub del cliente y el skeleton del
servidor (con -fall)+otros ficheros
Ambos automatizan las siguientes tareas (junto
con el ORB):
Ordenamiento y serializacin de parmetros (marshalling)
Generan la implementacin de la clase de interfaz
Registro y activacin de objetos
Localizacin y conexin (binding) a objetos
?(
>
Herramientas JavaIDL
idlj: compilador de IDL a Java
orbd: proceso demonio que contiene:
Boostrap Service
Servicio de Nombres Transitorios.
Servicio de Nombres Persistentes.
Un Gestor de Servidores
>
org.omg.CORBA
org.omg.CosNaming
org.omg.PortableServer
org.omg.CORBA.ORB
org.omg.PortableInterceptor
org.omg.DynamicAny
?(
>
Compilando IDL
Stubs
Cliente
Interfaz
IDL
Aplicacin
Cliente
-fclient
Compilador de IDL
($idltojava)
($idlj) en J2SE
-fserver
-fserver
A partir de J2SE
v1.4
-fall
Skeletons
Servidor
POA
Servidor
Aplicacin
Servidor
Implemen.
Servidor
?(
>
Servants
POA
POA Manager
ORB
El Servicio de Nombres
El servicio nombre guarda pares
<nombre, referencia a objeto>
Los nombres estn organizados
jerrquicamente (tree-type) en un S.N.
Servidor, crea un servant y lo registra en SN
asociando (bind) una referencia a objeto con un
nombre
Cliente pide al SN que a partir de un nombre le
d (resolve) una referencia a objeto CORBA
En JavaIDL el SN se activa ejecutando
tnameserv. Por defecto, escucha al puerto 900
No es persistente y est derogado
A partir de J2SE 1.4 orbd proporciona
persistencia si se desea
resolve(A)
Cliente
Servidor
Jeraqua de
Nombres
bind(x, ref)
x
ref
NamingContext ncRef =
NamingContext ncRef =
NamingContextHelper.narrow(refObj); NamingContextHelper.narrow(refObj);
NameComponent nc = new
NameComponent nc = new
NameComponent("Impresor Remoto",
NameComponent("Impresor Remoto", "");
"");
NameComponent [] ruta ={nc};
NameComponent [] ruta = { nc };
HolaMundoDistribuido ObjImpresor =
ncRef.rebind(ruta, IRem);
HolaMundoDistribuidoHelper.narrow(ncR
ef.resolve(ruta));
'
?(
>
2Utilizando