日期:2012-04-16  浏览次数:20407 次

Corba, 公用对象请求代理程序结构(Common Object Request Broker Architecture)的首字母缩写,在跨平台和跨语言(如J2EE)的分布式(多层)系统通信中具有广泛的应用,将CORBA连接到dotNET(在多层应用中,ASP.NET Web services and .NET Remoting 是其主要应用方式)是不容易的。要连接这两者,就需要一个能将dotNet的object描述成Corba的object,以至于J2EE能使它们交互的方法,同样,也能将Corba的object描述成dotNet的object,让dotNet的code能认识。换句话说,需要一些中间代码能翻译Corba和dotNet FrameWork之间的这些对象和方法调用。Borland的Janeva可以做到这一点。它简化了Corba或J2EE和dotNet之间的处理过程。

Getting Started
在开始之前,你需要有一个Corba Server,尽管有很多读者都有而且在运行,但是还有很多没有,所以,本文第一步就演示如何用C++创建一个简单的Corba Server以练习和测试。要说明的是Corba Server同样可以用Java,Delphi 以及其它语言编写。第二步演示如何利用Janeva连接这个Server。

本例中的Server是用Borland C++Builder 6 Enterprise.创建的。如果没有Borland C++Builder,你可以下载编译过的Server,这个Server是基于一个个人议事日程的系统,用来安排电子会议。这个日程编排的细节对于本文来说不是很重要。重要的是如何你用Janeva连接Servere和Client。

Build a CORBA Server
To build the CORBA server, start C++Builder, click the File | New menu, select Other and go to the Multitier tab of the Object Repository (see Figure 1).




Figure 1. C++Builder Object Repository: The Object Repository contains a list of the items available



Figure 2. CORBA Server Wizard: You use this wizard to specify project options for a new CORBA server project.

A number of icons appear in the dialog related to CORBA. To start a new CORBA server project from scratch, double-click on the CORBA Server icon, which will display the CORBA Server Wizard so you can specify the options for the new project (see Figure 2).

You can choose to create a Console Application (with or without using Borland's Visual Component Library) or a Windows Application. For this project, select the Windows Application option, because it's convenient to have the server display some visual (debug) information during development.

一个Corba服务能包含一个或多个Corba对象,这些Corba对象的接口声明通常都存储在IDL文件中,当新建一个Corba服务时,可以选择一个存在的IDL 文件或者新建一个空IDL文件,然后加入Corba对象的定义,本例中的IDL如下:

module DiarySrv

{

struct DateTime {

long Date; // example: 20030929

long Time; // example: 1200

};



interface ICorbaDiaryServer

{

exception MeetingImpossible

{

string Reason;

};



void Meeting(in wstring Names, in DateTime DayTime, in long Duration)

raises (MeetingImpossible);

};

};



当点击Corba Server的向导OK按钮后,C++Builder会创建一个新的工程,将Form保存为.MainForm.cpp,工程文件保存为BCB6CorbaServer.bpr。在IDE中将DiarySrv.idl加入工程。当你编译这个工程,C++Builder会运行idl2cpp工具,产生4个基于IDL定义的文件,即DiarySrv_c.cpp and DiarySrv_c.hh (for the stub classes,客户端使用) , DiarySrv_s.cpp and DiarySrv_s.hh (for the skeleton classes服务端使用)。




Figure 3. CORBA Object Implementation Wizard: You use this Wizard to specify the IDL file, select interface names, the delegation model, and other properties.

CORBA Object Implementation
分开skeleton和stub,要有一个能实际运行的DiarySrv Corba对象。可以使用向导。(see Figure 1 for the icon in the Object Repository). Click File | NewOther, go to the Multitier tab, and double-click on the CORBA Object Implementation icon, which will display the dialog shown in Figure 3。

对于每一个IDL文件,可以选择所有接口,根据你选择的是delegation model、data module并且是否需要在WinMain中创建一个对象示例,向导会建议你输入单元名、类名等。

点击OK,IDE会产生两个文件ICorbaDiaryServer.cpp,ICorbaDiaryServer.h。这就是写实际运行的Corba Server对象实现的地方。部分代码:

#pragma hdrstop



#include <corba.h>

#include "ICorbaDiaryServerServer.h"



#include "MainForm.h"



//----------------------------------------------------



#pragma package(smart_init)





ICorbaDiaryServerImpl::ICorbaDiaryServerImpl(

const char *object_name):

_sk_DiarySrv::_sk_ICorbaDiaryServer(object_name)