corba应用示例

服务器:
启动omninames服务

客户端
1.安装omni客户端omniORB-4.2.0.tar.gz,启动omni服务;
2.编写代码
示例代码如下:
CCorbaService.h文件
#ifndef __CORBA_SERVICE_H_
#define __CORBA_SERVICE_H_

#include <pthread.h>
#include <string>
#include "utilfunc.h"
#include "CorbabusImp.h"

using namespace std;
class CCorbaService
{
public:
 CCorbaService(const string& strCorbaName);
    virtual ~CCorbaService();
    int start();
private:
    CCorbaService();
    static void* initCorbaService(void* arg);
private:
    string m_strCorbaName;
};
#endif

//

CCorbaService.cpp文件
#include "CCorbaService.h"
CCorbaService::CCorbaService()
{
}
CCorbaService::CCorbaService(const string& strCorbaName)
{
    m_strCorbaName = strCorbaName;
}

CCorbaService::~CCorbaService()
{

}

int CCorbaService::start()
{
    pthread_t m_tcorba;
    if (pthread_create(&m_tcorba, NULL, initCorbaService, this) != 0)
    {
        cout << "create corbaservice start  thread failed" << endl;
        return -1;
    }
    return 0;
}

void*  CCorbaService::initCorbaService(void* arg)
{
 CCorbaService *_CorbaService = (CCorbaService *)arg;
 // 创建服务器对象
 CorbabusImp _corbabus;

 int argc = 0;
 char **argv = NULL;

 // Initialize the ORB.
 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

 try
 {
  vector<string> vCorbaName;
  InterceptString(_CorbaService->m_strCorbaName, "/", vCorbaName);
  int iSize = vCorbaName.size();
  if (iSize < 2)
  {
   cerr << "m_strCorbaName : " << _CorbaService->m_strCorbaName << "   error!" << endl;
   return NULL;
  }
  string strPoaName = vCorbaName[0].substr(0, vCorbaName[0].find_first_of('.'));
  string strName = vCorbaName[1].substr(0, vCorbaName[1].find_first_of('.'));

  cout << "POA:" << strPoaName << endl;
  cout << "NAME:" << strName << endl;


  // 创建服务器对象完成实时对象的配置
  CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
  PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(obj);

  CORBA::PolicyList policies;
  policies.length(1);
  policies[(CORBA::ULong)0] = rootPOA->create_lifespan_policy(PortableServer::PERSISTENT);

  // get the POA Manager
  PortableServer::POAManager_var poa_manager = rootPOA->the_POAManager();
  PortableServer::POA_var myPOA = rootPOA->create_POA(strPoaName.c_str(), poa_manager, policies);

  PortableServer::ObjectId_var observerId = myPOA->activate_object(&_corbabus);
  CORBA::Object_var objref = myPOA->id_to_reference(observerId);

  // 激活Poa的管理者
  poa_manager->activate();
  CosNaming::NamingContext_var rootContext, myContext;
  //得到命名服务的对象参考
  try
  {
   // Obtain a reference to the root context of the Name service:
   CORBA::Object_var obj = orb->resolve_initial_references("NameService");

   // Narrow the reference returned.
   rootContext = CosNaming::NamingContext::_narrow(obj);
   if (CORBA::is_nil(rootContext))
   {
    cerr << "Failed to narrow the root naming context." << endl;
    rootContext = NULL;
    return NULL;
   }

   // Bind the context to root.
   CosNaming::Name   Name;
   Name.length(1);
   Name[0].id = strPoaName.c_str();
   Name[0].kind = (const char*) "PoaName";

   try
   {
    myContext = rootContext->bind_new_context(Name);
   }
   catch (CosNaming::NamingContext::AlreadyBound& ex)
   {

    myContext = CosNaming::NamingContext::_narrow(rootContext->resolve(Name));
    cout << "myContext is Exist  " << myContext << endl;
    if (CORBA::is_nil(myContext))
    {
     cerr << "Failed to narrow naming context." << endl;
     myContext = NULL;
     return NULL;
    }
   }
  }
  catch (CORBA::ORB::InvalidName& ex)
  {
   // This should not happen!
   cerr << "Service required is invalid [does not exist]." << endl;
   return NULL;
  }

  //将Corba服务对象注册到命名服务上
  try
  {
   // Bind a context called "test" to the root context:
   CosNaming::Name   Name;
   Name.length(1);
   Name[0].id = strName.c_str();
   Name[0].kind = (const char*)"ObjectName";
   try
   {
    myContext->bind(Name, objref);
   }
   catch (CosNaming::NamingContext::AlreadyBound& ex)
   {
    cout << "Service Already Bound to root context,rebind." << endl;
    myContext->rebind(Name, objref);
    cout << "rebind success!" << endl;
   }
  }
  catch (CORBA::COMM_FAILURE& ex)
  {
   cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
    << "naming service." << endl;
   return NULL;
  }
  catch (CORBA::SystemException&)
  {
   cerr << "Caught a CORBA::SystemException while using the naming service\n";
   return NULL;
  }
  cout << "CCorbaService orb run" << endl;
  orb->run();
  cout << "orb run over" << endl;
 }
 catch (const CORBA::Exception& e)
 {
  cerr << "Init Poa And Servant Service Fail" << endl;
  return NULL;
 }
 cout << "Init corba success" << endl;
 return NULL;
}


