跟我从头学TAO编程系列(3) -- 通过实例学习CORBA核心概念
2010-10-08 17:27
585 查看
跟我从头学TAO编程系列
通过实例学习CORBA核心概
Stone Jiang 2005119@gmail.comhttp://www.ace-tao.org
本系列的第一篇我们学会了怎么下载和编译ACE,TAO,第二篇我们编写了最简单的TAO应用程序。学习CORBA,TAO与学习其它学科一样,一定要掌握其核心概念。这了节,我们通过第二篇的实例来学习CORBA的核心概念。
1.CORBA架构
CORBA的架构如下图:2. CORBA核心概念
2.1 CORBA Object
CORBA对象是一个“虚拟”的实体。它可以被ORB定位和被客户端调用。在上一篇的示例中,interface Hello即为corba对象, 定义在Hello.idl中
//@file: Test.idl //@author: StoneJiang<2005119@gmail.com> //@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 (); }; }; |
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; } |
发起的请求(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; }; |
下面是服务端的代码片断
#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_; }; |
#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,来开发更强大的服务端,敬请关注。
相关文章推荐
- 跟我从头学TAO编程系列(3) -- 通过实例学习CORBA核心概念
- 跟我从头学TAO编程系列(3) -- 通过实例学习CORBA核心概念
- NIO学习系列:核心概念及基本读写
- 一步一个脚印学习WCF系列之WCF概要—WCF服务的创建与调用HelloWorld实例,通过配置文件方式(六)
- freeswitch系列四 通过实例学习sip协议的注册、呼叫、挂断流程
- 一步一个脚印学习WCF系列之WCF概要—WCF服务的创建与调用HelloWorld实例,通过编码方式(四)
- NIO学习系列:核心概念及基本读写
- NIO学习系列:核心概念及基本读写
- NIO学习系列:核心概念及基本读写
- Elasticsearch简单使用系列--详细介绍ES的核心概念
- Maven学习总结(4)——Maven核心概念
- 实例学习ansible系列(9)常用模块之yum/service
- pthread系列函数实例学习(一)
- ElasticSearch系列04:核心概念
- 通过CTS学习Android API系列2——Bluetooth分析
- OAF学习 理解--OAF_OAF架构MVC系列 - Control的概述(概念)
- http协议学习和总结系列——基础概念篇
- Ember学习(1):Ember核心概念
- C#学习基本概念之构造函数之二(实例构造函数)
- 【Python学习系列四】Python程序通过hadoop-streaming提交到Hadoop集群执行MapReduce