跳轉到內容

ACE+TAO 開源程式設計筆記/建立伺服器

來自華夏公益教科書,自由的教科書

本節描述如何建立一個類似於 TAO 示例中 quoter 示例(ACE_wrappers/TAO/examples/Quoter)的簡單伺服器。
要使此功能正常工作,請建立 IDL 檔案並在該檔案上執行 tao_idl 可執行檔案。命令列看起來像這樣

tao_idl -GI basic.idl

這會生成許多原始檔,其中只有一兩個需要編輯。

原始檔 main.cpp

[編輯 | 編輯原始碼]
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream>
#include <cstdlib>
#include "basicI.h"
#include "basicC.h"
#include "ace/streams.h"

using namespace std;
using namespace CORBA;

int main (int argc, char* argv[])
{
    // First initialize the ORB, that will remove some arguments...
    ORB_var orb = ORB_init(argc, argv, "ORB" /* the ORB name, it can be anything! */);
    Object_var poa_object = orb->resolve_initial_references("RootPOA");
    PortableServer::POA_var poa = PortableServer::POA::_narrow(poa_object.in());
    PortableServer::POAManager_var poa_manager = poa->the_POAManager();
    poa_manager->activate();

    // Create the servant
    My_Factory_i my_factory_i;

    // Activate it to obtain the object reference
    My_Factory_var my_factory = my_factory_i._this();

    // Put the object reference as an IOR string
    String_var ior = orb->object_to_string (my_factory.in());
    // Print it out!
    //Note: You will be using this reference in the client to get it to connect to this server
    //Don't even bother copying it down, it changes with each invocation & is as long as your arm
    cout << ior.in() << endl;
    //By default, the following doesn't return unless there is an error
    orb->run();

    // Destroy the POA, waiting until the destruction terminates
    poa->destroy(1, 1);
    orb->destroy();
    return 0;
}

原始檔 basic.idl

[編輯 | 編輯原始碼]

針對以下原始碼執行 tao_idl 程式,會生成建立簡單伺服器所需的所有檔案。實際命令列應該是 'tao_idl -GI basic.idl' 命令列選項 '-GI' 用於生成下面列出的與伺服器相關的介面檔案。您只需實現此檔案(即填寫一些空成員函式),然後就可以開始運行了。

// Forward declare
interface Widget;

interface My_Factory
{
    // = TITLE
    //   A factory class for the widget interfaces
    //
    // = DESCRIPTION
    //   Return the Quoter interfaces based on their names
    //
    Widget get_widget (in string widget_name);
};

interface Widget
{
    // = TITLE
    //   A simple interface to query the name and price of a widget
    //
    // = DESCRIPTION
    //   Return the price and name of a single widget
    //

    readonly attribute string full_name;
    // Get the name.

    double price ();
    // Get the price

};

生成的伺服器介面原始碼

[編輯 | 編輯原始碼]

不要忘記實現伺服器的介面(basicI.cpp)。我通常將此作為對其他類的一個簡單呼叫,其中包含所有有趣的程式碼。

#include "basicI.h"

// Implementation skeleton constructor
My_Factory_i::My_Factory_i (void)
{
}

// Implementation skeleton destructor
My_Factory_i::~My_Factory_i (void)
{
}

::Widget_ptr My_Factory_i::get_widget (
    const char * widget_name
  )
  ACE_THROW_SPEC ((
    ::CORBA::SystemException
  ))
{
  // Add your implementation here
}

// Implementation skeleton constructor
Widget_i::Widget_i (void)
{
}

// Implementation skeleton destructor
Widget_i::~Widget_i (void)
{
}

char * Widget_i::full_name (
    
  )
  ACE_THROW_SPEC ((
    ::CORBA::SystemException
  ))
{
  return "Some widget name";
}

::CORBA::Double Widget_i::price (
    
  )
  ACE_THROW_SPEC ((
    ::CORBA::SystemException
  ))
{
  return 1.25;
}
華夏公益教科書