1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| #include <iostream>
#include <string>
#include "HelloImpl.h"
using namespace std;
static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, CORBA::Object_ptr);
int main(int argc, char* argv[])
{
int exitStatus = 0;
try {
// Init du ORB
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
cout << "ORB initialization \t\t\t [ ok ]\n";
// Création du serveur
HelloImpl *srv = new HelloImpl("Coucou");
cout << "Server création \t\t\t [ ok ]\n";
// Init du POA
CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
cout << "POA initialization \t\t\t [ ok ]" << endl;
// Enregistrement du serveur auprès du POA
PortableServer::ObjectId_var svId = poa->activate_object(srv);
cout << "Server POA registration \t\t [ ok ]" << endl;
// Binding entre le "nom" et la ref du srvHello
Hello_var srvReference = srv->_this();
CORBA::String_var srvToString = orb->object_to_string(srvReference);
if(bindObjectToName(orb, srvReference))
cout << "Server binding \t\t\t\t [ ok ]\n";
else
exitStatus = 1;
}
catch(CORBA::SystemException& e) {
cerr << "Caught CORBA::SystemException." << endl;
}
catch(CORBA::Exception& e) {
cerr << "Caught CORBA::Exception." << endl;
}
catch(...) {
cerr << "Caught unknown exception." << endl;
}
return exitStatus;
};
static CORBA::Boolean
bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref)
{
cout << "1\n";
CosNaming::NamingContext_var rootContext;
cout << "2\n";
try {
cout << "3\n";
// Obtain a reference to the root context of the Name service:
CORBA::Object_var obj;
cout << "3.1\n";
obj = orb->resolve_initial_references("NameService");
cout << "3.2\n";
// Narrow the reference returned.
rootContext = CosNaming::NamingContext::_narrow(obj);
cout << "3.3\n";
if( CORBA::is_nil(rootContext) ) {
cerr << "Failed to narrow the root naming context." << endl;
cout << "3.4\n";
return 0;
}
}
catch(CORBA::ORB::InvalidName& ex) {
cout << "4\n";
// This should not happen!
cerr << "Service required is invalid [does not exist]: "
// marche pas avec omniorb: << ex << endl
;
return 0;
}
cout << "5\n";
try {
// Bind a context called "test" to the root context:
CosNaming::Name contextName;
contextName.length(1);
contextName[0].id = (const char*) "test"; // string copied
contextName[0].kind = (const char*) "my_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.
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 objref with name Arbitre to the testContext:
CosNaming::Name objectName;
objectName.length(1);
objectName[0].id = (const char*) "Arbitre"; // string copied
objectName[0].kind = (const char*) "Object"; // 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/Arbitre 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.
// 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;
} |
Partager