LAB.docx - Yimg

looneyvillestaticΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 5 χρόνια και 2 μήνες)

286 εμφανίσεις


1.
RMI
-
PROGRAM FOR ADDITION OF ARRAY OF ELEMENTS

Program:

Server:

import java.rmi.*;

import java.net.*;

import java.io.*;

public class rmiserver

{

public static void main(String args[])

{

try

{

two twox=new two();

Naming.bind("addition",twox);

System.out.println("Object is registered");

}

catch(Exception e)

{

System.out.println("Exception: " +e);

}

}

}

Client:

import java.io.*;

import java.rmi.*;

import java.net.*;

public class rmiclient

{

public static void main(String args[]) throws Exception

{

try

{

String s1="rmi://localhost/addition";

one onex=(one)Naming.lookup(s1);

int x[]=new int[10];

for(int j=0;j<5;j++)

x[j]=Integer.parseInt(args[j]);

int m=onex.add(x);

System.out.println("addition:"+m);

}

catch(Exception e)

{

System.out.println("Except
ion:"+e);

}

}

}

Interface:

import java.rmi.*;

interface one extends Remote

{

public int add(int[] a) throws RemoteException;

}

Implementation:

import java.rmi.*;

import java.rmi.server.*;

public class two extends UnicastRemoteObject implements one

{

public

two() throws RemoteException{}

public int add(int[] a) throws RemoteException

{

int sum=0;

for(int i=0;i<5;i++)

{

sum=sum+a[i];

}

return(sum);

}

}

OUTPUT:

Commands:


C:
\
jdk
\
bin>javac rmiclient.java

C:
\
jdk
\
bin>javac rmiserver.java

C:
\
jdk
\
bin>javac one.java

C:
\
jdk
\
bin>javac two.java

C:
\
jdk
\
bin>start rmiregistry

C:
\
jdk
\
bin>java rmiserver

Object is registered


Commands:


C:
\
jdk
\
bin>java rmiclient 1 3 5 4 2


Addition is 15






2.
CORBA HELLO WORLD

//Helloservant.java

import helloapp.*;

import org.omg.CORBA.*;

import org.omg.PortableServer.*;

import org.omg.PortableServer.POA.*;

public class HelloServant extends helloPOA

{


private ORB orb;


public void setORB(ORB orb_val)


{



orb=orb_val;


}


public String sayHello()


{



return "HI User
\
n";


}


public void
shutDown()


{



orb.shutdown(false);


}

}






//Helloserver.java

import helloapp.*;

import java.io.*;

import org.omg.CosNaming.*;

import org.omg.CosNaming.NamingContextPackage.*;

import org.omg.CORBA.*;

import org.omg.PortableServer.*;

import org.omg.Port
ableServer.POA;

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();



HelloServant
helloImpl=new HelloServant();



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("The StockMarket Server is up and ready...");





orb.run();



}



catch(Exception e)



{




e.
printStackTrace(System.out);



}


}

}


//Helloclient.java

import helloapp.*;

import org.omg.CORBA.*;

import org.omg.CosNaming.*;

import org.omg.CosNaming.NamingContextPackage.*;

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=helloH
elper.narrow(ncref.resolve_str(name));



System.out.println("Get handle on :"+helloImpl);



System.out.println(helloImpl.sayHello());



helloImpl.shutDown();



}



catch(Exception e)



{




e.printStackTrace(System.out);



}


}

}


//helloapp.idl

module hel
loapp

{


interface hello


{



string sayHello();



oneway void shutDown();


};

};






3.
Stock Market

SimpleStocks.idl

module SimpleStocks

{

interface StockMarket

{


float get_price(in string symbol);

};

};

StockMarketClient.java

import org.omg.CORBA.*;

import org.omg.CosNaming.*;

import SimpleStocks.*;

public class StockMarketClient

