02c - Σδυ50 - Οσσ3 - Rmi

download 02c - Σδυ50 - Οσσ3 - Rmi

of 38

Transcript of 02c - Σδυ50 - Οσσ3 - Rmi

  • 7/25/2019 02c - 50 - 3 - Rmi

    1/38

    RMI

    3

  • 7/25/2019 02c - 50 - 3 - Rmi

    2/38

    ,

    2

  • 7/25/2019 02c - 50 - 3 - Rmi

    3/38

    RMI

    ,

    ,

    (remotemethod invocation RMI)

    3

  • 7/25/2019 02c - 50 - 3 - Rmi

    4/38

    RMI

    4

  • 7/25/2019 02c - 50 - 3 - Rmi

    5/38

    RMI

    5

  • 7/25/2019 02c - 50 - 3 - Rmi

    6/38

    RMI

    (), ()

    ,

    -, :

    6

  • 7/25/2019 02c - 50 - 3 - Rmi

    7/38

    RMI

    (Retry Request Message):

    , ,

    (Duplicate Filtering):

    , ,

    (Retransmission ofResults): history ,

    , 7

  • 7/25/2019 02c - 50 - 3 - Rmi

    8/38

    Invocation Semantics

    Maybe invocation semantics: , , . . , .

    At-Least-Once Invocation Semantics: , , . , . (.. ).

    At-Most-Once Invocation Semantics: , , . , .

    8

  • 7/25/2019 02c - 50 - 3 - Rmi

    9/38

    RMI

    RMI compiler Stub Proxy

    Skeleton

    Remote Method Invocation (RMI)

  • 7/25/2019 02c - 50 - 3 - Rmi

    10/38

    Client Server

    Stub SkeletonNetwork

    (marshalling)

    (unmarshalling)

    server

    server

    Stubs Skeletons

  • 7/25/2019 02c - 50 - 3 - Rmi

    11/38

    interfaces java.rmi RMI compiler

    rmic command line compiler JDK

    rmic [full class name including packages] :rmic RMITest

    :RMITest_Stub

    RMITest_Skel

    -keep stub skeleton

    Java RMI

  • 7/25/2019 02c - 50 - 3 - Rmi

    12/38

    To RMI : (remote objects)

    by reference (serializable objects)

    JVMs

    by value

    by value

    Remote

    Serializable

    (by value vs. by reference)

  • 7/25/2019 02c - 50 - 3 - Rmi

    13/38

    Java RMI

    5.16

    To Shape remote interface remote ( 2)

    GraphicalObject serializable ( 1) 13

  • 7/25/2019 02c - 50 - 3 - Rmi

    14/38

    (naming service)

    RMIregistry remote

    java.rmi.Naming

    registry URL-style ://computerName:portNumber/human-readable-objectName

    computerName: RMIregistry

    portNumber: RMIregistry

    human-readable-objectName:

    Java RMIregistry

  • 7/25/2019 02c - 50 - 3 - Rmi

    15/38

    RMIregistry (Naming class)

    15

  • 7/25/2019 02c - 50 - 3 - Rmi

    16/38

    Java RMI client

    O client ShapeListinterface ( 1)

    allShapes(

    2) 16

  • 7/25/2019 02c - 50 - 3 - Rmi

    17/38

    RMISecurityManager

    main client server security manager RMIclient/server

    O default security manager RMISecurityManager

    ,

    security manager RMISecurityManager

    17

  • 7/25/2019 02c - 50 - 3 - Rmi

    18/38

    Policy

    ..

    , , sockets..

    , , , , ( )

    policy SecurityManager defaultjava.home/lib/security/java.policy

    java.net.SocketPermission policy

    18

  • 7/25/2019 02c - 50 - 3 - Rmi

    19/38

    Policy

    grant {

    permission java.security.AllPermission;

    };

    grant {

    // Allow to connect to any port above 1024

    permission java.net.SocketPermission "localhost:1024-", "connect, resolve";

    // Allow to accept to any port above 1024

    permission java.net.SocketPermission "localhost:1024-", "accept, resolve";};

    19

    -Djava.security.manager -Djava.security.policy = myRMIapp.policy

  • 7/25/2019 02c - 50 - 3 - Rmi

    20/38

    Java RMI 1

    ,

    java.rmi.Remote java.rmi.RemoteException

    (servant class)

    java.rmi.UnicastRemoteObject

    20

  • 7/25/2019 02c - 50 - 3 - Rmi

    21/38

    Java RMI 2

    (server skeleton) () (client stub)

    javac

    RMI, rmic

    21

  • 7/25/2019 02c - 50 - 3 - Rmi

    22/38

    Java RMI 3

    ,

    22

  • 7/25/2019 02c - 50 - 3 - Rmi

    23/38

    Marshalling/Unmarshalling

    ( ,)

    RMI vs. Sockets

    RMI

  • 7/25/2019 02c - 50 - 3 - Rmi

    24/38

    Java RMI :Shared Whiteboard

    Shared Whiteboard = :, , , ..

    24

  • 7/25/2019 02c - 50 - 3 - Rmi

    25/38

    server

    version number (int) whiteboard

    version number server, version

    number 25

  • 7/25/2019 02c - 50 - 3 - Rmi

    26/38

    Java RMI

    To Shape remote interface

    remote ( 2)

    GraphicalObject serializable ( 1)

    26

  • 7/25/2019 02c - 50 - 3 - Rmi

    27/38

    GraphicalObject class

    : , ,line color, fill color

    /

    27

  • 7/25/2019 02c - 50 - 3 - Rmi

    28/38

    ShapeList

    28

    import java.rmi.*;import java.rmi.server.UnicastRemoteObject;import java.util.Vector;

    public class ShapeListServant extends UnicastRemoteObject implements ShapeList {private Vector theList; // contains the list of Shapes 1private int version;

    public ShapeListServant()throws RemoteException{...}public Shape newShape(GraphicalObject g) throws RemoteException { 2

    version++;Shape s = new ShapeServant( g, version);theList.addElement(s); 3return s;

    }public Vector allShapes()throws RemoteException{...}public int getVersion() throws RemoteException { ... }

    }

  • 7/25/2019 02c - 50 - 3 - Rmi

    29/38

    Java RMI Server

    29

    import java.rmi.*;public class ShapeListServer{

    public static void main(String args[]){System.setSecurityManager(new RMISecurityManager());

    try{ShapeList aShapeList = new ShapeListServant(); 1Naming.rebind("rmi://127.0.0.1/ShapeList", aShapeList ); 2System.out.println("ShapeList server ready");

    }catch(Exception e) {System.out.println("ShapeList server main " + e.getMessage());}

    }}

  • 7/25/2019 02c - 50 - 3 - Rmi

    30/38

    Java RMI client ()

    O client ShapeListinterface ( 1)

    allShapes( 2)

    30

    rmi://127.0.0.1/ShapeList

  • 7/25/2019 02c - 50 - 3 - Rmi

    31/38

    Java RMI client () client

    allShapes(), getAllState() Shapeinterface ,

    , newShape() server, GraphicalObject

    O version number server ( ) server ,

    31

  • 7/25/2019 02c - 50 - 3 - Rmi

    32/38

    Java RMI :

    add server

    32

  • 7/25/2019 02c - 50 - 3 - Rmi

    33/38

    Java RMI

    import java.rmi.*;

    public interface AddServerIntf extends Remote {

    double add(double d1, double d2) throws RemoteException;}

    33

  • 7/25/2019 02c - 50 - 3 - Rmi

    34/38

    remote objectimport java.rmi.*;

    import java.rmi.server.*;

    public class AddServerImpl extends UnicastRemoteObject implementsAddServerIntf {

    public AddServerImpl() throws RemoteException{

    }

    public double add(double d1, double d2) throws RemoteException

    { return d1 + d2;

    }

    }

    34

  • 7/25/2019 02c - 50 - 3 - Rmi

    35/38

    RMI server

    35

    import java.net.*;

    import java.rmi.*;

    public class AddServer {

    public static void main(String args[]) {

    try {AddServerImpl addServerImpl = new AddServerImpl();Naming.rebind("AddServer", addServerImpl);

    }

    catch(Exception e) { System.out.println("Exception: " + e); }}

    }

  • 7/25/2019 02c - 50 - 3 - Rmi

    36/38

    RMI clientimport java.rmi.*;

    public class AddClient {

    public static void main(String args[]) {

    try {

    String addServerURL = "rmi://" + args[0] + "/AddServer";

    AddServerIntf addServerIntf =

    (AddServerIntf)Naming.lookup(addServerURL);

    System.out.println("The first number is: " + args[1]);double d1 = Double.valueOf(args[1]).doubleValue();

    System.out.println("The second number is: " + args[2]);

    double d2 = Double.valueOf(args[2]).doubleValue();

    System.out.println("The sum is: " + addServerIntf.add(d1, d2));

    }

    catch(Exception e) {

    System.out.println("Exception: " + e);

    }

    }

    } 36

  • 7/25/2019 02c - 50 - 3 - Rmi

    37/38

    Compile all aforementioned Java source files and keep

    all generated .class files in the same directory. Before you can use the client and the server, you must

    generate the necessary stub.

    You may also need to generate a skeleton, if a serverruns on a machine that does not have Java 2, but hasan earlier version of JVM.

    1. Generate stubs and skeletons (deprecated!)

    To generate stubs and skeletons, you use the RMIcompiler: rmic AddServerImpl

    This command generates two new files:AddServerImpl_Skel.class (skeleton) and

    AddServerImpl_Stub.class (stub). 37

  • 7/25/2019 02c - 50 - 3 - Rmi

    38/38

    2. Start the RMI registry

    Go to the directory on the server machine where youkeep files mentioned above.

    If your server is on another Windows machine: startrmiregistry

    If your server is on a UNIX machine: rmiregistry &

    3. Start the server

    In the same directory: java AddServer

    4. Run the client

    j AddCli 6