//******************************************************************************
//
// Restricted Rights
// WARNING: This is a restricted distribution Zeligsoft file.
//
//******************************************************************************
//
// Copyright (C) 2006 Zeligsoft Inc. - All Rights Reserved
//

//******************************************************************************
//
//     FILE: zceOperatingEnvironment.cpp
//
//******************************************************************************


#include "zceOperatingEnvironment.h"

#include "ace/SString.h"

#include <ctype.h>

zceStatus _status;

CORBA::ORB_var OperatingEnvironment::orb_ = CORBA::ORB::_nil();

CORBA::Boolean OperatingEnvironment::AnyToSequence(const CORBA::Any& any, PortTypes::BooleanSequence& to)
{
	CORBA::Boolean result = false;

	const PortTypes::BooleanSequence* constToPtr = &to;
	result = (any >>= constToPtr);
	return result;
}

CORBA::Boolean OperatingEnvironment::AnyToSequence(const CORBA::Any& any, PortTypes::CharSequence& to)
{
	CORBA::Boolean result = false;

	const PortTypes::CharSequence* constToPtr = &to;
	result = (any >>= constToPtr);
	return result;
}

CORBA::Boolean OperatingEnvironment::AnyToSequence(const CORBA::Any& any, PortTypes::DoubleSequence& to)
{
	CORBA::Boolean result = false;

	const PortTypes::DoubleSequence* constToPtr = &to;
	result = (any >>= constToPtr);
	return result;
}

CORBA::Boolean OperatingEnvironment::AnyToSequence(const CORBA::Any& any, PortTypes::FloatSequence& to)
{
	CORBA::Boolean result = false;

	const PortTypes::FloatSequence* constToPtr = &to;
	result = (any >>= constToPtr);
	return result;
}

CORBA::Boolean OperatingEnvironment::AnyToSequence(const CORBA::Any& any, PortTypes::ShortSequence& to)
{
	CORBA::Boolean result = false;

	const PortTypes::ShortSequence* constToPtr = &to;
	result = (any >>= constToPtr);
	return result;
}

CORBA::Boolean OperatingEnvironment::AnyToSequence(const CORBA::Any& any, PortTypes::LongSequence& to)
{
	CORBA::Boolean result = false;

	const PortTypes::LongSequence* constToPtr = &to;
	result = (any >>= constToPtr);
	return result;
}

CORBA::Boolean OperatingEnvironment::AnyToSequence(const CORBA::Any& any, CF::OctetSequence& to)
{
	CORBA::Boolean result = false;

	const CF::OctetSequence* constToPtr = &to;
	result = (any >>= constToPtr);
	return result;
}

CORBA::Boolean OperatingEnvironment::AnyToSequence(const CORBA::Any& any, CF::StringSequence& to)
{
	CORBA::Boolean result = false;

	const CF::StringSequence* constToPtr = &to;
	result = (any >>= constToPtr);
	return result;
}

CORBA::Boolean OperatingEnvironment::AnyToSequence(const CORBA::Any& any, PortTypes::UlongSequence& to)
{
	CORBA::Boolean result = false;

	const PortTypes::UlongSequence* constToPtr = &to;
	result = (any >>= constToPtr);
	return result;
}

CORBA::Boolean OperatingEnvironment::AnyToSequence(const CORBA::Any& any, PortTypes::UshortSequence& to)
{
	CORBA::Boolean result = false;

	const PortTypes::UshortSequence* constToPtr = &to;
	result = (any >>= constToPtr);
	return result;
}

CORBA::Boolean OperatingEnvironment::AnyToSequence(const CORBA::Any& any, CosLwLog::LogLevelSequence& to)
{
	CORBA::Boolean result = false;

	const CosLwLog::LogLevelSequence* constToPtr = &to;
	result = (any >>= constToPtr);
	return result;
}

void OperatingEnvironment::AnyFromSequence(CORBA::Any& any, PortTypes::BooleanSequence& from)
{
	any <<= from;
}

void OperatingEnvironment::AnyFromSequence(CORBA::Any& any, PortTypes::CharSequence& from)
{
	any <<= from;
}

void OperatingEnvironment::AnyFromSequence(CORBA::Any& any, PortTypes::DoubleSequence& from)
{
	any <<= from;
}

void OperatingEnvironment::AnyFromSequence(CORBA::Any& any, PortTypes::FloatSequence& from)
{
	any <<= from;
}

void OperatingEnvironment::AnyFromSequence(CORBA::Any& any, PortTypes::ShortSequence& from)
{
	any <<= from;
}

void OperatingEnvironment::AnyFromSequence(CORBA::Any& any, PortTypes::LongSequence& from)
{
	any <<= from;
}

