//  This file was Automatically Generated on 03/18/2003 @ 22:10:23 from the
//  Component Information file.
//
//  Notes:
//  This file is automatically generated using
//  xml2ocp version 2.2 from the following
//  input file: ComponentInfo.xml
//
//  WARNING: Any manual modifications will be overwritten
//  the next time the code generation tool is run.

#include "ace/Version.h"
#include "ace/ACE.h"
#include "ace/Arg_Shifter.h"
#include "ace/Thread_Manager.h"

#if (ACE_MAJOR_VERSION == 5 && ACE_MINOR_VERSION >= 2 ) || (ACE_MAJOR_VERSION > 5)
#include "tao/ORB_Core.h"
#endif

// Controls API
#include "OCP_ProcessDefs.h"
#include "OCP_i.h"
#include "OCP_TriggerDrivenStrategy.h"
#include "OCP_DataDrivenStrategy.h"
#include "OCP_PortConnectorBus.h"

char* masterHostName = 0;
char* masterHostAddr = 0;

// Used to enable LM_DEBUG messages
bool  enableDebug = false;
// Used to enable LM_TRACE messages
bool  enableTrace = false;

#include "GtmaxLink.h"
#include "SimpleTrajectoryGenerator.h"

// This is the user specified includes section.  Includes added within the 
// following Preserve Region will be preserved when this component is regenerated.

// User specified includes - Preserve Region
// User specified includes - End Preserve Region

void ParseArgs(int argc, char* argv[])
{
   ACE_Arg_Shifter argShifter (argc, argv);

   // Ignore the first argument.  Program Name
   argShifter.ignore_arg ();

   while (argShifter.is_anything_left ())
   {
      char *currentArg = 0;
 
      if (  argShifter.cur_arg_strncasecmp("-debug") >= 0 )
      {
         enableDebug = true;
         argShifter.consume_arg();
      }
      else if (  argShifter.cur_arg_strncasecmp("-trace") >= 0 )
      {
         enableTrace = true;
         argShifter.consume_arg();
      }
      else if ( ( currentArg = argShifter.get_the_parameter("-masterHostName") ) )
      {
         masterHostName = CORBA::string_dup(currentArg);
         argShifter.consume_arg();
      }
      else if ( ( currentArg = argShifter.get_the_parameter("-masterHostAddr") ) )
      {
         masterHostAddr = CORBA::string_dup(currentArg);
         argShifter.consume_arg();
      }
   // User specified arguments - Preserve Region
      else if ( ( currentArg = argShifter.get_the_parameter("-gtmax") ) )
      {
		  GtmaxLink::SetFlightComputerName(CORBA::string_dup(currentArg));
		  argShifter.consume_arg();
	  }
      else if ( ( currentArg = argShifter.get_the_parameter("-V") ) )
      {
		  SimpleTrajectoryGenerator::V = atof(CORBA::string_dup(currentArg));
		  argShifter.consume_arg();
	  }
      else if ( ( currentArg = argShifter.get_the_parameter("-omega") ) )
      {
		  SimpleTrajectoryGenerator::omega = atof(CORBA::string_dup(currentArg));
		  argShifter.consume_arg();
	  }
   // User specified arguments - End Preserve Region
      else
      {
         ACE_DEBUG((LM_DEBUG, "Unrecognized Command Line Arg: %s.  Assuming user-defined parameter\n", argShifter.get_current()));
         argShifter.ignore_arg ();
      }
   }

   if (masterHostName)
      ACE_DEBUG((LM_DEBUG, "masterHostName %s\n", masterHostName));
   if (masterHostAddr)
      ACE_DEBUG((LM_DEBUG, "masterHostAddr %s\n", masterHostAddr));

}

// This is the user specified globals section.  Global definitions added within the 
// following Preserve Region will be preserved when this component is regenerated.

// User specified globals - Preserve Region
// User specified globals - End Preserve Region

