CorbaUtil.H

Go to the documentation of this file.
00001 /*!@file Corba/CorbaUtil.H */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Lior Elazary <lelazary@yahoo.com>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Corba/CorbaUtil.H $
00035 // $Id: CorbaUtil.H 14125 2010-10-12 06:29:08Z itti $
00036 //
00037 
00038 #ifndef CORBAUTIL_H
00039 #define CORBAUTIL_H
00040 
00041 #include <omniORB4/Naming.hh>
00042 #include <omniORB4/omniURI.h>
00043 #include <cstdio> // for sprintf
00044 
00045 OMNI_USING_NAMESPACE(omni);
00046 
00047 
00048 //! Build an object reference for many object within a context
00049 inline bool getMultiObjectRef(CORBA::ORB_ptr orb, const char *contextName, CORBA::Object_ptr *dst, int &nObj){
00050         LINFO("Looking for objects");
00051         CosNaming::NamingContext_var rootContext;
00052 
00053         try {
00054                 // Obtain a reference to the root context of the Name service:
00055                 CORBA::Object_var obj;
00056                 obj = orb->resolve_initial_references("NameService");
00057                 // Narrow the reference returned.
00058                 rootContext = CosNaming::NamingContext::_narrow(obj);
00059                 if( CORBA::is_nil(rootContext) ) {
00060                         LFATAL("Failed to narrow the root naming context.");
00061                         return false;
00062                 }
00063         }
00064         catch(CORBA::ORB::InvalidName& ex) {
00065                 // This should not happen!
00066                 LFATAL("Service required is invalid [does not exist].");
00067                 return false;
00068         }
00069 
00070         //find out how many objects we have to work with
00071 
00072         CosNaming::Name_var name = omniURI::stringToName(contextName);
00073 
00074         CORBA::Object_var obj = rootContext->resolve(name);
00075 
00076         CosNaming::NamingContext_var context;
00077         context = CosNaming::NamingContext::_narrow(obj);
00078 
00079         if (CORBA::is_nil(context)) {
00080                 LINFO("No objects found\n");
00081                 return false;
00082         }
00083 
00084         //get all the objects in the context
00085 
00086         CosNaming::BindingIterator_var bi;
00087         CosNaming::BindingList_var bl;
00088         CosNaming::Binding_var b;
00089 
00090         context->list(0, bl, bi);
00091 
00092         if (CORBA::is_nil(bi)){
00093                 LINFO("No objects found\n");
00094                 return false;
00095         }
00096 
00097         nObj=0;
00098         while (bi->next_one(b)){
00099 
00100                 CosNaming::Name name_comp = b->binding_name;
00101 
00102                 char* name = omniURI::nameToString(name_comp);
00103                 LINFO("Binding to %s ... ", name);
00104                 delete name;
00105 
00106                 CORBA::Object_ptr ref = context->resolve(name_comp);
00107                 dst[nObj] = ref;
00108 
00109                 if (!CORBA::is_nil(dst[nObj])){
00110                         nObj++;
00111                         LINFO("Done\n");
00112                 } else {
00113                         LINFO("Fail\n");
00114                 }
00115 
00116         }
00117         bi->destroy();
00118 
00119         return true;
00120 }
00121 
00122 //////////////////////////////////////////////////////////////////////
00123 
00124 inline static CORBA::Boolean
00125 bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,
00126                  const char* contextId, const char* contextKind,
00127                  const char* objectPrefix, CosNaming::Name& objectName)
00128 {
00129         CosNaming::NamingContext_var rootContext;
00130 
00131         try {
00132                 // Obtain a reference to the root context of the Name service:
00133                 CORBA::Object_var obj;
00134                 obj = orb->resolve_initial_references("NameService");
00135 
00136                 if( CORBA::is_nil(obj) ) {
00137                         LFATAL("Obj is null.");
00138                         return 0;
00139                 }
00140 
00141                 // Narrow the reference returned.
00142                 rootContext = CosNaming::NamingContext::_narrow(obj);
00143                 if( CORBA::is_nil(rootContext) ) {
00144                         LFATAL("Failed to narrow the root naming context.");
00145                         return 0;
00146                 }
00147         }
00148         catch(CORBA::ORB::InvalidName& ex) {
00149                 // This should not happen!
00150                 LFATAL("Service required is invalid [does not exist].");
00151                 return 0;
00152         }
00153 
00154         try {
00155                 // Bind a context called "test" to the root context:
00156 
00157                 CosNaming::Name contextName;
00158                 contextName.length(1);
00159                 contextName[0].id   = (const char*)contextId;   // string copied
00160                 contextName[0].kind = (const char*)contextKind; // string copied
00161                 // Note on kind: The kind field is used to indicate the type
00162                 // of the object. This is to avoid conventions such as that used
00163                 // by files (name.type -- e.g. test.ps = postscript etc.)
00164                 CosNaming::NamingContext_var testContext;
00165                 try {
00166                         // Bind the context to root.
00167                         testContext = rootContext->bind_new_context(contextName);
00168                 }
00169                 catch(CosNaming::NamingContext::AlreadyBound& ex) {
00170                         // If the context already exists, this exception will be raised.
00171                         // In this case, just resolve the name and assign testContext
00172                         // to the object returned:
00173                         CORBA::Object_var obj;
00174                         obj = rootContext->resolve(contextName);
00175                         testContext = CosNaming::NamingContext::_narrow(obj);
00176                         if( CORBA::is_nil(testContext) ) {
00177                                 LFATAL("Failed to narrow naming context.");
00178                                 return 0;
00179                         }
00180                 }
00181 
00182                 // Bind objref with name Echo to the testContext:
00183                 objectName.length(1);
00184 
00185 
00186                 bool bound = false;
00187                 char CmapID[100];
00188                 for (int i=0; i<100 && !bound; i++) {
00189                         sprintf(CmapID, "%s_%i", objectPrefix, i);
00190                         LINFO("Binding object %s", CmapID);
00191                         objectName[0].id   = (const char*) CmapID;   // string copied
00192                         objectName[0].kind = (const char*) "Object"; // string copied
00193 
00194                         bound = true;
00195                         try {
00196                                 testContext->bind(objectName, objref);
00197                         }
00198                         catch(CosNaming::NamingContext::AlreadyBound& ex) {
00199                                 //testContext->rebind(objectName, objref);
00200                                 bound = false;
00201                         }
00202 
00203                 }
00204 
00205                 if (!bound){
00206                         LFATAL("Can not bind object");
00207                         return 0;
00208                 } else {
00209                 }
00210 
00211 
00212                 // Amendment: When using OrbixNames, it is necessary to first try bind
00213                 // and then rebind, as rebind on it's own will throw a NotFoundexception if
00214                 // the Name has not already been bound. [This is incorrect behaviour -
00215                 // it should just bind].
00216         }
00217         catch(CORBA::COMM_FAILURE& ex) {
00218                 LFATAL("Caught system exception COMM_FAILURE -- unable to contact the naming service.");
00219                 return 0;
00220         }
00221         catch(CORBA::SystemException&) {
00222                 LFATAL("Caught a CORBA::SystemException while using the naming service.");
00223                 return 0;
00224         }
00225 
00226         return 1;
00227 }
00228 
00229 // unbind the object from the name server
00230 inline void unbindObject (CORBA::ORB_ptr orb, const char* contextId,
00231                           const char* contextKind,
00232                           CosNaming::Name &objectName){
00233         CosNaming::NamingContext_var rootContext;
00234 
00235         try {
00236                 // Obtain a reference to the root context of the Name service:
00237                 CORBA::Object_var obj;
00238                 obj = orb->resolve_initial_references("NameService");
00239 
00240                 if( CORBA::is_nil(obj) ) {
00241                         LFATAL("Obj is null.");
00242                         return;
00243                 }
00244 
00245                 // Narrow the reference returned.
00246                 rootContext = CosNaming::NamingContext::_narrow(obj);
00247                 if( CORBA::is_nil(rootContext) ) {
00248                         LFATAL("Failed to narrow the root naming context.");
00249                         return;
00250                 }
00251         }
00252         catch(CORBA::ORB::InvalidName& ex) {
00253                 // This should not happen!
00254                 LFATAL("Service required is invalid [does not exist].");
00255                 return;
00256         }
00257 
00258         CosNaming::Name contextName;
00259         contextName.length(2);
00260         contextName[0].id   = (const char*)contextId;
00261         contextName[0].kind = (const char*)contextKind;
00262         contextName[1] = objectName[0];
00263 
00264         rootContext->unbind(contextName);
00265 }
00266 
00267 /****************************** Convertion Util ***************************/
00268 inline Point2DOrb toOrb(const Point2D<int> &p){
00269         Point2DOrb_var pOrb;
00270         pOrb->i = p.i;
00271         pOrb->j = p.j;
00272         return pOrb._retn();
00273 }
00274 
00275 #endif
00276 
Generated on Sun May 8 08:40:23 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3