An RMI Soil Server

Let us consider the Soil component presented in the previous section, and implement it as a server using the RMI technology. The Soil component will provide its services to any other component or system residing in the same address space or in other servers located anywhere in the network. Therefore, the behavior of component Soil will be slightly different; besides the behavior needed in the simulation process, the Soil component will be provided with additional behavior to function as an RMI server.

First, an interface needs to be created that will define the functionalities provided by the object server. The interface is referred to as SoilRMIInterface and its definition in Java is shown in Figure 11-14. The behavior defined in the interface is the same as the one defined in Section 2.4, A CORBA Soil Server, with the only difference that RMI requires that methods defined in the interface should throw an exception, the RemoteException one.

1 import java.rmi.RemoteException;

2 import java.rmi.Remote;

3 interface SoilRMIInterface extends Remote {

4 public double getSoilDepth() throws RemoteException;

5 public double getWiltingPoint() throws RemoteException;

6 public void initialize^) throws RemoteException;

Figure 11-14. Definition of interface SoilRMIInterface in Java.

As the functionalities defined in the interface will be provided as services that can be invoked remotely, SoilRMIInterface should inherit from Remote interface, which is part of Java's arsenal for sending/receiving messages over a network. As SoilRMI object will provide its services from a remote server, it should be able to instantiate itself with server-like capabilities. Therefore, SoilRMI object should inherit the server-like behavior from a predefined Java server class named UnicastRemoteObject. The class diagram shown in Figure 11-15 presents links between objects and interfaces for remote communication.

Figure 11-15. Class diagram for the implementation of SoilRMI server.

Class SoilRMI implements interface SoilRMIInterface; therefore, it will provide an implementation for the methods defined in interface. The behavior of class SoilRMI is very similar to the behavior of Soil class, as they play the same role in the simulation process. Therefore, the behavior of class Soil can be reused by having class Soil be part of SoilRMI class definition. SoilRMI would be able to provide the functionality required for the simulation by delegating simulation-related messages to the class Soil. The behavior required in the simulation process is defined in interface Soillnterface. Thus, class Soil implements interface Soillnterface. The Java implementation for class SoilRMI is shown in Figure 11-16.

1 import java.rmi. server.*;

2 import java.rmi.Naming;

3 importjava.net.*;

4 import j ava.rmi.registry. *;

5 public class SoilRMIServer extends UnicastRemoteObject implements SoilRMIInterface {

6 public Soillnterface soil = new Soil();

7 public double getSoilDepth() {

8 return soil.getSoilDepth();

10 public double getWiltingPoint() {

11 return soil.getWiltingPoint();

13 public void initialize() {

14 soil.initialize();

16 public SoilRMIServer() throws Exception {}

17 static void main(String [] args) {

18 SoilRMIServer soilServer = null;

20 System, out.printing Starting SoilRMI Server");

22 String name = new String(fV/128.227.103.134:,,+port+M/Soi^,);

23 soilServer = new SoilRJViIServer();

24 LocateRegistry.createRegistry(port);

25 Naming.rebind(name,soilServer);

26 System.out.println(,fSoil RMI server is running in "+name);

28 catch(Exception e){System.out.println("Error in starting soil server");}

Figure 11-16. Implementation in Java of class SoilRMI (Part 2 of 2).

Lines 1 through 4 show the Java standard libraries that are needed to support the functionalities defined in class SoilRMI. Line 5 defines class SoilRMI as a subclass of UnicastRemoteObject and implementing interface SoilRMIInterface. Line 6 defines an attribute of type Soil and assigns to it a reference to an object of the same type. This is the implementation in Java of the whole-part relationship between SoilRMI and Soil as defined in Figure 1115. Lines 7 through 15 are the implementation of the methods defined in interface SoilRMIInterface. Note that each of the methods delegates the call to the object of type Soil defined in line 6.

Line 16 defines the constructor for class SoilRMI. Lines 17 through 30 define the method main that initializes the environment of class SoilRMI. Line

18 defines an instance of the class itself to be used later in line 23. Lines 19 through 29 define a try-catch block. In the case that an exception occurs while executing lines 19 through 27 in the try block, the control goes to the catch block and the system prints an error message. Within the try block, line 21 defines the communication port and assigns to it a value. Line 22 defines a stringified reference for the server object, including the communication port. Line 23 creates an instance of the class itself. Line 24 creates a registry for the communication port and line 25 binds the stringified reference for the server object to its name created in line 23. Lines 28 and 29 define the catch block that contains the actions that need to be executed in the case that an exception occurs within the try block.

0 0

Post a comment