跟我从头学TAO编程系列 (2) -- 编写最简单的TAO应用程序
2008-11-02 20:05
671 查看
跟我从头学TAO编程系列
编写最简单的TAO应用程序
Stone Jiang 2005119@gmail.comhttp://www.ace-tao.org
http://www.ace-tao.org/home/link.php?url=d3d3LmFjZS10YW8ub3JnL2Jicw%3D%3D
如果您对TAO有一定了解,却不知如何驾驭它,那请跟我来从头学学TAO编译。
如果您对TAO还不是非常了解的话,请跟我来,一起对TAO有一个初步的认识。
上一篇我们学会了如何下载、编译和安装TAO,这一节我们来编写最简单的TAO应用程序--Hello TAO!
1. 编写TAO应用程序的一般步骤
1) | 定义接口,编写idl文件 |
2) | 编写mpc文件,生成工程文件 |
3) | 编写server端代码 |
4) | 编写client端代码 |
5) | 编译,调试运行,查看结果 |
2.定义接口
接口(interface)在TAO(corba)编程在非重要,它定义了客户端(主动发起请求一方)向服务端(被动等待请求)之间通讯消息格式。在本例在, 我们定义了一个名为"Hello"的接口。Hello接口中具有两个方法:get_string() 和
shutdown()
TAO用接口定义语言(idl)来表示具体的接口定义。见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 (); }; }; |
我们将接口Hello放在 module Test中,避免全局名字污染。
3.编写mpc文件
在上一篇我们简单的讲解了mpc在编译和ACE,TAO中的应用,在本节我们进一步使用mpc工具,为我们生成项目工程文件。见Hello.mpc
//@file: Hello.mpc //@author: StoneJiang<2005119@gmail.com> // @ref : http://www.ace-tao.org project(*idl): taoidldefaults { idlflags += -Sp IDL_Files { Test.idl } custom_only = 1 } project(*Server): taoserver { after += *idl Source_Files { Hello.cpp server.cpp } Source_Files { TestC.cpp TestS.cpp } IDL_Files { } } project(*Client): taoclient { after += *idl Source_Files { client.cpp } Source_Files { TestC.cpp } IDL_Files { } } |
Hello.mpc定义了三个项目(project),其中*作为占位符,替换文件名Hello,所以这三个项目分别为
1) Hello_idl
2) Hello_Server
3) Hello_Client
4. 服务端代码的编写
服务端由 Server.cpp, Hello.cpp, Hello.h组成// $Id: server.cpp 82798 2008-09-21 10:07:12Z johnnyw $ //@file: server.cpp //@author: StoneJiang<2005119@gmail.com> //@ref : http://www.ace-tao.org #include "Hello.h" #include "ace/Get_Opt.h" #include "ace/OS_NS_stdio.h" const ACE_TCHAR *ior_output_file = ACE_TEXT ("test.ior"); int parse_args (int argc, ACE_TCHAR *argv[]) { ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:")); int c; while ((c = get_opts ()) != -1) switch (c) { case 'o': ior_output_file = get_opts.opt_arg (); break; case '?': default: ACE_ERROR_RETURN ((LM_ERROR, "usage: %s " "-o " "-e shutdown server" "/n", argv [0]), -1); } // Indicates sucessful parsing of the command line return 0; } 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 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; } |
//@file: Hello.cpp //@author: StoneJiang<2005119@gmail.com> // @ref : http://www.ace-tao.org #include "Hello.h" #include "ace/Log_Msg.h" Hello::Hello (CORBA::ORB_ptr orb) : orb_ (CORBA::ORB::_duplicate (orb)) { } char * Hello::get_string (void) { ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - get_string()./n")); return CORBA::string_dup ("Hello TAO!"); } void Hello::shutdown (void) { ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - shutdown()./n")); this->orb_->shutdown (0); } |
//@file: Hello.h //@author: StoneJiang<2005119@gmail.com> //@ref : http://www.ace-tao.org #ifndef HELLO_H #define HELLO_H #include /**/ "ace/pre.h" #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 /**/ "ace/post.h" #endif /* HELLO_H */ |
5. 客户端代码的编写
服务端由 client.cpp组件// @file: client.cpp StoneJiang<2005119@gmail.com> // @ref : http://www.ace-tao.org #include "TestC.h" #include "ace/Get_Opt.h" const ACE_TCHAR *ior = ACE_TEXT ("file://test.ior"); int end = 0; int parse_args (int argc, ACE_TCHAR *argv[]) { ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k: e")); int c; while ((c = get_opts ()) != -1) switch (c) { case 'k': ior = get_opts.opt_arg (); break; case 'e': end = 1; break; case '?': default: ACE_ERROR_RETURN ((LM_ERROR, "usage: %s " "-k " "/n", argv [0]), -1); } // Indicates sucessful parsing of the command line return 0; } int ACE_TMAIN(int argc, ACE_TCHAR *argv[]) { try { CORBA::ORB_var orb = CORBA::ORB_init (argc, argv); if (parse_args (argc, argv) != 0) return 1; CORBA::Object_var tmp = orb->string_to_object(ior); Test::Hello_var hello = Test::Hello::_narrow(tmp.in ()); if (CORBA::is_nil (hello.in ())) { ACE_ERROR_RETURN ((LM_DEBUG, "Nil Test::Hello reference <%s>/n", ior), 1); } CORBA::String_var the_string = hello->get_string (); ACE_DEBUG ((LM_DEBUG, "(%P|%t) - string returned <%C>/n", the_string.in ())); if (end) { hello->shutdown (); } orb->destroy (); } catch (const CORBA::Exception& ex) { ex._tao_print_exception ("Exception caught:"); return 1; } return 0; } |
6. 编译、调试和运行
6.1 生成工程文件
在Dos Shell中输入mwc.pl -type vc9
如下图:
6.2 打开工程文件
用Visual Studio 2008打开工程文档编译后得到 server.exe和client.exe
6.3 运行服务端
6.4 运行客户端
6.5 运行客户端,并让服务端退出
7. 结束
到此,我们最简单的已经运行成功。完整的源代码将上传到http://www.ace-tao.org/home/link.php?url=d3d3LmFjZS10YW8ub3JnL2Jicw%3D%3D
源代码中已为你生成了vc9,vc8,vc71以及linux GNUmakefile文件。
我们将接下来对本例在的代码作解释,敬请关注。
有任何问题,请来邮件 2005119@gmail.com或在bbs (http://www.ace-tao.org/bbs)上留言。
http://www.ace-tao.org/home/link.php?url=d3d3LmFjZS10YW8ub3JnL2Jicw%3D%3D
相关文章推荐
- 从头学TAO编程系列 编写最简单的TAO应用程序
- 跟我从头学TAO编程系列 (2) -- 编写最简单的TAO应用程序
- 从头学TAO编程系列 获取并编译TAO
- 跟我从头学TAO编程系列 -1
- MapReduce 编程 系列三 编写简单reducer
- JMS&MQ系列之简单JMS应用程序编写步骤
- MapReduce 编程 系列二 编写简单mapper
- Socket 由浅入深系列--------- 简单实现编程(三)
- Google Android开发者文档系列-创建有内容分享特性的应用之接收其它应用程序发送的简单数据
- 【自】JavaScript面向对象初探一:通过编写一个简单的Tab选项卡插件,让我们更直观的去认识面向对象的编程方式
- 基于51系列单片机的简单c语言编程
- 我给女朋友讲编程html系列(4) -- html常用简单标签
- Google Android开发者文档系列-创建有内容分享特性的应用之发送简单数据到其它应用程序
- 调用DirectX进行简单的多媒体编程系列(一)
- 调用DirectX进行简单的多媒体编程系列(二)
- 笨鸟先飞学编程系列之二 基础代码的编写3(转)
- 简单编程(十六)编写递归方法返回x和y的最大公约数
- 使用C#和.NET 4编写的并行应用程序“多核并发编程的规则”
- 利用C#编写一个简单的抓网页应用程序
- 【linux草鞋应用编程系列】_4_ 应用程序多线程