跟我从头学TAO编程系列(3) -- 通过实例学习CORBA核心概念

跟我从头学TAO编程系列

通过实例学习CORBA核心概

Stone Jiang [email protected]

http://www.ace-tao.org

 

本系列的第一篇我们学会了怎么下载和编译ACE,TAO第二篇我们编写了最简单的TAO应用程序。学习CORBA,TAO与学习其它学科一样,一定要掌握其核心概念。这了节,我们通过第二篇的实例来学习CORBA的核心概念。

 

1.CORBA架构

CORBA的架构如下图:

image

2. CORBA核心概念

 

2.1 CORBA Object

CORBA对象是一个“虚拟”的实体。它可以被ORB定位和被客户端调用。在上一篇示例中,

interface Hello即为corba对象, 定义在Hello.idl中

//@file:   Test.idl
//@author: StoneJiang<[email protected]>
//@ref :  http://www.ace-tao.org

/// Put the interfaces in a module, to avoid global namespace pollution
module Test
{
  /// A very simple interface
  interface Hello
  {
    /// Return a simple string
    string get_string ();

    /// A method to shutdown the ORB
    /**
     * This method is used to simplify the test shutdown process
     */
    oneway void shutdown ();
  };
};

 

CORBA对象并不真实存在,它只存在于概念中。因此,CORBA对象需要程序员用编程语言把它实现。

 

2.2 client,object reference, request,stub

在本例在,client指client.exe。它是发起请求的程序。它的代码片如下

#include "TestC.h"

int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
   CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);

   CORBA::Object_var tmp = orb->string_to_object("file://test.ior");

   Test::Hello_var hello = Test::Hello::_narrow(tmp.in ());

   CORBA::String_var the_string = hello->get_string ();

    ACE_DEBUG ((LM_DEBUG, "(%P|%t) - string returned <%C>/n",
                  the_string.in ()));

  return 0;
}

其中,对象引用(object reference,)此处是与字符串file://test.ior表示的;

发起的请求(request)为

CORBA::String_var the_string = hello->get_string ();

本例中的调用,采用的是static stub方式,与static stub相对的还有另一种调用方式DII,这里我们暂不讲述。我们可以把

TestC.h,TestC.cpp 文件视为 stub,它是由tao_idl.exe编译hello.idl时生成的文件。

 

 

2.2 target object, server, servant,,orb,poa

 

下面是HelloS.h的片断

class Hello;
typedef Hello *Hello_ptr;

class  Hello
  : public virtual PortableServer::ServantBase
{
protected:
  Hello (void);

public:
  // Useful for template programming.
  typedef ::Test::Hello _stub_type;
  typedef ::Test::Hello_ptr _stub_ptr_type;
  typedef ::Test::Hello_var _stub_var_type;
  Hello (const Hello& rhs);
  virtual ~Hello (void);
  ::Test::Hello *_this (void);
 
  virtual char * get_string (
      void) = 0;
   virtual void shutdown (
      void) = 0;
 
};

其中, get_string()和shutdown()两个成员函数,被定义为纯虚函数(如果对纯虚函数的概念缺少了解,请参考C++相关知识),这也表示,类Hello是一个抽象类,必须由派生类重载这两个函数。这个派生类称为servant。在本例在,servant由Hello.h来定义,由Hello.cpp来实现。

下面是服务端的代码片断

#include "TestS.h"

/// Implement the Test::Hello interface
class Hello
  : public virtual POA_Test::Hello
{
public:
  /// Constructor
  Hello (CORBA::ORB_ptr orb);

  // = The skeleton methods
  virtual char * get_string (void);

  virtual void shutdown (void);

private:
  /// Use an ORB reference to convert strings to objects and shutdown
  /// the application.
  CORBA::ORB_var orb_;
};

 

 

 

这里的文件TestS.h及TestS.cpp,也是由tao_idl.exe编译Hello.idl时生成的文件,这两个文件用于服务端,它就是skeleton。请参考CORBA构架图查看skeleton所在的位置。

 

 

#include "Hello.h"

int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
  try
    {
      CORBA::ORB_var orb =
        CORBA::ORB_init (argc, argv);

     CORBA::Object_var poa_object =
        orb->resolve_initial_references("RootPOA");

      PortableServer::POA_var root_poa =
        PortableServer::POA::_narrow (poa_object.in ());

      if (CORBA::is_nil (root_poa.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Panic: nil RootPOA/n"),
                          1);

      PortableServer::POAManager_var poa_manager = root_poa->the_POAManager ();
      if (parse_args (argc, argv) != 0)
        return 1;
     poa_manager->activate ();

      Hello *hello_impl = 0;
      ACE_NEW_RETURN (hello_impl,
                      Hello (orb.in ()),
                      1);

      PortableServer::ServantBase_var owner_transfer(hello_impl);

      PortableServer::ObjectId_var id =
        root_poa->activate_object (hello_impl);

      CORBA::Object_var object = root_poa->id_to_reference (id.in ());

      Test::Hello_var hello = Test::Hello::_narrow (object.in ());

      CORBA::String_var ior = orb->object_to_string (hello.in ());

      // Output the IOR to the <ior_output_file>
      FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
      if (output_file == 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Cannot open output file for writing IOR: %s/n",
                           ior_output_file),
                           1);
      ACE_OS::fprintf (output_file, "%s", ior.in ());
      ACE_OS::fclose (output_file);

      ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - wait for requsting from client./n"));

      orb->run ();

      ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished/n"));

      root_poa->destroy (1, 1);

      orb->destroy ();
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("Exception caught:");
      return 1;
    }

  return 0;
}

 

 

服务端的执行过程如下 (用颜色来对应代码)

1) 初始化orb

2) 解析poa

3) 获得poa管理器,并激活它。 poa是主动对象(有关主动对象,请参考ACE_Task以及POSA),它相当于在自己的线程在运行。

4) 创建corba对象的servant

5) 用poa激活corba对象

6) 将corba对象的引用字符串化,并保存在磁盘上,供客户端使用。

7) 运行orb的事件循环,等待客户端的连接

8) 结束事件循环后,销毁poa以及orb

orb由 函数CORBA::ORB_init ()通过命令行参数初始化而得,客户端与服务端都需要调用这个函数来初始化orb。orb在corba框架图在的最下面,是它完成了具体的网络连接的建立,以及请求/应答数据包在网络在的传输。

poa对可移植的对象实配器,它是corba对象的管理者,corba 对象的创建,激活、生命期的管理,将客户端的请求分发给servant 来实响应都是由poa来完成的。

3. 结束语

本节我们对corba中的概念作了介绍,特别地我们将概念与代码作了对应。欲更具体地了解CORBA概念请参考《Advanced CORBA Programming with C++》的第二章第4节。

在示例在,客户端需要访问磁盘文件file://test.ior文件来初始化对象,并且此对象的引用是暂时的,每次重启服务端后,test.ior文件就失效了。接下来,我们讲一起学习POA,来开发更强大的服务端,敬请关注。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章