Implementing the Server

In this example, the server class (RegisterServerImpl) will implement the remote interface (RegisterInterface) and extend UnicastRemoteObject.

Listing 5.2 RegisterServerImpl (complete code)

import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;

//server class
public class RegisterServerImpl extends UnicastRemoteObject 
    implements RegisterInterface {
		
     //constructor							
      public RegisterServerImpl()throws RemoteException{
	  System.out.println("Creating Server Object");
      }
		
      //method invoked by clients to register.  
      public String register(String name)
	   throws RemoteException,IOException {
			
	//Constructs a FileWriter object given a 
	//file name with a boolean indicating whether 
	//or not to append the data written.
	FileWriter file = new FileWriter("regfile.txt",true);
			
	//Creating a buffered character-output stream that 
	//uses a default-sized output buffer.
	BufferedWriter buf = new BufferedWriter(file);
	
	//Write the name of the user.
	 buf.write(name);
			
	//Write a line separator
	 buf.newLine();
			
	//Flush the stream.
	 buf.flush();
			
	//Close the stream.
	buf.close();
			
	//Close the stream.
 	file.close();
	return name +" registered successfully"; 
  }
		
      //method invoked remotely to get the
      //list of already registered users
      public String getList()
	throws RemoteException,IOException {

	//Create a new FileReader object
	FileReader file = new FileReader("regFile.txt");
	
	//Create a buffering character-input stream 
	//that uses a default-sized input buffer.
	BufferedReader buf = new BufferedReader(file);
	String list = "";
	String temp = "";
	do{
	
	//Read a line of text. A line is considered 
	//to be terminated by any one of a line 
	//feed ('\n'), a carriage return ('\r'), 
	//or a carriage return followed immediately 
	//by a linefeed.
	temp = buf.readLine();
	if(temp!=null)
	     list+=temp+"\n";
	}while(temp!=null);
	
	return list;	 	 
  }
		
     //main method
     public static void main(String [] args) {
		
         try{

	  //create a server object	
	  RegisterServerImpl regServer = new RegisterServerImpl();
	 	 
	  //Create and export a Registry on the 
	  //local host that accepts requests on 
	  //port number 2000.
	  Registry reg = LocateRegistry.createRegistry(2000);
			 
	  //Rebind the specified name to a 
	  //remote object. 
	  reg.rebind("Server",regServer);
	  System.out.println("Server is ready");
	  }catch(RemoteException ex){
	      System.out.println("Error :"+ex.getMessage());
	  }
    }
}

Explanation of Code

The register method takes the name of the user as its arguments and returns a string, if the name is successfully stored in the file. It creates a FileWriter object and then creates a buffered character-output stream, which uses a default-sized output buffer. BufferedWriter is used to write text to a character-output stream in an efficient manner. After writing information, the stream is closed by the method.

The getList() method returns a string containing the names of all the users. It creates a FileReader object and then creates a buffered character-input stream,which uses a default-sized input buffer. BufferedReader is used to read text from a character-input stream in an efficient way.

The main method of the server creates an instance of the remote object, which provides the service. For example:

RegisterServerImpl regServer = new RegisterServerImpl();

The LocateRegistry class can be used to obtain a reference to registry on a particular host (including the local host), or to create a registry that accepts calls on a specific port. The methods of the java.rmi.Naming class call the methods of an object that implements the Registry interface. The following line creates and exports a Registry on the local host that accepts requests on the specified "port".

Registry reg = LocateRegistry.createRegistry(2000);

Our next step is to bind the specified name to a remote object.

reg.rebind("Server",regServer);

Finally, the server prints a message.

System.out.println("Server is Ready");

 

 

 

RMI BOOK MAIN PAGE                Top

  
Copyright © 2001 www.universalteacher.com