190 likes | 328 Views
Example: RMI Program. How to write it. Program an ATM Machine. Allow the client to withdraw, deposit, transfer money to or from their account Also allow to obtain the current balance Some other stuff ATM machine is the client Bank with Account is the server. Write the code.
E N D
Example: RMI Program How to write it
Program an ATM Machine • Allow the client to withdraw, deposit, transfer money to or from their account • Also allow to obtain the current balance • Some other stuff • ATM machine is the client • Bank with Account is the server
Write the code • Write the Remote Interface, call it Account • Write the implementation: AccountImpl • Write the server: RegAccount • Write the client: AccountImpl • Stubs and skeletons will be generated
Remarks • Note that Account extends Remote • Every remote method in the interface throws a RemoteException • This interface is present on the server and on the client
Remarks • Since this class is remote it must implement the UnicastRemote interface • The concrete methods from the remote interface all must throw a RemoteException • The rest is just POJ (Plain old Java)
Define the Server • Make an instance whose methods will be called by the client • Register this object with the registry • Note that it contains the main{} that defines the server • No remote exceptions here
Write the Client:AccountClient.java • Need a Security Manager (later) • Get a stub from the server by Naming.lookup() • Make the remote calls--here getName() and getBalance() • The URL parameter to the lookup routine returns the remote object it references • main(){} here is the client.
Running the Example ATM • Compile the java files: javac Account*.java • Create stub from AccountImpl: rmic AccountImpl • Start the registry: rmiregistry • Start the server: java RegAccount • Start the client: java AccountClient
What is Missing • A Security Manager • A Security Policy • A Codebase • Compile and Execution commands that incorporate these • An appropriate deployment strategy
Security Manager • If your application only runs locally (sandbox) then there is no need for security • But RMI is designed to run on networks and the Internet • Hence the need for security • The Security Manager is a Java class that checks whether certain operations are permitted • Is available under java.lang.SecurityManager
Security Manager (ctd) • Checks permissions according to a security policy • Infractions of permissions cause a SecurityException • Set up by issuing: System.setSecurityManager (new RMISecurityManager()); • Enclose critical code in try block
Permissions • Set by a security policy as written by the programmer • For purposes of testing a blanket granting of permissions can be issued: permission java.security.AllPermission; • Then after deploying be a bit more restrictive: grant{ permission java.net.SocketPermission “*:1024-65535”, “connect, accept, resolve”; permission java.net.SocketPermission “*:80”, “connect”; };
Codebase • When starting the server indicate the download directory by means of a codebase property: For example java -D ... - Djava.rmi.server.codebase=http://localhost/download/ xxxServ This represents the URL location from which the stubs can be downloaded Note the final slash “/” on the URL
Running the Client • Need to include security with -D • Start the Client: • java -Dsecurity.policy=sec.policy xxxClient • sec.policy needs a path • Do this for both server and client