Distr File Sys RMI
Distr File Sys RMI
USING RMI
By George Koutsogiannakis
1
DISTRIBUTED SYSTEM
A
B
C D
4
IMPLEMENTATION
– JAVA ‘S RMI OVER IIOP (RMI OVER INTERNET INTER OPERABILITY
PROTOCOL) API.
• SERVICES CAN COMMUNICATE WITH EACH OTHER EVEN IF THEY ARE NOT
WRIITEN IN JAVA (AND THUS THEY ARE NOT USINGJAVA DATA TYPES)
• USES IDL (INTERFACE DEFINITION LANGUAGE) AS THE LANGUAGE TO
CREATE DATA TYPES THAT ARE UNDERSTOOD BY ALL NODES
REGARDLESS OF THE LANGUAGE USED TO REPRESENT A PROCESS AT THE
PARTICULAR NODE.
• JUST LIKE RMI IT FREES THE DEVELOPER FROM:
5
IMPLEMENTATION
– AN IMPLEMENTATION OF THE CORBA (COMMON OBJECT REQUEST
BROKER) SPECIFICATION.
• THERE ARE MANY VENDORS THAT PRODUCE IMPLEMENTATIONS IN
DIFFERENT PROGRAMMING LANGUAGES (INCLUDING JAVA OR C++).
• MS OFFERS IT IN ITS .NET FRAMEWORK. SUN MICROSYSTEMS OFFERS IT AS
A SEPARATE API IN THE JDK.
• HARDER TO USE THAN THE PREVIOUS TWO FRAMEWORKS.
– DEVELOPER H AS TO IMPLEMENT NETWORKING
– DEVELOPER HA STO IMPLEMENT SERIALIZATION ALGORITHMS
– DEVELOPER HAS TO IMPLEMENT IDL DATA TYPES MODULES.
6
IMPLEMENTATION USING RMI
SUPPOSE A SERVER HAS THE FILE SERVICE
That means that a client requesting the service(invoking the service openFile
remotely) has to pass to the server a string representing the name of the file to
be opened.
The server will execute the service and then return to the client an int
representing either success or the code for an error message.
7
IMPLEMENTATION USING RMI
Registry
2
1
1
client server
3
4
1. Server implements the method and registers an object that can be used by a client to a registry ( a database that
keeps track of all services and their locations). The client is aware of the remote object. It contacts the registry
to obtain information about the location of the server node, the port number where the process resides and the
object id number (Many clients can request the service simultaneously).
2. The registry responds with information about the server’ s IP address , port number for the service, object
id number etc.
3. The client sends a request to invoke the service on the server.
4. The server executes the service and returns the results to the client.
8
IMPLEMENTATION USING RMI
9
IMPLEMENTATION USING RMI
// implement the code for the remotely invokable method
public int openFile(name) {
//code to open the file
return int;
}
//main method for the class that starts the server
public static void main ( String [ ] args) {
// start the server by instantiating an instance of the class
MyRemoteFileSystemImpl rfs = new MyRemoteFileSystemImpl ( ) ;
//create a string that represents the location of the registry and the name of a remote object
that can be used by a client to invoke the service String
serverobjectname = “//localhost/remoteobject;
// we assume that the registry resides localhost (or it can be the ip address of another node
//proceed to register the remote object with the registry
Naming.rebind ( serverobjectname, rfs );
}//end of main
}// end of the class
THTAT’S ALL IS NEEDED FOR THE SERVER!
10
IMPLEMENTATION USING RMI
• CLIENT CODING
– Create a class that represents the client process.
public class Client {
// global declarations as needed
//constructor
//other methods as needed 9including a main method for the client
// method that needs to open a file from the File Server node
public void getRemoteFile ( ) {
String serverobjectname = “//localhost/remoteobject;
// lookup remote object on the registry. Notice that the name of the interface is used below
MyRemoteFileSystem myfile = mew (MyRemoteFileSystem) Naming.lookup ( serverobjectname) ;
//proceed with remote invocation
int c = myfile. openFile(filename) ;
// where filename is a string representing the name of the file. It is passed to the server. The server returns the
result which is capured by the int c.
} //end of the method
}//end of the client class
11
IMPLEMENTATION USING RMI
• As you can see the implementation is simple. Most of the work is handled by the system
under the hood.
• COMPILING
– Compile the server interface class first by using javac.
– Compile the server class that implemets the interface next by using the javac
compiler
– Compile the server class that implements the interface one more time by using the
rmic compiler (it comes with the jdk). This produces an extra class called the stub
> rmic –v1.2 MyRemoteFileSystemImpl //notice that no extension is used after
the file name
The file MyRemoteFile SystemImpl_stub.class is produced
– Compile the client class by using javac compilker
12
IMPLEMENTATION USING RMI
• MAKING IT WORK
– Place the compiled interface file ( MyRemoteInterface.class) in the client node (in
addition to the server node)
– Place the stub (MyRemoteFileSystemImpl_stub.class) in the client node also (in
addition to the server node).
– Place the server MyRemoteFielSystem.class on the server node
– Place the client code on the client node.
– Start the registry. RMI provides registry automatically
> start rmiregistry
– Start the server
>java MyRemoteFileSystemImpl
– Start the client
>java Client
• Note: In windows open a DOS window for each process.
13