1

我扩展了我之前的示例:CORBA C++/Java 应用程序中服务器端的分段错误(核心转储),我终于在 Stack 的用户 @Brian Neal 的帮助下完成了。

我还是 Corba 的新手,所以我想更多地了解 Corba 中的序列,我写了这个简单的(?)示例:

interface Task
{
    string getThingToDo();
};

#include "Task.idl"

interface Employee
{
    typedef sequence <Task> tasks;
    string getLastname();
    Task getTask(in short id);
};

#include "Employee.idl"

interface Work
{
    Employee getEmployee(in short id);
};

TaskImpl.h 和 TaskImpl.cpp:

#include "Task.hh"

class TaskImpl : public POA_Task
{
    private:
        CORBA::String_var thingToDo;

    public:
        TaskImpl(const char* thingToDo);
        char* getThingToDo();
};

#include "TaskImpl.h"

TaskImpl::TaskImpl(const char* thingToDo)
{
    this->thingToDo = CORBA::string_dup(thingToDo);
}

char* TaskImpl::getThingToDo()
{
    return CORBA::string_dup(this->thingToDo.in());
}

EmployeeImpl.h 和 EmployeeImpl.cpp:

#include "Employee.hh"
#include "TaskImpl.h"

class EmployeeImpl : public POA_Employee
{
    private:
        CORBA::String_var lastname;
        int id;
        Employee::tasks thingsToDo;

    public:
        EmployeeImpl(const char* lastname, int id);
        char* getLastname();
        Task_ptr getTask(::CORBA::Short id);
};

#include "EmployeeImpl.h"

EmployeeImpl::EmployeeImpl(const char* lastname, int id)
{
    this->lastname = CORBA::string_dup(lastname);
    this->id = id;
    this->thingsToDo.length(3);

    TaskImpl *t1, *t2, *t3;
    t1 = new TaskImpl("Print all the documents");
    t2 = new TaskImpl("Write the report");
    t3 = new TaskImpl("Make backup");

    this->thingsToDo[0] = Task::_duplicate(t1->_this());
    this->thingsToDo[1] = Task::_duplicate(t2->_this());
    this->thingsToDo[2] = Task::_duplicate(t3->_this());
}

char* EmployeeImpl::getLastname()
{
    return CORBA::string_dup(this->lastname.in());
}

Task_ptr EmployeeImpl::getTask(::CORBA::Short id)
{
    return this->thingsToDo[id-1]._retn();
}

WorkImpl.h 和 WorkImpl.cpp:

#include "Work.hh"
#include <vector>
#include "EmployeeImpl.h"
using namespace std;

class WorkImpl : public POA_Work
{
    private:
        vector<EmployeeImpl> employees;

    public:
        WorkImpl();
        Employee_ptr getEmployee(::CORBA::Short id);
};

#include "WorkImpl.h"

 WorkImpl::WorkImpl()
 {
    EmployeeImpl ei1(CORBA::string_dup("Doe"), 1);
    EmployeeImpl ei2(CORBA::string_dup("Smith"), 2);
    EmployeeImpl ei3(CORBA::string_dup("Brown"), 3);

    employees.push_back(ei1);
    employees.push_back(ei2);
    employees.push_back(ei3);
 }

Employee_ptr WorkImpl::getEmployee(::CORBA::Short id)
{
    return employees[id]._this();
}

服务器.cpp:

#include "WorkImpl.h"
#include <omniORB4/CORBA.h>
#include <omniORB4/Naming.hh>
#include <iostream>
using std::cout;
using std::cerr;

int main(int argc, char **argv)
{
 try
 {
  CORBA::ORB_ptr serverORB = CORBA::ORB_init(argc, argv);

  CORBA::Object_var myPOAObj = serverORB->resolve_initial_references("RootPOA");

  PortableServer::POA_var myPOA = PortableServer::POA::_narrow(myPOAObj);
  PortableServer::POAManager_var myManager = myPOA->the_POAManager();
  myManager->activate();

  WorkImpl *work = new WorkImpl();

  try
  {
   CORBA::Object_var nameServiceObj = serverORB->resolve_initial_references("NameService");

   if(!CORBA::is_nil(nameServiceObj))
   {
    CosNaming::NamingContext_ptr namingContext = CosNaming::NamingContext::_narrow(nameServiceObj);
    CosNaming::Name serviceName;
    serviceName.length(1);
    serviceName[0].id = CORBA::string_dup("WorkService");
    namingContext->rebind(serviceName, work->_this());
    cout << "WorkService is running ...\n";
   }

  }catch(CosNaming::NamingContext::NotFound&){
      cerr << "CosNaming::NamingContext::NotFound\n";
  }catch(CosNaming::NamingContext::InvalidName&){
      cerr << "CosNaming::NamingContext::InvalidName\n";
  }catch(CosNaming::NamingContext::CannotProceed&){
      cerr << "CosNaming::NamingContext::CannotProceed\n";
  }

  serverORB->run();

  delete work;
  serverORB->destroy();

  }catch(CORBA::SystemException&){
    cerr << "Caught CORBA::SystemException\n";
  }catch(CORBA::Exception&){
    cerr << "Caught CORBA::Exception  \n";
  }catch(omniORB::fatalException &fe){
    cerr << "Caught omniORB::fatalException\n";
    cerr << "File: " << fe.file() << "\n";
    cerr << "Line: " << fe.line() << "\n";
    cerr << "Msg: " << fe.errmsg() << "\n";
  }catch(...){
    cerr << "Caught unknown exception\n";
  }

   return 0;
}

