<commit_before>/*******************************************************************************
*    ALMA - Atacama Large Millimiter Array
*    (c) European Southern Observatory, 2002
*    Copyright by ESO (in the framework of the ALMA collaboration)
*    and Cosylab 2002, All rights reserved
*
*    This library is free software; you can redistribute it and/or
*    modify it under the terms of the GNU Lesser General Public
*    License as published by the Free Software Foundation; either
*    version 2.1 of the License, or (at your option) any later version.
*
*    This library is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*    Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public
*    License along with this library; if not, write to the Free Software
*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*
* "@(#) $Id: loggingHelper.cpp,v 1.43 2005/08/29 08:42:57 vwang Exp $"
*
* who       when        what
* --------  ----------  ----------------------------------------------
* msekoran  2003-05-23  removed exponential backoff
* msekoran  2001/12/23  created 
*/

#include <vltPort.h>
#include <loggingHelper.h>

#include <ace/SString.h>

#include <acsutilPorts.h>
#include <tao/corba.h>

#include <logging.h>

#define LOG_NAMESERVICE_REFERENCE  "NAMESERVICE_REFERENCE"

bool LoggingHelper::m_terminate = false;

void 
LoggingHelper::terminateResolving(bool terminate)
{
  m_terminate = terminate;
}

CosNaming::NamingContext_ptr
LoggingHelper::resolveNameService(CORBA::ORB_ptr orb,
			       int retries, unsigned int secTimeout)
{
    //ACS_TRACE("logging::LoggingHelper::resolveNameService");

  if (CORBA::is_nil(orb))
    return CosNaming::NamingContext::_nil();

  // use CORBA::ORB::resolve_intial_references
  
  try
    {
      CORBA::Object_var naming_obj =
        orb->resolve_initial_references ("NameService");
      

      if (!CORBA::is_nil (naming_obj.in ()))
	{
	  CosNaming::NamingContext_var naming_context =
	    CosNaming::NamingContext::_narrow (naming_obj.in ()
					       );
	  

	  if (naming_context.ptr() != CosNaming::NamingContext::_nil())
	    return naming_context._retn();
	}
    }
  catch(...)
    {
       ACS_SHORT_LOG((LM_ERROR, "(logging::LoggingHelper::resolveNameService) CORBA exception caught!"));
    }

  //ACS_LOG(LM_RUNTIME_CONTEXT, "logging::LoggingHelper::resolveNameService",
  //  (LM_DEBUG,"Unable to resolve NameService reference using CORBA::ORB::resolve_intial_references()."));

  // Environment variable LOG_NAMESERVICE_REFERENCE
  ACE_TCHAR * envRef = ACE_OS::getenv (LOG_NAMESERVICE_REFERENCE);
  if (envRef && *envRef)
    {
    //ACS_LOG(0, "logging::LoggingHelper::resolveNameService",
    //      (LM_INFO, "NameService reference obtained via environment: '%s'", envRef));
      
      // return reference
      return resolveNameService(orb, envRef, retries, secTimeout);
    }


  // corbaloc::<hostname>:<port>/NameService
  const char* hostname = 0;
  hostname = ACSPorts::getIP();
  if (hostname==0)
    {
    //ACS_LOG(LM_RUNTIME_CONTEXT, "logging::LoggingHelper::resolveNameService",
    //      (LM_ERROR, "Failed to obtain localhost address!"));

      return CosNaming::NamingContext::_nil();
    }

  ACE_TCHAR corbalocRef[240];
  ACE_OS::sprintf(corbalocRef, "corbaloc::%s:%s/NameService", hostname, ACSPorts::getNamingServicePort().c_str());

  //ACS_LOG(0, "logging::LoggingHelper::resolveNameService",
  //  (LM_INFO, "NameService reference generated using localhost address: '%s'", corbalocRef));
  
  // return reference
  return resolveNameService(orb, corbalocRef, retries, secTimeout);
 
}

CosNaming::NamingContext_ptr
LoggingHelper::resolveNameService(CORBA::ORB_ptr orb, 
			       const ACE_TCHAR * reference, 
			       int retries, unsigned int secTimeout)
{
    //ACS_TRACE("logging::LoggingHelper::resolveNameService");
  
  if (!reference || CORBA::is_nil(orb))
    return CosNaming::NamingContext::_nil();
 
  //ACS_DEBUG_PARAM("logging::LoggingHelper::resolveNameService", "Resolving reference: '%s'", reference);

  unsigned int secsToWait = 3, secsWaited = 0;
  int retried = 0;
  CosNaming::NamingContext_var ref = CosNaming::NamingContext::_nil();
      
  while (!m_terminate)
    {
    try
    	{
	  
	  CORBA::Object_var obj = orb->string_to_object(reference);
	  
	      
	  ref = CosNaming::NamingContext::_narrow(obj.in());
	  

	  //ACS_DEBUG("logging::LoggingHelper::resolveNameService", "NameService reference narrowed.");

	  return ref._retn(); 
	}
    catch(...)
	{
	//ACE_PRINT_EXCEPTION(ACE_ANY_EXCEPTION, "logging::LoggingHelper::resolveNameService");
	  ref = CosNaming::NamingContext::_nil();
	}
         
      if (
	  ((secTimeout != 0) && (secsWaited >= secTimeout)) || 
	  ((retries > 0) && (retried >= retries))
	  )
	break;
      else
	ACE_OS::sleep(secsToWait);
      
      secsWaited += secsToWait;

      retried++;
      
    }

  return CosNaming::NamingContext::_nil();
}



