1. Introduction now, many people now learn from CORBA, and everyone has already learned that CORBA is a completely intermediate language that can use Interface Definition Language (IDL) to define the use of interfaces using the interface to use the interface of the interface. . The specific implementation code of Client and Server is not written in the IDL definition, but uses the Idl compiler of a certain target language to generate the required code stub and Helper class, Client and Server to use the real programming language to perform specific implementation. In order to ensure that distributed objects built on different CORBA products can communicate with each other, Client and Server communicate via ORB (Object Request Agent). The general runtime process is that the client sends the request to the ORB, and the ORB will send the request to the Server, and Server sends the return result to the ORB, and the ORB sends the return result to the client. ORB can speak translators between Client and Server. Even if Client and Server are written in different programming languages, orb can also complete mutually communications as long as it is in line with the same IDL definition. All documents are emphasized that servers and clients can be Java or other languages (such as: Delphi) to write, but in order to talk more about how to deal with multilingual clients in detail in websites or books. "Java2 core technology" has some instructions, but it is only an introductory article, so I downloaded the Omniorb304, and I used Sun's TNAMSERV naming service. The server was written in Java, and the client was written in Java and C (VC6 Omniorb). Test, I hope to experience or understand CORBA thinking through a specific operating example of a programming. The overall writing process is as follows: Use IDL to define an interface file, draw the functions to be implemented, or it is to define a template to implement function (sysprop.idl) using the "IDL TO JAVA" compiler (here IDLJ) The IDL file is transformed into interface definitions written in the Java programming language, generating the required code stub and the Helper class using Java language to write client and server implementation programs. Using the "IDL TO C " compiler (here I OMNIIDL) transforms the IDL file to the interface definition written in the C programming language, generates the required code stub and the Helper class using C language to write client implementation programs (of course also written server Program, but this test did not perform) Start Naming Services TNAMESERV boot Java written service program written by Java and C calls the corresponding service 2. The setting of the operating environment: The overall environment is from JDK1.3 Omniorb3.0 (www.uk.research.att.comomniorbdoc3.0) VC6, which explains the specific installation.
2.1. Installing JDK1.3 From Sun Company Down JDK1.3 or by other means to install, set the corresponding environment variable, and the computer used in this article is as follows: classpath = .; java_home = D: JDK130 modifies the original path variable, add "% java_home% bin;", as follows, the following PTAH =% java_home% bin; the original variable Note: When I use JBuilder JDK1.3, the server can't work normally, I just I found such a thing, the specific reasons did not understand with this article, forgive me. 2.2. Installing the VC6 VC6 installed in a regular manner, note that the computer is installed on the computer used in this article C: Program FileSmicrosoft Visual Studio 2.3. Install Omniorb from http://www.uk.research.att.com/ Omniorb / Doc / 3.0 Download Omniorb3.0 (This article is tested by the files downloaded by Omniorb_304_x86_win32.zip). Use Winzip to extract OMNIORB_304_X86_WIN32.ZIP to the Omniorb_304_x86_win32 directory, there is an OMNI directory in the directory, copy the files within the Omni directory to you want to store. The test computer is installed in c: Omni according to C: Omnireadme.win32 document, because the runtime and command line are in the console, this test does not set an environment variable according to the documentation, but written an Omni .bat, first run when using the console. This test computer OMNI.BAT content is as follows set top = c: OmniSet path =% TOP% binx86_win32;% PATH% SET lib =% top% binx86_win32;% lib% set include =% top% include;% include% set vcosdir = SET Vscommondir = If your computer VC's environment variable has been set in your environment variable, then C: Program FileSmicrosoft Visual Studiovc98binvcvars32.bat can not be run. Otherwise, you should first run the vcvars32.bat before running Omni.bat. 3. Practice process agreed that all written files are saved in d: myworkt1, Omni.bat is also in this directory 3.1. Writing sysprop.idl, function is to return system properties interface sysprop {string getproperty (in string name);}; 3.2 Write Java's server 3.2.1. Translate the IDL file into the Java programming language code stub and Helper class.
Run the following command idlj -fall SysProp.idl under normal circumstances D: The following files are generated within myworkt1 directory, otherwise check your files and execute programs SysProp.java SysPropHelper.java SysPropHolder.java SysPropOperations.java _SysPropImplBase.java _SysPropStub. Java 3.2.2. Write syspropserver.java import org.omg.cosnaming. *; import org.omg.corba. *; // Write the corresponding service, must inherit from _ class name Implbase, and implement the corresponding method Class Sysprops Extends _SyspropImp1Base // Specific service Implement {public string getproperty (String Key) {system.out.println ("call" key); string S; s = system.getProperty (key); if (s == null) {S = "null";} system.out.println (key "=" s); return s;}}} public class syspropserver // Starting service Program {public static void main (string args []) {TRY { System.out.Println ("Create and Initialize ORB"); orb orb = orb.init (args, null); system.out.println ("Create a service object and register it to ORB"); sysprops = new sysprops (); orb.connect (impl); // print IOR string System.out.println (orb.object_to_string (impl)); org.omg.CORBA.Object namingContextObj = orb.resolve_initial_references ( "NameService"); NamingContext namingContext = NamingContexthelper.narrow (NamingContextobj); NA MeComponent [] path = {new nameComponent ("sysprop", "")}; system.out.println ("Binding Service ... sysprops"); NamingContext.Rebind (path, IMPL); System.out.println "Waiting to call ... sysprops"); java.lang.Object sync = new java.lang.object (); synchronized (sync) {sync.wait ();}} catch (exception e) {system.rr.println ("Error:" E); E.PrintStackTrace (System.out);}}} 3.3. Write Java's client 3.3.1. Writing syspropclient.java Way to use the iOR string to pay attention to a period of notes in the code The code, the beginning of "//" to start "//" // "// End of the method using ORB" ends.
This code is the code using the ORB method. If "// uses iOR's method" // using the method of IOR "," * / "will be added after" // using the method of IOR ", Remove "/ *" before "//" using ORB's method "," * / "behind" * / "after" // End of the method of ORB ", is the code using the ORB method, the program is running" SyspropClient [Environment Variable] ".
The following is the specific code: import org.cosnaming. *; Import org.omg.corba. *; Public class syspropclient {public static void main (string args []) {Try {string setInfo, returnfo, ref; org.omg .CORBA.Object objref; sysprop syspropref; orb orb = orb.init (args, null); // Start IF (args.length> = 1) {ref = args [0];} else {system. Out.println ("SysPropClient [Environment Variables]"); return;} Objref = orb.string_to_object (ref); syspropref = sysprophelper.narrow (objRef); // End // // method using ORB start objRef = orb.resolve_initial_references ( "NameService"); NamingContext ncRef = NamingContextHelper.narrow (objRef); // for positioning service NameComponent nc = new NameComponent ( "SysProp", ""); NameComponent path [] = {nc}; Syspropref = sysprophelper.narrow (ncref.resolve (path)); // End end * / if (args.length> 1) {setInfo = args [1];} else {setInfo = "java.home";} system.out.println ("Start call"); returnfo = syspropref.getProperty (setInfo); system.out.println (setInfo "=" returnifo);} catch (Exception E) {system .out.println ("Error:" E);}}} 3.3.2. Compiler D: myworkt1>
java tnameservInitial Naming Context: IOR: 000000000000002849444c3a6f6d672e6f72672f436f734e616d696e672f4e616d696e67436f6e746578743a312e3000000000010000000000000054000101000000000c3139322e3136382e302e31000ca6000000000018afabcafe00000002a999c4740000000800000000000000000000000100000001000000140000000000010020000000000001010000000000TransientNameServer: setting port for initial object references to:. 900Ready second console, the implementation of java SysPropServer test as follows D: myworkt1> java SysPropServer create and initialize the ORB to create a service object and ORB registration IOR: 000000000000001049444c3a53797350726f703a312e3000000000010000000000000054000101000000000c3139322e3136382e302e31000ca7000000000018afabcafe00000002a999dbeb0000000800000000000000000000000100000001000000140000000000010020000000000001010000000000 binding service ... SysPropS wait for the call ... SysPropS 3 console, execute java SysPropClient IOR: when XXX jAVA.HOME test shown in D as follows: myworkt1> java SysPropClient IOR: 000000000000001049444c3a53797350726f703a312e 3000000000010000000000000054000101000000000c3139322e3136382e302e31000ca7000000000018 afabcafe00000002a999dbeb0000000800000000000000000000000100000001000000140000000000010020000000000001010000000000 java.home start calling java.home = D:. beajdk130jre 3.5 IOR written in C is written in C client from the practice in terms of the client program is not much difference with JAVA, JAVA is simply to generate code stubs and helper classes with idlj Class, and OMNI is to generate code stubs and Helper classes with OmniIDL, while programming ideas and encoding processes are very similar. Since the C program is to call the related files of OMNI and VC6, then after entering the console, if the VC does not perform environment variable settings, then you must run C: Program FileSmicrosoft Visual Studiovc98binvcvars32.bat, run Omni.bat, otherwise directly run After omni.bat, compile the program and run the program. 3.5.1. Transform IDL files into C programming language code stub and helper class. Perform the following command omniidl -bcxx sysprop.IDL In normal case D: MyWorkt1 Code Root Class and Helper class sysprop.hh and syspropsk.cc are generated in the MYWORKT1 directory. Otherwise, check your executive and files.
3.5.2. Writing syspropc.cc #include #include int main (int Argc, char ** argv) {try {corba :: ORB_VAR ORB = CORBA :: ORB_INIT (Argc, Argv, "Omniorb3"); if (Argc < 2 || Argc> 3) {COUT << "Usage: syspropc [Environment Variable Name]" << Endl; Return 1;} CORBA :: Object_var Obj = ORB-> String_to_Object (Argv [1]); sysprop_ptr echoref = sysprop :: _ narrow (obj); if (corba :: is_nil (echoref)) {CERR << "No object" << Endl; return 1;} const char * message; if (argc == 3) {Message = argv [ 2];} else {message = "java.home";} corba :: string_var dest = echoref-> getProperty; cout << (char *) Message << "=" << (char *) DEST <
syspropc IOR: 000000000000001049444c3a53797350726f703a312e3000000000010000000000000054000101000000000c3139322e3136382e302e31000ca7000000000018afabcafe00000002a999dbeb0000000800000000000000000000000100000001000000140000000000010020000000000001010000000000 os.nameos.name = Windows 2000 3.6 NAME written in C way of a client in order to use NANE way, you must set the registry information OMNI software, to establish the following items (regedit) HKEY_LOCAL_MACHINESOFTWAREORLomniORB2.0NAMESERVICE in the registry. (String). NameService's value is TNAMESERV (JDK1.3bin program) started IOR value (add itself when you set it up). Note In order to use this way each time the TNAMESERV is used to change the old iOR value, I test the Omini Omninames.exe program to do the server, but the iOR value is constant, but the server is written in JVAV. Error. If you start TNAMSERV, you can run it on the server and client written with C. Initial present example as shown Naming Context: IOR: 000000000000002849444c3a6f6d672e6f72672f436f734e616d696e672f4e616d696e67436f6e746578743a312e3000000000010000000000000054000101000000000c3139322e3136382e302e31000ca6000000000018afabcafe00000002a999c4740000000800000000000000000000000100000001000000140000000000010020000000000001010000000000TransientNameServer: setting port for initial object references to: 900Ready then take the IOR:. Start (IOR :) contains the string into the back of the registry.
3.6.1. Writing syspropcc.cc// Use the Name mode client #include #include "sysprop.hh" static corba :: object_ptr getObjectReference (corba :: ORB_PTR ORB); int Main (int Argc, char ** argv) {IF (argc! = 2) {cout << "How to use: syspropcc