////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ /////////////////////////////// Java ORB example \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // 1. Write the IDL as an interface inside of a module // 2. Compile the IDL // idltojava -fno-cpp count.idl // 3. Create the servant (Distributed Object) and the DO server // class CountServant extends _CountImplBase { // // copy the method signatures from CountModule\Count.java // // implement the methods // public class countServer { // // ORB.init connect NameService rebind wait // 4. Create the client application // public class countClient { // 5. Compile the source // javac count*java CountModule\*.java // 6. Execute the ORB name server, DO server, and client application // start tnameserv -ORBInitialPort 1050 // start java countServer -ORBInitialPort 1050 // java countClient -ORBInitialPort 1050 /////////////////////////////////// count.idl \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ module CountModule { interface Count { void add( in short num ); short get(); }; }; //////////////////////////////// countServer.java \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ import CountModule.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; class CountServant extends _CountImplBase { private short total = 0; public void add( short in ) { total += in; System.out.println( "total is " + total ); } public short get() { return total; } } public class countServer { public static void main( String[] args ) { try { // create and initialize the ORB ORB orb = ORB.init( args, null ); // create servant and register it with the ORB CountServant countRef = new CountServant(); orb.connect( countRef ); // get the root naming context org.omg.CORBA.Object nsRef = orb.resolve_initial_references( "NameService" ); NamingContext ncRef = NamingContextHelper.narrow( nsRef ); // bind the Object Reference in Naming NameComponent nc = new NameComponent( "Count", "" ); NameComponent path[] = { nc }; ncRef.rebind( path, countRef ); // wait for invocations from clients java.lang.Object sync = new java.lang.Object(); synchronized (sync) { sync.wait(); } } catch (Exception e) { e.printStackTrace( System.out ); } } } //////////////////////////////// countClient.java \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ import CountModule.*; import org.omg.CosNaming.*; import org.omg.CORBA.*; public class countClient { 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 nsRef = orb.resolve_initial_references( "NameService" ); NamingContext ncRef = NamingContextHelper.narrow( nsRef ); // resolve the Object Reference in Naming NameComponent nc = new NameComponent( "Count", "" ); NameComponent path[] = { nc }; Count countRef = CountHelper.narrow( ncRef.resolve(path) ); System.out.print( "enter number (0 for total, -1 to exit): " ); short ans = (short) Read.anInt(); while (ans != -1) { if (ans == 0) System.out.println( " total is " + countRef.get() ); else countRef.add( ans ); System.out.print( "enter number (0 for total, -1 to exit): " ); ans = (short) Read.anInt(); } } catch (Exception e) { e.printStackTrace( System.out ); } } } // -- tnameserv -- // Initial Naming Context: // IOR:000000000000002849444c3a6f6d672e6f72672f436f734e616d696e672f4e616d69 ... // TransientNameServer: setting port for initial object references to: 1050 // // -- DO server -- // total is 1 // total is 3 // total is 6 // total is 10 // // -- client application -- // enter number (0 for total, -1 to exit): 1 // enter number (0 for total, -1 to exit): 2 // enter number (0 for total, -1 to exit): 3 // enter number (0 for total, -1 to exit): 0 // total is 6 // enter number (0 for total, -1 to exit): 4 // enter number (0 for total, -1 to exit): 0 // total is 10 // enter number (0 for total, -1 to exit): -1