ACE+TAO 开源编程笔记/创建服务器
外观
本节描述如何创建一个类似于 TAO 示例中 quoter 示例(ACE_wrappers/TAO/examples/Quoter)的简单服务器。
要使此功能正常工作,请创建 IDL 文件并在该文件上运行 tao_idl 可执行文件。命令行看起来像这样
tao_idl -GI basic.idl
这会生成许多源文件,其中只有一两个需要编辑。
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <cstdlib>
#include "basicI.h"
#include "basicC.h"
#include "ace/streams.h"
using namespace std;
using namespace CORBA;
int main (int argc, char* argv[])
{
// First initialize the ORB, that will remove some arguments...
ORB_var orb = ORB_init(argc, argv, "ORB" /* the ORB name, it can be anything! */);
Object_var poa_object = orb->resolve_initial_references("RootPOA");
PortableServer::POA_var poa = PortableServer::POA::_narrow(poa_object.in());
PortableServer::POAManager_var poa_manager = poa->the_POAManager();
poa_manager->activate();
// Create the servant
My_Factory_i my_factory_i;
// Activate it to obtain the object reference
My_Factory_var my_factory = my_factory_i._this();
// Put the object reference as an IOR string
String_var ior = orb->object_to_string (my_factory.in());
// Print it out!
//Note: You will be using this reference in the client to get it to connect to this server
//Don't even bother copying it down, it changes with each invocation & is as long as your arm
cout << ior.in() << endl;
//By default, the following doesn't return unless there is an error
orb->run();
// Destroy the POA, waiting until the destruction terminates
poa->destroy(1, 1);
orb->destroy();
return 0;
}
针对以下源代码运行 tao_idl 程序,会生成创建简单服务器所需的所有文件。实际命令行应该是 'tao_idl -GI basic.idl' 命令行选项 '-GI' 用于生成下面列出的与服务器相关的接口文件。您只需实现此文件(即填写一些空成员函数),然后就可以开始运行了。
// Forward declare
interface Widget;
interface My_Factory
{
// = TITLE
// A factory class for the widget interfaces
//
// = DESCRIPTION
// Return the Quoter interfaces based on their names
//
Widget get_widget (in string widget_name);
};
interface Widget
{
// = TITLE
// A simple interface to query the name and price of a widget
//
// = DESCRIPTION
// Return the price and name of a single widget
//
readonly attribute string full_name;
// Get the name.
double price ();
// Get the price
};
不要忘记实现服务器的接口(basicI.cpp)。我通常将此作为对其他类的一个简单调用,其中包含所有有趣的代码。
#include "basicI.h"
// Implementation skeleton constructor
My_Factory_i::My_Factory_i (void)
{
}
// Implementation skeleton destructor
My_Factory_i::~My_Factory_i (void)
{
}
::Widget_ptr My_Factory_i::get_widget (
const char * widget_name
)
ACE_THROW_SPEC ((
::CORBA::SystemException
))
{
// Add your implementation here
}
// Implementation skeleton constructor
Widget_i::Widget_i (void)
{
}
// Implementation skeleton destructor
Widget_i::~Widget_i (void)
{
}
char * Widget_i::full_name (
)
ACE_THROW_SPEC ((
::CORBA::SystemException
))
{
return "Some widget name";
}
::CORBA::Double Widget_i::price (
)
ACE_THROW_SPEC ((
::CORBA::SystemException
))
{
return 1.25;
}