greycellcatalyst-teaches.blogspot.com

Friday, December 16, 2005

Teaching COM and CORBA to Indian Undergraduates

Young minds, I know its a huge world with so many things around. I am sure you people will be amazed with the volume of jargons and IT keywords thrown around you in this world. But the key to success in this world is to know your basics right.
In this section I will teach CORBA and COM basics to you all. I know the web is full of this stuff, but I prefer to teach it with the level of your knowledge in mind.
Objective: To teach my lovable college going and C programming young wizards, the basics of distributed programming and interface based software development.
Pre-requisite: Well should have the taste to read .... and well know some basic programming.
Here I go..
Well there are two college going kids in my family, Chabby and Priya. Chabby wants to add two numbers in his program. Priya knows to add two numbers. Chabby wants to develop a multiplier in two days and Priya says I can develop you an adder in two days. As we know multiplication is repeatative addition, Chabby needs to use the addition program. So the young minds sit together and start penning down. Learning the mantra - Think, Ink , Think, Ink , smile and then code..
Ok, so the requirement is to develop a multiplier. Known fact is Multiplication is repeatative addition.
Good next - Priya will write the adder in two days. She will write it on her machine and wants the adder to run on her machine. She will NO way tell chabby how she wrote the add program.
Chabby says- I am ok with it as long as I can add. So i will be able to develop the multiplier in two days.
So, alls well.. but then Chabby says, Priya will take two days, I will take two days.. so basically we will require four days. Can we not do it in a lesser number of days?
I was just passing by.. I told them... well folks thats a nice thought, and I am sure you people can achieve the same.
Chabby was suprised - how can I compile or develop my code without the adder..
I say, well you do it this way. First, since you both know to use Java, just use the RMI support in it.
First both of you sit across the table.. Take 1/2 a day and design this class.

//AddInterface.java
import java.rmi.Remote; // need to support remote programming across machines. SO keep it.
public interface AddInterface extends java.rmi.Remote {
public int Add(int a, int b) throws java.rmi.RemoteException;
}

So basically you define a java interface, which will extend the java.rmi.Remote class. This inheritance is needed because your aim is to add and multiply. You shouldnt worry about about remote and distributed programming.
Now , Priya you can take this interface with you and chabby please take this with you. You both can use this interface and start your coding in parallel in two days. This interface for now, will help you compile and develop your classes independently.
Now, the critical part is, we should see how Chabby will do the development without the add method. Then we shall see how Priya will develop the add method.
Well Chabby, you write your code like this..

//Multiply.java
import java.rmi.RemoteException;import java.net.MalformedURLException;import java.rmi.NotBoundException;import javax.rmi.*;import java.util.Vector;import javax.naming.NamingException;import javax.naming.InitialContext;import javax.naming.Context;
public class Multiply{
public static void main( String args[] ) {
Context ic; Object objref; AddInterface Add;
try {
ic = new InitialContext(); //STEP1: Get the Object reference from the Name Service
// using JNDI call.
objref = ic.lookup("AddService");
System.out.println("Client: Obtained a ref. to Add server.");
// STEP 2: Narrow the object reference to the concrete type and
// invoke the method.
Add= (AddInterface) PortableRemoteObject.narrow( objref, AddInterface.class); System.out.println("The First Int is 10"); System.out.println("The First Int is 20");
int k=0;
for(int i=0;i<=20;i++){
k = Add.Add(k,10);
}
System.out.println(" The final product of 10 * 30 = " + k);
} catch( Exception e ) { System.err.println( "Exception " + e + "Caught" ); e.printStackTrace( ); return; } }}


The above code just does this creates an instance of the interface. Asks the naming server, could you find me this service provider. Then finds the service provider and uses the add method. The remoteness found here is, the person who wants to add doesnt need to know where the adder is. he can ask the naming service for the same.
Ok, How will the naming service know who has the adder service. This will be done by Priya.
Now, before Chabby runs his code, Priya Implements the add method and then runs it as a server. This can be done like this
First Implement the add interface

//AddImpl.javaimport javax.rmi.PortableRemoteObject;
public class AddImpl extends PortableRemoteObject implements AddInterface {
public AddImpl() throws java.rmi.RemoteException {
super(); // invoke rmi linking and remote object initialization
}
public int Add(int a, int b) throws java.rmi.RemoteException {
System.out.println( "Add " + a + " + " + b );
System.out.flush(); return a + b;
}
}


After you implement the add method you need to run this as a add service. So priya will run this add service, informing the naming service where she is up and running.

//AddServer.javaimport javax.naming.InitialContext;import javax.naming.Context;
public class AddServer{
public static void main(String[] args) {
try {
// Step 1: Instantiate the Add servant
AddImpl Addref= new AddImpl();
// Step 2: Publish the reference in the Naming Service
// using JNDI API Context initialNamingContext = new InitialContext(); initialNamingContext.rebind("AddService", Addref);
System.out.println("Add Server: Ready...");
} catch (Exception e) { System.out.println("Trouble: " + e); e.printStackTrace(); } }}

Once this is done.. Priya and Chabby can test this on three command prompts on the machine.
on one command prompt , do run tnameserv.
in another command prompt , just say

java -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory -Djava.naming.provider.url=iiop://localhost:900 AddServer

in the third say java -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory -Djava.naming.provider.url=iiop://localhost:900 Multiply

Here the number 900 is the port on which the tnameserver is running !
Well you all can see the output for yourself and appreciate distributed programming. Ok, i have told you how to run this on one PC. Try to run the same on multiple PC on your own? If you do this, you have learnt the basics completely.
Also please read the documentation of the classes used here, the remote objects and rmi classes.