首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在解析名称上下文后,CORBA omniORB无法获取远程对象

在解析名称上下文后,CORBA omniORB无法获取远程对象
EN

Stack Overflow用户
提问于 2016-03-30 10:59:37
回答 1查看 1.6K关注 0票数 1

我有如下所示的IDL

代码语言:javascript
复制
module IClientServer {

  interface IClient 
  {
    void serverResponse(in string name, in string data);

    void start();

    void stop();

    void shutdown();

  };

  interface IServer 
  {

     // Server calls back to client just once in a
     // recursive call before returning.
     // void one_time(in CallBack cb, in string mesg);
    void DataFromX(in string name,in string data,in long lbytes,in short usg);

    void Authenticate(in IClient client, in string dataToNegotiate);

     // Shuts down the server.
    void shutdown();

  };
};

为此,我使用idl2cpp实用工具(onmiORB)生成代理和框架,并按照文档中的建议将生成的文件链接到服务器和客户端应用程序。

然后,我启动了名称服务(omniNames),并按照文档中的建议添加了注册表项omniORB\InitRef,以便不使用命令行参数连接服务器和客户端应用程序。

下面的是服务器代码

代码语言:javascript
复制
int _tmain(int argc, _TCHAR* argv[])
{
    try 
    {
    int argc = 0;
        _BRDTRACE("Initializing....\n");
        CORBA::ORB_var orb = CORBA::ORB_init(argc, NULL);
//          cerr << "Initialized." << endl;

        CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
        _BRDTRACE("Resolved.\n");


        PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
        _BRDTRACE("Narrowed..\n");

            // Obtain a reference to the object, and register it in
            // the naming service.
        server_i* myserver = new server_i();
//          cerr << "Constructed." << endl;

        obj = myserver->_this();
        _BRDTRACE("obj retrieved.\n");

        CORBA::String_var x;
        x = orb->object_to_string(obj);
        _BRDTRACE("obj to string.\n");

        if( !bindObjectToName(orb, obj) )
        {
//            cerr << "Failed to bind obj to name." << endl;
          throw;
        }

        _BRDTRACE("binded\n");
        myserver->_remove_ref();
//          cerr << "removed ref." << endl;

        PortableServer::POAManager_var pman = poa->the_POAManager();
        pman->activate();
        _BRDTRACE("activated.\n");

//          cerr << "Executing..." << endl;
        orb->run();
        _BRDTRACE("Terminated.\n");

        myserver->shutdown();
//          cerr << "Shutdown." << endl;

    }
    catch(CORBA::SystemException&) 
    {
        _BRDTRACE("Caught CORBA::SystemException.\n");
    }
    catch(CORBA::Exception&) {
        _BRDTRACE("Caught CORBA::Exception.\n");
    }
    catch(omniORB::fatalException&) 
    {
        _BRDTRACE("Caught omniORB::fatalException:\n");
//          cerr << "  file: " << fe.file() << endl;
//          cerr << "  line: " << fe.line() << endl;
//          cerr << "  mesg: " << fe.errmsg() << endl;
    }
    catch(...) {
        _BRDTRACE("Caught unknown exception.\n");
    }

}

static CORBA::Boolean
bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref)
{
  CosNaming::NamingContext_var rootContext;

  try {
    // Obtain a reference to the root context of the Name service:
    CORBA::Object_var obj;
    obj = orb->resolve_initial_references("NameService");

    // Narrow the reference returned.
    rootContext = CosNaming::NamingContext::_narrow(obj);
    if( CORBA::is_nil(rootContext) ) {
      _BRDTRACE("Failed to narrow the root naming context.\n");
      return 0;
    }
  }
  catch(CORBA::ORB::InvalidName& ex) {
    // This should not happen!
    _BRDTRACE("Service required is invalid [does not exist].\n");
    return 0;
  }

  try {
    // Bind a context called "test" to the root context:

    CosNaming::Name contextName;
    contextName.length(1);
    contextName[0].id   = (const char*) "birdseye";       // string copied
    contextName[0].kind = (const char*) "collections_context"; // string copied
    // Note on kind: The kind field is used to indicate the type
    // of the object. This is to avoid conventions such as that used
    // by files (name.type -- e.g. test.ps = postscript etc.)

    //CosNaming::NamingContext_var testContext;
    try {
      // Bind the context to root.
      rootContext->bind(contextName, objref);
    }
    catch(CosNaming::NamingContext::AlreadyBound& ex) {
      // If the context already exists, this exception will be raised.
      // In this case, just resolve the name and assign testContext
      // to the object returned:
      CORBA::Object_var obj;
      obj = rootContext->resolve(contextName);
      CosNaming::NamingContext_var testContext = CosNaming::NamingContext::_narrow(obj);
      if( CORBA::is_nil(testContext) ) {
        _BRDTRACE("Failed to narrow naming context.\n");
        return 0;
      }
    }
  } catch(CORBA::COMM_FAILURE& ex) {
   _BRDTRACE("Caught system exception COMM_FAILURE -- unable to contact the naming service.\n");
    return 0;
  }
  catch(CORBA::SystemException&) {
    _BRDTRACE("Caught a CORBA::SystemException while using the naming service.\n");
    return 0;
  }

  return 1;
}

,但是客户端下面的代码在名称上下文解析后返回零对象。无法解决这个问题。请帮忙!

代码语言:javascript
复制
int _tmain(int argc, _TCHAR* argv[])
{
    CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
    CORBA::Object_var objRef = orb->resolve_initial_references("NameService");
    CORBA::Object_var obj = getObjectReference(orb);
    IClientServer::IServer_var svr = IClientServer::IServer::_narrow(obj.in());
  if( CORBA::is_nil(svr) ) {    **//THIS IS WHERE THE ISSUE IS**
 //   _BRDTRACE("cb_client: The server reference is nil!\n");
    return 0;
  }
    return 0;
}

static CORBA::Object_ptr
getObjectReference(CORBA::ORB_ptr orb)
{
  CosNaming::NamingContext_var rootContext;

  try {
    // Obtain a reference to the root context of the Name service:
    CORBA::Object_var obj;
    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;
      return CORBA::Object::_nil();
    }
  }
  catch(CORBA::ORB::InvalidName& ) {
    // This should not happen!
    return CORBA::Object::_nil();
  }
  // Create a name object, containing the name test/context:
  CosNaming::Name name;
  name.length(1);

  name[0].id   = (const char*) "birdseye";       // string copied
  name[0].kind = (const char*) "collections_context"; // string copied
  // Note on kind: The kind field is used to indicate the type
  // of the object. This is to avoid conventions such as that used
  // by files (name.type -- e.g. test.ps = postscript etc.)


  try {
    // Resolve the name to an object reference.
    return rootContext->resolve(name);
  }
  catch(CosNaming::NamingContext::NotFound& nf) {
  }
  catch(CORBA::COMM_FAILURE& ) {
  }
  catch(CORBA::SystemException&) {
  }

  return CORBA::Object::_nil();
}

更新-5pm:实际上服务器端代码也有相同的问题服务器->身份验证从来没有被调用,因为没有引用。

猜测:使用idl2cpp工具生成的代理和存根会出现问题吗?

UPDATE-7:30PM存根上的歧义

更新3-31\11上午11我使用的是omniORB 4.0.3,这是超过10年的历史。这在用VC6编译的早期windows OS版本中非常有效,我怀疑在VS 2008上重新编译时是否存在问题。只是考虑一下升级到去年发布的ommiORB 4.2。什么都不知道..。

更新3-31\5:30下午5:30目前正在构建全方位的ORB4.2.1源代码。虽然我这样做了,但我仍然想知道是否存在连接在旧系统中生成的.lib文件的问题。在这种情况下,我在Windows7中使用的omniORB .lib文件是构建在Windows上的,这会是一个问题吗?即使是this帖子也无法回答,我有一个旧的.lib,它编译和链接良好,没有任何问题&甚至运行时也没有崩溃。

更新4-01\4:30下午4:30实际上我注意到没有服务器运行,我之前发布的服务器代码也是客户端,我现在更新了真正的服务器代码(绑定名称到服务器obj的代码)。但是,即使在这次调整之后,问题仍然没有改变。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-01 11:36:48

最后我解决了这个问题。有两个问题与代码问题1我已经更新了问题(缺少服务器代码)问题2是与名称上下文绑定。

下面的是服务器中bindObjectToName的修改版本