void OperatingEnvironment::AnyFromSequence(CORBA::Any& any, CF::OctetSequence& from)
{
	any <<= from;
}

void OperatingEnvironment::AnyFromSequence(CORBA::Any& any, CF::StringSequence& from)
{
	any <<= from;
}

void OperatingEnvironment::AnyFromSequence(CORBA::Any& any, PortTypes::UlongSequence& from)
{
	any <<= from;
}

void OperatingEnvironment::AnyFromSequence(CORBA::Any& any, PortTypes::UshortSequence& from)
{
	any <<= from;
}

void OperatingEnvironment::AnyFromSequence(CORBA::Any& any, CosLwLog::LogLevelSequence& from)
{
	any <<= from;
}

std::string IfIsDCEUUIDConvertToUpper( std::string candidate)
{

	if (candidate.length()!=40)
		{
			return candidate;
		}

	for (unsigned int i = 0; i < candidate.length(); i+=1) 
		{
			if (i==0 && candidate[i]!='D') 
	{
		return candidate;
	}
			else if (i==0 && candidate[i]=='D') 
	{
		continue;
	}
			else if (i==1 && candidate[i]!='C') 
	{
		return candidate;
	}
			else if (i==1 && candidate[i]=='C') 
	{
		continue;
	}
			else if (i==2 && candidate[i]!='E') 
	{
		return candidate;
	}
			else if (i==2 && candidate[i]=='E') 
	{
		continue;
	}
			else if (i==3 && candidate[i]!=':') 
	{
		return candidate;
	}
			else if (i==3 && candidate[i]==':') 
	{
		continue;
	}
			else if ( (i==12 || i==17 || i ==22 || i==27) && candidate[i]!='-') 
	{
		return candidate;
	}
			 else if ( (i==12 || i==17 || i ==22 || i==27) && candidate[i]=='-') 
	{
		continue;
	}
			else 
	{
		char c = candidate[i];
		switch (c) 
			{
			case 'a':
			case 'b':
			case 'c':
			case 'd':
			case 'e':
			case 'f':
			case 'A':
			case 'B':
			case 'C':
			case 'D':
			case 'E':
			case 'F':
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
				{
		break;
				}
			default:
				{
		return candidate;
				}			
			}
	}
		}

	std::string newstring = candidate;
	for (unsigned int j=0; j<newstring.length(); ++j)
		{
			newstring[j] = (char) toupper(newstring[j]);
		}


	return newstring;
	

}


void convert_set_to_string(std::set<std::string>::iterator begin, std::set<std::string>::iterator end, std::string& result)
{
	result += "{";

	for(std::set<std::string>::iterator p = begin; p != end; ++p)
	{
		if(p != begin) result += ", ";
		result += *p;
	}

	result += "}";
}

void convert_map_to_string(std::map<std::string, std::string>::iterator begin, std::map<std::string, std::string>::iterator end, std::string& result)
{
	result += "{";

	for(std::map<std::string, std::string>::iterator p = begin; p != end; ++p)
	{
		if(p != begin) result += ", ";
		result += "(" + p->first + ", " + p->second + ")";
	}

	result += "}";
}

int str_case_cmp(const char* str1, const char* str2)
{
	const char *char1 = str1, *char2 = str2;

	while((*char1 != '\0') && (tolower((int)(*char1)) == tolower((int)(*char2))))
	{
		++char1;
		++char2;
	}

	return tolower(*char1) - tolower(*char2);
}

void get_host_ip_address(char* pszBuffer)
{
	//To do
	strcpy(pszBuffer, "127.0.0.1");
	return;
}

void exception_to_string(const CORBA::Exception& e, std::string& exString)
{
	ACE_CString aceString = e._info();
	exString = aceString.fast_rep();
}

void CtrlHandler(int iSignal)
{
	//We'll take care of shutting down ourselves
	while(_status != ZCE_STATUS_FINISHED)
	{
		if((_status != ZCE_STATUS_SHUTTINGDOWN))
		{
			Shutdown_Thread(NULL);
		}
		sleep(1);  // 1 sec
	}
	return; //Prevent default behaviour to occur
}

void* Shutdown_Thread(void* data)
{
		ZCE_EXC_ENV;

	if(_status == ZCE_STATUS_RUNNING)
	{
		_status = ZCE_STATUS_SHUTTINGDOWN;
		sleep(1);  // 1 sec

		ZCE_EXC_TRY
		{
			OperatingEnvironment::orb_->shutdown(false);
		}
		ZCE_EXC_CATCHANY
		{
			std::cerr << "zceOperatingEnvironment : Exception caught during orb->shutdown()." << std::endl;
			return NULL;
		}
		ZCE_EXC_ENDTRY(NULL)
	}

	return NULL;
}

// End-Of-File
//------------------------------------------------------------------------------
