idl interface
TRANSCRIPT
PROGRAM:
// Define IDL Interfacemodule simplestocks
{
interface StockMarket
{
//declare the methods here which are need to be implemented
float get_price(in string symbol);
};
};
// Implement the interface
import org.omg.CORBA.*;
import simplestocks.*;
public class StockMarketImpl extends StockMarketPOA
{
private ORB orb;
public void setORB(ORB v)
{
orb=v;
}
//implement the get_price method
public float get_price(String symbol)
{
float price=0;
for(int i=0;i<symbol.length();i++)
{
price+=(int)symbol.charAt(i);
}
price/=5;
return price;
}
public StockMarketImpl()
{
super();
}
}
//Server Program
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA.*;
import java.util.Properties;
import simplestocks.*;
public class StockMarketServer
{
public static void main(String[] args)
{
try
{
//create and initialize the orb
ORB orb=ORB.init(args,null);
//get reference to root POA and activate the POA manager
POA rootpoa=POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
//create an implementation and register it with ORB
StockMarketImpl ss=new StockMarketImpl();
ss.setORB(orb);
//get object reference from the servant
org.omg.CORBA.Objectref=rootpoa.servant_to_reference(ss);
StockMarket hrf=StockMarketHelper.narrow(ref);
//get the root naming context
//naming service invokes the name service
org.omg.CORBA.Object orf=orb.resolve_initial_references("NameService");
//use namingcontextext which is the part of the interoperable naming service specification
NamingContextExt ncrf=NamingContextExtHelper.narrow(orf);
//bind the object reference in the naming service
NameComponent path[]=ncrf.to_name("StockMarket");
ncrf.rebind(path,hrf);
System.out.println("StockMarket server is ready");
//Thread.currentThread().join();
//wait for invocations from client
orb.run();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
// Client Program
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import simplestocks.*;
import org.omg.CosNaming.NamingContextPackage.*;
public class StockMarketClient
{
public static void main(String[] args)
{
try
{
//create and initialize the orb
ORB orb=ORB.init(args,null);
//get the root naming context
NamingContextExt
ncRef=NamingContextExtHelper.narrow(orb.resolve_initial_references("NameSe
rvice"))
// use NamingContextExt instead of namingcontext.This is part of the
interoperable naming service
NameComponent path[]={new NameComponent("NASDAQ","")};
//resolve the object reference in the naming
StockMarketmarket=StockMarketHelper.narrow(ncRef.resolve_str("StockMarket
"));
//the result is obtained from the get_price method
System.out.println("Price of My company
is"+market.get_price("My_COMPANY"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
PROGRAM:
HELLOCLIENTimport HelloApp.*;import org.omg.CosNaming.*;import org.omg.CosNaming.NamingContextPackage.*;import org.omg.CORBA.*;public class HelloClient{ static Hello helloImpl;
public static void main(String args[]) { try
{ ORB orb = ORB.init(args, null); org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService"); NamingContextExt ncRef =NamingContextExtHelper.narrow(objRef); String name = "Hello";
helloImpl = HelloHelper.narrow(ncRef.resolve_str(name));System.out.println("Obtained a handle on server object: " + helloImpl);
System.out.println(helloImpl.sayHello()); helloImpl.shutdown();
} catch (Exception e) {
System.out.println("ERROR : " + e) ; e.printStackTrace(System.out);
} }}HELLOSERVERimport HelloApp.*;import org.omg.CosNaming.*;import org.omg.CosNaming.NamingContextPackage.*;import org.omg.CORBA.*;import org.omg.PortableServer.*;import org.omg.PortableServer.POA;import java.util.Properties;class HelloImpl extends HelloPOA {
private ORB orb; public void setORB(ORB orb_val) {
orb = orb_val; }
public String sayHello() {
return "\nHello world !!\n"; }
public void shutdown() {
orb.shutdown(false); }}public class HelloServer {
public static void main(String args[]) {
try{
ORB orb = ORB.init(args, null); POA rootpoa = POAHelper.narrow (orb.resolve_initial_references("RootPOA")); rootpoa.the_POAManager().activate(); HelloImpl helloImpl = new HelloImpl(); helloImpl.setORB(orb); org.omg.CORBA.Object ref = rootpoa.servant_to_reference(helloImpl); Hello href = HelloHelper.narrow(ref);
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService") NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
String name = "Hello"; NameComponent path[] = ncRef.to_name( name ); ncRef.rebind(path, href); System.out.println("HelloServer ready and waiting ..."); orb.run(); } catch (Exception e) { System.err.println("ERROR: " + e); e.printStackTrace(System.out); } System.out.println("HelloServer Exiting ..."); }}
HELLO.IDLmodule HelloApp
{ interface Hello { string sayHello(); oneway void shutdown();
};};
PROGRAM:
//one.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface one extends Remote
{
double add(double n1, double n2) throws RemoteException;
double sub(double n1, double n2) throws RemoteException;
}
//oneimpl.java
import java.rmi.RemoteException;
import javax.rmi.PortableRemoteObject;
class oneimpl extends PortableRemoteObject implements one
{
public oneimpl() throws RemoteException
{
super();
}
public double add(double n1, double n2) throws RemoteException
{
System.out.println("Number added are " + n1 + " " + n2);
System.out.println("Total is " + (n1+n2));
return n1 + n2;
}
public double sub(double n1, double n2) throws RemoteException
{
System.out.println("Number added are " + n1 + " " + n2);
System.out.println("Total is " + (n1-n2));
return n1 - n2;
}
}
//rmiserver.java
import javax.naming.*;
import java.util.Properties;
public class rmiserver
{
public static void main(String args[])
{
try
{
oneimpl oneimplObj = new oneimpl();
Properties props = new Properties();
props.put("java.naming.factory.initial","com.sun.jndi.cosnaming.CNCtxFactory");
props.put("java.naming.provider.url","iiop://localhost:900");
InitialContext ic = new InitialContext(props);
ic.rebind("cal", oneimplObj);
System.out.println("RmiServer Started \n");
}
catch (Exception ex)
{
ex.printStackTrace();
} }
}
//rmiclient.java
import javax.rmi.PortableRemoteObject;
import javax.naming.InitialContext;
import java.util.Properties;
public class rmiclient
{
public static void main(String args[])
{
try
{
Properties props = new Properties();
props.put("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory");
props.put("java.naming.provider.url", "iiop://localhost");
InitialContext ic = new InitialContext(props);
one oneimpObj = (one) PortableRemoteObject.narrow(ic.lookup ("cal"), one.class );
double result1 = oneimpObj.add(4,6);
double result2 = oneimpObj.sub(7,6);
System.out.println("Result is: " + result1);
System.out.println("Result is: " + result2);
}
catch (Exception ex)
{
ex.printStackTrace();
} } }
PROGRAM:
IN VISUAL BASIC DLL
Public Function add (ByVal a As Integer, ByVal b As Integer)
MsgBox "Result:" + CStr(a + b)
End Function
IN JAVA
NativeImpl.java
import java.io.DataInputStream;
import java.io.IOException;
class NativeImpl
{
public native void add(int a, int b);
public static void main(String args[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);
NativeImpl impl=new NativeImpl();
System.out.println("Enter number 1 & 2:");
impl.add(Integer.parseInt(in.readLine()),
Integer.parseInt(in.readLine()));
}
static
{
System.loadLibrary("dllproj");
}
}
IN VC++ WRAPPER
STDAFX.H FILE:
#include "c:\jdk1.5\include\jni.h"
#include "c:\wings\jnidemo\NativeImpl.h"
#import "c:\wings\jnidemo\project1.dll"
using namespace Project1;
DLLPROJ.CPP FILE:
JNIEXPORT void JNICALL Java_NativeImpl_add(JNIEnv *, jobject, jint a, jint b)
{
HRESULT hresult;
CLSID clsid;
_Class1 *t;
int num1=(int)a;
int num2=(int)b;
int num3;
jint ans;
CoInitialize(NULL);
hresult=CLSIDFromProgID(OLESTR("Project1.Class1"), &clsid);
hresult=CoCreateInstance(clsid,NULL,CLSCTX_INPROC_SERVER,
__uuidof(_Class1),(LPVOID*) &t);
if(hresult == S_OK)
{
t->add(a,b);
}
else
{
AfxMessageBox("Error");
}
}
PROGRAM:
//TempCalc.idl
moduleTempconvcalc
{
interfaceTempCalc
{
doubleget_celcius(in string symbol);
};
};
//TempCalcImpl.java
importorg.omg.CosNaming.*;
importorg.omg.CORBA.*;
importTempconvcalc.*;
public class TempCalcImpl extends _TempCalcImplBase
{
public double get_celcius(String symbol)
{
doublecelcius=Double.parseDouble(symbol);
doublefarenheit=1.8*celcius+32;
returnfarenheit;
}
publicTempCalcImpl()
{
super();
}
}
//TemplCalcserver.java
importorg.omg.CosNaming.*;
importorg.omg.CORBA.*;
importTempconvcalc.*;
public class TempCalcserver
{
public static void main(String args[])
{
try
{
ORB orb=ORB.init(args,null);
TempCalcImpltempImpl=new TempCalcImpl();
orb.connect(tempImpl);
org.omg.CORBA.ObjectobjRef=orb.resolve_initial_references("NameService");
NamingContextncRef=NamingContextHelper.narrow(objRef);
NameComponentnc=new NameComponent("TEMPCALC"," ");
NameComponentpath[]={nc};
ncRef.rebind(path,tempImpl);
System.out.println("Tempconv server is ready");
Thread.currentThread().join();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//TempCalcclient.java
importorg.omg.CosNaming.*;
importorg.omg.CORBA.*;
importTempconvcalc.*;
importjava.io.DataInputStream;
public class TempCalcclient
{
public static void main(String args[])
{
try
{
DataInputStream din=new DataInputStream(System.in);
ORB orb=ORB.init(args,null);
NamingContext ncRef=NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));
NameComponentpath[]={new NameComponent("TEMPCALC"," ")};
TempCalccalc=TempCalcHelper.narrow(ncRef.resolve(path));
System.out.println("Enter the celcius:");
String cel=din.readLine();
System.out.println("Temperature of the city in Farenheit is"+calc.get_celcius(cel));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Output:
Server
Client: