Building a CORBA Client

March 9, 2008 at 12:38 pm (CORBA, Unit 4) ()

Clients are often (though not always) simpler than servers by nature, so they are easier to implement in that regard as well.

Implementing the Client

There are only a handful of concepts involved: how to use client stubs in the client implementation, how to locate a server object, and how to use the interfaces of a server object after it has been located.

Using Client Stubs

When StockServer.idl is compiled, the IDL compiler generated client stubs as well as server skeletons. Open the _StockServerStub.java file and have a look at it:

public float getStockValue(String symbol) {

}

public String[] getStockSymbols() {

}

Marshal the parameters through the ORB to the remote object and then marshal the return value back to the client. The Java compiler is smart enough to compile this file automatically, but in C++ client implementation, the client stub object should be linked with the rest of the client application. The other thing to know about the client stub is that it specifies the actual interfaces for the server object.

Locating a Server Object

After a server registers itself with the Name Server, clients can locate that server object through the Name Server, bind to that server object, and subsequently call methods on the server object. In the StockMarketClient, binding to the server object takes place in the connect() method, as shown in Listing 1.5. This method first binds to the Name Server by looking for an object with the name NameService. Upon successfully locating a Name Server, the client proceeds to bind to an object with the name StockServer, which incidentally is the same name registered the StockServerImpl under. After this object is bound, the client is ready to do some work.

Listing 1.5. Binding to the StockServer server.

// Connect to the StockServer.

protected void connect() {

try {

// Get the root naming context.

org.omg.CORBA.Object obj = ourORB.

resolve_initial_references(”NameService”);

NamingContext namingContext = NamingContextHelper.narrow(obj);

// Attempt to locate a StockServer object in the naming context.

NameComponent nameComponent = new NameComponent(”StockServer”, “”);

NameComponent path[] = { nameComponent };

myStockServer = StockServerHelper.narrow(namingContext. resolve(path));

}

catch (Exception ex) {

System.err.println(”Couldn’t resolve StockServer: ” + ex);

myStockServer = null;

return;

}

System.out.println(”Succesfully bound to a StockServer.”);

}

Using Server Object Interfaces

Listing 1.6 shows an example of how the server object interfaces are used, after the client has bound the server object.

Listing 1.6. Using the StockServer services.

// Do some cool things with the StockServer.

protected void doSomething() {

try {

// Get the valid stock symbols from the StockServer.

String[] stockSymbols = myStockServer.getStockSymbols();

// Display the stock symbols and their values.

for (int i = 0; i < stockSymbols.length; i++) {

System.out.println(stockSymbols[i] + ” ” +

myStockServer.getStockValue(stockSymbols[i]));

}

}

catch (org.omg.CORBA.SystemException ex) {

System.err.println(”Fatal error: ” + ex);

}

}

In Listing 1.6, the StockServer is first asked, through a call to getStockSymbols(), for a list of all stock symbols recognized by the server. The client then iterates through the list of stock symbols and queries the server, using getStockValue(), for the value of each stock. Each stock symbol and its respective value are printed to standard output.

Compiling and Running the Client

The entire listing for StockMarketClient.java appears in Listing 1.7. Note that most of the work for the client is done in the connect() and doSomething() methods, which you’ve already looked at.

Listing 1.7. StockMarketClient.java.

// StockMarketClient.java

package StockMarket;

import org.omg.CORBA.ORB;

import org.omg.CosNaming.NameComponent;

import org.omg.CosNaming.NamingContext;

import org.omg.CosNaming.NamingContextHelper;

// StockMarketClient is a simple client of a StockServer.

public class StockMarketClient {

// Create a new StockMarketClient.

StockMarketClient() {

}

// Run the StockMarketClient.

public void run() {

connect();

if (myStockServer != null) {

doSomething();

}

}

// Connect to the StockServer.

protected void connect() {

try {

// Get the root naming context.

org.omg.CORBA.Object obj = ourORB.

resolve_initial_references(”NameService”);

NamingContext namingContext = NamingContextHelper.narrow(obj);

// Attempt to locate a StockServer object in the naming context.

NameComponent nameComponent = new NameComponent(”StockServer”, “”);

NameComponent path[] = { nameComponent };

myStockServer = StockServerHelper.narrow(namingContext.resolve(path));

}

catch (Exception ex) {

System.err.println(”Couldn’t resolve StockServer: ” + ex);

myStockServer = null;

return;

}

System.out.println(”Succesfully bound to a StockServer.”);

}

// Do some cool things with the StockServer.

protected void doSomething() {

try {

// Get the valid stock symbols from the StockServer.

String[] stockSymbols = myStockServer.getStockSymbols();

// Display the stock symbols and their values.

for (int i = 0; i < stockSymbols.length; i++) {

System.out.println(stockSymbols[i] + ” ” +

myStockServer.getStockValue(stockSymbols[i]));

}

}

catch (org.omg.CORBA.SystemException ex) {

System.err.println(”Fatal error: ” + ex);

}

}

// Start up a StockMarketClient.

public static void main(String args[]) {

// Initialize the ORB.

ourORB = ORB.init(args, null);

StockMarketClient stockClient = new StockMarketClient();

stockClient.run();

// This simply waits forever so that the DOS window doesn’t

// disappear (for developers using Windows IDEs).

while (true)

;

}

// My ORB.

public static ORB ourORB;

// My StockServer.

private StockServer myStockServer;

}

Compiling the client application is an uncomplicated process. Like the server, the client can be compiled simply with the command

javac StockMarket\StockMarketClient.java

Again, ensure proper directory when compiling the client. The client program should be in the same directory as the one in which the server is compiled. First, start the Name Service and the StockServer application, if they aren’t running already. Then to execute the client application, type the command

java StockMarket.StockMarketClient

If the client runs successfully, the output similar to Listing 1.8 will be displayed in the screen.

Listing 1.8. StockMarketClient output.

1: Succesfully bound to a StockServer.

2: PTLF 72.00064

3: SWPK 37.671585

4: CHHL 78.37782

5: JTUX 75.715645

6: HUPB 41.85024

7: OHQR 14.932466

8: YOEX 64.3376

9: UIBP 75.80115

10: SIPR 91.13683

11: XSTD 16.010124

The stock symbols and their values will appear exactly as they appear in the server output.

Post a Comment

You must be logged in to post a comment.