Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

Create a application using RMI, where an RMI client can download a text file from the RMI server. Also identify the design pattern being used

Create a distributed application using RMI, where an RMI client can download a text file from the RMI server. Also identify the design pattern being used.

Java Program - 

RemoteInterface.java - 

package com.gpm.ex3;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.rmi.Remote;

import java.rmi.RemoteException;

import java.util.ArrayList;


public interface RemoteInterface extends Remote

{

    public boolean CheckAvailability(String filename) throws RemoteException;

    public ArrayList<String> DownloadFileData() throws RemoteException,FileNotFoundException,IOException;


}


FileServant.java - 

package com.gpm.ex3;

import java.rmi.RemoteException;

import java.rmi.server.UnicastRemoteObject;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.BufferedReader;

import java.io.IOException;

import java.util.ArrayList;


public class FileServant extends UnicastRemoteObject implements RemoteInterface {

    String path = "D:/Manthan/";

    String fullPath;

    public FileServant() throws RemoteException

    {

        super();

    }

    @Override

    public boolean CheckAvailability (String filename) throws RemoteException

    {

        boolean isAvailable =false;

        fullPath = path.concat(filename);


        File file = new File(fullPath);


        if(file.exists())

        {

           if (file.canRead())

           {

               System.out.println("File is available");

               isAvailable = true;

           }

        }

        return isAvailable;

    }

    @Override

    public ArrayList<String> DownloadFileData() throws RemoteException, FileNotFoundException, IOException

    {

        ArrayList<String>data = new ArrayList<String>();

        FileReader fileReader = new FileReader(fullPath);

        BufferedReader bufferedReader = new BufferedReader(fileReader);


        System.out.println("Reading File>>");

        String currentLine = bufferedReader.readLine();

        while(currentLine != null)

        {

            data.add(currentLine);

            currentLine = bufferedReader.readLine();

        }

        System.out.println(("Read complete>> "));

        fileReader.close();


        System.out.println("Transfering file to the client>>");

        return data;

    }

}


Server.java - 

package com.gpm.ex3;

import java.rmi.RemoteException;

import java.rmi.registry.Registry;

import java.rmi.registry.LocateRegistry;

public class Server {
    public static void main(String args[]) throws RemoteException
    {
        System.out.println("Server started>>");
        Registry registry = LocateRegistry.createRegistry(6000);
        registry.rebind("FileServant", new FileServant());
    }
}

Client.java - 

package com.gpm.ex3;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.net.MalformedURLException;

import java.rmi.Naming;

import java.rmi.NotBoundException;

import java.rmi.RemoteException;

import java.util.ArrayList;

import java.io.File;

import java.io.FileWriter;

public class Client {
    public static void main(String args[]) throws NotBoundException, MalformedURLException, RemoteException, FileNotFoundException, IOException{
        System.out.println("Client started");
        String filename = "test.txt";
        String filePath = "D:/Manthan/Testing/";
        ArrayList<String>data = new ArrayList<String>();
        RemoteInterface FileInterface = (RemoteInterface)Naming.lookup("rmi://192.168.1.14:6000/FileServant");
        if(FileInterface.CheckAvailability(filename))
        {
            data = FileInterface.DownloadFileData();
        }
        if (data != null)
        {
            String finalPath = filePath.concat(filename);
            File file = new File(finalPath);
            file.createNewFile();
            if(file.exists())
            {
                if(file.canRead())
                {
                    FileWriter writer = new FileWriter(finalPath, true);
                    for (String line : data)
                    {
                        writer.write(line);
                    }
                    System.out.println("Download complete>>");
                    writer.close();
                }
            }
        }

    }
}


Output - 




Post a Comment

0 Comments

Ad Code

Responsive Advertisement