RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac /home2/fccheng/temp/RMI2 javac...

18
RMI Example

description

rmic RemoteServer -rw-r--r-- 1 fccheng staff 1712 May 12 17:40 RemoteServer_Skel.class -rw-r--r-- 1 fccheng staff 3223 May 12 17:40 RemoteServer_Stub.class

Transcript of RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac /home2/fccheng/temp/RMI2 javac...

Page 1: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

RMI Example

Page 2: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

Compilation:

/home2/fccheng/temp/RMI2> javac MyObject.java/home2/fccheng/temp/RMI2> javac RemoteServer.java/home2/fccheng/temp/RMI2> javac RemoteInterface.java/home2/fccheng/temp/RMI2> javac RemoteClient1.java/home2/fccheng/temp/RMI2> javac RemoteClient2.java

Page 3: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

rmic RemoteServer

-rw-r--r-- 1 fccheng staff 1712 May 12 17:40 RemoteServer_Skel.class

-rw-r--r-- 1 fccheng staff 3223 May 12 17:40 RemoteServer_Stub.class

Page 4: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

Java policy for RMI

grant { permission java.net.SocketPermission "*:1024-65535", "connect,accept"; permission java.net.SocketPermission "*:80","connect";};

Page 5: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

start rmiregistry

$ rmiregistery 1111 &Default TCP port: 1099

Page 6: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

Run Server

• Run server program: (see RunServer script)

java -Djava.rmi.server.codebase=http://140.129.20.87/~fccheng/ temp/RMI2/ -Djava.rmi.server.hostname=140.129.20.87 -Djava.security.policy=java.policy RemoteServer IP TCP-port

Page 7: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

Run Clients

• Run client program: (see RunClient1 and RunClient2) java -Djava.rmi.server.codebase=http://140.129.20.110/~cheng/ temp/RMI2/ -Djava.security.policy=java.policy RemoteClient1 ServerIP:TCP-Port

Page 8: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

Running Example

Host Information:

Host IP JDK OS App.====== ============ ======== ===== ==============alpha: 140.129.20.247 JDK 1.1.6 Sol2.5 (client only)gamma: 140.129.20.87 JDK 1.2 Sol2.6 (client and server)aimm: 140.129.20.110 JDK 1.2 Sol2.6 (client and server)====== ============ ======== ===== ==============

Page 9: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

Snapshot:Client site aimm*****************************************************aimm:/export/home/cheng/temp/RMI2> RunClient1 140.129.20.87:1112Running Client...Sending MyObject to Server ...The server says :Total Message: 1Client 1aimm:/export/home/cheng/temp/RMI2> RunClient2 140.129.20.87:1112Running Client...Sending MyObject to Server ...The server says :Total Message: 2Client 12nd Clientaimm:/export/home/cheng/temp/RMI2> *****************************************************

Page 10: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

Snapshot:Client site alpha*****************************************************alpha:/home2/cheng/temp/RMI2> java RemoteClient1 140.129.20.87:1112Running Client...Sending MyObject to Server ...The server says :Total Message: 3Client 12nd ClientClient 1alpha:/home2/cheng/temp/RMI2> *****************************************************

Page 11: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

Snapshot: Server gamma

*****************************************************gamma:/home2/fccheng/temp/RMI2> RunServer 140.129.20.87 1112Running Server ...Server:rebining ...Server got[8]:Client 1Server got[10]:2nd ClientServer got[8]:Client 1*****************************************************

Page 12: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

/* MyObject.java */public class MyObject implements java.io.Serializable {

private int x;private String msg;public MyObject (String s) {

msg = s;x = msg.length();

}public int lenMsg() {

return x;}public String getMsg() {

return msg;}public void setMsg(String s) {

msg = s;}

}

Page 13: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

/* RemoteClient1.java */ import java.rmi.*; public class RemoteClient1 { public static void main(String args[]) { System.out.println("Running Client..."); System.setSecurityManager(new RMISecurityManager()); try { RemoteInterface server = (RemoteInterface) Naming.lookup("rmi://"+args[0] + "/"+"ObjectServerTest"); MyObject msgObj = new MyObject("Client 1"); System.out.println("Sending MyObject to Server ..."); MyObject retObj = server.msgsend(msgObj); System.out.println("The server says :\n" + retObj.retMsg()); } catch (Exception e){

System.out.println("Error while performing RMI"); } }}

Page 14: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

/* RemoteClient2.java */ import java.rmi.*; public class RemoteClient2 { public static void main(String args[]) { System.out.println("Running Client..."); System.setSecurityManager(new RMISecurityManager()); try { RemoteInterface server = (RemoteInterface) Naming.lookup("rmi://"+args[0] + "/"+"ObjectServerTest"); MyObject msgObj = new MyObject(”2nd Client"); System.out.println("Sending MyObject to Server ..."); MyObject retObj = server.msgsend(msgObj); System.out.println("The server says :\n" + retObj.retMsg()); } catch (Exception e){

System.out.println("Error while performing RMI"); } }}

Page 15: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

/* RemoteInterface.java */public interface RemoteInterface extends

java.rmi.Remote {MyObject msgsend (MyObject message) throws

java.rmi.RemoteException;}

Page 16: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

/* RemoteServer.java */import java.rmi.*;import java.rmi.server.UnicastRemoteObject;public class RemoteServer extends UnicastRemoteObject

implements RemoteInterface{ String name; static int messageCount; static String globalMessage="";

public RemoteServer(String name) throws RemoteException{

super();this.name = name;

}

Page 17: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

public MyObject msgsend(MyObject message) throws RemoteException{

System.out.println("Server got[" + message.lenMsg() + "]:" + message.getMsg());

messageCount++;globalMessage=globalMessage+"\n"+message.getMsg();message.setMsg("Total Message: "+

Integer.toString(messageCount)+globalMessage);return message;

}

Page 18: RMI Example. Compilation: /home2/fccheng/temp/RMI2 javac   /home2/fccheng/temp/RMI2 javac RemoteServer.java /home2/fccheng/temp/RMI2 javac.

public static void main (String args[]){ System.out.println("Running Server ...");

System.setSecurityManager (new RMISecurityManager());

try{ String myName = "//"+args[0]+":"+args[1]+

"/ObjectServerTest";RemoteServer theServer =

new RemoteServer (myName);System.out.println("Server:rebining ...");Naming.rebind(myName,theServer);

} catch (Exception e){System.out.println("An Exception occurred

while creating server");e.printStackTrace();

} }

}