1 purpose of the experiment
(1) master the concept of component development, understand CORBA model and ORB mechanism;
(2) master the programming method of CORBA Components.
2 experiment content
Steps:
(1) configuration environment JDK environment.
(2) compile IDL interface.
(3) compile the server program.
(4) compile client program.
(5) operation test and debugging.
(6) complete the report.
3. code
HelloWorld
Compile IDL Interface: X:\corba>idlj –fall HelloWorld.idl _HelloWorldStub.java HelloWorld.java HelloWorldHelper.java HelloWorldHolder.java HelloWorldOperations.java HelloWorldPOA.java //Write and compile the server program: HelloWorldServer.java import sample.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import org.omg.CORBA.portable.*; import org.omg.PortableServer.*; class HelloWorldServant extends HelloWorldPOA{ //Object implementation class public String sayHello(){ return "\nHello World!\n"; } } public class HelloWorldServer{ //Service procedure public static void main(String args[]){ try{ //Initialize ORB ORB orb = ORB.init(args, null); //Take a reference to the root POA org.omg.CORBA.Object poaobj = orb.resolve_initial_references ("RootPOA"); org.omg.PortableServer.POA rootPOA = org.omg.PortableServer.POAHelper.narrow(poaobj); org.omg.PortableServer.POAManager manager = rootPOA.the_POAManager(); //Create a servo object HelloWorldServant objRef = new HelloWorldServant(); HelloWorld obj = objRef._this(orb); //Bind naming service NamingContext ncRef = NamingContextHelper.narrow(orb.resolve_initial_references("NameService")); NameComponent nc = new NameComponent("Hello", ""); NameComponent path[] = {nc}; ncRef.rebind(path, obj); //Activate POA Manager manager.activate(); //Waiting for client requests to be processed System.out.println("HelloWorld is running!"); orb.run(); }catch (Exception e) { System.err.println("ERROR: " + e); e.printStackTrace(System.out); } } } //Write and compile the client program: HelloWorldClient.java import sample.*; import org.omg.CosNaming.*; import org.omg.CORBA.*; public class HelloWorldClient { public static void main(String args[]) { try{ ORB orb = ORB.init(args, null); org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); NameComponent nc = new NameComponent("Hello",""); NameComponent path[] = {nc}; HelloWorld helloWorld = HelloWorldHelper.narrow(ncRef.resolve(path)); String hello = helloWorld.sayHello(); System.out.println(hello); } catch (Exception e) { System.out.println("ERROR : " + e) ; e.printStackTrace(System.out); } } } //Start nameserver: X: \ CORBA > tnameserv - orbinitialport 1050 //Start the server program: X: \ CORBA > java helloworldserver - orbinitialport 1050 //Output: HelloWorld is running //Start the client program: X: \ CORBA > java helloworldclient - orbinitalport 1050 //Output: Hello World!
Counter
// Write the IDL interface counter.idl: module CounterApp{ interface Counter{ readonly attribute long value; void inc(); void dec(); }; }; //Compile IDL interface: X: \ CORBA > idlj – fall counter.idl //The counter app package is generated from the translation result, and the following files are generated _CounterStub.java Counter.java CounterHelper.java CounterHolder.java CounterOperations.java CounterPOA.java // Write and compile the object implementation code: CounterImpl.java import CounterApp.*; public class CounterImpl extends CounterPOA { private int count; public CounterImpl(){ count = 0; } public void inc(){ count++; } public void dec(){ count - -; } public int value(){ return count; } } // Write and compile the server program: Server.java import CounterApp.*; import java.io.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import org.omg.CORBA.portable.*; import org.omg.PortableServer.*; public class Server { public static void main(String[] args){ try{ ORB orb = ORB.init(args, null); org.omg.CORBA.Object poaobj = orb.resolve_initial_references ("RootPOA"); org.omg.PortableServer.POA rootPOA = org.omg.PortableServer.POAHelper.narrow(poaobj); org.omg.PortableServer.POAManager manager = rootPOA.the_POAManager(); CounterImpl c_impl = new CounterImpl(); Counter c = c_impl._this(orb); NamingContext ncRef = NamingContextHelper.narrow(orb.resolve_initial_references("NameService")); NameComponent nc = new NameComponent("Count", ""); NameComponent path[] = {nc}; ncRef.rebind(path, c); FileOutputStream file = new FileOutputStream("Counter.ref"); PrintWriter writer = new PrintWriter(file); String ref = orb.object_to_string(c); writer.println(ref); writer.flush(); file.close(); System.out.println("Server started."+" Stop:Ctrl-c"); rootPOA.the_POAManager().activate(); orb.run(); }catch(IOException ex){ System.out.println("File error:"+ex.getMessage()); System.exit(2); }catch(Exception ex){ System.out.println("Exception: "+ex.getMessage()); System.exit(1); } } } //Write and compile client program: Client.java import CounterApp.*; import java.util.*; import java.io.*; import org.omg.CORBA.*; import org.omg.CosNaming.*; public class Client { public static void main(String[] args){ try{ ORB orb = ORB.init(args, null); org.omg.CORBA.Object obj = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(obj); NameComponent nc = new NameComponent("Count",""); NameComponent path[] = {nc}; String ref = null; try{ Scanner reader = new Scanner(new File("Counter.ref")); ref = reader.nextLine(); }catch(IOException ex){ System.out.println("File error: "+ex.getMessage()); System.exit(2); } obj = orb.string_to_object(ref); if(obj == null){ System.out.println("Invalid IOR"); System.exit(4); } Counter c = null; try{ c = CounterHelper.narrow(obj); }catch(BAD_PARAM ex){ System.out.println("Narrowing failed"); System.exit(3); } int inp = -1; do{ System.out.print("Counter value: "+c.value()+"\nAction(+/-/e)?"); System.out.flush(); do{ try{ inp = System.in.read(); }catch(IOException ioe){} }while(inp != '+' && inp != '-' && inp != 'e'); if(inp == '+') c.inc(); else if(inp == '-') c.dec(); }while(inp != 'e'); }catch(Exception ex){ System.out.println("Exception: "+ex.getMessage()); System.exit(1); } } } //Start nameserver: X: \ CORBA > tnameserv - orbinitialport 1050 //Start the server program: X: \ CORBA > java server - orbinitialport 1050 //Output: Server started. Stop: Ctrl-c //Start the client program: X: \ CORBA > java client - orbinitialport 1050
Running screenshot:
What's your feeling?
Learn to compile and run java files in the command prompt environment. When running the program, you need to go to the directory where the class file is located to start the corresponding program; name server, server and client need to start a DOS command prompt interface respectively.
Through this computer experiment, I am familiar with the CORBA component development method. With these two examples, I realize the advantages of component development and expand the new way of thinking.
CORBA defines an object-oriented software component construction method, so that different applications can share the software components;
Each object encapsulates its internal operation details, and at the same time provides precisely defined interfaces to the outside world, thus reducing the complexity of the application system,