Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

22
Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software

Transcript of Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

Page 1: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

Vakgroep Informatietechnologie - IBCN

CORBA & RMIDesign of Distributed Software

Page 2: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

2ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Objectives

After this lab session you should be able to Write client and server applications Devlop object-oriented based distributed systems Use the CORBA and Java RMI technology CORBA = Common Object Request Brokerage Architecture Language neutral technology Allowing software components on multiple computers to

communicate

Java RMI Java specific technology Allowing software components on multiple computers to

communicate

2

Page 3: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

CORBA &

Java RMI

Distributed system

Page 4: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

4ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Outline

1. CORBAHow to write a CORBA application

Page 5: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

5ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Writing CORBA in 5 steps

Writing the IDL files

Writing the Server

Writing the Servant

Writing the Client

Starting the application

Page 6: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

6ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Writing the IDL files

The IDL file describes Which classes can be accessed remotely The signature of each remote method

Compile the file: idlj –fall server.idl

module ods{

module corba{

module bmicalculator{

interface BMICalculator{

double calculateBMI(in double length, in double weight);

};

};

};

};

Page 7: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

7ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Writing the Server

Several classes were generatedWriting the server

Extending the abstract POA class No CORBA specific code is needed

package ods.corba.bmicalculator;

public class BMICalculatorImpl extends BMICalculatorPOA{

public double calculateBMI(double length,

double weight){

return weight / (length * length);

}

}

Page 8: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

8ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Writing the servant

Servant: main class to start the serverSteps to take:

Initialize the ORB (Object Request Broker)

Start the Server object

java.util.Properties props = System.getProperties(); props.put("org.omg.CORBA.ORBInitialPort”,157.193.214.253); props.put("org.omg.CORBA.ORBInitialHost", 1049);

ORB orb = ORB.init(new String[0], props);

BMICalculatorImpl bmi_calc = new BMICalculatorImpl();

Page 9: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

9ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Writing the servant

Find the Naming Service & Root POA

Activate the Server object with the POA

POA rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));

NamingContext namingContext = NamingContextHelper.narrow(orb.resolve_initial_references

("NameService"));

rootPOA.activate_object(bmi_calc);

BMICalculator bmicalcRef = BMICalculatorHelper.

narrow(rootPOA.servant_to_reference(bmi_calc));

Page 10: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

10ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Writing the servant

Register the server object with the Naming Service

Activate the POA and start the ORB

10

NameComponent nc = new NameComponent("BMICalculator", ""); NameComponent path[] = { nc };

namingContext.rebind(path, bmicalcRef);

rootPOA.the_POAManager().activate();

orb.run();

Page 11: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

11ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Writing the client

Steps to take: Initialize the ORB (Object Request Broker)

Find the Naming Service

java.util.Properties props = System.getProperties(); props.put("org.omg.CORBA.ORBInitialPort”,157.193.214.253); props.put("org.omg.CORBA.ORBInitialHost", 1049);

ORB orb = ORB.init(new String[0], props);

NamingContext namingContext = NamingContextHelper.narrow(orb.resolve_initial_references

("NameService"));

Page 12: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

12ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Writing the client

Obtain a reference to the remote object

Invoke a method just as a regular object

NameComponent nc = new NameComponent("BMICalculator", ""); NameComponent path[] = { nc };

BMICalculator server = BMICalculatorHelper.narrow(ncRef.resolve(path));

System.out.println(server.calculateBMI(1.87, 75));

Page 13: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

13ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Starting the application

Starting the Naming Service

Starting the Server

Starting the client

13

start orbd –ORBInitialPort 1049

start java ods.corba.bmicalculator.

StartBMICalculatorServer 157.193.214.253 1049

java ods.corba.bmicalculator.client.BMICalculatorClient 157.193.214.253 1049

Page 14: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

14ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Outline

2. Java RMIHow to write a Java RMI application

Page 15: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

15ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Writing Java RMI in 5 steps

Writing the remote Java interface

Writing the Server

Writing the Servant

Writing the Client

Starting the application

Writing the policy file

Page 16: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

16ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Writing the remote interface

Define the class as remote Extend Remote interface

Define the available remote methods Add “throws RemoteException”

16

package ods.rmi.bmicalculator.server;

import java.rmi.Remote;

import java.rmi.RemoteException;

public interface BMICalculatorServerInterface extends Remote {

public double calculateBMI(double length, double weight)

throws RemoteException;

}

Page 17: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

17ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Implementing the remote interface No specific Java RMI code is needed

17

public class BMICalculatorServer implements ods.rmi.bmicalculator.server.BMICalculatorServerInterface{

public BMICalculatorServer() {

super();

}

public double calculateBMI(double length, double weight)

throws RemoteException {

return weight/(length*length);

}

}

Writing the Server

Page 18: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

18ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Writing the servant

Servant: main class to start the serverSteps to take:

Initialize the Security Manager

Start the Server object

if (System.getSecurityManager() == null) {

System.setSecurityManager(new SecurityManager());

}

BMICalculatorServerInterface bmiCalc = new

BMICalculatorServer();

BMICalculatorServerInterface stub =

(BMICalculatorServerInterface)

UnicastRemoteObject.exportObject(bmiCalc, 0);

Page 19: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

19ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Writing the servant

Register the server with the Registry

Registry registry = LocateRegistry.getRegistry();

String name = (args[0]==null)?args[0]:"BMICalculator";

registry.rebind(name, stub);

Page 20: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

20ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Writing the client

Steps to take: Initialize the Security Manager

Obtain a reference to the remote object

Invoke a method

if (System.getSecurityManager() == null) {

System.setSecurityManager(new SecurityManager());

}

Registry registry = LocateRegistry.(args[0]);

BMICalculatorServerInterface calculator =

(BMICalculatorServerInterface)registry.lookup(args[1]);

System.out.println(calculator.calculateBMI(1.87, 75));

Page 21: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

21ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Writing a policy file

Policy file needed to allow network communication

21

grant{

permission java.net.SocketPermission "*:1024-65535", "connect,accept";

};

Page 22: Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.

22ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN

Starting the application

Starting the Registry

Starting the Server

Starting the client

22

rmiregistry [port]

start java -Djava.security.policy=java.policy ods.rmi.bmicalculator.server.BMICalculatorServer BMICalculator

java -Djava.security.policy=java.policy

ods.rmi.bmicalculator.client.BMIClientProgram

157.193.214.124 BMICalculator