连接Corba和dotNet

** 连接 ** ** Corba ** ** 和 ** ** dotNet **

** 翻译 ** ** HowCanIDo **

** 原文 ** ** http://www.devx.com/interop/Article/19916/0/page/1 **

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).

![](http://dev.csdn.net/article/38/C:/Documents and Settings\Administrator\My Documents\My Pictures\8347.png)

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

![](http://dev.csdn.net/article/38/C:/Documents and Settings\Administrator\My Documents\My Pictures\8348.png)

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 服务端使用 ) 。

![](http://dev.csdn.net/article/38/C:/Documents and Settings\Administrator\My Documents\My Pictures\8350.png)

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

  1<corba.h>
  2
  3#include "ICorbaDiaryServerServer.h" 
  4
  5#include "MainForm.h" 
  6
  7//---------------------------------------------------- 
  8
  9#pragma package(smart_init) 
 10
 11ICorbaDiaryServerImpl::ICorbaDiaryServerImpl( 
 12
 13const char *object_name): 
 14
 15_sk_DiarySrv::_sk_ICorbaDiaryServer(object_name) 
 16
 17{ 
 18
 19if (Form1) 
 20
 21Form1-&gt;Caption = 
 22
 23"ICorbaDiaryServer up and running..."; 
 24
 25} 
 26
 27void ICorbaDiaryServerImpl::Meeting( 
 28
 29const CORBA::WChar* _Names, 
 30
 31const DiarySrv::DateTime&amp; _DayTime, CORBA::Long 
 32
 33_Duration) 
 34
 35{ 
 36
 37if (Form1) 
 38
 39Form1-&gt;Memo1-&gt;Lines-&gt;Add( 
 40
 41"Meeting with " + AnsiString(_Names)); 
 42
 43if (_Duration &gt; 60) 
 44
 45throw 
 46
 47DiarySrv::ICorbaDiaryServer::MeetingImpossible( 
 48
 49"I don't like long meetings..."); 
 50
 51} 
 52
 53Figure 4 shows the running application. 
 54
 55![](http://dev.csdn.net/article/38/C:/Documents and Settings\\Administrator\\My Documents\\My Pictures\\8351.png)
 56
 57Figure 4. The Running CORBA Server: Here's a screenshot of the completed CORBA server. 
 58
 59注意检查会议时间的代码,如果会议持续时间超过一个小时,会抛出一个错误信息:  "I don't like long meetings."  ,很显然,这个异常纯粹是演示  Corba  异常,以及  C  #客户端接受并回应这个错误。 
 60
 61在运行  Corba Server  之前,要保证  VisiBroker  在运行状态。 
 62
 63#  Build a CORBA Client 
 64
 65我已经用  C  #  builder  创建了一个  dotNet Corba  的客户端,但理论上来说,任何  dotNet  语言都是可以的。  Start C#Builder Enterprise or Architect, and create a new C# application. In the New Application dialog you can specify the name as well as the location of the new project (see Figure 5). 
 66
 67![](http://dev.csdn.net/article/38/C:/Documents and Settings\\Administrator\\My Documents\\My Pictures\\8352.png)
 68
 69#  Borland Janeva 1.0 
 70
 71Borland Janeva  可以让  Microsoft .NET Framework  应用和  J2EE/CORBA server  对象无缝结合并高效集成。  C  #  Builder  的企业版和架构版都带有  Janeva  ,你也可以到  Borland  网站上下载(和其它  dotNet  开发环境结合,详细信息参见本文结尾)。 
 72
 73![](http://dev.csdn.net/article/38/C:/Documents and Settings\\Administrator\\My Documents\\My Pictures\\8353.png)
 74
 75Figure 6. Add CORBA Reference: Janeva adds two items to the Add References menu, letting you select J2EE or CORBA references as well as standard file or Web references. 
 76
 77在免费注册后,你可以得到一个用来测试的  licence  。但是,如果你要分发  .Net  客户端,则需要购买分发  licence  。 
 78
 79安装完  Janeva  ,你就可以右击  C  #  builder  中的工程文件,增加一个  J2EE  或  Corba  引用(见图  6  ),没有  Janeva  ,你只能增加普通接口或  Web reference  。 
 80
 81因为本例是  Corba server  ,选择增加  Corba  引用,将会显示一个对话框,可以选择  Corba  服务定义的  IDL  文件。如图  7  。 
 82
 83Corba Server  应用发布“接口”定义是通过  IDL  文件,支持  Corba  客户端应用的开发环境通常都支持将  IDL  转化为计算机语言,如  C++  、  java  ,然而对于  dotNet  客户端,必须要将  IDL  转化为  dotNet  语言,如  VB.NET  或  c  #,  IDL  -  to  -  C  #(  IDL2CS  )是  Borland Janeva  最受欢迎的特征。 
 84
 85![](http://dev.csdn.net/article/38/C:/Documents and Settings\\Administrator\\My Documents\\My Pictures\\8354.png)
 86
 87Figure 7. Adding a CORBA Reference: Browse to the location of the DiarySrv.idl file to add the reference. 
 88
 89注意,尽管你导入了  Corba Server  的定义,但是你还没有指明如何连接它。 
 90
 91添加了  IDL  文件后,  Janeva  导入并编译  IDL  文件,产生一个相应的  CS  文件(  DiarySvr.cs  )。 
 92
 93![](http://dev.csdn.net/article/38/C:/Documents and Settings\\Administrator\\My Documents\\My Pictures\\8355.png)
 94
 95"Figure 8. Manual Janeva Compile: If you need to alter an IDL file after adding it, right-click the file in Project Manager and select Janeva Compile from the context menu. 
 96
 97如果将来你要改变  IDL  文件,可以在工程管理器中右击  IDL  文件(如图  8  )选择重新编译。 
 98
 99要使用  DiarySrv.cs  ,在  using  中增加  Corba  命名空间和产生的  DiarySrv  命名空间。 
100
101using System; 
102
103using System.Drawing; 
104
105using System.Collections; 
106
107using System.ComponentModel; 
108
109using System.Windows.Forms; 
110
111using System.Data; 
112
113using CORBA; 
114
115using DiarySrv; 
116
117在  WinForm  的构造函数中,创建  ORB  (对象请求代理)的实例,来连接客户端和服务端。要做到这样,利用  vbroker  代理端口和  14000  参数(  visibroker  默认端口-译者注)调用  CORBA.ORB.Init  即可。 
118
119我使用的是默认的  14000  端口,你可以使用另外的端口,端口的配置相信信息请参见  Corba ORB  的文档。 
120
121string[] args = new string[] 
122
123{"-vbroker.agent.port", "14000"}; 
124
125orb = CORBA.ORB.Init(args); 
126
127#  Calling the CORBA Server 
128
129配置完客户端程序后,可以利用一个按钮来创建  Corba DiarySrv  对象的实例。然后调用  Meeting  方法。 
130
131private void button1_Click(object sender, 
132
133System.EventArgs e) 
134
135{ 
136
137try 
138
139{ 
140
141MyCorbaDiaryServer = 
142
143ICorbaDiaryServerHelper.Bind( 
144
145"ICorbaDiaryServerObject"); 
146
147DiarySrv.DateTime MyDateTime = new 
148
149DiarySrv.DateTime(); 
150
151MyDateTime.Date = 
152
153Convert.ToInt32(textBox2.Text); 
154
155MyDateTime.Time = 
156
157Convert.ToInt32(textBox3.Text); 
158
159MyCorbaDiaryServer.Meeting(textBox1.Text, 
160
161MyDateTime, Convert.ToInt32(textBox4.Text)); 
162
163} 
164
165catch 
166
167(DiarySrv.ICorbaDiaryServerNS.MeetingImpossible 
168
169ex) 
170
171{ 
172
173MessageBox.Show(ex.Reason, "CORBA Exception"); 
174
175} 
176
177catch (Exception ex) 
178
179{ 
180
181MessageBox.Show(ex.Message, "Error"); 
182
183} 
184
185} 
186
187对照代码,可以发现: 
188
189首先,必须通过正确的名字(在  Corba  服务端创建  Corba Diary  服务对象时指定的,本例中为  ICorbaDiaryServerObject  )调用绑定方法。 
190
191绑定完  Corba  服务器端对象后,才能调用  Meeting  方法。但是不要没有检索  Date  和  Time  ,并构造特殊的  DiarySrv.DateTime  (  IDL  文件和  Corba  服务端实现的类型)  对象。 
192
193![](http://dev.csdn.net/article/38/C:/Documents and Settings\\Administrator\\My Documents\\My Pictures\\8356.png)
194
195Figure 9. C#Builder CORBA Client: The figure shows the main window for the completed CORBA client running. 
196
197代码还包含了  4  个  Label  和  TextBox  控件,用来让最终用户输入  Meeting  方法需要的名称,日期,时间,持续时间等信息。在运行阶段,应用程序效果如图  9 
198
199注意,持续时间设定为  61  分钟,仅仅是让  Corba  客户端抛出异常(如图  10  )。 
200
201![](http://dev.csdn.net/article/38/C:/Documents and Settings\\Administrator\\My Documents\\My Pictures\\8349.png)
202
203Figure 10. CORBA Exception: A MessageBox display of an exception received by the C#Builder CORBA client. 
204
205只有将时间限制在  60  分钟内。就可以安全的增加会议安排到议事日程中。 
206
207你在这里所看到的就是  Janeva  作为所谓的在  Corba server  和  dotNetClient  间的企业应用  集成(  EAI  )层。这项技术的主要优点就是你可以不需重写或修改已经存在的  Corba  服务就可以连接  dotNet  应用。 
208
209――――――――――――――――――――――――――――――――――― 
210
211第一次翻译,请指正。</corba.h>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus