NetRatServer Implementation Third Iteration


The RMI-Based Client

This section presents an RMI-based client application that can send commands to a RobotRat object via a reference retrieved from the server’s RMI registry.

Referring to example 20-10 — the RMI_NetRatClient class allows the user to send commands to a remote RobotRat. Users enter movement commands via a grid of JButtons. The constructor method looks up the Robot_Rat service on the server at the designated IP address.

Example 20.10: RMI_NetRatClient.java

image from book
 1     import java.rmi.*; 2     import java.awt.*; 3     import java.awt.event.*; 4     import javax.swing.*; 5 6     public class RMI_NetRatClient extends JFrame implements ActionListener { 7 8       private JButton _button1 = null; 9       private JButton _button2 = null; 10      private JButton _button3 = null; 11      private JButton _button4 = null; 12      private JButton _button5 = null; 13      private JButton _button6 = null; 14      private JButton _button7 = null; 15      private JButton _button8 = null; 16      private JButton _button9 = null; 17      private RobotRatInterface _robot_rat = null; 18 19 20      public RMI_NetRatClient(String host){ 21        super("Robot Rat Control Panel"); 22 23      try{ 24         _robot_rat = (RobotRatInterface)Naming.lookup("rmi://" + host + "/Robot_Rat"); 25 26         }catch(Exception e){ 27          e.printStackTrace(); 28         } 29        this.setUpGui(); 30      } 31 32      public void actionPerformed(ActionEvent ae){ 33       try{ 34       if(ae.getActionCommand().equals("N")){ 35          _robot_rat.moveNorth(); 36        }else if(ae.getActionCommand().equals("NE")){ 37          _robot_rat.moveNorthEast(); 38        }else if(ae.getActionCommand().equals("E")){ 39          _robot_rat.moveEast(); 40        }else if(ae.getActionCommand().equals("SE")){ 41          _robot_rat.moveSouthEast(); 42        }else if(ae.getActionCommand().equals("S")){ 43          _robot_rat.moveSouth(); 44        }else if(ae.getActionCommand().equals("SW")){ 45          _robot_rat.moveSouthWest(); 46        }else if(ae.getActionCommand().equals("W")){ 47          _robot_rat.moveWest(); 48        }else if(ae.getActionCommand().equals("NW")){ 49          _robot_rat.moveNorthWest(); 50        } 51 52       }catch(RemoteException re){ 53         System.out.println("actionPerformed(): problem calling remote robot_rat method."); 54         re.printStackTrace(); 55        } 56      } // end constructor 57 58      public void setUpGui(){ 59       _button1 = new JButton("NW"); 60       _button1.addActionListener(this); 61 62       _button2 = new JButton("N"); 63       _button2.addActionListener(this); 64 65       _button3 = new JButton("NE"); 66       _button3.addActionListener(this); 67 68       _button4 = new JButton("W"); 69       _button4.addActionListener(this); 70 71       _button5 = new JButton(""); 72       _button5.addActionListener(this); 73 74       _button6 = new JButton("E"); 75       _button6.addActionListener(this); 76 77       _button7 = new JButton("SW"); 78       _button7.addActionListener(this); 79 80       _button8 = new JButton("S"); 81       _button8.addActionListener(this); 82 83       _button9 = new JButton("SE"); 84       _button9.addActionListener(this); 85 86       this.getContentPane().setLayout(new GridLayout(3,3,0,0)); 87       this.getContentPane().add(_button1); 88       this.getContentPane().add(_button2); 89       this.getContentPane().add(_button3); 90       this.getContentPane().add(_button4); 91       this.getContentPane().add(_button5); 92       this.getContentPane().add(_button6); 93       this.getContentPane().add(_button7); 94       this.getContentPane().add(_button8); 95       this.getContentPane().add(_button9); 96       this.setSize(200, 200); 97       this.setLocation(300, 300); 98       this.pack(); 99       this.show(); 100     } // end setUpGui() 101 102 103     public static void main(String[] args){ 104        try{ 105          new RMI_NetRatClient(args[0]); 106        }catch(ArrayIndexOutOfBoundsException e1){ 107           System.out.println("Usage: java NetRatClient <host>"); 108         } 109         catch(Exception e2){ 110           e2.printStackTrace(); 111         } 112     }// end main() 113   }// end RMI_NetRatClient class definition
image from book

To test the RMI_NetRatClient code the server must be running on the local machine or remote server. Figure 20-15 shows the RMI_NetRatClient application running.

image from book Figure 20-15: RMI_NetRatClient Application

image from book
Figure 20-16: The Floor After Testing RMI_NetRatClient.

Testing Multiple RMI Clients

The RMI_NetRatClient application can be used to test multiple RMI client connections. To do so simply start up another instance of the RMI_NetRatClient application. Given the current NetRatServer design, testing reveals that multiple RMI clients control only one RobotRat around the floor, which is the one bound to the service name Robot_Rat in the body of the NetRatServer main() method. What’s needed is a modification to the server application design that will allow each RMI client to control their very own RobotRat. This is the objective of the third NetRatServer development iteration.

Quick Review

RMI-based clients gain access to an RMI object via its service name. The remote object’s interface and stub classes must be deployed with the client application. Calls to the remote object look like ordinary method calls. All network communication required between the RMI-based client and remote object is handled by the RMI runtime environment.




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