Honhar Engineer

Honhar Engineer
Thanks for visiting,stay connected for more updates !

Thursday, September 26, 2013

Definition of RMI & RPC,Difference between RPC & RMI,Codes for RPC & RMI

Remote Procedure Call (RPC)
Remote Procedure Call (RPC) is a powerful technique for constructing distributed client-server based applications.

RPC is a client /server infrastructure that increases the interoperability, portability and flexibility of an application by allowing the application to be distributed over multiple heterogeneous platforms.

An RPC is analogous to function call.

How RPC Works

An RPC is analogous to a function call. Like a function call, when an RPC is made, the calling arguments are passed to the remote procedure and the caller waits for a response to be returned from the remote procedure. Figure 32.1 shows the flow of activity that takes place during an RPC call between two networked systems. The client makes a procedure call that sends a request to the server and waits. The thread is blocked from processing until either a reply is received, or it times out. When the request arrives, the server calls a dispatch routine that performs the requested service, and sends the reply to the client. After the RPC call is completed, the client program continues. RPC specifically supports network applications.

Fig. 32.1 Remote Procedure Calling Mechanism A remote procedure is uniquely identified by the triple: (program number, version number, procedure number) The program number identifies a group of related remote procedures, each of which has a unique procedure number. A program may consist of one or more versions. Each version consists of a collection of procedures which are available to be called remotely. Version numbers enable multiple versions of an RPC protocol to be available simultaneously. Each version contains a a number of procedures that can be called remotely. Each procedure has a procedure number.

RPC Application Development

Consider an example:

A client/server lookup in a personal database on a remote machine. Assuming that we cannot access the database from the local machine (via NFS).

We use UNIX to run a remote shell and execute the command this way. There are some problems with this method:

* the command may be slow to execute.
* You require an login account on the remote machine.

The RPC alternative is to

* establish an server on the remote machine that can repond to queries.
* Retrieve information by calling a query which will be quicker than previous approach.

To develop an RPC application the following steps are needed:

* Specify the protocol for client server communication
* Develop the client program
* Develop the server program

The programs will be compiled seperately. The communication protocol is achieved by generated stubs and these stubs and rpc (and other libraries) will need to be linked in.

Reomote Procedure Calling Mechanism

A remote procedure is uniquely identified by the triple: ( program number, version number, procedure number).

ADVANTAGES:

Many distributed systems use Remote Procedure Calls(RPCs) as their main communication mechanism.

RMI is a Remote method invocation


- RMI (Remote Method Invocation) is a way that a programmer, using the Java programming language and development environment, can write object-oriented programming in which objects on different computers can interact in a distributed network. RMI is the Java version of what is generally known as a remote procedure call (RPC), but with the ability to pass one or more objects along with the request. The object can include information that will change the service that is performed in the remote computer. Sun Microsystems, the inventors of Java, calls this "moving behavior." For example, when a user at a remote computer fills out an expense account, the Java program interacting with the user could communicate, using RMI, with a Java program in another computer that always had the latest policy about expense reporting. In reply, that program would send back an object and associated method information that would enable the remote computer program to screen the user's expense account data in a way that was consistent with the latest policy. The user and the company both would save time by catching mistakes early. Whenever the company policy changed, it would require a change to a program in only one computer.

Sun calls its object parameter-passing mechanism object serialization. An RMI request is a request to invoke the method of a remote object. The request has the same syntax as a request to invoke an object method in the same (local) computer. In general, RMI is designed to preserve the object model and its advantages across a network.

RMI is implemented as three layers:

* A stub program in the client side of the client/server relationship, and a corresponding skeleton at the server end. The stub appears to the calling program to be the program being called for a service. (Sun uses the term proxy as a synonym for stub.)
* A Remote Reference Layer that can behave differently depending on the parameters passed by the calling program. For example, this layer can determine whether the request is to call a single remote service or multiple remote programs as in a multicast.
* A Transport Connection Layer, which sets up and manages the request.

A single request travels down through the layers on one computer and up through the layers at the other end.



Difference between RMI and RPC?


RMI or Remote Method Invokation is very similar to RPC or Remote Proceedure call in that the client both send proxy objects (or stubs) to the server however the subtle difference is that client side RPC invokes FUNCTIONS through the proxy function and RMI invokes METHODS through the proxy function. RMI is considered slightly superior as it is an object-oriented version of RPC.




