Friday, 28 February 2014

How To...

Proxy Pattern:
Provides a surrogate or placeholder for another object to control access to it. "Proxy" is actually a repesentative of another object.
A "remote Proxy" controls access to a remote object.
A "virtual Proxy" controls access to a resource that expensive to create.
A "protection Proxy" controls access to a resourse based on access rights.(security)
In this blog I am going to explain first one . This involves RMI ie Remote Method Invocation. This is the one topic in Java , I couldn't get to see, or touch.  Again all information here are referenced from Head First Design Patterns Book.
Image is taken from http://www.oodesign.com/proxy-pattern.html

Suppose client wants to call some methods on a object which is in Server's heap. Server is remote to the client and it has to talk to the server which is on the other side of  the network. RMI stub  act as a client proxy on the client's heap , where as RMI Skeleton act as server proxy on the Server's heap. Mainly these proxies help in serialization (write object in server side, readobject in client side-deserializing, This is before java 2. Now a days we can get proxies dynamically on the go. - just note that Proxies are  different pattern from decorator patterns.)
This needs two sides programming
 1) Client  side program or code  which lookup the RMI registry to get the stub object ,client invokes the method on the stub object, as if the stub is the real service.
       MyInterface service = (MyInterface)Naming.lookup("rmi://127.0.0.1/RemoteHelloworld");
2)Server side programming to give service.
           5 steps for these.
    i) Make a  Remote interface
            a)Extend java.rmi.Remote
              b)Declare that all methods throw a RemoteException.
               c)be sure arguments and return values are primitives or Serializable.
              import java.rmi.*;
                public interface MyRemoteInterface extends Remote {
                  public String helloWorld () throws RemoteException;
                 }
           ii)Make a Remote implementation
                a)Implement the Remote interface
                  b)Extend UnicastRemoteObject
                   c)Write a no-org constructor that declares a RemoteException
                   d)Register Service with the RMI registry
              public class MyImplementation extends UnicastRemoteObject implements MyRemoteInterface {
   public MyImplementation() throws RemoteException {}
      public String helloWorld() {
           return "hello world";
}
//more code
}
In tester in the main method you register the service with the RMI registry
  try {
MyInterface service = new MyImplementation();
Naming.rebind("RemoteHelloworld",service); //  you can give any name to hide your object.
} catch (Exception ex){....}
iii) generate stubs and skeletons
        run rmic on the remote implementation class (not on remote interface)
           %rmic MyImplementation
  iv)run rmiregistry
        bring up a terminal and start the rmiregistry
     %rmiregistry
v)start the service
        bring up another terminal and start your service
%java MyImplementation
 (run the main method of service impementation class)
Suppose I am a client wants to monitor some dogs at remote service. Then,
 First on the server side, we have to make the service available.
To make remote interface:
     import java rmi.*;
    public interface DogRemote extends Remote {
        public int getMicroChipNo() throws RemoteException;
        public String getSpecies() throws RemoteException;
         public String getLocation() throws RemoteExcetion;
         public String getCondition() throws RemoteExeption;
}
If you want to make a method like
public Collar getCollar() throws RemoteException; then you have to make "Collar " Serializable.  Just you can do public class Collar implements Serializable  or extends serializable .(there is no methods to implement).But you have to import java.io.*;   If the class contains any other objects you can make them serializable or declare it as transient.
make remote implementation
import java.rmi.*;
import java.rmi.server.*;
public class Dog extends UnicastRemoteObject implements DogRemote
//instance variable here
public Dog(String location,String species,int microchipno) throws RemoteException{
//code here
}
public int getMicroChipNo() {
    return microchipno;
}
public String getSpecies(){
return species;
}
// all other get codes here
}
Registering with RMI registry
public class DogTester {
   public static void main(String[] args){//you can also input arguments here.
try {
DogRemote pudsy = new Dog("London","poodle", 1234);
Naming.rebind("//"+"London.dogcanal.com" + "/dog",pudsy);/**here you can add differnt locations and different names for objects by passing args if you want.**/
}catch (Exception e){
e.printStackTrace();
}
}
}
Then run rmiregistry followed by the main method of DogTester.
On the client side,
Class to monitor dog and a tester
import java.rmi.*
public class DogMonitor{
  DogRemote pudsy;
public DogMonitor (DogRemote pudsy){
this.pudsy = pudsy;
}
public void dogDetails(){
try
{
      System.out.println("Dog from"+pudsy.getLocation());
     System.out.println("Dog Species"+pudsy.getSpecies());
      System.out.println("Condition"+pudsy.getCondition());
      System.out.println("Microchip number"+getMicroChipNo());
}  catch (RemoteException e){
e.printStackTrace();
     }
   }
}
Write a monitor tester
      import java.rmi.*;
     public class DogMonitorTester{
            public static void main(String[] args){
                  try
                     {
                          DogRemote  pudsy = (DogRemote)Naming.lookup("rmi://London.dogcanal.com/dog");
                          DogMonitor monitor = new DogMonitor(pudsy);
                            }
                              catch (Exception e){
                                    e.printStackTrace();
                             }
                         }
                          monitor.dogDetails();
}
}

       How to monitor dogs( or objects) remotely.             

Comments are welcome. (So that I can correct my mistakes)

No comments:

Post a Comment