Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

NamingHelper_T.h

Go to the documentation of this file.
00001 /*
00002  * NamingHelper_T.h
00003  *
00004  * Copyright (c) 2003 The University of Utah and the Flux Group.
00005  * All rights reserved.
00006  *
00007  * This file is licensed under the terms of the GNU Public License.  
00008  * See the file "license.terms" for restrictions on redistribution 
00009  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00010  */
00011 /*  -*- C++ -*- */
00012 
00013 //==========================================================================
00014 /**
00015  * @file     NamingHelper_T.h
00016  *
00017  * NamingHelper_T.h,v 1.4 2003/11/20 17:13:44 stack Exp
00018  *
00019  * @author  Craig Rodrigues (crodrigu@bbn.com)
00020  */
00021 //==========================================================================
00022                              
00023 
00024 #ifndef NAMING_HELPER_T_H
00025 #define NAMING_HELPER_T_H
00026 
00027 #include "CosNamingC.h"
00028 
00029 /**
00030   * @class NamingHelper
00031   *
00032   * @brief  Utility functions for using the CORBA Naming Service.
00033   *
00034   * This class uses template functions outlined in Chapter 18
00035   * of "Advanced CORBA Programming with C++" by Michi Henning and
00036   * Steve Vinoski.
00037   */
00038 template<class T>
00039 class NamingHelper
00040 {
00041 public:
00042   /// template function for resolve_initial_references()
00043   static
00044   typename T::_ptr_type
00045   resolve_init(CORBA::ORB_ptr orb, const char *id,
00046                size_t tries = 1,
00047                ACE_Time_Value timeout = ACE_Time_Value::zero)
00048     throw ( CORBA::SystemException,
00049             CORBA::ORB::InvalidName
00050             );
00051 
00052   /// template function for resolving a name in the CORBA Naming Service
00053   static
00054   typename T::_ptr_type
00055   resolve_name( CosNaming::NamingContext_ptr nc,
00056                 const CosNaming::Name& name,
00057                 size_t tries = 1,
00058                 ACE_Time_Value timeout = ACE_Time_Value::zero)
00059     throw( CORBA::SystemException,
00060            CosNaming::NamingContext::NotFound,
00061            CosNaming::NamingContext::CannotProceed,
00062            CosNaming::NamingContext::InvalidName); 
00063 
00064   /// template function to bind a name to the CORBA Naming Service
00065   /** template function for binding to the CORBA Naming Service,
00066    * with tries and a timeout value
00067    */
00068   static
00069   void bind( CosNaming::NamingContext_ptr nc,
00070              const CosNaming::Name& n, CORBA::Object_ptr obj,
00071              size_t tries = 1, 
00072              ACE_Time_Value timeout = ACE_Time_Value::zero)
00073     throw ( CORBA::SystemException,
00074             CosNaming::NamingContext::NotFound,
00075             CosNaming::NamingContext::CannotProceed,
00076             CosNaming::NamingContext::InvalidName,
00077             CosNaming::NamingContext::AlreadyBound );
00078 
00079   /// template function to rebind a name to the CORBA Naming Service
00080   /** template function for rebinding to the CORBA Naming Service,
00081    * with tries and a timeout value
00082    */
00083   static
00084   void rebind ( CosNaming::NamingContext_ptr nc,
00085                 const CosNaming::Name & n, CORBA::Object_ptr obj,
00086                 size_t tries = 1, 
00087                 ACE_Time_Value timeout = ACE_Time_Value::zero)
00088     throw (
00089            CORBA::SystemException,
00090            CosNaming::NamingContext::NotFound,
00091            CosNaming::NamingContext::CannotProceed,
00092            CosNaming::NamingContext::InvalidName
00093            );                                     
00094 
00095 
00096 };
00097 
00098 
00099 template<class T>
00100 typename T::_ptr_type
00101 NamingHelper<T>::resolve_init(CORBA::ORB_ptr orb, const char *id,
00102                               size_t tries,
00103                               ACE_Time_Value timeout)
00104   throw ( CORBA::SystemException,
00105           CORBA::ORB::InvalidName
00106           )
00107 {
00108   size_t i;
00109   CORBA::Object_var obj;
00110   for(i=0; i < tries; i++ ){
00111     try{
00112       obj = orb->resolve_initial_references( id );
00113       break;  // we resolved a name without throwing an exception
00114       // so break out of this for loop
00115     }
00116     catch( ... ){
00117       if( i < tries ){
00118         ACE_DEBUG((LM_DEBUG, "Retrying resolve() of %s ...%d\n", id, i));
00119 
00120         // ACE_OS::sleep() does a select() with a timeout, so is
00121         // threadsafe
00122         ACE_OS::sleep(timeout);
00123       }else{
00124         // We have exceeded our tries, rethrowing exception
00125         ACE_DEBUG((LM_DEBUG, "Rethrowing exception\n"));
00126         throw;
00127       }
00128     }
00129   } 
00130   //assert( !CORBA::is_nil(obj.in() ));
00131 
00132   typename T::_var_type ref;
00133   try{
00134     ref = T::_narrow(obj.in() );
00135   }catch( const CORBA::SystemException& e){
00136     throw;
00137   }
00138               
00139   return ref._retn(); 
00140 }
00141 
00142 
00143 template< class T>
00144 typename T::_ptr_type
00145 NamingHelper<T>::resolve_name( CosNaming::NamingContext_ptr nc,
00146                                const CosNaming::Name& name, 
00147                                size_t tries,
00148                                ACE_Time_Value timeout)
00149   throw( CORBA::SystemException,
00150          CosNaming::NamingContext::NotFound,
00151          CosNaming::NamingContext::CannotProceed,
00152          CosNaming::NamingContext::InvalidName 
00153          )
00154 {
00155   CORBA::Object_var obj;
00156   size_t i;
00157 
00158   for(i=0; i < tries; i++ ){
00159     try{
00160       obj = nc->resolve( name );
00161       break;  // we resolved a name without throwing an exception
00162       // so break out of this for loop
00163     }
00164     catch( ... ){
00165       if( i < tries ){
00166         ACE_DEBUG((LM_DEBUG, "Retrying resolve() of %s ...%d\n", name[0].id.in(), i));
00167 
00168         // ACE_OS::sleep() does a select() with a timeout, so is
00169         // threadsafe
00170         ACE_OS::sleep(timeout);
00171       }else{
00172         // We have exceeded our tries, rethrowing exception
00173         ACE_DEBUG((LM_DEBUG, "Rethrowing exception\n"));
00174         throw;
00175       }
00176     }
00177   } 
00178   if( CORBA::is_nil( obj.in() )){
00179     // do something
00180 
00181   }
00182 
00183   typename T::_var_type ref;
00184   try{
00185     ref = T::_narrow(obj.in() );
00186   }catch(const CORBA::SystemException& e){
00187     e._tao_print_exception("");
00188     throw;
00189   }          
00190 
00191   if( CORBA::is_nil(ref.in() )){
00192     // Do something
00193          
00194   }
00195 
00196   return ref._retn();
00197 }
00198 
00199 template< class T>
00200 void
00201 NamingHelper<T>::bind( CosNaming::NamingContext_ptr nc,
00202                        const CosNaming::Name& n, CORBA::Object_ptr obj,
00203                        size_t tries, ACE_Time_Value timeout)
00204   throw ( CORBA::SystemException,
00205           CosNaming::NamingContext::NotFound,
00206           CosNaming::NamingContext::CannotProceed,
00207           CosNaming::NamingContext::InvalidName,
00208           CosNaming::NamingContext::AlreadyBound )
00209 {
00210   
00211   size_t i;
00212 
00213   ACE_DEBUG((LM_DEBUG, "Calling bind() with %d tries\n", tries));
00214   for(i = 0; i < tries; i++)
00215     {
00216       try
00217         {
00218           nc->bind(n, obj);  // bind the name
00219 
00220           break;     // an exception was not thrown,
00221           // success, get out of this loop
00222         }catch(const CORBA::SystemException& e){
00223           ACE_DEBUG((LM_DEBUG, "bind() threw exception\n"));
00224           e._tao_print_exception("");
00225           // binding threw an exception, try to recover
00226           if(i < tries)
00227             {
00228               // sleep, and try again in the next iteration
00229               // of this loop.  ACE_OS::sleep() does a select()
00230               // on a timeout, therefore is threadsafe
00231               ACE_OS::sleep(timeout);
00232               ACE_DEBUG((LM_DEBUG, "Retrying bind() of %s...%d\n",
00233                          n[0].id.in(), i));
00234             }    
00235           else{
00236             // we have exceed our tries,  rethrowing
00237             ACE_DEBUG((LM_DEBUG, "Rethrowing bind() exception\n"));
00238             throw;
00239           } 
00240         } catch (const CORBA::Exception& e) {
00241           ACE_DEBUG((LM_DEBUG, "bind() threw non-system exception\n"));
00242           e._tao_print_exception("");
00243           throw;
00244         }
00245     }
00246 }
00247 
00248 template< class T>
00249 void
00250 NamingHelper<T>::rebind( CosNaming::NamingContext_ptr nc,
00251                          const CosNaming::Name& n, CORBA::Object_ptr obj,
00252                          size_t tries, ACE_Time_Value timeout)
00253   throw ( CORBA::SystemException,
00254           CosNaming::NamingContext::NotFound,
00255           CosNaming::NamingContext::CannotProceed,
00256           CosNaming::NamingContext::InvalidName)
00257 {
00258   
00259   size_t i;
00260 
00261   for(i = 0; i < tries; i++)
00262     {
00263       try
00264         {
00265           nc->rebind(n, obj);  // rebind the name
00266 
00267           break;     // an exception was not thrown,
00268           // success, get out of this loop
00269         }catch(...){
00270           // rebinding threw an exception, try to recover
00271           if(i < tries)
00272             {
00273               // sleep, and try again in the next iteration
00274               // of this loop.  ACE_OS::sleep() does a select()
00275               // on a timeout, therefore is threadsafe
00276               ACE_OS::sleep(timeout);
00277               ACE_DEBUG((LM_DEBUG, "Retrying...%d\n", i));
00278             }    
00279           else{
00280             // we have exceed our tries,  rethrowing
00281             ACE_DEBUG((LM_DEBUG, "Rethrowing rebind() exception\n"));
00282             throw;
00283           } 
00284         }
00285     }
00286 }
00287 
00288 #endif   /* NAMING_HELPER_T_H */
00289 

Generated on Mon Dec 1 16:29:06 2003 for CPU Broker by doxygen 1.3.4