corbaUtil.cc

Go to the documentation of this file.
00001 /*!@file corbaUtil.cc */
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/SalGlasses/SalGlassesServer/corbaUtil.cc $
00035 // $Id: corbaUtil.cc 9108 2007-12-30 06:14:30Z rjpeters $
00036 //
00037 
00038 #include <stdio.h>
00039 #include "corbaUtil.h"
00040 
00041 //! Build an object reference for many object within a context
00042 bool getMultiObjectRef(CORBA::ORB_ptr orb, const char *contextName, CORBA::Object_ptr *dst, int &nObj){
00043         printf("Looking for objects\n");
00044         CosNaming::NamingContext_var rootContext;
00045 
00046         try {
00047                 // Obtain a reference to the root context of the Name service:
00048                 CORBA::Object_var obj;
00049                 obj = orb->resolve_initial_references("NameService");
00050                 // Narrow the reference returned.
00051                 rootContext = CosNaming::NamingContext::_narrow(obj);
00052                 if( CORBA::is_nil(rootContext) ) {
00053                         printf("Failed to narrow the root naming context.\n");
00054                         return false;
00055                 }
00056         }
00057         catch(CORBA::ORB::InvalidName& ex) {
00058                 // This should not happen!
00059                 printf("Service required is invalid [does not exist].\n");
00060                 return false;
00061         }
00062 
00063         //find out how many objects we have to work with
00064 
00065         CosNaming::Name_var name = omniURI::stringToName(contextName);
00066 
00067         CORBA::Object_var obj = rootContext->resolve(name);
00068 
00069         CosNaming::NamingContext_var context;
00070         context = CosNaming::NamingContext::_narrow(obj);
00071 
00072         if (CORBA::is_nil(context)) {
00073                 printf("No objects found\n");
00074                 return false;
00075         }
00076 
00077         //get all the objects in the context
00078 
00079         CosNaming::BindingIterator_var bi;
00080         CosNaming::BindingList_var bl;
00081         CosNaming::Binding_var b;
00082 
00083         context->list(0, bl, bi);
00084 
00085         if (CORBA::is_nil(bi)){
00086                 printf("No objects found\n");
00087                 return false;
00088         }
00089 
00090         nObj=0;
00091         while (bi->next_one(b)){
00092 
00093                 CosNaming::Name name_comp = b->binding_name;
00094 
00095                 char* name = omniURI::nameToString(name_comp);
00096                 printf("Binding to %s ... ", name);
00097                 delete name;
00098 
00099                 CORBA::Object_ptr ref = context->resolve(name_comp);
00100                 dst[nObj] = ref;
00101 
00102                 if (!CORBA::is_nil(dst[nObj])){
00103                         nObj++;
00104                         printf("Done\n");
00105                 } else {
00106                         printf("Fail\n");
00107                 }
00108 
00109         }
00110         bi->destroy();
00111 
00112         return true;
00113 }
00114 
00115 //////////////////////////////////////////////////////////////////////
00116 
00117 CORBA::Boolean
00118 bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,
00119                  const char* contextId, const char* contextKind,
00120                  const char* objectPrefix, CosNaming::Name& objectName)
00121 {
00122         CosNaming::NamingContext_var rootContext;
00123 
00124         try {
00125                 // Obtain a reference to the root context of the Name service:
00126                 CORBA::Object_var obj;
00127                 obj = orb->resolve_initial_references("NameService");
00128 
00129                 if( CORBA::is_nil(obj) ) {
00130                         printf("Obj is null.\n");
00131                         return 0;
00132                 }
00133 
00134                 // Narrow the reference returned.
00135                 rootContext = CosNaming::NamingContext::_narrow(obj);
00136                 if( CORBA::is_nil(rootContext) ) {
00137                         printf("Failed to narrow the root naming context.\n");
00138                         return 0;
00139                 }
00140         }
00141         catch(CORBA::ORB::InvalidName& ex) {
00142                 // This should not happen!
00143                 printf("Service required is invalid [does not exist].\n");
00144                 return 0;
00145         }
00146 
00147         try {
00148                 // Bind a context called "test" to the root context:
00149 
00150                 CosNaming::Name contextName;
00151                 contextName.length(1);
00152                 contextName[0].id   = (const char*)contextId;   // string copied
00153                 contextName[0].kind = (const char*)contextKind; // string copied
00154                 // Note on kind: The kind field is used to indicate the type
00155                 // of the object. This is to avoid conventions such as that used
00156                 // by files (name.type -- e.g. test.ps = postscript etc.)
00157                 CosNaming::NamingContext_var testContext;
00158                 try {
00159                         // Bind the context to root.
00160                         testContext = rootContext->bind_new_context(contextName);
00161                 }
00162                 catch(CosNaming::NamingContext::AlreadyBound& ex) {
00163                         // If the context already exists, this exception will be raised.
00164                         // In this case, just resolve the name and assign testContext
00165                         // to the object returned:
00166                         CORBA::Object_var obj;
00167                         obj = rootContext->resolve(contextName);
00168                         testContext = CosNaming::NamingContext::_narrow(obj);
00169                         if( CORBA::is_nil(testContext) ) {
00170                                 printf("Failed to narrow naming context.\n");
00171                                 return 0;
00172                         }
00173                 }
00174 
00175                 // Bind objref with name Echo to the testContext:
00176                 objectName.length(1);
00177 
00178 
00179                 bool bound = false;
00180                 char CmapID[100];
00181                 for (int i=0; i<100 && !bound; i++) {
00182                         sprintf(CmapID, "%s_%i", objectPrefix, i);
00183                         printf("Binding object %s\n", CmapID);
00184                         objectName[0].id   = (const char*) CmapID;   // string copied
00185                         objectName[0].kind = (const char*) "Object"; // string copied
00186 
00187                         bound = true;
00188                         try {
00189                                 testContext->bind(objectName, objref);
00190                         }
00191                         catch(CosNaming::NamingContext::AlreadyBound& ex) {
00192                                 //testContext->rebind(objectName, objref);
00193                                 bound = false;
00194                         }
00195 
00196                 }
00197 
00198                 if (!bound){
00199                         printf("Can not bind object\n");
00200                         return 0;
00201                 } else {
00202                 }
00203 
00204 
00205                 // Amendment: When using OrbixNames, it is necessary to first try bind
00206                 // and then rebind, as rebind on it's own will throw a NotFoundexception if
00207                 // the Name has not already been bound. [This is incorrect behaviour -
00208                 // it should just bind].
00209         }
00210         catch(CORBA::COMM_FAILURE& ex) {
00211                 printf("Caught system exception COMM_FAILURE -- unable to contact the naming service.\n");
00212                 return 0;
00213         }
00214         catch(CORBA::SystemException&) {
00215                 printf("Caught a CORBA::SystemException while using the naming service.\n");
00216                 return 0;
00217         }
00218 
00219         return 1;
00220 }
00221 
00222 // unbind the object from the name server
00223 void unbindObject (CORBA::ORB_ptr orb, const char* contextId,
00224     const char* contextKind,
00225     CosNaming::Name &objectName){
00226   CosNaming::NamingContext_var rootContext;
00227 
00228         try {
00229                 // Obtain a reference to the root context of the Name service:
00230                 CORBA::Object_var obj;
00231                 obj = orb->resolve_initial_references("NameService");
00232 
00233                 if( CORBA::is_nil(obj) ) {
00234                         printf("Obj is null.\n");
00235                         return;
00236                 }
00237 
00238                 // Narrow the reference returned.
00239                 rootContext = CosNaming::NamingContext::_narrow(obj);
00240                 if( CORBA::is_nil(rootContext) ) {
00241                         printf("Failed to narrow the root naming context.\n");
00242                         return;
00243                 }
00244         }
00245         catch(CORBA::ORB::InvalidName& ex) {
00246                 // This should not happen!
00247                 printf("Service required is invalid [does not exist].\n");
00248                 return;
00249         }
00250 
00251         CosNaming::Name contextName;
00252         contextName.length(2);
00253         contextName[0].id   = (const char*)contextId;
00254         contextName[0].kind = (const char*)contextKind;
00255         contextName[1] = objectName[0];
00256 
00257         rootContext->unbind(contextName);
00258 }
Generated on Sun May 8 08:06:44 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3