简体   繁体   中英

Java RMI p2p system binding with registry.lookup()

I am working on a P2P simple system and i have a problem when the client after successfully locates the file on the index server, tries to obtain the file requested. It seems through the research i have already done that there is a problem when binding to the server's rmiregistry which is "CentralIndex" in my case. The client cannot bound the lookup() object. Please HELP!. Server's code (working great):

public class CIServer implements SharedInterface{

Hashtable<String,Integer> rmiregistry;

public CIServer(){
    super();
    this.rmiregistry = new Hashtable<String,Integer>();
}

@Override
public int peerSearch(String filename){

    if(rmiregistry.containsKey(filename)){
        return rmiregistry.get(filename);
    }
    System.out.println("The file does not exist.");
    return 1000;
}

@Override
public boolean insertToRegistry(int peerID,String filename){
    rmiregistry.put(filename, peerID);
    return rmiregistry.isEmpty();
}

public static void main(String[] args){
    System.setProperty("java.security.policy", "server.policy");
    //System.setProperty("java.rmi.server.codebase", "file:/build/classes");

    if (System.getSecurityManager() == null){
        System.setSecurityManager(new SecurityManager());
    }

    try{
    String servername = "CentralIndex";
    SharedInterface handler = new CIServer();        
    SharedInterface stub = (SharedInterface) UnicastRemoteObject.exportObject(handler,0); 
    Registry reg = LocateRegistry.getRegistry();
    reg.rebind(servername, stub);
    System.out.println("CentralIndex service bound");
    }catch (Exception e){
    System.err.println("CentralIndex exception: ");
    e.printStackTrace();
    }
}

}

client's code:

public class RMIClient {
static String ServerName = "CentralIndex";
static int peerID = 1;
static File fileDirectory = new File("Client_files\\");
static String downloadDirectory = "temp";

boolean download(byte[] file, String filename){
    BufferedOutputStream out;
    try{
        out = new BufferedOutputStream
                (new FileOutputStream(downloadDirectory+filename));
        out.write(file, 0, file.length);
        out.flush();
        out.close();
    }catch (FileNotFoundException e){
        System.err.println("This file path does not exist.");
        e.printStackTrace();
        return false;
    }catch (IOException e){
        e.printStackTrace();
        return false;
    }
    return true;        
}

void directoryCheck(SharedInterface file){
    String[] subDirectory = fileDirectory.list();
    if (subDirectory == null) {

    }else {
        for (int c = 0; c < subDirectory.length; c++){
            String name = subDirectory[c];
            try{
                file.insertToRegistry(peerID, name);                    
            }catch (RemoteException e){
                System.err.println("Registry error");
                e.printStackTrace();
            }
        }
    }
}

public static void main(String[] args){        
    RMIClient client = new RMIClient();
    Scanner in = new Scanner(System.in);
    int peerAsServerID = 0;
    System.out.println("##############################\n"
            + " Welcome to AARK p2p network. \n"
            + "##############################");
    System.out.println("To search a file just\n"
            + " type the name below: \n -->");
    String filename = in.nextLine();
    System.setProperty("java.security.policy", "client.policy");
    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new SecurityManager());
    }
    try {
            Registry reg = LocateRegistry.getRegistry();
            SharedInterface handler = (SharedInterface) reg.lookup(ServerName);
            client.directoryCheck(handler);
            peerAsServerID = handler.peerSearch(filename);
            System.out.println(peerAsServerID);

            if (peerAsServerID == 1000){
                System.out.println("No such file.");
            }else {
                System.out.println("File "+filename+" found on peer: "+peerAsServerID);
            }
        }catch (Exception e){
            System.err.println("Handler exception");
            e.printStackTrace();
        }


        try{
            if (peerAsServerID == 1000 || peerAsServerID == 0){
                System.out.println("File:"+filename+" not found.");
            } else{

                String clientAsServerName = String.valueOf(peerAsServerID);     

                Registry reg = LocateRegistry.getRegistry();

                Handler stub = (Handler)reg.lookup(clientAsServerName);

                byte[] file = stub.obtain(filename);


                if (client.download(file,filename)){
                    System.out.println("Download successful.\n "
                            + "The requested file is at your temp folder.");
                }else{
                    System.out.println("Download unsuccessful.\n"
                            + " There was a problem. Please try again.");
                }
            }
         }catch (Exception e){
            System.err.println("Download exception");
            e.printStackTrace();
            }
}
}

Interface:

public interface SharedInterface extends Remote {
int peerSearch (String filename) throws RemoteException;
boolean insertToRegistry(int peerID, String filename) throws RemoteException;
}

another interface used by clients as servers:

public interface Handler extends Remote {
byte[] obtain(String filename) throws RemoteException;
}

and this is finally what i got:

file5.txt
File found on peer: 2
Download exception
java.rmi.NotBoundException: 2
at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:136)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:409)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:267)
at sun.rmi.transport.Transport$1.run(Transport.java:177)
at sun.rmi.transport.Transport$1.run(Transport.java:174)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)

at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:377)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at RMIClient2.RMIClient2.main(RMIClient2.java:94)
    BUILD SUCCESSFUL (total time: 2 seconds)

PLease someone help me fix that asap! Thank you all in advance.

You're looking up the wrong Registry. You need to lookup the Registry at the server host, not the one at the client. You don't even need a Registry at the client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM