Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.
-
Upload
ellen-fisher -
Category
Documents
-
view
222 -
download
0
Transcript of Vakgroep Informatietechnologie - IBCN CORBA & RMI Design of Distributed Software.
Vakgroep Informatietechnologie - IBCN
CORBA & RMIDesign 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
ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN
CORBA &
Java RMI
Distributed system
4ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN
Outline
1. CORBAHow to write a CORBA application
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
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);
};
};
};
};
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);
}
}
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();
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));
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();
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"));
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));
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
14ODS – Introduction to CORBA & RMIVakgroep Informatietechnologie - IBCN
Outline
2. Java RMIHow to write a Java RMI application
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
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;
}
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
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);
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);
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));
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";
};
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