Application Difference Between RPC and RMI?


The only real difference between RPC and RMI is that there is objects involved in RMI: instead of invoking functions through a proxy function, we invoke methods through a proxy.

What this means in practice is that we now want the client to hold references to remote objects that it can invoke methods on. These references should behave just like local objects, but when invoked dispatch the method invocation to the remote object.

Because we need to refer to both an object and a method now - not just a function - we extend the protocol so it first sends an object id across the socket, then the method and then the arguments. The server then dispatches the method to the remote object based on id and method name.-Rituparno Ganguly


Name :- SANJAY KUMAR JHA
Assignment name :- Remote Procedure Call (RPC)

// SERVER

import java.io.*;
import java.net.*;

class ClientHandler extends Thread {
protected Socket incoming;

public ClientHandler(Socket incoming) {
this.incoming = incoming;
}
public void run() {
int x,y;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
PrintWriter out=null ;// = new PrintWriter(new OutputStreamWriter(incoming.getOutputStream()));
out=new PrintWriter(incoming.getOutputStream(),true);
String str = in.readLine();
if (str.equals("add"))
{
x=Integer.parseInt(in.readLine());
y=Integer.parseInt(in.readLine());
int z=add(x,y);
out.flush();
out.println(z);
}
else if(str.equals("sub"))
{
x=Integer.parseInt(in.readLine());
y=Integer.parseInt(in.readLine());
int z=sub(x,y);
String s1="" + z;
out.println(s1);
}
incoming.close();
} catch (Exception e) {
System.out.println("Error: " + e);
}
}

public int add(int x1,int y1)
{
return x1+y1;
}
public int sub(int x1,int y1)
{
return x1-y1;
}

}

