An example (CORBA)

Source: Internet
Author: User
Tags bind current time string format

The code shown here may not be exhaustive because different orbs have different ways of accessing the CORBA service, so whatever the example will depend on the specific vendor (the following example uses Javaidl, which is a free product of sun Company). It provides a simplified version of the Orb, a naming service, and a "idl→java" compiler. In addition, because Java is still in the early stages of development, it does not include all CORBA features in different Java/corba products.
We want to implement a server that runs on some machines, and other machines can query it for the right time. We also want to implement a customer to make it request the right time. In this case, we have two programs that are implemented in Java. However, in practical applications, different languages are often used respectively.

1. Write IDL source code
The first step is to write an IDL description for the service provided. This is usually done by the server programmer. Then the programmer can implement the server in any language, with only one CORBA IDL compiler in that language.
The IDL file has been distributed to the client programmer and is a bridge between the two languages.
The following example shows the IDL description of the time server:

Module Remotetime {
   interface Exacttime {
      string getTime ();
   }
;


This is a declaration of the Exacttime interface within the Remotetime namespace. The interface is composed of a single method that returns the current time in string format.

2. Create root dry
The second step is to compile IDL and create Java root dry code. We will use this code to implement customers and servers. The tools provided with the Javaidl product are Idltojava:
idltojava-fserver-fclient remotetime.idl
Where two tags tell idltojava to generate code for both root and dry. Idltojava generates a Java package that is named after the IDL module, Remotetime, and the generated Java files are placed into the remotetime subdirectory. _exacttimeimplbase.java represents the "dry" we use to implement the server object, while _exacttimestub.java is used for customers. In Exacttime.java, the IDL interface is represented in Java. Additional supporting files are included, such as files for simplifying access to the naming service.

3. Implementing servers and Customers
What you see below is the code used by the server side. The server object is implemented in the Exacttimeserver class. Remotetimeserver This application is to create a server object, register it with the Orb, specify the name to use when referencing the object, and then "quietly" wait for the customer to issue a request.
 

Import remotetime.*;
Import org.omg.cosnaming.*;
Import org.omg.cosnaming.namingcontextpackage.*;

Import org.omg.corba.*;
Import java.util.*;

Import java.text.*; Server object Implementation Class Exacttimeserver extends _exacttimeimplbase{public String getTime () {return Da
        Teformat.
          Gettimeinstance (Dateformat.full).
  Format (New Date (System.currenttimemillis ());
    }//Remote application Implementation public class Remotetimeserver {public static void main (String args[]) {
      try {//Orb creation and Initialization:orb orb = orb.init (args, null);
      Create the server object and register It:exacttimeserver timeserverobjref = new Exacttimeserver ();
      Orb.connect (TIMESERVEROBJREF); Get the root naming context:org.omg.CORBA.Object ObjRef = orb.resolve_initial_references ("Na
      Meservice ");
    NamingContext ncref = Namingcontexthelper.narrow (OBJREF);  Assign a string name to the//object reference (Binding): namecomponent NC = new Namecomponent
      ("Exacttime", "");
      Namecomponent path[] = {NC};
      Ncref.rebind (path, timeserverobjref);
      Wait for client requests:java.lang.Object sync = new Java.lang.Object ();
      Synchronized (sync) {sync.wait ();
      The catch (Exception e) {System.out.println ("Remote Time server Error:" + E);
    E.printstacktrace (System.out); }
  }
}


As you can see, the implementation of the server object is very simple; it is an ordinary Java class that inherits from the "dry" code generated by the IDL compiler. But when contacting the Orb and other CORBA services, the situation becomes slightly more complex.

4. Some CORBA services
Here is a brief introduction to the work done by Javaidl related code (note that the fact that CORBA code is related to different vendors is temporarily ignored). The first line of code in main () is used to start the orb. And, of course, that's why the server object needs to communicate with it. Immediately after the Orb initialization, a server object is created. In fact, its official name should be "Short service object": Receive a request from a customer, and the "Live Time" is the same as the process that created it. Once a short service object is created, it is registered with the Orb. This means that the orb already knows it exists and can forward the request to it.
So far, all we have is a timeserverobjref--an object reference that is valid only in the current server process. The next step is to assign a string form name to this service object. The client will look for the service object based on that name. We do this through the naming Service (naming services). First, we need an object reference to the naming service. by calling Resolve_initial_references (), you can obtain a String object reference to the naming service ("Nameservice" in Javaidl) and return the reference. This is the model for a particular namingcontext reference to the Narrow () method. We can now start using the naming service.
To bind a service object to an object reference in the form of a string, we first create a Namecomponent object and initialize it with "Exacttime". "Exacttime" is the name string we want to use to bind the service object. The rebind () method is then used, which is a string reference that is restricted by the object reference. We use rebind () to assign a reference--even if it already exists. If the reference already exists, then bind () can cause an exception. In CORBA, names are made up of a series of namecontext-that's why we use an array to bind names to object references.
The service object is best prepared to be used by the customer. At this point, the server process enters a waiting state. Similarly, because it is a "short-term service," the time to live is limited by the server process. Javaidl has not yet provided support for persistent objects, as long as the process in which they are created remains running, and the objects persist.
Now, we have a certain understanding of the work of server code. Next look at the customer code:

Import remotetime.*;
Import org.omg.cosnaming.*;

Import org.omg.corba.*; public class Remotetimeclient {public static void main (String args[]) {try {//ORB creation and Initializat
      Ion:orb ORB = orb.init (args, null); Get the root naming context:org.omg.CORBA.Object ObjRef = orb.resolve_initial_references ("Na
      Meservice ");
      NamingContext ncref = Namingcontexthelper.narrow (OBJREF); Get (Resolve) the Stringified object//reference for the time server:namecomponent NC = new Nam
      Ecomponent ("Exacttime", "");
      Namecomponent path[] = {NC};
      Exacttime timeobjref = Exacttimehelper.narrow (ncref.resolve (path));
      Make requests to the server object:string exacttime = Timeobjref.gettime ();
    System.out.println (Exacttime);
      catch (Exception e) {System.out.println ("Remote Time server Error:" + E); E.printstacktrace (System.ouT); }
  }
}


The first few lines work the same as they do in the server process: Orb gets initialized and resolves a reference to the naming service.
Next, we need to use an object reference to the service object, so we pass the object reference of the string form directly to the resolve () method and use the narrow () method to sculpt the result into the Exacttime interface reference. The last Call to GetTime ().

5. Activate the name service process
Now we have a separate server and a client application that are ready to communicate with each other. You know both need to take advantage of the naming service bindings and parse the object references in the form of strings. Before running a service or customer, we must start the naming service process. In Javaidl, the naming service belongs to a Java application and is provided with a product kit. But it may be different from other products. The Javaidl naming service runs in one instance of the JVM and (by default) monitors network Port 900.

6. Activate Server and customer
Now, we are ready to start the server and the customer application (in this order because the server exists is "short term"). If all aspects are set correctly, you will get a line of output in the Client console window to remind us what the current time is. Of course, there is nothing exciting about this result in itself. However, a problem should be noted: even on the same machine, client and server applications are still running in different virtual machines. Communication between them is done through a basic integration layer-the integration of the Orb and the naming service.
This is just a simple example for non-network environment design. However, the Orb is usually configured as "location-independent." If the server is on a separate machine from the customer, the orb can resolve the remote string reference using a component named "Setup Library" (Implementation Repository). Although the "Installation Library" is part of CORBA, it has few specific specifications, so the vendor implementations are different.
As you can see, there are many aspects of CORBA that are not covered here in detail. However, through the above introduction, it should have a basic understanding of it. For more detailed information about CORBA, the most fax starting point is the OMB Web site, the address is http://www.omg.org. This place provides a wealth of documentation, white pages, programs, and links to other CORBA resources and products.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.