//
// generated by Qedo
//

#include "container_service_MonImpl.h"


// BEGIN USER INSERT SECTION file
// END USER INSERT SECTION file


namespace container_service {


// BEGIN USER INSERT SECTION MonExec
unsigned long
MonExec::get_new_event_number()
{
	Qedo::QedoLock lock (counter_mutex);
	counter++;
	return counter;
};

IOP::Codec_ptr
MonExec::get_cdr_codec_ptr()
{
	return m_cdrCodec.in();
}

// END USER INSERT SECTION MonExec


MonExec::MonExec()
{
// BEGIN USER INSERT SECTION MonExec::MonExec
	counter = 0;

	//codec

	int dummy = 0;
    CORBA::ORB_var orb = CORBA::ORB_init (dummy, 0);
	CORBA::Object_var obj = orb->resolve_initial_references ("CodecFactory");


	IOP::CodecFactory_var factory = IOP::CodecFactory::_narrow(obj);

    if ( CORBA::is_nil(factory) )
	{
		std::cout << "no CDR" << std::endl;
    	return;
	}

    /* 
	 * Create codec
	 */

    IOP::Encoding how;

    how.major_version = 1;
    how.minor_version = 0;
    how.format		  =	IOP::ENCODING_CDR_ENCAPS;

    try
    {
		m_cdrCodec = factory->create_codec(how);
    }
    catch(const IOP::CodecFactory::UnknownEncoding& _ex)
    {
		std::cout << "no CDR" << std::endl;
    }

// END USER INSERT SECTION MonExec::MonExec
}


MonExec::~MonExec()
{
// BEGIN USER INSERT SECTION MonExec::~MonExec
// END USER INSERT SECTION MonExec::~MonExec

}


void
MonExec::set_context(::container_service::CCM_monitor_ContextImpl_ptr context)
    throw (CORBA::SystemException, Components::CCMException)
{
    context_ = ::container_service::CCM_monitor_ContextImpl::_duplicate(context);
}


void
MonExec::configuration_complete()
    throw (CORBA::SystemException, Components::InvalidConfiguration)
{
// BEGIN USER INSERT SECTION MonExec::configuration_complete
	std::cout << "Monitorung Serivce: configuration_complete" << std::endl;

	Components::ContainerPortableInterceptor::ServerContainerInterceptorRegistration_ptr server_reg =
		context_->get_server_interceptor_dispatcher_registration();

	server_interceptor_ = new Qedo::ServerContainerInterceptor(context_,this);

	server_cookie_ = server_reg->register_server_interceptor(server_interceptor_);

	Components::ContainerPortableInterceptor::ClientContainerInterceptorRegistration_ptr client_reg =
		context_->get_client_interceptor_dispatcher_registration();

	client_interceptor_ = new Qedo::ClientContainerInterceptor(context_,this);

	client_reg->register_client_interceptor(client_interceptor_);
	
//	std::string component_id = "";
//	server_reg->register_interceptor_for_component(server_interceptor_, component_id.c_str());

//	context_->register_servant_locator_for_all(server_interceptor_);
// END USER INSERT SECTION MonExec::configuration_complete
}


void
MonExec::remove()
    throw (CORBA::SystemException)
{
// BEGIN USER INSERT SECTION MonExec::remove
	Components::ContainerPortableInterceptor::ServerContainerInterceptorRegistration_ptr server_reg =
	context_->get_server_interceptor_dispatcher_registration();

	server_reg->unregister_server_interceptor(server_cookie_);

	Components::ContainerPortableInterceptor::ClientContainerInterceptorRegistration_ptr client_reg =
	context_->get_client_interceptor_dispatcher_registration();

	client_reg->unregister_client_interceptor(client_cookie_);

// END USER INSERT SECTION MonExec::remove
}


// BEGIN USER INSERT SECTION MonImpl
// END USER INSERT SECTION MonImpl


MonImpl::MonImpl()
:component_(new MonExec())
{
// BEGIN USER INSERT SECTION MonImpl::MonImpl
// END USER INSERT SECTION MonImpl::MonImpl
}


MonImpl::~MonImpl()
{
// BEGIN USER INSERT SECTION MonImpl::~MonImpl
// END USER INSERT SECTION MonImpl::~MonImpl

    component_->_remove_ref();
}


::CORBA::Object*
MonImpl::obtain_executor(const char* name)
    throw (CORBA::SystemException)
{
    if (! strcmp ( name, "component" ) ) {
        return Components::EnterpriseComponent::_duplicate (component_);
    }
    
    return Components::EnterpriseComponent::_nil();
}


void
MonImpl::release_executor(::CORBA::Object_ptr executor)
    throw (CORBA::SystemException)
{
    CORBA::release (executor);
}


void
MonImpl::configuration_complete()
    throw (CORBA::SystemException, Components::InvalidConfiguration)
{
    component_->configuration_complete();

// BEGIN USER INSERT SECTION MonImpl::configuration_complete
//	register_container_interceptors(context_);
// END USER INSERT SECTION MonImpl::configuration_complete
}


void
MonImpl::set_extension_context(::Components::ExtensionContext_ptr context)
    throw (CORBA::SystemException, Components::CCMException)
{
    #ifdef TAO_ORB
    ::container_service::CCM_monitor_Context_ptr tmp_context;
    
    tmp_context = dynamic_cast<::container_service::CCM_monitor_ContextImpl*>(context);
    
    if (tmp_context)
        context_ = ::container_service::CCM_monitor_ContextImpl::_duplicate(tmp_context);
    else
        context_ = ::container_service::CCM_monitor_ContextImpl::_nil();
        
    #else
    context_ = ::container_service::CCM_monitor_ContextImpl::_narrow(context);
    
    #endif
    component_->set_context(context_);
}


void
MonImpl::ccm_activate()
    throw (CORBA::SystemException, Components::CCMException)
{
// BEGIN USER INSERT SECTION MonImpl::ccm_activate
// END USER INSERT SECTION MonImpl::ccm_activate
}


void
MonImpl::ccm_passivate()
    throw (CORBA::SystemException, Components::CCMException)
{
// BEGIN USER INSERT SECTION MonImpl::ccm_passivate
// END USER INSERT SECTION MonImpl::ccm_passivate
}


void
MonImpl::ccm_remove()
    throw (CORBA::SystemException, Components::CCMException)
{
// BEGIN USER INSERT SECTION MonImpl::ccm_remove
// END USER INSERT SECTION MonImpl::ccm_remove
}


// BEGIN USER INSERT SECTION HomeMonExec
// END USER INSERT SECTION HomeMonExec


HomeMonExec::HomeMonExec()
{
// BEGIN USER INSERT SECTION HomeMonExec::HomeMonExec
// END USER INSERT SECTION HomeMonExec::HomeMonExec
}


HomeMonExec::~HomeMonExec()
{
// BEGIN USER INSERT SECTION HomeMonExec::~HomeMonExec
// END USER INSERT SECTION HomeMonExec::~HomeMonExec

}


void
HomeMonExec::set_context(Components::HomeContext_ptr ctx)
    throw (CORBA::SystemException, Components::CCMException)
{
    context_ = Components::HomeContext::_duplicate(ctx);
    
}


::Components::EnterpriseComponent_ptr
HomeMonExec::create ()
    throw (CORBA::SystemException, Components::CreateFailure)
{
// BEGIN USER INSERT SECTION HomeMonExec::create
	std::cout << "create called." << std::endl;
// END USER INSERT SECTION HomeMonExec::create
    return new MonImpl();
}


};


//
// entry point
//
::Components::HomeExecutorBase_ptr
create_monitor_homeE(void)
{
// BEGIN USER INSERT SECTION create_monitor_home
// END USER INSERT SECTION create_monitor_home

    return new ::container_service::HomeMonExec();
}