// ************************************************************************
//
// REVISION HISTORY:
//
// $Log: loggingHelper.cpp,v $
// Revision 1.43  2005/08/29 08:42:57  vwang
// fix problem about return str().c_str()
//
// Revision 1.42  2004/10/27 22:06:48  dfugate
// Removed CORBA::string_free's related to ACSPorts::getIP as these are no longer needed.
//
// Revision 1.41  2004/10/14 20:46:18  gchiozzi
// Cleaned up logging messages:
// - uses now only ACS_* macros and not any more ACE_* macros
// - Removed all new line characters
// - fixed problem with ACS logging that was not putting proper new line
//   characters when logging messages on stdout BEFORE the logging itself
//   was initialised.
//
// Revision 1.40  2004/03/25 22:20:09  dfugate
// Use ACSPorts::getIP instead of ACE_OS::hostname function.
//
// Revision 1.39  2004/03/17 07:38:33  bjeram
// ported to ACE 5.4 and TAO 1.4
//
// Revision 1.38  2003/10/24 19:27:07  dfugate
// Fixed a few serious bugs and now use native exceptions.
//
// Revision 1.37  2003/10/23 07:39:08  acaproni
// True native exception handling. No more extra parameters
//
// Revision 1.36  2003/10/15 22:19:08  dfugate
// Use ACSPorts class to gain access to dynamic port numbers of CORBA processes.<commit_msg>solved resolveNameService that it does not use multicast for finding NameService + improved error hansling a bit<commit_after>/*******************************************************************************
*    ALMA - Atacama Large Millimiter Array
*    (c) European Southern Observatory, 2002
*    Copyright by ESO (in the framework of the ALMA collaboration)
*    and Cosylab 2002, All rights reserved
*
*    This library is free software; you can redistribute it and/or
*    modify it under the terms of the GNU Lesser General Public
*    License as published by the Free Software Foundation; either
*    version 2.1 of the License, or (at your option) any later version.
*
*    This library is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*    Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public
*    License along with this library; if not, write to the Free Software
*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*
* "@(#) $Id: loggingHelper.cpp,v 1.44 2007/05/23 08:59:11 bjeram Exp $"
*
* who       when        what
* --------  ----------  ----------------------------------------------
* msekoran  2003-05-23  removed exponential backoff
* msekoran  2001/12/23  created 
*/

#include <vltPort.h>
#include <loggingHelper.h>

#include <ace/SString.h>

#include <acsutilPorts.h>
#include <tao/corba.h>

#include <logging.h>

#define LOG_NAMESERVICE_REFERENCE  "NAMESERVICE_REFERENCE"

bool LoggingHelper::m_terminate = false;

void 
LoggingHelper::terminateResolving(bool terminate)
{
  m_terminate = terminate;
}

CosNaming::NamingContext_ptr
LoggingHelper::resolveNameService(CORBA::ORB_ptr orb,
			       int retries, unsigned int secTimeout)
{
    //ACS_TRACE("logging::LoggingHelper::resolveNameService");

  if (CORBA::is_nil(orb))
    return CosNaming::NamingContext::_nil();

  // use CORBA::ORB::resolve_intial_references
  
  try
    {
    // first we check if we have if we have initalize reference for NameService
    CORBA::ORB::ObjectIdList_ptr svcsList = 
	orb->list_initial_services();
    // ... we can start with 12th if exsist, because from 0 to 11 are "hardcoded" SVCS 
    for(unsigned int i=12; i<svcsList->length(); i++)
	{
	ACE_CString o ((*svcsList)[i]);
	if (o.find("NameService")!=ACE_CString::npos)
	    {
	    // printf("Svc %d = %s\n", i, o.c_str());
	    // we find NameService so we can use resolve_initial_references w/o using multicast for discovering NameService
	    CORBA::Object_var naming_obj =
		orb->resolve_initial_references ("NameService");
      

	    if (!CORBA::is_nil (naming_obj.in ()))
		{
		CosNaming::NamingContext_var naming_context =
		    CosNaming::NamingContext::_narrow (naming_obj.in ()
			);
	  

		if (naming_context.ptr() != CosNaming::NamingContext::_nil())
		    return naming_context._retn();
		}
	    }//if
	}//for
    
    }
  catch(CORBA::SystemException &ex)
      {
      ACE_PRINT_EXCEPTION(ex, "logging::LoggingHelper::resolveNameService");
      ACS_SHORT_LOG((LM_WARNING, "(logging::LoggingHelper::resolveNameService) CORBA exception caught! - will try to use other possiblities"));
      }
  catch(...)
    {
       ACS_SHORT_LOG((LM_WARNING, "(logging::LoggingHelper::resolveNameService) unknown exception caught! - will try to use other possiblities"));
    }

  //ACS_LOG(LM_RUNTIME_CONTEXT, "logging::LoggingHelper::resolveNameService",
  //  (LM_DEBUG,"Unable to resolve NameService reference using CORBA::ORB::resolve_intial_references()."));

  // Environment variable LOG_NAMESERVICE_REFERENCE
  ACE_TCHAR * envRef = ACE_OS::getenv (LOG_NAMESERVICE_REFERENCE);
  if (envRef && *envRef)
    {
    //ACS_LOG(0, "logging::LoggingHelper::resolveNameService",
    //      (LM_INFO, "NameService reference obtained via environment: '%s'", envRef));
      
      // return reference
      return resolveNameService(orb, envRef, retries, secTimeout);
    }


  // corbaloc::<hostname>:<port>/NameService
  const char* hostname = 0;
  hostname = ACSPorts::getIP();
  if (hostname==0)
    {
    //ACS_LOG(LM_RUNTIME_CONTEXT, "logging::LoggingHelper::resolveNameService",
    //      (LM_ERROR, "Failed to obtain localhost address!"));

      return CosNaming::NamingContext::_nil();
    }

  ACE_TCHAR corbalocRef[240];
  ACE_OS::sprintf(corbalocRef, "corbaloc::%s:%s/NameService", hostname, ACSPorts::getNamingServicePort().c_str());

  //ACS_LOG(0, "logging::LoggingHelper::resolveNameService",
  //  (LM_INFO, "NameService reference generated using localhost address: '%s'", corbalocRef));
  
  // return reference
  return resolveNameService(orb, corbalocRef, retries, secTimeout);
 
}