int main (int argc, char *argv[])
{
   // User specified initializations - Preserve Region
   // User specified initializations - End Preserve Region

   const char* theOCP_Names[] = { "OCP_P1" };

   if (argc > 1)
   {
      // Parse any command line arguments
      ParseArgs(argc, argv);
   }
   
   if (enableDebug)
   {
      ACE_Log_Msg::enable_debug_messages(LM_DEBUG);
   }
   else
   {
      ACE_Log_Msg::disable_debug_messages(LM_DEBUG);
   }

   if (enableTrace)
   {
      ACE_Log_Msg::enable_debug_messages(LM_TRACE);
   }
   else
   {
      ACE_Log_Msg::disable_debug_messages(LM_TRACE);
   }

   // Declare the OCP on the stack
   OCP_i theOCP(theOCP_Names,
                OCP_ProcessDefs::PROCESS_P1,
                OCP_ProcessDefs::NUM_OF_PROCESSES,
                masterHostName,
                masterHostAddr);

   // Construct the OCP
   theOCP.CreateOCP();

   // Create Static OCP Components
   // Process P1 Components

   // This is the user specified pre component creation section.  Definitions added within the 
   // following Preserve Region will be preserved when this component is regenerated.

   // User specified pre create - Preserve Region
   // User specified pre create - End Preserve Region

   ACE_DEBUG((LM_DEBUG, "=== Creating Component GtmaxLink gtmaxLink ===\n"));
   GtmaxLink *gtmaxLink = ACE_static_cast(GtmaxLink*, theOCP.Create("gtmaxLink"));
   if (gtmaxLink == 0)
   {
      ACE_ERROR((LM_ERROR,"Creation of gtmaxLink Failed!\n%a"));
   }

   ACE_DEBUG((LM_DEBUG, "=== Creating Component SimpleTrajectoryGenerator simpleTrajGen ===\n"));
   SimpleTrajectoryGenerator *simpleTrajGen = ACE_static_cast(SimpleTrajectoryGenerator*, theOCP.Create("simpleTrajGen"));
   if (simpleTrajGen == 0)
   {
      ACE_ERROR((LM_ERROR,"Creation of simpleTrajGen Failed!\n%a"));
   }



   theOCP.Connect("simpleTrajGen",
                  "stateIn",
                  "gtmaxLink",
                  "stateOut",
                  false);
   theOCP.Connect("gtmaxLink",
                  "commandIn",
                  "simpleTrajGen",
                  "commandOut",
                  false);

   // This is the user specified pre initialization section.  Definitions added within the 
   // following Preserve Region will be preserved when this component is regenerated.

   // User specified pre init - Preserve Region
   // User specified pre init - End Preserve Region

   ACE_DEBUG((LM_DEBUG, "=== Initializing Component GtmaxLink gtmaxLink ===\n"));
   theOCP.Init("gtmaxLink");

   ACE_DEBUG((LM_DEBUG, "=== Initializing Component SimpleTrajectoryGenerator simpleTrajGen ===\n"));
   theOCP.Init("simpleTrajGen");


   // Start the OCP
   theOCP.StartOCP();

   // This is the user specified pre activation section.  Definitions added within the 
   // following Preserve Region will be preserved when this component is regenerated.

   // User specified pre activate - Preserve Region
   // User specified pre activate - End Preserve Region

   ACE_DEBUG((LM_DEBUG, "=== Activating Component GtmaxLink gtmaxLink ===\n"));
   theOCP.On("gtmaxLink", ALL_INPUTS, ALL_OUTPUTS);

   ACE_DEBUG((LM_DEBUG, "=== Activating Component SimpleTrajectoryGenerator simpleTrajGen ===\n"));
   theOCP.On("simpleTrajGen", ALL_INPUTS, ALL_OUTPUTS);




   // End of Static Initialization.

   // This is the user specified post static initialization section.  Definitions added within the 
   // following Preserve Region will be preserved when this component is regenerated.

   // User specified post init - Preserve Region
   // User specified post init - End Preserve Region

#if (ACE_MAJOR_VERSION == 5 && ACE_MINOR_VERSION >= 2 ) || (ACE_MAJOR_VERSION > 5)
   // Continue processing until all ORB threads exit.
   CORBA::Environment env;
   TAO_ORB_Core_instance ()->orb()->run( env );
#else
   // Continue processing until all the threads exit.
   ACE_Thread_Manager::instance ()->wait ();
#endif

   return 0;
}
