CORBA 程式設計/Echo/物件
回聲物件是一個簡單的物件,只有一個方法。該方法接受一個字串引數並返回該字串,不變。
任何 CORBA 物件都需要在 CORBA 的 介面定義語言 (IDL) 中定義。IDL 語法基於 C/C++,包括使用 C 預處理器。因此,我們需要通常的保護措施來防止雙重包含
#ifndef __TEST_ECHO_DEFINED
#define __TEST_ECHO_DEFINED
接下來,我們定義一個模組名稱。雖然對於這樣一個小的示例來說不是嚴格需要的,但最好不要用類汙染全域性名稱空間,而是使用模組。
module Test
{
在 CORBA IDL 中,類稱為“介面”。這不要與 Ada 或 Java 中的介面混淆。CORBA 介面是類的公共部分——您可以與之互動的類部分。
interface Echo
{
如前所述,我們只定義了一個方法。它接受一個名為 Message 的字串作為輸入引數,並返回一個字串作為結果。
string
Echo_String (
in string Message
);
};
// end interface Echo
};
// end module Test
#endif
在下載下面的規範之前,您應該嘗試呼叫以下 PolyORB 命令。它將為您生成實現模板。
idlac -i test-echo.idl
大多數 CORBA 實現將為所選的實現語言生成規範和實現模板檔案。您可以從哪些實現語言中選擇取決於您的 ORB。在本例中,它是 Ada。
with CORBA;
with PortableServer;
CORBA 模組 (Test) 和類 (Echo) 被對映到 Ada 包層次結構——這是在 Ada 中進行面向物件程式設計的正常方式。CORBA 語言對映始終嘗試將實現語言對映得儘可能接近。Echo 層次結構包含多個子包,這些子包完全生成,不需要注意。只有子包“Impl”包含我們 Echo 類的實際實現,需要編輯。
package Test.Echo.Impl
is
Object 型別包含我們的類可能想要儲存的任何資料。但是,所有儲存的資料都是私有的,實際上 CORBA 物件中的所有資料都是私有的。
type Object
is new
PortableServer.Servant_Base
with private;
type Object_Ptr is access Object'Class;
Echo 函式被指定為對普通 Ada 類的普通原始操作。如果您有興趣瞭解 Ada 類是如何宣告的,您可以閱讀 Ada 程式設計/面向物件。
function Echo_String (
-- The Object itself
Self : access Object;
-- The string which should be echoed.
Message : in CORBA.String)
return
-- The returned string - which is the same as Message
CORBA.String;
private
在這裡,在包的私有部分,我們現在可以為我們的類宣告資料成員。但是我們簡單的回聲類不需要任何資料,所以我們只宣告一個“空記錄”。如果您有興趣瞭解它在包含資料成員的情況下會是什麼樣子,您可以閱讀 Ada 程式設計/型別/記錄。
type Object
is new
PortableServer.Servant_Base
with null record;
end Test.Echo.Impl;
現在我們知道了 CORBA 物件在 Ada 中的樣子,我們必須實際實現該物件。實現比需要的大一點——使用 PolyORB 日誌記錄功能來生成診斷輸出。
with PolyORB.Log;
with Test.Echo.Skel;
--
-- The following packages are only initialized but not used otherwise.
--
pragma Warnings (Off, Test.Echo.Skel);
--
-- Test.Echo.Skel is only initialized but not used.
--
pragma Elaborate (Test.Echo.Skel);
package body Test.Echo.Impl
is
--
-- a Test Module to test the basic PolyORB functions
--
--------------------------------------------------------------------------------
--
-- Initialize logging from configuration file.
--
package Log is new PolyORB.Log.Facility_Log ("test.echo");
--------------------------------------------------------------------------------
--
-- Log Message when Level is at least equal to the user-requested
-- level for Facility.
--
procedure Put_Line (
Message : in Standard.String;
Level : in PolyORB.Log.Log_Level := PolyORB.Log.Info)
renames
Log.Output;
--------------------------------------------------------------------------------
--
-- Echo the given string back to the client.
--
function Echo_String (
-- The Object itself
Self : access Object;
-- The string which should be echoed.
Message : in CORBA.String)
return
-- The returned string - which is the same as Message
CORBA.String
is
pragma Unreferenced (Self);
begin
Put_Line (
"Echoing string: « " &
CORBA.To_Standard_String (Message) &
" »");
return Message;
end Echo_String;
--------------------------------------------------------------------------------
end Test.Echo.Impl;