您的位置:首页 > 移动开发 > Objective-C

CORBA Programming with TAO - 5.Hello World(简单实例)

2007-08-16 23:19 387 查看
[align=center]CORBA Programming with TAO - 5.Hello World(简单实例)[/align]
摘要:
前面对CORBA编程的基础知识进行了简单介绍,本篇中,我们将实例讲解如何利用TAO中进行CORBA编程。
一、编写IDL
1、建一个空的Solution,然后在该Solution目录下新建一个名为idl的Win32 Static Library工程(对于一般的TAO工程,无需单独为idl建一个Project,但由于我们要重用该idl,所以单独为其建立一个工程比较便于管理)。
然后新建一个文本文件,添加如下内容:
 
#ifndef __ECHO_IDL__
#define __ECHO_IDL__
 
interface Echo {
 string echoString(in string mesg);
};
 
#endif // __ECHO_IDL__
 
将该文件命名为echo.idl,保存在idl工程目录下,并将其添加到idl工程中。
2、在echo.idl上点击右键,选择Properties,将其编译工具由默认的midl改为Custom Build Tool,选择Apply,在Custom Build Step标签页Command栏内填入:
 
tao_idl -Sc $(InputFileName)
 
Outputs栏填入:
 
echoC.h
echoC.cpp
echoC.inl
echoS.h
echoS.cpp
echoS.inl
 
点击OK,保存设置,在echo.idl上点击Compile编译该idl文件,编译后,将在idl工程目录下产生上述各文件。
将echoC.h/echoC.cpp/echoS.h/echoS.cpp添加到idl工程中,在idl工程上点击右键,选择Properties,在Librarian下添加如下Additional Dependencies:
aced.lib
taod.lib
TAO_AnyTypeCoded.lib
按F7编译上述工程。
二、直接通过IOR访问Servant
新建一个eg1_svr工程,添加到当前Solution,在该工程中添加一个名为eg1_svr.cpp的文件,其内容如下:
 
#include <iostream>
#include "../idl/echoS.h"
using namespace std;
 
class Echo_i : public POA_Echo,
public PortableServer::RefCountServantBase
{
public:
      inline Echo_i() {}
      virtual ~Echo_i() {}
      virtual char* echoString(const char* mesg);
};
 
char* Echo_i::echoString(const char* mesg)
{
      cerr << "Upcall " << mesg << endl;
      return CORBA::string_dup(mesg);
}
 
//////////////////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
      try {
            // initialize ORB
            CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
            // find RootPOA
            CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
            PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
            // create servant object and activate it
            Echo_i* myecho = new Echo_i();
            PortableServer::ObjectId_var myechoid = poa->activate_object(myecho);
            // Obtain a reference to the object, and print it out as a
            // stringified IOR.
            obj = myecho->_this();
            CORBA::String_var sior(orb->object_to_string(obj));
            cerr << (char*)sior << endl;
            myecho->_remove_ref();
            // find and activate POAManager
            PortableServer::POAManager_var pman = poa->the_POAManager();
            pman->activate();
            orb->run();
      }
      catch(CORBA::SystemException&) {
            cerr << "Caught CORBA::SystemException." << endl;
      }
      catch(CORBA::Exception&) {
            cerr << "Caught CORBA::Exception." << endl;
      }
      catch(...) {
            cerr << "Caught unknown exception." << endl;
      }
      return 0;
}
 
使该工程依赖于前面的工程idl。由于这是一个Server程序,需要链接TAO_PortableServerd.lib。
按F7,编译该工程。
 
新建一个eg1_cli工程,添加到当前Solution,在该工程中添加一个名为eg1_cli.cpp的文件,其内容如下:
 
#include <iostream>
#include "../idl/echoC.h"
using namespace std;
 
static void hello(Echo_ptr e)
{
      CORBA::String_var src = (const char*) "Hello!";
      CORBA::String_var dest = e->echoString(src);
      cerr << "I said, /"" << (char*)src << "/"." << endl
            << "The Echo object replied, /"" << (char*)dest <<"/"." << endl;
}
 
//////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[]) {
      try {
            // initialize orb
            CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
            // check arguments
            if (argc != 2) {
                  cerr << "Usage: eg1_cli <object reference>" << endl;
                  throw 1;
            }
            // Obtain reference from servant IOR
            CORBA::Object_var obj = orb->string_to_object(argv[1]);
            Echo_var echoref = Echo::_narrow(obj);
            if( CORBA::is_nil(echoref) ) {
     
4000
             cerr << "Can't narrow reference to type Echo (or it was nil)." << endl;
                  return 1;
            }
            for (CORBA::ULong count=0; count<10; count++)
                  // communicate with servant
                  hello(echoref);
            orb->destroy();
      }
      catch(CORBA::COMM_FAILURE&) {
            cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
                  << "object." << endl;
      }
      catch(CORBA::SystemException&) {
            cerr << "Caught a CORBA::SystemException." << endl;
      }
      catch(CORBA::Exception&) {
            cerr << "Caught CORBA::Exception." << endl;
      }
      catch(...) {
            cerr << "Caught unknown exception." << endl;
      }
 
      return 0;
}
 
使该工程依赖于前面的工程idl,编译该工程。
 
代码比较简单,请自行参照注释阅读源代码。
三、运行
运行时应先启动eg1_svr,该程序会在标准输出上输出一长串字符,即servant的IOR信息,我们有了该信息就可以通过orb->string_to_object(...)连接到相应的servant了。
现在可以启动客户程序eg1_cli了,启动eg1_cli时应将上面获得的IOR串作为参数传递给该程序(注意,从Console上拷贝下来的时候会折行,将其拷贝到文本编辑器中去掉折行再拷出来即可)。
 
附:本文示例源代码
参考:
1.      Duncan Grisby, Sai-Lai Lo, David Riddoch. The omniORB Version4.0 User's Guide. http://omniorb.sourceforge.net/omni40/omniORB.pdf

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=855880
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息