CORBA 版 helloworld

Note: Article from:

http://lavasoft.blog.51cto.com/62575/240268/


First, the definition IDL
 
module HelloApp{ 
         interface Hello{ 
                string sayHello(); 
                oneway  void shutdown(); 
        }; 
};
 
IDL is no special development tools to write error-prone and unsightly, IDEA reluctantly supports syntax highlighting, but does not support formatting and grammar check!
 
Second, the IDL generated stubs
 
Into the IDL file storage directory, then execute: idlj -fall Hello.idl
 
It will generate a number of Java files:
 
helloCorba\src\HelloApp
HelloPOA.java 
_HelloStub.java 
HelloHolder.java 
HelloHelper.java 
Hello.java 
HelloOperations.java
 
Third, to achieve IDL interface, the developer server program
 
import HelloApp.Hello; 
import HelloApp.HelloHelper; 
import HelloApp.HelloPOA; 
import org.omg.CORBA.ORB; 
import org.omg.CosNaming.NameComponent; 
import org.omg.CosNaming.NamingContextExt; 
import org.omg.CosNaming.NamingContextExtHelper; 
import org.omg.PortableServer.POA; 
import org.omg.PortableServer.POAHelper; 

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 { 
                         //创建一个ORB实例 
                        ORB orb = ORB.init(args,  null ); 

                         // get a reference RootPOA, and activates the POAManager 
                        a POA rootPOA = POAHelper.narrow (ORB.resolve_initial_references ( "RootPOA" )); 
                        . Rootpoa.the_POAManager () the activate (); 

                         // Create Register and the servant The IT with the ORB 
                         // create an instance of HelloImpl (servant), and registered with the ORB 
                        HelloImpl the HelloImpl =  new new  HelloImpl (); 
                        helloImpl.setORB (ORB); 

                         // get the object from the service referenced 
                        org.omg.CORBA.Object ref = rootpoa. servant_to_reference (HelloImpl); 
                        the Hello the href = HelloHelper.narrow (REF); 

                         // get a root context name 
                        objRef = ORB.resolve_initial_references the org.omg.CORBA.Object ( "The NameService" ); 
                         // Which IS Part of the Use The NamingContextExt the Interoperable 
                         // the Naming-Service (the INS) Specification. 
                        NamingContextExt The ncRef = NamingContextExtHelper.narrow (objRef); 

                         // in naming context bound object 
                        String name =  "the Hello"
                        of NameComponent path [] = ncRef.to_name (name); 
                        ncRef.rebind (path, the href); 

                        System.out.println ( "Waiting ... and the HelloServer READY" ); 

                         // start the thread service, the client waits for the call 
                        orb.run(); 
                } catch (Exception e) { 
                        System.err.println( "ERROR: " + e); 
                        e.printStackTrace(System.out); 
                } 
                System.out.println( "HelloServer Exiting ..."); 
        } 
}
 
Fourth, implement CORBA clients
 
Import  HelloApp.Hello; 
Import  HelloApp.HelloHelper; 
Import  the org.omg.CORBA.ORB; 
Import  an org.omg.CosNaming.NamingContextExt; 
Import  org.omg.CosNaming.NamingContextExtHelper; 

public   class  the HelloClient { 
         static  the Hello HelloImpl; 

         public   static   void  main ( args String []) { 
                 the try  { 
                         // create an instance of the ORB 
                        ORB ORB = the ORB.init (args,  null ); 

                         // get the root naming context 
                        objRef = ORB.resolve_initial_references the org.omg.CORBA.Object ( "The NameService" ); 
                         . // the Use of a NamingContext This NamingContextExt INSTEAD IS 
                         // The Part of the Interoperable Naming-Service. 
                        NamingContextExt The ncRef = NamingContextExtHelper.narrow (objRef); 

                         // from obtaining interface naming context objects 
                        String name =  "the Hello"
                        HelloImpl = HelloHelper.narrow (ncRef.resolve_str (name)); 

                         // call interface object methods 
                        System.out.println ( "handle Obtained ON a Server object:"  + helloImpl); 
                        System.out.println(helloImpl.sayHello()); 
                        helloImpl.shutdown(); 

                }  catch (Exception e) { 
                        System.out.println( "ERROR : " + e); 
                        e.printStackTrace(System.out); 
                } 
        }
}
 
6.3.5 Operating
 
1) Run CORBA Services
orbd -ORBInitialPort 1050 -ORBInitialHost 192.168.14.117
 
2) The server runs the CORBA application
java HelloServer -ORBInitialPort 1050
 
Output:
HelloServer ready and waiting ... 
HelloServer Exiting ...
 
3) The client application running CORBA
java HelloClient -ORBInitialHost 192.168.14.117 -ORBInitialPort 1050
 
Obtained a handle on server object: IOR:000000000000001749444c3a48656c6c6f4170702f48656c6c6f3a312e30000000000001000000000000008a000102000000000f3139322e3136382e31342e313137000006db000000000031afabcb00000000206d3bb80000000001000000000000000100000008526f6f74504f410000000008000000010000000014000000000000020000000100000020000000000001000100000002050100010001002000010109000000010001010000000026000000020002 

Hello world !!



orbd.exe

Usage: orbd <options>   
where <options> include:   
-port start ORBD activation port, default is 1049 (optional)   
-defaultdb ORBD file directory, which defaults to "./orb.db" (available option)   
-serverid ORBD server identifier, a default value (optional)   
-ORBInitialPort initial port (mandatory)   
-ORBInitialHost initial host name (required)





Reproduced in: https: //my.oschina.net/u/2552902/blog/543828

Guess you like

Origin blog.csdn.net/weixin_33939843/article/details/92326845