Salome HOME
Moved some functionality to VTKViewer_Utilities.h
[modules/kernel.git] / src / NamingService / SALOME_NamingService.cxx
index c4865c196ba759a4d4f49d11eaed14b22d7cf323..e939d53418a1be1bff596f676c1f5d4aea076a68 100644 (file)
 //  Module : SALOME
 //  $Header$
 
-using namespace std;
 #include "SALOME_NamingService.hxx"
 #include "ServiceUnreachable.hxx"
+
+#include "utilities.h"
+
 #include <iostream>
 #include <cstring>
 
+using namespace std;
+
 //----------------------------------------------------------------------
 /*! Function : SALOME_NamingService
  *  Purpose  : Constructor and Initialisation of _root_context
@@ -183,7 +187,7 @@ void SALOME_NamingService::Register(CORBA::Object_ptr ObjRef,
                      _temp_context =
                        _current_context->bind_new_context(_context_name);
                      _current_context = _temp_context;
-                     //MESSAGE("This context was'nt created, it's now done");
+                     //INFOS("This context was'nt created, it's now done");
                    }
                }
            }
@@ -456,7 +460,7 @@ bool SALOME_NamingService::Create_Directory(const char* Path)
              _temp_context =
                _current_context->bind_new_context(_context_name);
              _current_context = _temp_context;
-             MESSAGE("This context was'nt created, it's now done");
+             INFOS("This context was'nt created, it's now done");
                }
        }
     }
@@ -563,31 +567,31 @@ bool SALOME_NamingService::Change_Directory(const char* Path)
          _return_code = false;
          CosNaming::Name n = ex.rest_of_name;
          if (ex.why == CosNaming::NamingContext::missing_node)
-           MESSAGE( "Change_Directory() : " << (char *) n[0].id
+           INFOS( "Change_Directory() : " << (char *) n[0].id
                  << " (" << (char *) n[0].kind << ") not found")
          if (ex.why == CosNaming::NamingContext::not_context)
-           MESSAGE("Change_Directory() : " << (char *) n[0].id
+           INFOS("Change_Directory() : " << (char *) n[0].id
                 << " (" << (char *) n[0].kind
                 << ") is not a context" )
          if (ex.why == CosNaming::NamingContext::not_object)
-           MESSAGE( "Change_Directory() : " << (char *) n[0].id
+           INFOS( "Change_Directory() : " << (char *) n[0].id
                 << " (" << (char *) n[0].kind
                 << ") is not an object" )
        }
       catch(CosNaming::NamingContext::CannotProceed&)
        {
          _return_code = false;
-         MESSAGE( "!!!Change_Directory() : CosNaming::NamingContext::CannotProceed" )
+         INFOS( "!!!Change_Directory() : CosNaming::NamingContext::CannotProceed" )
        } 
       catch(CosNaming::NamingContext::InvalidName&) 
        {
          _return_code = false;
-         MESSAGE( "!!!Change_Directory() : CosNaming::NamingContext::InvalidName" )
+         INFOS( "!!!Change_Directory() : CosNaming::NamingContext::InvalidName" )
        }
       catch(CORBA::COMM_FAILURE&)
        {
          _return_code = false;
-         MESSAGE( "!!!Change_Directory() :CORBA::COMM_FAILURE : unable to contact"
+         INFOS( "!!!Change_Directory() :CORBA::COMM_FAILURE : unable to contact"
               << "the naming service")
          throw ServiceUnreachable();
        }
@@ -631,7 +635,7 @@ char* SALOME_NamingService::Current_Directory()
     }
   catch(CORBA::COMM_FAILURE&)
     {
-      MESSAGE("!!!Current_Directory(): CORBA::COMM_FAILURE : unable to contact"
+      INFOS("!!!Current_Directory(): CORBA::COMM_FAILURE : unable to contact"
           << " the naming service" )
       throw ServiceUnreachable();
     }
@@ -641,7 +645,7 @@ char* SALOME_NamingService::Current_Directory()
       // the directories
       length_path = length_path + strlen(result_path[k]) + 1;
     }
-  char* return_Path = new char[length_path +1];
+  char* return_Path = new char[length_path +2];
   return_Path[0] = '/' ;
   return_Path[1] = '\0' ;
   for (int k = 0 ; k <i ;k++) 
@@ -685,7 +689,7 @@ void SALOME_NamingService::list()
        }
       catch (ServiceUnreachable&)
        {
-         MESSAGE( "!!!list(): ServiceUnreachable" )
+         INFOS( "!!!list(): ServiceUnreachable" )
          throw ServiceUnreachable(); 
        }
 
@@ -733,6 +737,23 @@ vector<string> SALOME_NamingService::list_directory()
   return _list;
 }
 
+//----------------------------------------------------------------------
+/*! Function : list_directory_recurs
+ *  Purpose  : method to get all the contexts contained in the current 
+ *             directory
+ *             Get only objects and is recursive 
+ *  If the NamingService is out, the exception ServiceUnreachable is thrown
+ */ 
+//----------------------------------------------------------------------
+vector<string> SALOME_NamingService::list_directory_recurs()
+    throw(ServiceUnreachable)
+{
+  vector<string> _list ;
+  char *currentDir=Current_Directory();
+  _list_directory_recurs(_list,0,currentDir);
+  delete [] currentDir;
+  return _list;
+}
 
 //----------------------------------------------------------------------
 /*! Function : Destroy_Name 
@@ -777,28 +798,28 @@ void SALOME_NamingService::Destroy_Name(const char* Path)
        {
          CosNaming::Name n = ex.rest_of_name;
          if (ex.why == CosNaming::NamingContext::missing_node)
-           MESSAGE(  "Destroy_Name() : " << (char *) n[0].id
+           INFOS(  "Destroy_Name() : " << (char *) n[0].id
                  << " (" << (char *) n[0].kind << ") not found" )
          if (ex.why == CosNaming::NamingContext::not_context)
-           MESSAGE( "Destroy_Name() : " << (char *) n[0].id
+           INFOS( "Destroy_Name() : " << (char *) n[0].id
                 << " (" << (char *) n[0].kind
                 << ") is not a context" )
          if (ex.why == CosNaming::NamingContext::not_object)
-           MESSAGE( "Destroy_Name() : " << (char *) n[0].id
+           INFOS( "Destroy_Name() : " << (char *) n[0].id
                 << " ("  << (char *) n[0].kind
                 << ") is not an object" )
        } 
       catch (CosNaming::NamingContext::InvalidName &) 
        {
-         MESSAGE( "!!!Destroy_Name() : CosNaming::NamingContext::InvalidName" )
+         INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::InvalidName" )
        }
       catch (CosNaming::NamingContext::CannotProceed &)
        {
-         MESSAGE( "!!!Destroy_Name(): CosNaming::NamingContext::CannotProceed" )
+         INFOS( "!!!Destroy_Name(): CosNaming::NamingContext::CannotProceed" )
        }
       catch(CORBA::COMM_FAILURE&)
        {
-         MESSAGE( "!!!Destroy_Name() : CORBA::COMM_FAILURE : unable to contact"
+         INFOS( "!!!Destroy_Name() : CORBA::COMM_FAILURE : unable to contact"
               << " the naming service")
          throw ServiceUnreachable();
        }
@@ -821,28 +842,28 @@ void SALOME_NamingService::Destroy_Name(const char* Path)
     {
       CosNaming::Name n = ex.rest_of_name;
       if (ex.why == CosNaming::NamingContext::missing_node)
-       MESSAGE(  "Destroy_Name() : " << (char *) n[0].id
+       INFOS(  "Destroy_Name() : " << (char *) n[0].id
              << " (" << (char *) n[0].kind << ") not found" )
       if (ex.why == CosNaming::NamingContext::not_context)
-       MESSAGE( "Destroy_Name() : " << (char *) n[0].id
+       INFOS( "Destroy_Name() : " << (char *) n[0].id
             << " (" << (char *) n[0].kind
             << ") is not a context" )
       if (ex.why == CosNaming::NamingContext::not_object)
-       MESSAGE( "Destroy_Name() : " << (char *) n[0].id
+       INFOS( "Destroy_Name() : " << (char *) n[0].id
             << " ("  << (char *) n[0].kind
             << ") is not an object" )
     }
   catch(CosNaming::NamingContext::CannotProceed&)
     {
-      MESSAGE( "!!!Destroy_Name() : CosNaming::NamingContext::CannotProceed")
+      INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::CannotProceed")
     } 
   catch(CosNaming::NamingContext::InvalidName&) 
     {
-      MESSAGE( "!!!Destroy_Name() : CosNaming::NamingContext::InvalidName")
+      INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::InvalidName")
     }
   catch(CORBA::COMM_FAILURE&)
     {
-      MESSAGE( "!!!Destroy_Name() :CORBA::COMM_FAILURE : unable to contact" 
+      INFOS( "!!!Destroy_Name() :CORBA::COMM_FAILURE : unable to contact" 
           << " the naming service") 
       throw ServiceUnreachable();
     }
@@ -900,28 +921,28 @@ void SALOME_NamingService::Destroy_Directory(const char* Path)
        {
          CosNaming::Name n = ex.rest_of_name;
          if (ex.why == CosNaming::NamingContext::missing_node)
-           MESSAGE(  "Destroy_Directory() : " << (char *) n[0].id
+           INFOS(  "Destroy_Directory() : " << (char *) n[0].id
                  << " (" << (char *) n[0].kind << ") not found")
          if (ex.why == CosNaming::NamingContext::not_context)
-           MESSAGE( "Destroy_Directory() : " << (char *) n[0].id
+           INFOS( "Destroy_Directory() : " << (char *) n[0].id
                 << " (" << (char *) n[0].kind
                 << ") is not a context" )
          if (ex.why == CosNaming::NamingContext::not_object)
-           MESSAGE( "Destroy_Directory() : " << (char *) n[0].id
+           INFOS( "Destroy_Directory() : " << (char *) n[0].id
                 << " ("  << (char *) n[0].kind
                 << ") is not an object" )
        } 
       catch (CosNaming::NamingContext::InvalidName &) 
        {
-         MESSAGE( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName" )
+         INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName" )
        }
       catch (CosNaming::NamingContext::CannotProceed &)
        {
-         MESSAGE("!!!Destroy_Directory(): CosNaming::NamingContext::CannotProceed" )
+         INFOS("!!!Destroy_Directory(): CosNaming::NamingContext::CannotProceed" )
        }
       catch(CORBA::COMM_FAILURE&)
        {
-         MESSAGE( "!!!Destroy_Directory() : CORBA::COMM_FAILURE : unable to contact"
+         INFOS( "!!!Destroy_Directory() : CORBA::COMM_FAILURE : unable to contact"
               << " the naming service" )
          throw ServiceUnreachable();
        }
@@ -944,28 +965,28 @@ void SALOME_NamingService::Destroy_Directory(const char* Path)
     {
       CosNaming::Name n = ex.rest_of_name;
       if (ex.why == CosNaming::NamingContext::missing_node)
-       MESSAGE(  "Destroy_Directory() : " << (char *) n[0].id
+       INFOS(  "Destroy_Directory() : " << (char *) n[0].id
              << " (" << (char *) n[0].kind << ") not found" )
       if (ex.why == CosNaming::NamingContext::not_context)
-       MESSAGE( "Destroy_Directory() : " << (char *) n[0].id
+       INFOS( "Destroy_Directory() : " << (char *) n[0].id
             << " (" << (char *) n[0].kind
             << ") is not a context" )
       if (ex.why == CosNaming::NamingContext::not_object)
-       MESSAGE( "Destroy_Directory() : " << (char *) n[0].id
+       INFOS( "Destroy_Directory() : " << (char *) n[0].id
             << " ("  << (char *) n[0].kind
             << ") is not an object" )
     } 
   catch (CosNaming::NamingContext::InvalidName &) 
     {
-      MESSAGE( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName" )
+      INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName" )
     }
   catch (CosNaming::NamingContext::CannotProceed &)
     {
-      MESSAGE( "!!!Destroy_Directory(): CosNaming::NamingContext::CannotProceed" )
+      INFOS( "!!!Destroy_Directory(): CosNaming::NamingContext::CannotProceed" )
     }
   catch(CORBA::COMM_FAILURE&)
     {
-      MESSAGE( "!!!Destroy_Directory() : CORBA::COMM_FAILURE : unable to contact"
+      INFOS( "!!!Destroy_Directory() : CORBA::COMM_FAILURE : unable to contact"
           << " the naming service" )
       throw ServiceUnreachable();
     }
@@ -979,12 +1000,12 @@ void SALOME_NamingService::Destroy_Directory(const char* Path)
     }
   catch(CosNaming::NamingContext::NotEmpty&)
     {
-      MESSAGE( "!!!Destroy_Directory() : CosNaming::NamingContext::NoEmpty "
+      INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::NoEmpty "
           << Path << " is not empty" )
     } 
   catch(CORBA::COMM_FAILURE&)
     {
-      MESSAGE( "!!!Destroy_Directory() :CORBA::COMM_FAILURE : "
+      INFOS( "!!!Destroy_Directory() :CORBA::COMM_FAILURE : "
           << "unable to contact the naming service") 
       throw ServiceUnreachable();
     }
@@ -999,28 +1020,28 @@ void SALOME_NamingService::Destroy_Directory(const char* Path)
     {
       CosNaming::Name n = ex.rest_of_name;
       if (ex.why == CosNaming::NamingContext::missing_node)
-       MESSAGE(  "Destroy_Directory() : " << (char *) n[0].id
+       INFOS(  "Destroy_Directory() : " << (char *) n[0].id
              << " (" << (char *) n[0].kind << ") not found" )
       if (ex.why == CosNaming::NamingContext::not_context)
-       MESSAGE( "Destroy_Directory() : " << (char *) n[0].id
+       INFOS( "Destroy_Directory() : " << (char *) n[0].id
             << " (" << (char *) n[0].kind
             << ") is not a context" )
       if (ex.why == CosNaming::NamingContext::not_object)
-       MESSAGE( "Destroy_Directory() : " << (char *) n[0].id
+       INFOS( "Destroy_Directory() : " << (char *) n[0].id
             << " ("  << (char *) n[0].kind
             << ") is not an object" )
     }
   catch(CosNaming::NamingContext::CannotProceed&)
     {
-      MESSAGE( "!!!Destroy_Directory() : CosNaming::NamingContext::CannotProceed")
+      INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::CannotProceed")
     } 
   catch(CosNaming::NamingContext::InvalidName&) 
     {
-      MESSAGE( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName")
+      INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName")
     }
   catch(CORBA::COMM_FAILURE&)
     {
-      MESSAGE( "!!!Destroy_Directory() :CORBA::COMM_FAILURE : unable to contact" 
+      INFOS( "!!!Destroy_Directory() :CORBA::COMM_FAILURE : unable to contact" 
           << " the naming service") 
       throw ServiceUnreachable();
     }
@@ -1306,4 +1327,62 @@ SALOME_NamingService::_current_directory(char** result_path,
   // We go to the last directory where an occurence was found
   _current_context = _ref_context ; 
 }
+
+
+//----------------------------------------------------------------------
+/*! Function :_list_directory_recurs.
+ * Purpose  : method to list recursively all the objects contained in the tree of absCurDirectory/relativeSubDir.
+ *  \param myList The list that will be filled.
+ *  \param relativeSubDir The directory from absCurDirectory in which the objects are found.
+ *  \param absCurDirectory The directory in ABSOLUTE form.
+ *  _current_context must refer to absCurDirectory.
+ */
+//----------------------------------------------------------------------
+void SALOME_NamingService::_list_directory_recurs(vector<string>& myList, const char *relativeSubDir,const char *absCurDirectory)
+{
+  CosNaming::BindingList_var _binding_list;
+  CosNaming::BindingIterator_var _binding_iterator;
+  unsigned long nb=0 ; // for using only the BindingIterator to access the bindings
+  CosNaming::Binding_var _binding ;
+  char *absDir;
+
+  CosNaming::NamingContext_var _ref_context = _current_context;
+  if(relativeSubDir)
+    {
+      Change_Directory(relativeSubDir);
+      absDir=new char[strlen(absCurDirectory)+2+strlen(relativeSubDir)];
+      strcpy(absDir,absCurDirectory);
+      strcat(absDir,relativeSubDir);
+      strcat(absDir,"/");
+    }
+  else
+    absDir=(char *)absCurDirectory;
+  _current_context->list(nb, _binding_list, _binding_iterator) ;
+
+  while (_binding_iterator->next_one(_binding)) {
+    CosNaming::Name _bindingName = _binding->binding_name;
+    if (_binding->binding_type == CosNaming::ncontext) {
+      _list_directory_recurs(myList,_bindingName[0].id,absDir);
+    }
+    else if (_binding->binding_type == CosNaming::nobject) {
+      char *elt=new char[strlen(absDir)+2+strlen(_bindingName[0].id)];
+      strcpy(elt,absDir);
+      strcat(elt,_bindingName[0].id);
+      myList.push_back(elt);
+      delete [] elt;
+    }
+  }
+  if(relativeSubDir)
+    {
+      _current_context = _ref_context ;
+      delete [] absDir;
+    }
+
+  _binding_iterator->destroy();
+}
+
 //----------------------------------------------------------------------
+
+char * SALOME_NamingService::getIORaddr(){
+   return _orb->object_to_string(_root_context);
+}