{

public static void main(String arg[])

{

try

{

ORB orb=ORB.init(arg,null);

NamingContext
ncref=NamingContextHelper.narrow(orb.resolve_initial_references("NameService
"));

NameComponent path[]={new NameComponent("NASDAQ","")};

StockMarket market=StockMarketHelper.narrow(ncref.resolve(path));

System.out.println("Price of my company is
$"+market.get_price("MYCOMPANY"));

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

StockMa
rketImpl.java

import org.omg.CORBA.*;

import SimpleStocks.*;

public class StockMarketImpl extends _StockMarketImplBase

{

public StockMarketImpl()

{

super();

}

public float get_price(String symbol)

{

float price=0;

for(int i=0;i<symbol.length();i++)

{

price
=price+(int)symbol.charAt(i);

}

price=price/5;

return price;

}

}

StockMarketServer.java

import org.omg.CORBA.*;

import org.omg.CosNaming.*;

import SimpleStocks.*;

public class StockMarketServer

{

public static void main(String args[])

{

try

{


ORB orb=
ORB.init(args,null);

StockMarketImpl sml=new StockMarketImpl();

orb.connect(sml);

org.omg.CORBA.Object objref=orb.resolve_initial_references("NameService");

NamingContext ncref=NamingContextHelper.narrow(objref);

NameComponent nc=new NameComponent("NASDAQ"
,"");

NameComponent path[]={nc};

ncref.rebind(path,sml);

System.out.println("the StockMarket Server is up and ready");

Thread.currentThread().join();

}

catch(Exception e)

{

e.printStackTrace();

}

}

}







4.
CORBA NAMING SERVICE

Program:

Module
:

module
Tempconvcalc

{

interface Tempcalc


{


double get_celcius(in string symbol);

};

};

Implementation:

import org.omg.CosNaming.*;

import org.omg.CORBA.*;

import Tempconvcalc.*;

public class TempcalcImpl extends _TempcalcImplBase{

public double get_celcius
(String symbol){

double celcius=Double.parseDouble(symbol);

double farenheit=1.8*celcius+32;

return farenheit;

}

public TempcalcImpl(){

super();

}

}

Server:

import org.omg.CosNaming.*;

import org.omg.CORBA.*;

import Tempconvcalc.*;

public class
Tempcalcserver{

public static void main(String args[]){

try{

ORB orb=ORB.init(args,null);

TempcalcImpl tempImpl=new TempcalcImpl();

orb.connect(tempImpl);

org.omg.CORBA.Object objRef=orb.resolve_initial_references("NameService");

NamingContext ncRef=Naming
ContextHelper.narrow(objRef);

NameComponent nc=new NameComponent("TEMPCALC","");

NameComponent path[]={nc};

ncRef.rebind(path,tempImpl);

System.out.println("Tempconv server is ready");

Thread.currentThread().join();

}

catch(Exception e){

e.printStackTrace(
);

}

}

}

Client:

import org.omg.CosNaming.*;

import org.omg.CORBA.*;

import Tempconvcalc.*;

import java.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("NameServic
e"));

NameComponent path[]={new NameComponent("TEMPCALC","")};

Tempcalc calc=TempcalcHelper.narrow(ncRef.resolve(path));

System.out.println("E
nter 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 side commands:


C:
\
CORBA>set path=c:
\
jdk
\
bin


C:
\
CORBA>idlj TempCalc.idl


C:
\
CORBA>idlj

fall

oldImplBase TempCalc.idl


C:
\
CORBA>javac *. java


C:
\
CORBA>start ordb

ORBInitialPort 1050


C:
\
CORBA>start java TempCalcServer

ORBInitialPort 1050

ORBInitialHost
Localhost



Temperature
Conversion Server is ready to run.

Need Input in celcius.

Will give output in Fahrenheit.


Client side commands:


C:
\
CORBA>set path=c:
\
jdk
\
bin

C:
\
CORBA>java TempCalcClient

ORBInitialPort 1050

ORBInitialHost
Localhost

C:
\
CORBA>

Enter the Celcius value:

6
4

Temperature of the day in Fahrenheit is 147.2




5.
EJB

1.

open netbeans


2.

create new project


3.

select enterprise


4.

in right pane select enterprise application


5.

click next


6.

give project name SimpleEjb


7.

click next


8.

select the glassfish server


9.

check all 3 module(EJB, Web appli and Application Client)


10.


click finish


11.

in the projects tab rightclick SimpleEjb
-
ejb



12.

choose new session bean


13.

give EJB Name as Calc


14.

give package name as sessionbean


15.

Select Session Type as Stateless


16.

Select Remote interface


17.

Click Finish

Within the session bean package,


2 java files are created as Cal
cBean.java
and CalcRemote.java

in CalcBean.java type,


public int getSum(int a, int b) {



return (a+b);



}


in CalcRemote.java, type


public
int getSum(int a,int b);


18.

Right Click SimpleEjb with triangle icon in project pane


19.

select run(to deploy the ejb and check whether the server is running)


20.

A JSP page is displayed with "Hello World"


21.

Expand simpleEJB
-
app
-
client


22.

expand source packages


23.

clic
k main.java from simpleejb package


24.

type the following code inside the main method.


System.out.println("the sum of the 2 numbers is") ;



try {



InitialContext ctx = new InitialContext();



CalcRemote bean = ( CalcRemote)
ctx.lookup("ses
sionbean.CalcRemote");






System.out.println(bean.getSum(3, 4));



} catch (NamingException e) {



e.printStackTrace();



}



25.

right click main.java and select run


26.

the output is seen in the output pane


27.

i've

attached the required 3 java files in the attachment.


check it out









6.JAVA RMI
-
MESSAGE TRANSFER


-
Compile

javac ReceiveMessageInterface.java

javac RmiServer.java

javac RmiClient.java

rmic RmiServer


start rmiregistry

-


Execution

1.


(
at one host,) java RmiServer

2.


(at another host) java RmiClient <server’s address> 3232 <message
text>

-


Interface:

import java.rmi.*;

public interface ReceiveMessageInterface extends Remote

{


void receiveMessage(String x) throws RemoteException;

}









RmiServer.java

import java.rmi.*;

import java.rmi.registry.*;

import java.rmi.server.*;

import java.net.*;

public class RmiServer extends java.rmi.server.UnicastRemoteObject

implements ReceiveMessageInterface

{


int thisPort;


String thisAddress;


Registry registry;


public void receiveMessage(String x) throws RemoteException


{


System.out.println(x);


}



public RmiServer() throws RemoteException


{



try





{




thisAddres
s= (InetAddress.getLocalHost()).toString();



}





catch(Exception e)


{


throw new RemoteException("can't get inet address.");


}

thisPort=3232;



System.out.println("this address="+thisAddress+",port="+thisPort);



try{





registry = LocateRegistry.createRegistry( thisPort );


registry.rebind("rmiServer", this);


}



catch(RemoteException e)


{



throw e;


}


}




static public void main(String args[])


{



try{



RmiServer s=new RmiServer();


}


catch (Exception e) {


e.printStackTrace();


System.exit(1);


}


}

}



RmiClient.java

import java.rmi.*;

import java.rmi.registry.*;

import java.net.*;

public class RmiClient

{


static public void main(String args[])


{


ReceiveMessageInterface rmiServer;


Registry registry;


String serverAddress=args[0];


String serverPort=args[1];


String text=args[2];


System.out.println("sending "+text+"
to "+serverAddress+":"+serverPort);


try{


registry=LocateRegistry.getRegistry(


serverAddress,


(new Integer(serverPort)).intValue()


);


rmiServer=


(
ReceiveMessageInterface)(registry.lookup("rmiServer"));


rmiServer.receiveMessage(text);


}


catch(RemoteException e){


e.printStackTrace();


}


catch(NotBoundException e){


e.printStackTrace();



}


}

}