The RMI-Based Client


Implementing NetRatServer - Second Iteration

The second development iteration will focus on implementing RMI functionality. Table 20-5 gives the design considerations and decisions for this iteration.

Table 20-5: Second Iteration Design Considerations and Decisions

Check-Off

Design Considerations

Design Decisions

 

Objectives for the second iteration

Implement Remote Method Invocation (RMI) functionality. This will entail creating an interface called RobotRatInterface that contains all the method declarations currently implemented in the RobotRat class. Once the RobotRatInterface is complete the RobotRat class needs to be retrofitted to implement RobotRatInterface and extend UnicastRemoteObject. The rmic tool then needs to be run to create the required RobotRat stub class. A slight modification will also have to be made to the NetRatServer class in order to utilize the RMI version of RobotRat.

 

RobotRatInterface

The RobotRatInterface must extend the java.rmi.Remote interface and provide method declarations for all the move methods currently defined in the RobotRat class.

 

RobotRat class

The RobotRat class must extend the java.rmi.server.UnicastRemoteObject and implement the RobotRatInterface.

 

rmic tool

The rmic tool needs to be run against the RobotRat class file to create the RobotRat_stub.class file.

 

NetRatServer

The NetRatServer needs to be slightly modified to properly handle the RMI version of the RobotRat class. This will entail wrapping RobotRat method calls in try-catch blocks and adding the code to start the registry and bind an instance of RobotRat to a service name.

Second Iteration Code

The following code is written or modified during the second iteration:

Referring to example 20.7 through 20.9 — the RobotRatInterface extends the java.rmi.Remote interface and provides a declaration for each of the move methods currently defined in the RobotRat class.

Example 20.7: RobotRatInterface.java

image from book
 1     import java.rmi.*; 2 3     interface RobotRatInterface extends Remote { 4 5       public void moveEast() throws RemoteException; 6       public void moveSouth() throws RemoteException; 7       public void moveWest() throws RemoteException; 8       public void moveNorth() throws RemoteException; 9       public void moveNorthWest() throws RemoteException; 10      public void moveSouthWest() throws RemoteException; 11      public void moveNorthEast() throws RemoteException; 12      public void moveSouthEast() throws RemoteException; 13 14    }
image from book

Example 20.8: RobotRat.java (mod 1)

image from book
 1     import javax.swing.*; 2     import java.util.*; 3     import java.rmi.*; 4     import java.rmi.server.*; 5 6     public class RobotRat extends UnicastRemoteObject implements RobotRatInterface { 7 8       private static JFrame _frame = null; 9       private static Floor _floor = null; 10      private static Vector _rats = null; 11      private Rat _its_rat = null; 12 13      static { 14        _rats  = new Vector(); 15        _floor = new Floor(_rats); 16        _frame = new JFrame("Rat Movement Floor"); 17        _frame.getContentPane().add(_floor); 18        _frame.setSize(300, 300); 19        _frame.setLocation(200, 200); 20        _frame.show(); 21      } 22 23 24      public RobotRat() throws RemoteException { 25        _its_rat = new Rat(); 26        _rats.addElement(_its_rat); 27        _floor.repaint(); 28       } 29 30      public void moveEast(){ 31        _its_rat.setX(_its_rat.getX() + 3); 32        _floor.repaint(); 33      } 34 35      public void moveSouth(){ 36        _its_rat.setY(_its_rat.getY() + 3); 37        _floor.repaint(); 38      } 39 40      public void moveWest(){ 41        _its_rat.setX(_its_rat.getX() - 3); 42        _floor.repaint(); 43      } 44 45      public void moveNorth(){ 46        _its_rat.setY(_its_rat.getY() - 3); 47        _floor.repaint(); 48      } 49 50      public void moveNorthWest(){ 51        _its_rat.setY(_its_rat.getY() - 3); 52        _its_rat.setX(_its_rat.getX() - 3); 53        _floor.repaint(); 54      } 55 56      public void moveSouthWest(){ 57        _its_rat.setY(_its_rat.getY() + 3); 58        _its_rat.setX(_its_rat.getX() - 3); 59        _floor.repaint(); 60      } 61 62      public void moveNorthEast(){ 63         _its_rat.setY(_its_rat.getY() - 3); 64         _its_rat.setX(_its_rat.getX() + 3); 65         _floor.repaint(); 66      } 67 68      public void moveSouthEast(){ 69        _its_rat.setY(_its_rat.getY() + 3); 70        _its_rat.setX(_its_rat.getX() + 3); 71        _floor.repaint(); 72      } 73    } // end RobotRat class definition 
