1

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

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

服务器.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;
}

}

客户端.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;
    }
}
4

1 回答 1

0

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

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

于 2017-11-17T08:43:18.743 回答