Programming Example: Array Adder
Now let's see how to use the POA to develop a CORBA application. The application that we will develop here is an array adder: the client provides two arrays and the server adds them together and sends the result back to the client. We will develop two versions of the application: a transient server and a persistent server.
Array Adder: Transient Server
The first step in developing any CORBA application is to define the interface in the OMG Interface Definition Language (IDL). The IDL interface for the array adder is shown in Code Sample 1. Here I define a module ArithApp (which is equivalent to a Java package), an interface Add that contains a constant, a new data type array (which is a synonym for an array of longs and an operation addArrays that takes in two arrays as input (specified using the in) and another array as the output holder (specified using the out).
Code Sample 1: Add.idl
module ArithApp {
interface Add {
const unsigned short SIZE=10;
typedef long array[SIZE];
void addArrays(in array a, in array b,
out array result);
};
};
You can now compile this IDL interface to map it to Java and generate stubs and skeletons. This can be done using the idlj compiler. When you run this tool you can request it to generate client stubs only, server side skeletons only, or both. Here you want to generate both, client stubs and server skeletons. To do so use the following command:
prompt> idlj -fall Add.idl
This command will generate several files. Check the local directory where you run the command from to see the files. You will notice that a new subdirectory with the name ArithApp has been created. This is because an OMG IDL module is mapped to a Java package. For more information on the idlj compiler and the options you can use, please see the IDL-to-Java Compiler.
--------------------------------------------------------------------------------
Note: The new idlj compiler in J2SE 1.4 generates server-side mappings for the Portable Object Adapter (POA). The new compiler is, however, backward compatible with earlier releases since it provides the -ImplBase flag that can be used to generate server-side mappings for existing applications that have been created using J2SE 1.3 or earlier versions. Therefore, in order to talk to existing applications that have been created using J2SE 1.3 or earlier, you need to use the -ImplBase flag to generate server-side mappings. New applications do not need to generate these deprecated server-side mappings.
--------------------------------------------------------------------------------
The next step is to implement the IDL interface in Code Sample 1. An implementation is shown in Code Sample 2. The AddImpl class is a subclass of AddPOA, which is generated by the idlj compiler from the IDL interface. Note the third parameter to the addArrays operation. Here I am using an array holder simply because I am using the out parameter as a holder for the output array.
Code Sample 2: AddImpl.java
import ArithApp.*;
import org.omg.CORBA.*;
class AddImpl extends AddPOA {
private ORB orb;
public AddImpl(ORB orb) {
this.orb = orb;
}
// implement the addArrays() method
public void addArrays(int a[], int b[],
ArithApp.AddPackage.arrayHolder result) {
result.value = new int[ArithApp.Add.SIZE];
for(int i=0; i<ArithApp.Add.SIZE; i++) {
result.value[i] = a[i] + b[i];
}
}
}
The next step is to develop the server. A sample server is shown in Code Sample 3. The server performs the following tasks:
Creates and initializes the ORB.
Creates an instance of the interface implementation and registers it with the ORB.
Gets a reference to the RootPOA and activates the POAManager.
Gets an object reference from the servant.
Gets the root naming context from the naming service and registers the new object under the name "Add".
Waits for invocations from clients.
Code Sample 3: AddServer.java
import ArithApp.*;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import org.omg.CosNaming.NamingContextPackage.*;
public class AddServer {
public static void main(String args[]) {
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// create an implementation and register it with the ORB
AddImpl impl = new AddImpl(orb);
// get reference to rootpoa & activate the POAManager
POA rootpoa = POAHelper.narrow(
orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
// get object reference from the servant
org.omg.CORBA.Object ref =
rootpoa.servant_to_reference(impl);
Add href = AddHelper.narrow(ref);
// get the root naming context
// NameService invokes the name service
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
// Use NamingContextExt which is part of the Interoperable
// Naming Service (INS) specification.
NamingContextExt ncRef =
NamingContextExtHelper.narrow(objRef);
// bind the Object Reference in Naming
String name = "Add";
NameComponent path[] = ncRef.to_name( name );
ncRef.rebind(path, href);
System.out.println("AddServer
ready to add up your arrays ....");
// wait for invocations from clients
orb.run();
} catch (Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
System.out.println("AddServer Exiting ....");
}
}
Now, implement the client. A sample client is shown in Code Sample 4. The client code performs the following tasks:
Creates and initializes the ORB.
Obtains a reference to the root naming context.
Looks up the "Add" object in the naming context and obtains a reference to it.
Invokes the addArrays method and prints the results.
Code Sample 4: AddClient.java
import ArithApp.*;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
public class AddClient {
public static void main(String args[]) {
try {
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// get the root naming context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
// Use NamingContextExt instead of NamingContext. This is
// part of the Interoperable Naming Service.
NamingContextExt ncRef =
NamingContextExtHelper.narrow(objRef);
// resolve the Object Reference in Naming
String name = "Add";
Add impl = AddHelper.narrow(ncRef.resolve_str(name));
System.out.println("Handle
obtained on server object: " + impl);
// the arrays to be added
int a[] = {6, 6, 6, 6, 6, 6, 6, 6, 6, 6};
int b[] = {7, 7, 7, 7, 7, 7, 7, 7, 7, 7};
// the result will be saved in this new array
ArithApp.AddPackage.arrayHolder c =
new ArithApp.AddPackage.arrayHolder();
// invoke the method addArrays()
impl.addArrays(a, b, c);
// print the new array
System.out.println("The sum of the two arrays is: ");
for(int i=0;i<ArithApp.Add.SIZE;i++) {
System.out.println(c.value[i]);
}
} catch (Exception e) {
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
}
}
}
Now you can compile the classes AddImpl, AddServer, AddClient, and the stubs and skeletons that were generated by the idlj compiler. This is done using the javac compiler as follows:
prompt> javac *.java ArithApp/*.java
To run the application:
Start the orbd, which is a name server:
prompt> orbd -ORBInitialPort 2500
The number 2500 is the port number where you want the orbd to run. Note that the -ORBInitialPort is a require command-line argument.
Start the AddServer:
prompt> java AddServer -ORBInitialPort 2500
This command starts the server as shown in Figure 2.
Figure 2: Starting the AddServer
Here we are assuming that both the AddServer and orbd are running on the same host. If the orbd is running on a different host, use the -ORBInitialHost argument to inform the server where to find the orbd.
Start the AddClient:
prompt> java AddClient -ORBInitialPort 2500
You should see the sum of the two arrays as shown in
Figure 3: Starting the AddClient
分享到:
相关推荐
CORBA(Common Object Request Broker Architecture,公共对象请求代理体系结构)是用于分布式计算的一种标准,它允许不同系统间的软件对象进行交互,就像它们在同一个进程内一样。在C++环境中进行CORBA开发可以帮助...
Java CORBA,全称为Java Common Object Request Broker Architecture(Java通用对象请求代理架构),是一种用于分布式计算的技术,允许不同网络上的对象或服务之间进行交互。它实现了对象请求代理(ORB)的概念,...
《iManager U2000 V100R006C02 北向CORBA接口 用户指南》是华为公司为网络管理平台iManager U2000提供的一份详细的技术文档,主要针对该系统如何通过CORBA(Common Object Request Broker Architecture,公共对象...
CORBA支持多种传输协议和编码格式,如IIOP(Internet Inter-ORB Protocol)和GIOP(General Inter-ORB Protocol),确保了跨网络和系统的互操作性。 当涉及到COM与CORBA的互用时,由于它们的设计理念和实现机制不同...
CORBA(Common Object Request Broker Architecture,公共对象请求代理体系结构)是用于分布式计算的一个标准,它允许不同系统间的对象进行交互,仿佛它们都在同一进程中。Java版的CORBA利用Java平台的强大功能,...
** Ultra Corba Simulator 1.3.6:深入理解与应用** Ultra Corba Simulator是一款针对Corba技术的专业模拟工具,版本为1.3.6。Corba,全称为Common Object Request Broker Architecture(通用对象请求代理架构),...
《Advanced CORBA Programming with C》是一本面向有C++基础的程序员的专业书籍,旨在深入探讨CORBA(Common Object Request Broker Architecture)技术在C++中的高级编程应用。这本书以英文原版的形式提供,确保了...
### CORBA技术详解与实践案例 #### 一、CORBA技术概述 CORBA(Common Object Request Broker Architecture,通用对象请求代理系统)是一种分布式计算环境下的软件架构标准,它由OMG(Object Management Group,...
Java 编写的通用Corba Client是一种强大的工具,用于测试和交互与Corba(Common Object Request Broker Architecture)服务。Corba是一种标准的中间件技术,它允许不同操作系统、编程语言和网络环境中的对象相互通信...
CORBA(Common Object Request Broker Architecture)标准,即通用对象请求代理体系结构,是由OMG(Object Management Group)定义的分布式计算的关键技术之一。CORBA标准旨在提供一种机制,允许不同的软件组件...
UCS Ultra Corba Simulator 中文使用说明书 UCS Ultra Corba Simulator 是一个模拟器工具,旨在帮助用户快速学习和掌握 Corba 技术。下面是对 UCS 用户手册的详细解释和知识点总结: 项目背景 UCS Ultra Corba ...
### CORBA原理及应用知识点详解 #### 一、CORBA简介 - **CORBA**(Common Object Request Broker Architecture,通用对象请求代理体系结构)是一种分布式计算标准,旨在提供跨网络和平台之间的对象交互能力。 - **...
【标题】:CORBA程序源代码 【描述】:这些源代码示例涵盖了多个使用CORBA(Common Object Request Broker Architecture)技术的程序,包括Java RMI(Remote Method Invocation)、Inprocess通信、简单的HELLO世界...
**CORBA核心规范——一个简单的CORBA/Java示例** **一、CORBA基础** **CORBA(Common Object Request Broker Architecture)**,即公共对象请求代理架构,是一种跨平台、跨语言的分布式计算模型,旨在解决不同系统...
**基于C++的CORBA实现** CORBA(Common Object Request Broker Architecture)是一种开放的、标准的中间件技术,用于构建跨平台、跨网络、跨语言的分布式计算环境。它允许不同计算机上的对象相互通信,仿佛它们都在...
CORBA(Common Object Request Broker Architecture,公共对象请求代理体系结构)是一种分布式计算技术,它允许在不同的操作系统、网络环境和编程语言之间进行无缝通信。CORBA技术系列丛书之"CORBA服务"着重于深入...
【CORBA概述】 CORBA(Common Object Request Broker Architecture,公用对象请求代理体系结构)是一种分布式计算框架,旨在解决不同计算平台上的对象之间的交互问题。它由Object Management Group(OMG)组织提出...
### JAVA与CORBA的深度整合:构建服务端与客户端实例 #### 一、CORBA概述与架构 CORBA(Common Object Request Broker Architecture)是对象管理组织(OMG)提出的一种面向对象的分布式计算架构标准,旨在实现不同...