Mapping Clients  «Prev  Next»
Lesson 9 Writing a basic client
Objective Describe the basic startup sequence of a CORBA client.

Writing a Basic Corba Client

A basic CORBA client simply needs to connect to a CORBA object and then use it. All the work in this process lies in obtaining a reference to the object after that. The remoteness of the object should be completely transparent to the client. Let us look at the basic sequence of actions performed by CORBA clients this will provide you with the basic structure that you will use when writing clients.
  • Client sequence The basic steps performed by a CORBA client are:
    1. Initialize the ORB.
    2. Obtain an object reference to the desired remote object.
    3. Narrow the object reference to the type of the remote object.
    4. Use the reference to invoke methods on the remote object.
    Later in the course, you will learn some of the various ways to look up and obtain object references (step 2). You will learn the details of narrowing a generic object reference to an interface-specific reference (step 4) later in this module, when we discuss helper classes.
Let us look at some code that embodies the above steps by constructing a sample client for our WeatherService server.

Corba Programming C++
1) Initialize the ORB
1) Initialize the ORB
package weather;
import jacorb.naming.NameServer;
import org.omg.CosNaming.*;
public class SimpleWeatherClient{
  public static void main(String args[]){
    try{
      WeatherServic wServcie;
      org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.int(args, null);
      org.omg.CORBA.Ojbect wRef = (Module 5)//Lookup reference

2) Obtain an object reference to the remote object
2) Obtain an object reference to the remote object
package weather;

import jacorb.naming.NameServer;
import org.omg.CosNaming.*;

public class SimpleWeatherClient
{
    public static void main(String args[])
    {
        try
        {
            WeatherService wService;
            org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
            // 2) Obtain an object reference to the remote object
            org.omg.CORBA.Object wRef = // Lookup reference
                                        (Module 5);

            wService = WeatherServiceHelper.narrow(wRef);

            System.out.println("Weather = " + wService.getReport("New York"));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

3) Narrow the generic reference to the interface-specific type reference
3) Narrow the generic reference to the interface-specific type reference

4) Use the interface type reference (invoke methods)
4) Use the interface type reference (invoke methods)

IDL Interfaces

The OMG Interface Definition Language (IDL) is used to define interfaces through which a client is informed of the services supported by an object implementation. OMG IDL is purely a descriptive language so that an interface is defined in a languageneutral way. It enables potential clients to identify the available operations and their invocation methods for a particular object implementation. An interface definition written in OMG IDL completely defines the interface and fully specifies the operation signatures, embracing a set of named operations and the parameters to those operations. Hence in CORBA, the OMG IDL definitions are used to create the stubs and skeletons, as well as to populate the Interface Repository. To specify the parameter types and return types for operations, OMG IDL provides a set of data types that are similar to those found in a number of programming languages. It supports built-in data types such as long, short, float, double, char and boolean. The sizes, byte ordering and interpretation of all these basic types are precisely defined to ensure interoperability across heterogeneous platforms. OMG IDL also supports constructed types such as struct and discriminated union, and template types such as string and sequence whose exact characteristics are defined at declaration time.

Object Reference Types

More importantly, interfaces are used as object reference types to describe CORBA objects. An operation can take object references as arguments and return the appropriate object references. In addition, OMG IDL provides interface inheritance, allowing derived interfaces to inherit the operations and types defined in the base interfaces. All IDL interfaces are implicitly derived from a root interface called Object defined in the CORBA module, which provides services common to all ORB objects.
CORBA objects, expressed in OMG IDL, are mapped into particular programming languages or object systems according to the specifications of the object interfaces. A client uses the IDL interface specifications, but the actual call is done in the native programming language, as a result of mapping of the IDL interfaces to the programming language. As expected, the mapping of OMG IDL to a particular programming language is the same for all ORB implementations. This includes definition of the language-specific data types and procedure interfaces to access objects through the ORB. OMG IDL obeys the same lexical rules as C++. It has similar syntax and the grammar is actually a subset of the C++ grammar with additions necessary for distributed invocations. The C++ concept of class roughly corresponds to the concept of an IDL interface. In the next lesson, you will learn about the stub model that is used under the hood of the client operations we just discussed.