#include "supplier.h"


Supplier::Supplier (ACE_Thread_Manager *thr_mgr, LrsApp *app)
: _es(thr_mgr),
_task(thr_mgr, this),
_lrsinterfaces(app)
{
  // No-Op.
}

Supplier::~Supplier (void)
{
 ACE_DEBUG ((LM_DEBUG,"Supplier::~Supplier\n"));
}

int
Supplier::handle_signal (int signum, siginfo_t *, ucontext_t *)
{
  ACE_DEBUG ((LM_DEBUG,
              "%S\n",
              signum));

  this->close ();
  return 0;
}

int 
Supplier::activate()
{
	return this->_task.activate();
}

int
Supplier::run (void)
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
  {	  	

  if (_es.activate() == -1)
	  ACE_ERROR_RETURN ((LM_ERROR,
                "%p\n",
                "Event_supplier::run"),
				-1);

  this->_orb_Manager.orb()->run (ACE_TRY_ENV);
  }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "event_supplier shutdown");
	  return -1;
    }
  ACE_ENDTRY;

  return 0;
}

void
Supplier::queue_event(CORBA::Any data)
{
  // Insert the data into the queue to be pushed to the notifier
	this->_es.queue_event(data);
}
void
Supplier::close (void)
{    
  _es.close ();
  _lrsinterfaces.close();
  _nh.close ();     
}

int
Supplier::init (int argc, char *argv[])
{  

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
  {	  	

  // Init the orb, root poa and child poa
  if(this->initORB(argc, argv) == -1)
	ACE_ERROR_RETURN ((LM_ERROR,
		       "%p\n",
		       "ORB, root poa or child poa did not init\n"),
                      -1);  
  // Initialize the naming service
  else if (initNamingService(argc, argv) == -1)  
  ACE_ERROR_RETURN ((LM_ERROR,
		       "%p\n",
		       "Naming services did not init\n"),
                      -1);  	
  else if (this->_nh.init (&this->_orb_Manager,
					 this->_namingContext_var, 
					  this) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
		       "%p\n",
		       "Notifier_Handler did not init\n"),
                      -1);  
  else if (this->_es.init (argc, argv, &_nh) == -1)  
	  ACE_ERROR_RETURN ((LM_ERROR,
		       "%p\n",
		       "Event supplier did not init\n"),
                      -1);
  else if (_nh.reactor ()->register_handler (SIGINT, this) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
		       "%p\n",
		       "register_handler"),
                      -1);
  else if (_lrsinterfaces.init(&this->_orb_Manager, this->_namingContext_var, ACE_TRY_ENV) == -1)
  ACE_ERROR_RETURN ((LM_ERROR,
		       "%p\n",
		       "LRS interface did not init"),
			   -1);

  ACE_DEBUG ((LM_DEBUG,
              "Supplier initialized.. \n"));

  }
  ACE_CATCHANY
  {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "Supplier init failed");
	  return -1;
  }
  ACE_ENDTRY;
  return 0;
}

int Supplier::initNamingService(int argc, char *argv[])
{	

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
  {	  	

	// Get the Naming Service object reference.
    CORBA::Object_var namingObj_var =  this->_orb_Manager.orb()->resolve_initial_references ("NameService");

    if (CORBA::is_nil (namingObj_var.in ()))
		ACE_ERROR_RETURN ((LM_ERROR,
                        " (%P|%t) Unable to get the Naming Service.\n"),
                        -1);

    this->_namingContext_var =  CosNaming::NamingContext::_narrow (namingObj_var.in (), ACE_TRY_ENV);

    ACE_TRY_CHECK;

  }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "event_supplier shutdown");
	  return -1;
    }
  ACE_ENDTRY;

  return 0;
}

int Supplier::initORB(int argc, char *argv[])
{
	
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
  {	  	
  // Retrieve the ORB.
  this->_orb_Manager.init(argc,
						  argv,
						  0,
						  ACE_TRY_ENV);

  // Call the init of <TAO_ORB_Manager> to initialize the ORB and
  // create the child poa under the root POA.
 if (this->_orb_Manager.init_child_poa (argc,
                                        argv,
                                        "child_poa",
                                        ACE_TRY_ENV) == -1)

  ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                      "init_child_poa"),
                      -1);
 ACE_CHECK_RETURN (-1);

 this->_orb_Manager.activate_poa_manager (ACE_TRY_ENV);

 ACE_CHECK_RETURN (-1);
  }
  ACE_CATCHANY
  {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "ORB init failed");
	  return -1;
  }
  ACE_ENDTRY;

  return 0;
}