0% found this document useful (0 votes)
363 views5 pages

Corba

The document describes a simple client-server application using CORBA. It defines a Hello interface with a sayHello and shutdown method. It then shows the Java code for a Hello client that looks up the Hello object in the naming service and calls its methods. It also shows the server code that implements the Hello object, binds it to the naming context and runs the ORB. The client obtains a reference to the server object and calls its sayHello method, printing the response.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
363 views5 pages

Corba

The document describes a simple client-server application using CORBA. It defines a Hello interface with a sayHello and shutdown method. It then shows the Java code for a Hello client that looks up the Hello object in the naming service and calls its methods. It also shows the server code that implements the Hello object, binds it to the naming context and runs the ORB. The client obtains a reference to the server object and calls its sayHello method, printing the response.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Hello.

idl module HelloApp { interface Hello { string sayHello(); oneway void shutdown(); }; };

HelloClient.java

import import import import

HelloApp.*; org.omg.CosNaming.*; org.omg.CosNaming.NamingContextPackage.*; org.omg.CORBA.*;

public class HelloClient { static Hello helloImpl; public static void main(String args[]) { try{ // create and initialize the ORB ORB orb = ORB.init(args, null); // get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); // Use NamingContextExt instead of NamingContext. This is // part of the Interoperable naming Service. NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef); // resolve the Object Reference in Naming 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); } } }

HelloServer.java import HelloApp.*;

import import import import import

org.omg.CosNaming.*; org.omg.CosNaming.NamingContextPackage.*; org.omg.CORBA.*; org.omg.PortableServer.*; org.omg.PortableServer.POA;

import java.util.Properties; class HelloImpl extends HelloPOA { private ORB orb; public void setORB(ORB orb_val) { orb = orb_val; } // implement sayHello() method public String sayHello() { return "\nHello world !!\n"; } // implement shutdown() method public void shutdown() { orb.shutdown(false); } } public class HelloServer { public static void main(String args[]) { try{ // create and initialize the ORB ORB orb = ORB.init(args, null); // get reference to rootpoa & activate the POAManager POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA")); rootpoa.the_POAManager().activate(); // create servant and register it with the ORB HelloImpl helloImpl = new HelloImpl(); helloImpl.setORB(orb); // get object reference from the servant org.omg.CORBA.Object ref = rootpoa.servant_to_reference(helloImpl); Hello href = HelloHelper.narrow(ref); // get the root naming context // NameService invokes the name service org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); // Use NamingContextExt which is part of the Interoperable // Naming Service (INS) specification. NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef); // bind the Object Reference in Naming String name = "Hello"; NameComponent path[] = ncRef.to_name( name ); ncRef.rebind(path, href);

System.out.println("HelloServer ready and waiting ..."); // wait for invocations from clients orb.run(); } catch (Exception e) { System.err.println("ERROR: " + e); e.printStackTrace(System.out); } System.out.println("HelloServer Exiting ..."); } }

Output:-

Terminal1:
Last login: Tue Mar 6 08:08:39 on ttys000 Digvijay-Singhs-MacBook-Pro:~ Digvijay$ cd Desktop/ Digvijay-Singhs-MacBook-Pro:Desktop Digvijay$ cd Corba/

Digvijay-Singhs-MacBook-Pro:Corba Digvijay$ idlj -fall Hello.idl Digvijay-Singhs-MacBook-Pro:Corba Digvijay$ javac *.java HelloApp/*.java Note: HelloApp/HelloPOA.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Digvijay-Singhs-MacBook-Pro:Corba Digvijay$ orbd -ORBInitialPort 1050& [1] 336 Digvijay-Singhs-MacBook-Pro:Corba Digvijay$ java HelloServer -ORBInitialPort 1050 -ORBInitialHost localhost& [2] 339 Digvijay-Singhs-MacBook-Pro:Corba Digvijay$ HelloServer ready and waiting ... HelloServer Exiting ... Mar 6, 2012 8:12:41 AM com.sun.corba.se.impl.orb.ORBImpl checkShutdownState WARNING: "IOP01600004: (BAD_INV_ORDER) ORB has shutdown" Digvijay-Singhs-MacBook-Pro:Corba Digvijay$

Terminal 2:
Last login: Tue Mar 6 08:10:44 on ttys000 Digvijay-Singhs-MacBook-Pro:~ Digvijay$ cd Desktop/ Digvijay-Singhs-MacBook-Pro:Desktop Digvijay$ cd Corba/ Digvijay-Singhs-MacBook-Pro:Corba Digvijay$ java HelloClient -ORBInitialPort 1050 -ORBInitialHost localhost Obtained a handle on server object: IOR:000000000000001749444c3a48656c6c6f4170702f48656c6c6f3a312e3000000 00000010000000000000082000102000000000a3132372e302e302e3100c0120000 0031afabcb0000000020e5e23f1a00000001000000000000000100000008526f6f745 04f41000000000800000001000000001400000000000002000000010000002000000 0000001000100000002050100010001002000010109000000010001010000000026 000000020002 Hello world !! Digvijay-Singhs-MacBook-Pro:Corba Digvijay$

You might also like