#ifndef _CORBABUSIMP_H_
#define _CORBABUSIMP_H_

#ifdef _TCMALLOC
#include "google/malloc_extension.h"
#endif

#ifdef VSCORBA
#include "corbabus_s.hh"
#endif
#ifdef OMNICORBA
#include "corbabus.hh"
#endif

#include <iostream>
#include <string>
using namespace std;
class CorbabusImp : public virtual POA_com::linkage::system::corbabus::CorbaBusAdpater,
public virtual PortableServer::RefCountServantBase
{
 
private:
 
public:
 CorbabusImp ();
 ~CorbabusImp ();
 char * regListener(const com::linkage::system::corbabus::stringArray &topics, const char *str, com::linkage::system::corbabus::MsgListener_ptr listener);
 void sendMsg(const char *topic, const com::linkage::system::corbabus::Node &msg);
 com::linkage::system::corbabus::Node* sendAndRecvMsg(const char *topic ,const com::linkage::system::corbabus::Node &msg);
};



#include "CorbabusImp.h"
#include "CCorbaService.h"
using namespace std;

CorbabusImp::CorbabusImp()
{

}

CorbabusImp::~CorbabusImp()
{

}

char * CorbabusImp:: regListener(const com::linkage::system::corbabus::stringArray &topics, const char *str, com::linkage::system::corbabus::MsgListener_ptr listener)
{
    CORBA::String_var aa = t_cmq.m_Orb->object_to_string(listener);
    cout <<"reg listener:" << str << " listen ior:" << aa << endl;
    g_pLog->WriteLog(level_Info, "reg lister listen name: %s", str);
    list<string> lstTopic;
    for (int i = 0; i < topics.length(); i++)
    {
        cout << "title: " << topics[i].in() << endl;
        lstTopic.push_back(topics[i].in());
    }
    com::linkage::system::corbabus::MsgListener_var listenerVar = com::linkage::system::corbabus::MsgListener::_narrow(listener);
    bool bRet = g_objDataOper.AddListener(str, listenerVar, lstTopic);
    if (bRet)
    {
        g_pLog->WriteLog(level_Info, "reg lister name: %s ok",  str);
    }
 
    char* c = new char[200];
    memset(c, 200, 0);
    strcpy(c, str);
    strcpy(c + strlen(str), " register OK");
    return c;
}

void CorbabusImp::sendMsg(const char *topic, const com::linkage::system::corbabus::Node &msg)
{
    string strTopic = topic;
    m_iHeartTime = time(NULL);
    if ( strTopic == "/aa/bb/cc" )
    {
 /
    }
}

com::linkage::system::corbabus::Node* CorbabusImp::sendAndRecvMsg(const char* topic ,const com::linkage::system::corbabus::Node &msg)
{
    return NULL;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值