Google Ads

Wednesday, March 10, 2010

STOCK MARKET EXCHANGE INFORMATION USING CORBA


StockMarket.idl

module SimpleStocks
{
interface StockMarket
{
float get_price(in string symbol);
};
};

StockMarketImpl.java

import org.omg.CORBA.*;
import SimpleStocks.*;
public class StockMarketImpl extends StockMarketPOA
{
private ORB orb;
public StockMarketImpl(ORB orb)
{
this.orb=orb; }
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;
}
}

StockMarketServer.java

import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import SimpleStocks.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import org.omg.CosNaming.NamingContextPackage.*;
public class StockMarketServer
{
public static void main(String[] args)
{
try
{
ORB orb = ORB.init(args,null);
StockMarketImpl stockMarketImpl = new StockMarketImpl(orb);
POA rootpoa=POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
org.omg.CORBA.Object ref=rootpoa.servant_to_reference(stockMarketImpl);
StockMarket href=StockMarketHelper.narrow(ref);
org.omg.CORBA.Object object = orb.resolve_initial_references("NameService");
NamingContextExt root = NamingContextExtHelper.narrow(object);
NameComponent name = new NameComponent("NASDAQ","");
NameComponent path[] = {name};
root.rebind(path,href);
System.out.println("Server is Ready.............");
orb.run();
}
catch(Exception e)
{
System.err.println("ERROR"+e);
}
}}

StockMarketClient.java

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
{
ORB orb = ORB.init(args,null);
org.omg.CORBA.Object object=orb.resolve_initial_references("NameService");
NamingContextExt root = NamingContextExtHelper.narrow(object);
NameComponent name = new NameComponent("NASDAQ","");
NameComponent path[] = {name};
StockMarket market = StockMarketHelper.narrow(root.resolve(path));
System.out.println("Price of MY COMPANY is " + market.get_price("MY_COMPANY"));
}
catch(Exception e )
{
System.err.println( e ); } }}


Output:

C:\jdk1.5.0\bin>javac StockMarketServer.java
C:\jdk1.5.0\bin>javac StockMarketClient.java
C:\jdk1.5.0\bin>java StockMarketServer ORBInitialPort-2500
Server is Ready.............
C:\jdk1.5.0\bin>java StockMarketClient ORBInitialPort-2500
Price of MY COMPANY is 159.2

1 comment: