Advanced CORBA Programming with omniORB (二)对象引用获取之:Stringify

 可互操作对象引用主要提供了三种方式:Stringify/NamingService/TCP.下面我们通过一个实例来介绍如何通过字符串方式获取对象引用:

 

// EchoImpl.h: interface for the EchoImpl class.
//
//

#if !defined(AFX_ECHOIMPL_H__7F65277A_10AA_4D67_908A_24D76CB4584F__INCLUDED_)
#define AFX_ECHOIMPL_H__7F65277A_10AA_4D67_908A_24D76CB4584F__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "Echo.h"
class EchoImpl:  public POA_Echo,public PortableServer::RefCountServantBase
{
public:
 EchoImpl();
 virtual ~EchoImpl();
 // methods corresponding to defined IDL attributes and operations
 virtual char* echoString(const char* mesg);
 virtual long  echoInt();
 virtual short echShort();

};

#endif // !defined(AFX_ECHOIMPL_H__7F65277A_10AA_4D67_908A_24D76CB4584F__INCLUDED_)

 

// EchoImpl.cpp: implementation of the EchoImpl class.
//
//

#include "stdafx.h"
#include "EchoImpl.h"
#include <iostream>
using namespace std;
//
// Construction/Destruction
//

EchoImpl::EchoImpl()
{

}

EchoImpl::~EchoImpl()
{

}


//   Methods corresponding to IDL attributes and operations
char* EchoImpl::echoString(const char* mesg)
{
 cerr<<"Recv:"<<mesg<<endl;
 return  CORBA::string_dup(mesg);
}

long EchoImpl::echoInt()
{
 cerr<<"method:echoInt."<<endl;
 return 100000;
}
short EchoImpl::echShort()
{
 cerr<<"metod:echoShort."<<endl;
 return 100;
}

 

下面是主控程序代码:

 

#include <iostream>
#include "EchoImpl.h"
#include <fstream>
using namespace std;

 

int main(int argc, char** argv)
{
  try
  {
    // Initialise the ORB.

    CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

    // Obtain a reference to the root POA.

    CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
    PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);

    

 // We allocate the objects on the heap.  Since these are reference
    // counted objects, they will be deleted by the POA when they are no
    // longer needed.
    EchoImpl* myEcho_i = new EchoImpl();
 
 

    // Activate the objects.  This tells the POA that the objects are
    // ready to accept requests.
    PortableServer::ObjectId_var myEcho_iid = poa->activate_object(myEcho_i);


    // Obtain a reference to each object and output the stringified
    // IOR to stdout
    {
      // IDL interface: Echo
      CORBA::Object_var ref = myEcho_i->_this();
      CORBA::String_var sior(orb->object_to_string(ref));
      cout << "IDL object Echo IOR = '" << (char*)sior << "'" << endl;

   ofstream file("D://ior.ior");
   if (file.is_open())
   {
    file.write((char*)sior,strlen((char*)sior));
    file.close();
   }

    }
   myEcho_i->_remove_ref();
    // Obtain a POAManager, and tell the POA to start accepting
    // requests on its objects.
    PortableServer::POAManager_var pman = poa->the_POAManager();
    pman->activate();

    orb->run();


    orb->destroy();
  }
  catch(CORBA::SystemException&)
  {
    cerr << "Caught CORBA::SystemException." << endl;
  }
  catch(CORBA::Exception&)
  {
    cerr << "Caught CORBA::Exception." << endl;
  }
  catch(omniORB::fatalException& fe)
  {
    cerr << "Caught omniORB::fatalException:" << endl;
    cerr << "  file: " << fe.file() << endl;
    cerr << "  line: " << fe.line() << endl;
    cerr << "  mesg: " << fe.errmsg() << endl;
  }
  catch(...)
  {
    cerr << "Caught unknown exception." << endl;
  }

  return 0;
}

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值