image from book

Example 20.9: NetRatServer.java (mod 1)

image from book
 1     import javax.swing.*; 2     import java.awt.*; 3     import java.awt.event.*; 4     import java.rmi.*; 5     import java.rmi.registry.*; 6     import java.net.*; 7 8     public class NetRatServer extends JFrame implements ActionListener { 9 10      private JButton button1 = null; 11      private RobotRatInterface _r1 = null; 12      private RobotRatInterface _r2 = null; 13 14      public NetRatServer(){ 15        try{ 16        _r1 = new RobotRat(); 17        _r2 = new RobotRat(); 18        }catch(Exception ignored){ ignored.printStackTrace(); } 19        button1 =  new JButton("move"); 20        button1.addActionListener(this); 21        this.getContentPane().add(button1); 22        this.setSize(100, 100); 23        this.setLocation(300, 300); 24        this.show(); 25      } 26 27      public void actionPerformed(ActionEvent ae){ 28        if(ae.getActionCommand().equals("move")){ 29          try{ 30          _r1.moveEast(); 31          _r2.moveSouthEast(); 32          }catch(Exception ignored){ } 33        } 34      } 35 36      public static void main(String[] args){ 37        System.out.println("NetRatServer Lives!!"); 38        new NetRatServer(); 39        try{ 40        System.out.println("Starting registry..."); 41        LocateRegistry.createRegistry(1099); 42        System.out.println("Registry started on port 1099."); 43        System.out.println("Binding service name to remote object..."); 44        Naming.bind("Robot_Rat", new RobotRat()); 45        System.out.println("Bind successful."); 46        System.out.println("Ready for remote method invocation."); 47 48        }catch(Exception e){ 49          e.printStackTrace(); 50        } 51      } 52    }// end NetRatServer class definition
image from book

The RobotRat class extends the java.rmi.server.UnicastRemoteObject and implements RobotRatInterface. Notice that the only modifications required to RobotRat include the extended class declaration and the addition of a throws clause to the constructor.

The NetRatServer class has been modified in several ways to accommodate the RMI-enabled RobotRat class. First, the RobotRatInterface is used on lines 11 and 12 to declare references r1 and r2. Next, an instance of a registry is started programmatically in the main() method on line 41, and on line 44 an instance of RobotRat is bound to the service name “Robot_Rat”.

Testing Second Iteration Code

Before you can test this version of NetRatServer the rmic tool must be run against the compiled RobotRat class file to generate the RobotRat_stub.class file. This file must then be deployed with the rest of the server code. Figure 20-13 shows the NetRatServer terminal window output and floor at application start-up. Figure 20-14 shows how the floor looks after approximately ten clicks of the move button.

image from book
Figure 20-13: RMI-Enabled NetRatServer Application at Start-up

image from book
Figure 20-14: RMI-Enabled NetRatServer Application After Approximately 10 move Button Clicks

Referring to figure 20-14 — notice now there are three rat images displayed on the floor: the two that move to the right when the move button is clicked, and the third in the upper left corner that corresponds to the instance of RobotRat that was bound to the service name “Robot_Rat” in the body of the main() method.

To fully test the NetRatServer RMI functionality it is now necessary to create the RMI-based client application.

Quick Review

Remote Method Invocation functionality is often easier to implement than socket-based functionality. This is because the RMI runtime handles socket communication and thread management issues behind the scenes. Implementing RMI functionality, however, requires the programmer to generate the necessary stub files for deployment with the server and client applications. RMI servers must bind an instance of the RMI object to a service name in an RMI registry.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net