26-08-2017, 03:53 PM
Objective: Create a distributed application to download multiple files from multiple servers using RMI
Algorithm:
Create four files: file interface, file deployment, file client and file server
1. In the file interface, the class specifies the prototype of the method to implement in the file implementation class
2. In the file implementation class, specify the implementation encoding for the method defined above (download file [])
3. Try to read the contents of a file in this class
4. In the fileclient class try to write some content in a file
5. In the fileserver class, try to register / bind the methods with the rmiregistry
6. Compile all files and run as specified to get the desired output.
Program
Algorithm:
Create four files: file interface, file deployment, file client and file server
1. In the file interface, the class specifies the prototype of the method to implement in the file implementation class
2. In the file implementation class, specify the implementation encoding for the method defined above (download file [])
3. Try to read the contents of a file in this class
4. In the fileclient class try to write some content in a file
5. In the fileserver class, try to register / bind the methods with the rmiregistry
6. Compile all files and run as specified to get the desired output.
Program
Code:
FileInterface.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface FileInterface extends Remote {
public byte[] downloadFile(String fileName) throws RemoteException ; }
FileImpl.java
import java.io.*;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class FileImpl extends UnicastRemoteObject
implements FileInterface {
private String name;
public FileImpl(String s) throws RemoteException{
super();
name = s; }
public byte[] downloadFile(String fileName){
try {
File file = new File(fileName);
byte buffer[] = new byte[(int)file.length()];
BufferedInputStream input = new
BufferedInputStream(new FileInputStream(fileName));
input.read(buffer,0,buffer.length);
input.close();
return(buffer);
} catch(Exception e){
System.out.println(“FileImpl: “+e.getMessage());
e.printStackTrace();
return(null); } }}
FileServer.java
import java.io.*;
import java.rmi.*;
public class FileServer {
public static void main(String argv[]) {
try {
FileInterface fi = new FileImpl(“FileServer”);
Naming.rebind(“//127.0.0.1/FileServer”, fi);
} catch(Exception e) {
System.out.println(“FileServer: “+e.getMessage());
e.printStackTrace(); } }}
FileClient.java
import java.io.*;
import java.rmi.*;
public class FileClient{
public static void main(String argv[]) {
if(argv.length != 2) {
System.out.println(“Usage: java FileClient fileName machineName”);
System.exit(0); }
try {
String name = “//” + argv[1] + “/FileServer”;
FileInterface fi = (FileInterface) Naming.lookup(name);
byte[] filedata = fi.downloadFile(argv[0]);
System.out.println(“enter the file to download”);
BufferedReader br = new BufferedReader(new InputStreamReader(system.in));
String newname = br.readLine();
File file = new File(newname);
BufferedOutputStream output = new
BufferedOutputStream(new FileOutputStream(file.getName()));
output.write(filedata,0,filedata.length);
output.flush();
output.close();
} catch(Exception e) {
System.err.println(“FileServer exception: “+ e.getMessage());
e.printStackTrace(); } }}