Pagina Principale   Moduli   Lista dei namespaces   Gerarchia delle classi   Lista in ordine alfabetico   Lista dei composti   Lista dei files   Membri dei namespaces   Membri dei composti   Membri dei files   Esempi  

/home/fbassi/devel/projects/reti/utils/NameServer.cc

Vai alla documentazione di questo file.
00001 /*
00002  *    FBFS Distributed Object Repository
00003  *    Copyright (C) 2001 Francesco V. Bassi
00004  *
00005  *    This program is free software; you can redistribute it and/or modify
00006  *    it under the terms of the GNU General Public License as published by
00007  *    the Free Software Foundation; either version 2 of the License, or
00008  *    (at your option) any later version.
00009  *
00010  *    This program is distributed in the hope that it will be useful,
00011  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *    GNU General Public License for more details.
00014  *
00015  *    You should have received a copy of the GNU General Public License
00016  *    along with this program; if not, write to the Free Software
00017  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 
00020 #include <iostream.h>
00021 #include "NameServer.h"
00022 
00028 using namespace utils;
00029 
00030 CORBA::Object_ptr NameServer::get_object( const CORBA::ORB_ptr orb, const utils::URI &uri) throw(Exception) {
00031     string str_contextname = uri.nameServerURL();
00032     string str_servicename = uri.serviceName();
00033     const char *contextname = str_contextname.c_str();
00034     const char *servicename = str_servicename.c_str();
00035     CosNaming::NamingContextExt_var nc;
00036     //cout << "Context:" << str_contextname << " Service:" << str_servicename << "\n";  
00037     try {
00038         CORBA::Object_var obj = orb->string_to_object( contextname );
00039         nc = CosNaming::NamingContextExt::_narrow(obj);
00040         if( CORBA::is_nil(nc) ) {
00041             cerr << "Failed to narrow to naming context";
00042             throw Exception();
00043         };
00044         CORBA::Object_ptr return_object = nc->resolve_str( servicename );
00045         return return_object;
00046     }
00047     catch(CosNaming::NamingContext::InvalidName &) {
00048         cout << "invalid name\n";
00049         throw InvalidNameException();
00050     }
00051     catch(CosNaming::NamingContext::NotFound &) {
00052         cout << "not found\n";
00053         throw NotFoundException();
00054     }
00055     catch(...) {
00056         cout << "other\n";
00057         throw Exception();
00058     };
00059 };
00060 
00061 CORBA::Boolean NameServer::unbindObject(const char* context, const char* name, const char* kind, const CORBA::ORB_ptr orb) {
00062     CosNaming::NamingContext_var rootContext;
00063     try {
00064         // Obtain a reference to the root context of the Name service:
00065         CORBA::Object_var obj;
00066         obj = orb->resolve_initial_references("NameService");
00067 
00068         // Narrow the reference returned.
00069         rootContext = CosNaming::NamingContext::_narrow(obj);
00070         if( CORBA::is_nil(rootContext) ) {
00071             cerr << "Failed to narrow the root naming context." << endl;
00072             return false;
00073         }
00074     }
00075     catch(CORBA::ORB::InvalidName& ex) {
00076         // This should not happen!
00077         cerr << "Service required is invalid [does not exist]." << endl;
00078         return false;
00079     }
00080 
00081     try {
00082         // Open the required context
00083         CosNaming::Name contextName;
00084         contextName.length(1);
00085         contextName[0].id   = (const char*) context; // string copied
00086         contextName[0].kind = (const char*) "utils"; // string copied
00087 
00088         CORBA::Object_var obj = rootContext->resolve(contextName);
00089         CosNaming::NamingContext_var testContext = CosNaming::NamingContext::_narrow(obj);
00090         if( CORBA::is_nil(testContext) ) {
00091             cerr << "Failed to narrow naming context." << endl;
00092             return false;
00093         }
00094 
00095         // Unbind the specified object
00096         CosNaming::Name objectName;
00097         objectName.length(1);
00098         objectName[0].id   = (const char*) name;
00099         objectName[0].kind = (const char*) kind;
00100 
00101         try {
00102             testContext->unbind(objectName);
00103         }
00104         catch(CosNaming::NamingContext::NotFound& ex) {
00105             cerr << "The object in not registered in the selected context." << endl;
00106         }
00107 // It appears to bring omniNames in an inconsistent state.
00108 //      try {
00109 //          testContext->destroy();
00110 //      }
00111 //      catch(CosNaming::NamingContext::NotEmpty& ex) { }
00112     }
00113     catch(CORBA::COMM_FAILURE& ex) {
00114         cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
00115             << "naming service." << endl;
00116         return false;
00117     }
00118     catch(CORBA::SystemException&) {
00119         cerr << "Caught a CORBA::SystemException while using the naming service." << endl;
00120         return false;
00121     }
00122     return true;
00123 }
00124 
00125 CORBA::Boolean NameServer::bindObjectToName(const char* context, const char* name, const char* kind, const CORBA::ORB_ptr orb, const CORBA::Object_ptr objref)
00126 {
00127     CosNaming::NamingContext_var rootContext;
00128     try {
00129         // Obtain a reference to the root context of the Name service:
00130         CORBA::Object_var obj;
00131         obj = orb->resolve_initial_references("NameService");
00132 
00133         // Narrow the reference returned.
00134         rootContext = CosNaming::NamingContext::_narrow(obj);
00135         if( CORBA::is_nil(rootContext) ) {
00136             cerr << "Failed to narrow the root naming context." << endl;
00137             return false;
00138         }
00139     }
00140     catch(CORBA::ORB::InvalidName& ex) {
00141         // This should not happen!
00142         cerr << "Service required is invalid [does not exist]." << endl;
00143         return false;
00144     }
00145 
00146     try {
00147         // Bind a context called "test" to the root context:
00148         CosNaming::Name contextName;
00149         contextName.length(1);
00150         contextName[0].id   = (const char*) context;
00151         contextName[0].kind = (const char*) PROJECT_NAME;
00152 
00153         CosNaming::NamingContext_var testContext;
00154         try {
00155             // Bind the context to root.
00156             testContext = rootContext->bind_new_context(contextName);
00157         }
00158         catch(CosNaming::NamingContext::AlreadyBound& ex) {
00159             // If the context already exists, this exception will be raised.
00160             // In this case, just resolve the name and assign testContext
00161             // to the object returned:
00162             CORBA::Object_var obj;
00163             obj = rootContext->resolve(contextName);
00164             testContext = CosNaming::NamingContext::_narrow(obj);
00165             if( CORBA::is_nil(testContext) ) {
00166                 cerr << "Failed to narrow naming context." << endl;
00167                 return false;
00168             }
00169         }
00170 
00171         // Bind objref with name Echo to the testContext:
00172         CosNaming::Name objectName;
00173         objectName.length(1);
00174         objectName[0].id   = (const char*) name;
00175         objectName[0].kind = (const char*) kind;
00176 
00177         try {
00178             testContext->bind(objectName, objref);
00179         }
00180         catch(CosNaming::NamingContext::AlreadyBound& ex) {
00181             testContext->rebind(objectName, objref);
00182         }
00183         // Note: Using rebind() will overwrite any Object previously bound
00184         //       to /test/Echo with obj.
00185         //       Alternatively, bind() can be used, which will raise a
00186         //       CosNaming::NamingContext::AlreadyBound exception if the name
00187         //       supplied is already bound to an object.
00188 
00189         // Amendment: When using OrbixNames, it is necessary to first try bind
00190         // and then rebind, as rebind on it's own will throw a NotFoundexception if
00191         // the Name has not already been bound. [This is incorrect behaviour -
00192         // it should just bind].
00193     }
00194     catch(CORBA::COMM_FAILURE& ex) {
00195         cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
00196             << "naming service." << endl;
00197         return false;
00198     }
00199     catch(CORBA::SystemException&) {
00200         cerr << "Caught a CORBA::SystemException while using the naming service." << endl;
00201         return false;
00202     }
00203     return true;
00204 }

Generato il Thu Feb 15 13:24:57 2001 per A Simple Distributed Object Repository with CORBA & C++. da doxygen1.2.3 scritto da Dimitri van Heesch, © 1997-2000