TAO 中间件2
来源:互联网 发布:社交软件的英文单词 编辑:程序博客网 时间:2024/06/06 16:39
跟我从头学TAO编程系列
编写最简单的TAO应用程序
Stone Jiang 2005119@gmail.com
http://www.ace-tao.org
http://www.ace-tao.org/home/link.php?url=d3d3LmFjZS10YW8ub3JnL2Jicw%3D%3D
如果您对TAO有一定了解,却不知如何驾驭它,那请跟我来从头学学TAO编译。
如果您对TAO还不是非常了解的话,请跟我来,一起对TAO有一个初步的认识。
上一篇我们学会了如何下载、编译和安装TAO,这一节我们来编写最简单的TAO应用程序--HelloTAO!
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 {
Hello.idl
}
custom_only = 1
}
project(*Server): taoserver {
after += *idl
Source_Files {
Hello.cpp
server.cpp
}
Source_Files {
HelloC.cpp
HelloS.cpp
}
IDL_Files {
}
}
project(*Client): taoclient {
after += *idl
Source_Files {
client.cpp
}
Source_Files {
HelloC.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 "HelloS.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。(要运行mwc.pl,当然得下载安装ActivePerl在此不作详述)
如下图:
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文件。
我们将接下来对本例在的代码作解释,敬请关注。
- TAO 中间件2
- TAO 中间件
- tao
- TAO
- 中间件2
- 跟我从头学TAO编程系列 (2) -- 编写最简单的TAO应用程序
- 跟我从头学TAO编程系列 (2) -- 编写最简单的TAO应用程序
- 关于中间件2
- express-2-中间件
- 中间件
- 中间件
- 中间件?
- 中间件
- 中间件
- 中间件
- 中间件
- 中间件
- 中间件
- 影响项目进度的几大因素
- 开学心得
- C#设计模式之6——原型模式
- c++中const的作用
- CSS核心内容
- TAO 中间件2
- 验证用户名的格式
- 链表的创建和遍历算法
- java日期获取前一天和后一天
- 对纹理个参数的分析
- 网络防火墙开发二三事
- Netcat使用方法
- 结构体对齐
- Android Handle的使用(转)