代码语言:javascript
复制
static CORBA::Boolean
bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref)
{
  CosNaming::NamingContext_var rootContext;

  try {
    // Obtain a reference to the root context of the Name service:
    CORBA::Object_var obj;
    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;
      return 0;
    }
  }
  catch(CORBA::ORB::InvalidName& ex) {
    // This should not happen!
  //  cerr << "Service required is invalid [does not exist]." << endl;
    return 0;
  }

  try {
    // Bind a context called "test" to the root context:

    CosNaming::Name contextName;
    contextName.length(1);
    contextName[0].id   = (const char*) "birdsEYE";       // string copied
    contextName[0].kind = (const char*) "DataCollectionS"; // string copied
    // Note on kind: The kind field is used to indicate the type
    // of the object. This is to avoid conventions such as that used
    // by files (name.type -- e.g. test.ps = postscript etc.)

    CosNaming::NamingContext_var testContext;
    try {
      // Bind the context to root.
      testContext = rootContext->bind_new_context(contextName);
    }
    catch(CosNaming::NamingContext::AlreadyBound& ex) {
      // If the context already exists, this exception will be raised.
      // In this case, just resolve the name and assign testContext
      // to the object returned:
      CORBA::Object_var obj;
      obj = rootContext->resolve(contextName);
      testContext = CosNaming::NamingContext::_narrow(obj);
      if( CORBA::is_nil(testContext) ) {
    //    cerr << "Failed to narrow naming context." << endl;
        return 0;
      }
    }

    // Bind sender :
    CosNaming::Name objectName;
    objectName.length(1);
    objectName[0].id   = (const char*) "clienT";   // string copied
    objectName[0].kind = (const char*) "sendeR"; // string copied

    try {
      testContext->bind(objectName, objref);
    }
    catch(CosNaming::NamingContext::AlreadyBound& ex) {
      testContext->rebind(objectName, objref);
    }
    // Note: Using rebind() will overwrite any Object previously bound
    //       to /test/Echo with obj.
    //       Alternatively, bind() can be used, which will raise a
    //       CosNaming::NamingContext::AlreadyBound exception if the name
    //       supplied is already bound to an object.

    // Bind receiver :
    CosNaming::Name objectName2;
    objectName2.length(1);
    objectName2[0].id   = (const char*) "clienT";   // string copied
    objectName2[0].kind = (const char*) "receiveR"; // string copied

    try {
      testContext->bind(objectName2, objref);
    }
    catch(CosNaming::NamingContext::AlreadyBound& ex) {
      testContext->rebind(objectName2, objref);
    }


    // Amendment: When using OrbixNames, it is necessary to first try bind
    // and then rebind, as rebind on it's own will throw a NotFoundexception if
    // the Name has not already been bound. [This is incorrect behaviour -
    // it should just bind].

  }
  catch(CORBA::COMM_FAILURE& ex) {
    //cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
    //     << "naming service." << endl;
    return 0;
  }
  catch(CORBA::SystemException&) {
    //cerr << "Caught a CORBA::SystemException while using the naming service."
    // << endl;
    return 0;
  }

  return 1;
}

和客户端代码中修改的getObjectReference以下的静态CORBA::Object_ptr

代码语言:javascript
复制
getObjectReference(CORBA::ORB_ptr orb)
{
  CosNaming::NamingContext_var rootContext;

  try {
    // Obtain a reference to the root context of the Name service:
    CORBA::Object_var obj;
    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;
      return CORBA::Object::_nil();
    }
  }
  catch(CORBA::ORB::InvalidName& ex) {
    // This should not happen!
  //  cerr << "Service required is invalid [does not exist]." << endl;
    _BRDTRACE("getObjRef: service required is invalid.\n");
    exit(0);
    return CORBA::Object::_nil();
  }


  // Create a name object, containing the name test/context:
  CosNaming::Name name;
  name.length(2);

  name[0].id   = (const char*) "birdsEYE";       // string copied
  name[0].kind = (const char*) "DataCollectionS"; // string copied
  name[1].id   = (const char*) "clienT";
  name[1].kind = (const char*) "sendeR";
  // Note on kind: The kind field is used to indicate the type
  // of the object. This is to avoid conventions such as that used
  // by files (name.type -- e.g. test.ps = postscript etc.)


  try {
    // Resolve the name to an object reference.
    return rootContext->resolve(name);
  }
  catch(CosNaming::NamingContext::NotFound& ex) {
    // This exception is thrown if any of the components of the
    // path [contexts or the object] aren't found:
    //cerr << "Context not found." << endl;
    _BRDTRACE("getObjRef: context not found.\n");
    exit(0);
  }
  catch(CORBA::COMM_FAILURE& ex) {
    //cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
     //    << "naming service." << endl;
    _BRDTRACE("getObjRef: caught system exception COMM_FAILURE.\n");
    exit(0);
  }
  catch(CORBA::SystemException&) {
    //cerr << "Caught a CORBA::SystemException while using the naming service."
     //<< endl;
    _BRDTRACE("getObjRef: caught system exception while using the name service.\n");
    exit(0);
  }

  return CORBA::Object::_nil();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36306835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档