Implementing the Client

The client in this example remotely invokes the getList() and register() methods. Here is the code:

Listing 5.3 RegisterClient

import java.rmi.*;
import java.rmi.registry.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class RegisterClient extends JFrame{

 //{{ declare variables //
 private RegisterInterface server = null;
 private JButton btnList,btnRegister;
 private JTextArea txtArea = null;
 private JTextField txtName = null;
 
 //constructor //
 public RegisterClient(){
	
     super("Register Clients");

     //creating panels //
     JPanel 
	btnPanel = new JPanel(),
	topPanel = new JPanel();
		
     //components //
	   JLabel lblTitle = new JLabel("Enter your name");
      txtArea = new JTextArea(12,35);
      txtArea.setEditable(false);
      txtName = new JTextField(15);
		
      //{{ button related code //
      ButtonHandler btnHandler = new ButtonHandler();
	
      btnRegister = new JButton("Register");
      btnRegister.addActionListener(btnHandler);
	
		
      btnList = new JButton("View List");
      btnList.addActionListener(btnHandler);
	
     //{{ end of button related code//

     // add components
     topPanel.add(lblTitle);
     topPanel.add(txtName);
     btnPanel.add(btnRegister);
     btnPanel.add(btnList);
    
     //get a content pane object
     Container cp = getContentPane();
   
    //add the components to the content pane
    cp.add(txtArea,BorderLayout.CENTER);
    cp.add(btnPanel,BorderLayout.SOUTH);
    cp.add(topPanel,BorderLayout.NORTH);
   
   //set the default close operation 
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
   //pack() causes the window to be sized to fit the 
   //preferred size and layouts of its subcomponents. 
   //The Window will be validated after the 
   //preferredSize is calculated.
   pack();
 
   //set the location of window
   setLocation(25,25);

   //shows the window
   show();
	
   //method used to get a remote reference
    connectServer();
		 	 
}

 
//{{ event handling class //
private class ButtonHandler 
    implements ActionListener{ 
     
    public void actionPerformed(ActionEvent ae){

	//get the source
	JButton temp = (JButton)ae.getSource();
	if(temp == btnRegister){
	  try{
	     String name = txtName.getText().trim(); 
	     if(!name.equals("")){
		 	
		//call the remote method to 
		//register the user
		String message = server.register(name);
		 txtArea.setText(message);
	      }
	 }catch(Exception ex){
	    txtArea.setText("Error: "+ex.getMessage());
	 }
	 
        }
	else {
	     try{
		//invoke the remote method to
		//get the list
		String list = server.getList();
		txtArea.setText(list);
		}catch(Exception ex){
		    txtArea.setText("Error: "+ex.getMessage());
      	        }
	 
       }
    }
}


private void connectServer(){
	try{
	    //Get a reference to the remote 
	    //object Registry for the local host 
	    //on port number 2000.
 	    Registry reg = LocateRegistry.getRegistry(2000);
	 
	    //Get a reference, a stub, for the 
  	   //remote object associated with the 
	   //specified name.
	   server = (RegisterInterface)reg.lookup("Server");
 	   txtArea.setText("Connected");
	 }catch(Exception ex) {
	    txtArea.setText("Error: "+ex.getMessage());
	 }
}

//main method	
public static void main(String args[]){
	
    new RegisterClient();
  }
}

Explanation of Code

The constructor creates a number of components and containers. It creates a TextField instance for entering the name of the user, a TextArea to show the list of names and other messages. Moreover, it creates instances of buttons to register and get the list of names. It adds event listener to the buttons. Finally, it calls connectServer() method that obtains a reference to the registry and remote object.

The ButtonHandler class is used to handle action events generated by buttons. When a user presses the button, the actionPerformed() method is invoked. This method first checks the source and then invokes methods on the remote object. The values returned by the register() and getList() methods will be displayed in the TextArea.

The connectServer()method is used to get a remote reference. The getRegistry() method of LocateRegistry class returns a reference to the registry for the local host on the specified "port". The following line locates a reference to the registry on port no. 2000.

Registry reg = LocateRegistry.getRegistry(2000);

After execution of the following statement the variable "server" will contain a reference to an instance of the stub class that actually handles the client side RMI. Now the client can invoke methods on the remote object.

server = (RegisterInterface)reg.lookup("Server");

Finally, the method sets the text of TextArea to "connected" if no exception is thrown.

 

 

 

RMI BOOK MAIN PAGE                Top

  
Copyright © 2001 www.universalteacher.com