跟我从头学TAO编程系列 (2) -- 编写最简单的TAO应用程序

跟我从头学TAO编程系列

编写最简单的TAO应用程序

Stone Jiang [email protected]

http://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
//@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
// @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
//@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 <iorfile> " <br> "-e shutdown server" <br> "\n", <br> argv [0]), <br> -1); <br> } <br> // Indicates sucessful parsing of the command line <br> return 0; <br>} </iorfile>

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><br> FILE *output_file= ACE_OS::fopen (ior_output_file, "w"); <br> if (output_file == 0) <br> ACE_ERROR_RETURN ((LM_ERROR, <br> "Cannot open output file for writing IOR: %s\n", <br> ior_output_file), <br> 1); <br> ACE_OS::fprintf (output_file, "%s", ior.in ()); <br> ACE_OS::fclose (output_file); </ior_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
// @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
//@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
// @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 <ior> " <br> "\n", <br> argv [0]), <br> -1); <br> } <br> // Indicates sucessful parsing of the command line <br> return 0; <br>} </ior>

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 \n",
ior),
1);
}

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

ACE_DEBUG ((LM_DEBUG, "(%P|%t) - string returned \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

如下图:

image

6.2 打开工程文件

用Visual Studio 2008打开工程文档

image

编译后得到 server.exe和client.exe

image

6.3 运行服务端

image

6.4 运行客户端

image

6.5 运行客户端,并让服务端退出

image

7. 结束

到此,我们最简单的已经运行成功。

完整的源代码将上传到http://www.ace-tao.org/home/link.php?url=d3d3LmFjZS10YW8ub3JnL2Jicw%3D%3D

源代码中已为你生成了vc9,vc8,vc71以及linux GNUmakefile文件。

我们将接下来对本例在的代码作解释,敬请关注。

有任何问题,请来邮件 [email protected]或在bbs (http://www.ace-tao.org/bbs)上留言。

http://www.ace-tao.org/home/link.php?url=d3d3LmFjZS10YW8ub3JnL2Jicw%3D%3D

你可能感兴趣的:(编程,c,PHP,OS,Gmail)