CORBA客户端/服务器应用程序,从服务器获取信息(空值)



我用Java和C++在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.hTaskImpl.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.hEmploye_Impl.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] = 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]);
}

WorkImpl.hWorkImpl.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::NotFoundn";
  }catch(CosNaming::NamingContext::InvalidName&){
      cerr << "CosNaming::NamingContext::InvalidNamen";
  }catch(CosNaming::NamingContext::CannotProceed&){
      cerr << "CosNaming::NamingContext::CannotProceedn";
  }
  serverORB->run();
  delete work;
  serverORB->destroy();
  }catch(CORBA::SystemException&){
    cerr << "Caught CORBA::SystemExceptionn";
  }catch(CORBA::Exception&){
    cerr << "Caught CORBA::Exception  n";
  }catch(omniORB::fatalException &fe){
    cerr << "Caught omniORB::fatalExceptionn";
    cerr << "File: " << fe.file() << "n";
    cerr << "Line: " << fe.line() << "n";
    cerr << "Msg: " << fe.errmsg() << "n";
  }catch(...){
    cerr << "Caught unknown exceptionn";
  }
   return 0;
}

Client.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((short)1);
        System.out.println(t.getThingToDo());
        }catch(Exception e){ System.out.println(e.getMessage()); 
            }
    }
}

当我有EmployeImpl.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] = 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();
}

我第一次运行客户端时,我看到:

Smith
Print all the documents

这是正确的。但当我第二次(和第三次,等等…)运行我的客户时,我看到:

Smith
null

问题出在哪里?我已经考虑了很多,并再次编写了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方面是否好。有什么建议吗?

使用这些命令编译和运行服务器和客户端文件(我使用omniORB 4.1.6和Kubuntu 12.04上的默认jdk包):

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
cd java; java OtherPackage.Client -ORBInitRef NameService=corbaloc::localhost:2809/NameService

这是我的工作代码:http://www9.zippyshare.com/v/46287610/file.html

仔细想想,我不确定我的答案是否真的正确,一旦答案被接受,stackoverflow就不会让我删除。

相关内容

  • 没有找到相关文章

最新更新