客户端.java:

package OtherPackage;

import java.util.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;
public class Client
{
    public static void main(String [] args)
    {
     try
     {
        org.omg.CORBA.ORB clientORB = org.omg.CORBA.ORB.init(args, null);

        if (clientORB == null)
        {
            System.out.println("Problem while creating ORB");
            System.exit(1);
        }

        org.omg.CORBA.Object objRef = clientORB.resolve_initial_references("NameService");
        NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

        Work work = WorkHelper.narrow(ncRef.resolve_str("WorkService"));
        Employee e = work.getEmployee((short)1);
        System.out.println(e.getLastname());

        Task t = e.getTask();
        System.out.println(t.getThingToDo((short)2));

            System.out.println(work.getEmployee((short)2).getThingToDo((short)1));

        }catch(Exception e){ System.out.println(e.getMessage()); }
    }
}

当我尝试编译我的客户端时,我收到一个错误:

Client.java:29: getTask(short) in OtherPackage.EmployeeOperations cannot be applied to ()
                Task t = e.getTask();
                          ^
Client.java:30: getThingToDo() in OtherPackage.TaskOperations cannot be applied to (short)
                System.out.println(t.getThingToDo((short)2));
                                    ^
2 errors

怎么了?可以这样写吗?

我使用的一些命令:

Compile (and run) server files (just copy and paste):

g++ -c *.cpp -I$OMNIORB_HOME/include -I$OMNIORB_HOME/include/omniORB4
g++ -c *.cc -I$OMNIORB_HOME/include -I$OMNIORB_HOME/include/omniORB4
g++ -o Server Server.o EmployeeSK.o WorkSK.o WorkImpl.o EmployeeImpl.o TaskImpl.o TaskSK.o -L$OMNIORB_HOME/lib -lomnithread -lomniORB4
./Server -ORBInitRef NameService=corbaloc::localhost:2809/NameService

Compile (and run) client files (just copy and paste):

cd EmployeePackage; javac -cp .. *.java
cd OtherPackage; javac -cp .. *.java

我的整个例子:http ://www21.zippyshare.com/v/95586459/file.html

4

2 回答 2

2

您定义getTask了一个short id参数:

Task getTask(in short id);

但是,您尝试在没有任何参数的情况下调用它:

Task t = e.getTask();

另一个错误本质上非常相似。您定义getThingToDo有任何空参数列表:

string getThingToDo();

...而且,您再次尝试使用不正确的参数调用它;在这种情况下,您试图通过short

System.out.println(t.getThingToDo((short)2));

我建议您改为尝试如下操作,我相信这可以实现您的意图:

final Task t = e.getTask((short) 2);
System.out.println(t.getThingToDo());

...这应该打印第二个员工的第二个任务的描述,即"Write the report"。请记住,它std::vector是零索引的。

于 2012-09-09T17:33:58.620 回答
1

正如@oldrinb 所说,我更改了我的代码,现在一切都很好(我的意思是,编译)。

问题现在在应用程序中(逻辑?)。当我第一次运行客户端时,我看到:

Smith
Print all the documents

哪个是对的。但是当我第二次(和第三次,等等......)运行我的客户端时,我看到:

Smith
null

现在问题出在哪里?这是我的工作代码:http ://www9.zippyshare.com/v/46287610/file.html

要运行客户端,cd到 java 目录并执行命令: java OtherPackage.Client -ORBInitRef NameService=corbaloc::localhost:2809/NameService

我改变了EmployeeImpl.cpp

#include "EmployeeImpl.h"

EmployeeImpl::EmployeeImpl(const char* lastname, int id)
{
    this->lastname = CORBA::string_dup(lastname);
    this->id = id;
    this->thingsToDo.length(3);

    TaskImpl *t1, *t2, *t3;
    t1 = new TaskImpl("Print all the documents");
    t2 = new TaskImpl("Write the report");
    t3 = new TaskImpl("Make backup");

    this->thingsToDo[0] = t1->_this();
    this->thingsToDo[1] = t2->_this();
    this->thingsToDo[2] = t3->_this();
}

char* EmployeeImpl::getLastname()
{
    return CORBA::string_dup(this->lastname.in());
}

Task_ptr EmployeeImpl::getTask(::CORBA::Short id)
{
    return Task::_duplicate(this->thingsToDo[id-1]);
}

现在一切似乎都很好。但我不确定它是否以 CORBA 方式好。有什么建议吗?

于 2012-09-10T13:00:12.807 回答