CosNaming::NamingContext_ptr
LoggingHelper::resolveNameService(CORBA::ORB_ptr orb, 
			       const ACE_TCHAR * reference, 
			       int retries, unsigned int secTimeout)
{
    //ACS_TRACE("logging::LoggingHelper::resolveNameService");
  
  if (!reference || CORBA::is_nil(orb))
    return CosNaming::NamingContext::_nil();
 
  //ACS_DEBUG_PARAM("logging::LoggingHelper::resolveNameService", "Resolving reference: '%s'", reference);

  unsigned int secsToWait = 3, secsWaited = 0;
  int retried = 0;
  CosNaming::NamingContext_var ref = CosNaming::NamingContext::_nil();
      
  while (!m_terminate)
    {
    try
    	{
	  
	  CORBA::Object_var obj = orb->string_to_object(reference);
	  
	      
	  ref = CosNaming::NamingContext::_narrow(obj.in());
	  

	  //ACS_DEBUG("logging::LoggingHelper::resolveNameService", "NameService reference narrowed.");

	  return ref._retn(); 
	}
    catch(...)
	{
	//ACE_PRINT_EXCEPTION(ACE_ANY_EXCEPTION, "logging::LoggingHelper::resolveNameService");
	  ref = CosNaming::NamingContext::_nil();
	}
         
      if (
	  ((secTimeout != 0) && (secsWaited >= secTimeout)) || 
	  ((retries > 0) && (retried >= retries))
	  )
	break;
      else
	ACE_OS::sleep(secsToWait);
      
      secsWaited += secsToWait;

      retried++;
      
    }

  return CosNaming::NamingContext::_nil();
}



// ************************************************************************
//
// REVISION HISTORY:
//
// $Log: loggingHelper.cpp,v $
// Revision 1.44  2007/05/23 08:59:11  bjeram
// solved resolveNameService that it does not use multicast for finding NameService + improved error hansling a bit
//
// Revision 1.43  2005/08/29 08:42:57  vwang
// fix problem about return str().c_str()
//
// Revision 1.42  2004/10/27 22:06:48  dfugate
// Removed CORBA::string_free's related to ACSPorts::getIP as these are no longer needed.
//
// Revision 1.41  2004/10/14 20:46:18  gchiozzi
// Cleaned up logging messages:
// - uses now only ACS_* macros and not any more ACE_* macros
// - Removed all new line characters
// - fixed problem with ACS logging that was not putting proper new line
//   characters when logging messages on stdout BEFORE the logging itself
//   was initialised.
//
// Revision 1.40  2004/03/25 22:20:09  dfugate
// Use ACSPorts::getIP instead of ACE_OS::hostname function.
//
// Revision 1.39  2004/03/17 07:38:33  bjeram
// ported to ACE 5.4 and TAO 1.4
//
// Revision 1.38  2003/10/24 19:27:07  dfugate
// Fixed a few serious bugs and now use native exceptions.
//
// Revision 1.37  2003/10/23 07:39:08  acaproni
// True native exception handling. No more extra parameters
//
// Revision 1.36  2003/10/15 22:19:08  dfugate
// Use ACSPorts class to gain access to dynamic port numbers of CORBA processes.
{'fraction_non_alphanumeric': 0.14836264836264837, 'fraction_numerical': 0.0316030316030316, 'mean_word_length': 3.5783960720130934, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4224594', 'n_tokens_mistral': 4901, 'n_tokens_neox': 4414, 'n_words': 1156}