gpt4 book ai didi

c++ - CORBA omniorb C++ 多仆人

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:18:34 26 4
gpt4 key购买 nike

大家好,我是 CORBA C++ 的新手,我阅读了一篇关于如何在 Linux 上安装 CORBA 的教程,它的工作和代码编译都很好,我想执行多个服务器实例,因为我想创建一个对等的 -同行。例如,当我创建一个服务器实例和多个客户端实例时工作正常,但是当我尝试执行两个或多个服务器实例以便从客户端接收消息时,只有最后一个服务器实例接收消息,请问有人能帮我吗?

这是我的服务器和客户端代码

server.cpp

#include "MyExampleInterface_impl.h"
#include <iostream>
#include <CORBA.h>
#include <Naming.hh>

/** Server name, clients needs to know this name */
#define SERVER_NAME "MyServerName"

using namespace std;

int main(int argc, char ** argv)
{
cerr << "CORBA SERVER RUNNING..." << endl;
try {

//------------------------------------------------------------------------
// Initialize CORBA ORB
//------------------------------------------------------------------------
CORBA::ORB_ptr orb = CORBA::ORB_init(argc, argv);

//------------------------------------------------------------------------
// Initialize POA: Get reference to root POA
//
// Servant must register with POA in order to be made available for client
// Get reference to the RootPOA.
//-----------------------------------------------------------------------
CORBA::Object_var poa_obj = orb->resolve_initial_references("RootPOA");
PortableServer::POA_var poa = PortableServer::POA::_narrow(poa_obj);
PortableServer::POAManager_var manager = poa->the_POAManager();

//------------------------------------------------------------------------
// Create service
//------------------------------------------------------------------------
MyExampleInterface_impl * service = new MyExampleInterface_impl;
cerr << "ya se creo el servicio procediendo a crear instancia" << endl;
try {
//------------------------------------------------------------------------
// Bind object to name service as defined by directive InitRef
// and identifier "NameService" in config file omniORB.cfg.
//------------------------------------------------------------------------
CORBA::Object_var ns_obj = orb->resolve_initial_references("NameService");
if (!CORBA::is_nil(ns_obj)) {
//------------------------------------------------------------------------
// Narrow this to the naming context
//------------------------------------------------------------------------
CosNaming::NamingContext_ptr nc = CosNaming::NamingContext::_narrow(ns_obj);

//------------------------------------------------------------------------
// Bind to CORBA name service. Same name to be requested by client.
//------------------------------------------------------------------------
CosNaming::Name name;
name.length(1);
name[0].id = CORBA::string_dup(SERVER_NAME);
name[0].kind = CORBA::string_dup("");
nc->rebind(name, service->_this());

//------------------------------------------------------------------------
// Intizialization ready, server runs
//------------------------------------------------------------------------
cout << argv[0] << " C++ (omniORB) server '" << SERVER_NAME << "' is running .." << endl;
}
} catch (CosNaming::NamingContext::NotFound &) {
cerr << "Caught CORBA exception: not found" << endl;
} catch (CosNaming::NamingContext::InvalidName &) {
cerr << "Caught CORBA exception: invalid name" << endl;
} catch (CosNaming::NamingContext::CannotProceed &) {
cerr << "Caught CORBA exception: cannot proceed" << endl;
}

//------------------------------------------------------------------------
// Activate the POA manager
//------------------------------------------------------------------------
manager->activate();
//------------------------------------------------------------------------
// Accept requests from clients
//------------------------------------------------------------------------
cerr << "aceptando peticiones de cliente con run()" << endl;
orb->run();
cerr << "hecho ahora esperando..." << endl;
//------------------------------------------------------------------------
// Clean up
//------------------------------------------------------------------------
//delete service;

//------------------------------------------------------------------------
// Destroy ORB
//------------------------------------------------------------------------
orb->destroy();

} catch (CORBA::UNKNOWN) {
cerr << "Caught CORBA exception: unknown exception" << endl;
} catch (CORBA::SystemException &) {
cerr << "Caught CORBA exception: system exception" << endl;
}

client.cpp

#include "example.hh"
#include <iostream>
#include <CORBA.h>
#include <Naming.hh>

/** Name is defined in the server.cpp */
#define SERVER_NAME "MyServerName"

using namespace std;

int main(int argc, char ** argv)
{
try {
//------------------------------------------------------------------------
// Initialize ORB object.
//------------------------------------------------------------------------
CORBA::ORB_ptr orb = CORBA::ORB_init(argc, argv);

//------------------------------------------------------------------------
// Resolve service
//------------------------------------------------------------------------
ExampleInterface_ptr service_server = 0;

try {

//------------------------------------------------------------------------
// Bind ORB object to name service object.
// (Reference to Name service root context.)
//------------------------------------------------------------------------
CORBA::Object_var ns_obj = orb->resolve_initial_references("NameService");

if (!CORBA::is_nil(ns_obj)) {
//------------------------------------------------------------------------
// Bind ORB object to name service object.
// (Reference to Name service root context.)
//------------------------------------------------------------------------
CosNaming::NamingContext_ptr nc = CosNaming::NamingContext::_narrow(ns_obj);

//------------------------------------------------------------------------
// The "name text" put forth by CORBA server in name service.
// This same name ("MyServerName") is used by the CORBA server when
// binding to the name server (CosNaming::Name).
//------------------------------------------------------------------------
CosNaming::Name name;
name.length(1);
name[0].id = CORBA::string_dup(SERVER_NAME);
name[0].kind = CORBA::string_dup("");

//------------------------------------------------------------------------
// Resolve "name text" identifier to an object reference.
//------------------------------------------------------------------------
CORBA::Object_ptr obj = nc->resolve(name);

if (!CORBA::is_nil(obj)) {
service_server = ExampleInterface::_narrow(obj);
}
}
} catch (CosNaming::NamingContext::NotFound &) {
cerr << "Caught corba not found" << endl;
} catch (CosNaming::NamingContext::InvalidName &) {
cerr << "Caught corba invalid name" << endl;
} catch (CosNaming::NamingContext::CannotProceed &) {
cerr << "Caught corba cannot proceed" << endl;
}

//------------------------------------------------------------------------
// Do stuff
//------------------------------------------------------------------------
if (!CORBA::is_nil(service_server)) {
char * server = service_server->send_message("Message from C++ (omniORB) client");
cout << "response from Server: " << server << endl;
CORBA::string_free(server);
}

//------------------------------------------------------------------------
// Destroy OBR
//------------------------------------------------------------------------
orb->destroy();

} catch (CORBA::UNKNOWN) {
cerr << "Caught CORBA exception: unknown exception" << endl;
}
}

最佳答案

命名服务就像电话簿,您使用已知名称存储对象引用。目前你的代码中有“MyServerName”,并且只能有一个服务器注册到它。启动第二台服务器会将命名服务更新为仅使用第二台服务器,所有新客户端将从那时起使用它。

您可以使用唯一名称在命名服务中注册每个服务器,并让客户端决定使用哪个服务器。

关于c++ - CORBA omniorb C++ 多仆人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47338138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com