public class Server {
public static void main(String[] args) {
System.out.println("Server started.");
try {
ServerSocket s = new ServerSocket(9090);
for (;;) {
Socket incoming = s.accept();
new ClientHandler(incoming).start();
}
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}




// CLIENT



import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;

@SuppressWarnings("serial")
public class Client extends Applet implements ActionListener
{
Button ADD,SUB;
TextField t1,t2,t3;
public void init()
{
ADD=new Button("ADD");
SUB=new Button("SUB");
t1=new TextField(10);
t2=new TextField(10);
t1.setBounds(430,00,70,20);
t2.setBounds(500, 00, 70, 20);
ADD.setBounds(430, 50, 70, 20);
SUB.setBounds(500, 50, 70, 20);
ADD.addActionListener(this);
SUB.addActionListener(this);
t3=new TextField(10);
t3.setBounds(500, 100, 70, 20);
add(t3);
add(t1);
add(t2);
add(ADD);
add(SUB);
}
public void start() {
}
public void actionPerformed(ActionEvent ae)
{
String x1;
String s=ae.getActionCommand();
String p1,p2;
if(s.equals("ADD"))
{
BufferedReader in=null;
PrintWriter out=null;
Socket t=null;
String line1="add";
try {
String host="localhost";
t = new Socket(host, 9090);
in=new BufferedReader(new InputStreamReader(t.getInputStream()));
out=new PrintWriter(t.getOutputStream(),true);
out.println(line1);
p1=t1.getText();
p2=t2.getText();
System.out.println("x= "+p1+ "y= "+p2);
out.println(p1);
out.println(p2);
System.out.println("add1");
x1=in.readLine();
System.out.println("Addition of " + p1 + "and " + p2 + " : " + x1);
t3.setText(x1);
} catch (Exception e) {System.out.println("Error: " + e); }
System.out.println("Addition Performed");
}
else if(s.equals("SUB"))
{
BufferedReader in=null;
PrintWriter out=null;
Socket t=null;
String line1="sub";
try {
String host="localhost";
t = new Socket(host, 9090);
in=new BufferedReader(new InputStreamReader(t.getInputStream()));
out=new PrintWriter(t.getOutputStream(),true);
out.println(line1);
p1=t1.getText();
p2=t2.getText();
out.println(p1);
out.println(p2);
x1=in.readLine();
System.out.println("Subtraction of " + p1 + "and " + p2 + " : " + x1);
t3.setText(x1);
} catch (Exception e) { System.out.println("Error: " + e); }
System.out.println("Subtraction Performed");
}
}
}


// RMI

//SERVER


import java.net.*;
import java.rmi.*;

public class server
{
public static void main(String args[])
{
try
{
serverImpl serverImpl=new serverImpl();
Naming.rebind("Server",serverImpl);

}
catch (Exception ex)
{
System.out.println ("Exception .."+ex);
}
}
}

//SERVERIMPL

import java.rmi.*;
import java.rmi.server.*;
public class serverImpl extends UnicastRemoteObject implements

Serverintf
{

public serverImpl()throws RemoteException
{}
public double add(double d1,double d2)throws RemoteException
{
return d1+d2;
}
public double sub(double d1,double d2)throws RemoteException
{
return d1-d2;
}
public double mm(double d1,double d2)throws RemoteException
{
return d1*d2;
}
public double dd(double d1,double d2)throws RemoteException
{
return d1/d2;
}
}

// SERVER INTF

import java.rmi.*;

public interface Serverintf extends Remote
{
double add(double d1,double d2)throws RemoteException;
double sub(double d1,double d2)throws RemoteException;
double mm(double d1,double d2)throws RemoteException;
double dd(double d1,double d2)throws RemoteException;

}

// CALCULATOR

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.rmi.*;

class calculator extends JFrame implements ActionListener
{
String op,o1,o2;
JButton

b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b;
JTextField r;
JPanel p;
calculator()
{
p=(JPanel)getContentPane();
r=new JTextField(15);
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");
b8=new JButton("8");
b9=new JButton("9");
b10=new JButton("0");

b11=new JButton("+");
b12=new JButton("-");
b13=new JButton("*");
b14=new JButton("/");
b15=new JButton(".");
b16=new JButton("=");
b17=new JButton("Clear");

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);

b1.setActionCommand("1");
b2.setActionCommand("2");
b3.setActionCommand("3");
b4.setActionCommand("4");
b5.setActionCommand("5");
b6.setActionCommand("6");
b7.setActionCommand("7");
b8.setActionCommand("8");
b9.setActionCommand("9");
b10.setActionCommand("0");
b11.setActionCommand("+");
b12.setActionCommand("-");
b13.setActionCommand("*");
b14.setActionCommand("/");
b15.setActionCommand(".");
b16.setActionCommand("=");
b17.setActionCommand("Clear");

p.setLayout(new FlowLayout());
p.add(r);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b10);
p.add(b11);
p.add(b12);
p.add(b13);
p.add(b14);
p.add(b15);
p.add(b16);
p.add(b17);

addWindowListener(new WEvent());

}
class WEvent extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
}
public void actionPerformed(ActionEvent e)
{
String s,s1;
s=e.getActionCommand();
if (s.equals("="))
{

try
{
String

ServerURL="rmi://127.0.0.1/Server";
// String ServerURL="rmi://192.168.100.10/Server";
Serverintf

serverintf=(Serverintf)Naming.lookup(ServerURL);
o2=r.getText();
double

d1=Double.valueOf(o1).doubleValue();
double

d2=Double.valueOf(o2).doubleValue();

double res=0;

if(op.equals("+"))
{
res=serverintf.add(d1,d2);
}

else if(op.equals("-"))
{
res=serverintf.sub(d1,d2);
}
else if(op.equals("*"))
{
res=serverintf.mm(d1,d2);
}
else if(op.equals("/"))
{
res=serverintf.dd(d1,d2);
}

String result=""+res;
r.setText(result);

}
catch(Exception ee)
{
System.out.println("Exception"+ee);
}
}
else

if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/"))
{
op=s;
o1=r.getText();
r.setText("");
}

else if(s.equals("Clear"))
{
op="";
o1="";
o2="";
r.setText("");
}
else
{
s1=r.getText();
r.setText(s1+s);
}
}

public static void main(String args[])
{
calculator c;
c=new calculator();
c.setSize(200,250);
c.setTitle("CALCULATOR");
c.setVisible(true);

}
}
Do you Like this story..?

Get Free Email Updates Daily!

Follow us!

No comments:

Post a Comment