Example #1
0
int main(int argc, char** argv)
{
  
  /*
   * Process command line arguments
   */
  const std::string compclass="[CompClass]";
  const std::string compclasslower="[compclass]";

  std::string description = "usage: flowvr [options] "+ compclasslower;

  flowvr::utils::CmdLine cmdline(description.c_str());

  std::string  prefixFile = compclasslower;

  // Parse commande line
  // Exit if error or --help option
  bool error = false;
  if ( !cmdline.parse(argc, argv, &error ) )
  {
    std::cout << cmdline.help() << std::endl;
    if(error)
      return 1;
    return 0;
  }


  /*
   * Retrieving application name
   */
  std::string compname;

  if  ( cmdline.args.size() !=  1 || cmdline.args[0].empty() )
  {
      std::cerr << "One and only one Component Class Name needed as input argument" << std::endl;
      return 1;
  }
  // component name  (also the component class name)
  compname = cmdline.args[0];
  
  

  // We actually always use the lower case version of the component name. 
  // So we are case insensitive regarding the component name 
  // The dynamic component loader (@class GenClass) also register the component name using its lower case name.
  std::string compnamelower = compname;
  std::transform(compnamelower.begin(), compnamelower.end(),compnamelower.begin(),::tolower);

  // Restore option values to default value if not overwritten
  if (prefixFile == compclasslower)
      prefixFile=compnamelower;



  
  /*
   * Loading .run.xml and .cmd.xml files
   */
  // Loading the .run.xml produced by py-app
  std::string runFile = compnamelower+".run.xml";
  DOMDocument* fRun = new  DOMDocument(runFile);

  // Process the run.xml
  if(!fRun->LoadFile())
  {
    std::cerr << prefixFile.c_str() << ".run.xml cannot be loaded." << std::endl;
    return 1;
  }

  std::string cmdFile = compnamelower+".cmd.xml";
  DOMDocument* fCmd = new DOMDocument(cmdFile);

  // Process cmd.xml file
  if(!fCmd->LoadFile())
  {
    std::cerr << prefixFile.c_str() << ".cmd.xml cannot be loaded." << std::endl;
    return 1;
  }



  /*
   * Launching the application
   */
  BasicController appController;
  if (!appController.init(prefixFile))
  {
    std::cerr << "Cannot launch the application" << std::endl
      << "\tMake sure a flowvr daemon is running" << std::endl 
      << "\tLaunching aborted" << std::endl;
    delete fCmd;
    delete fRun;
    return 1;
  }

  std::string parent = appController.getParent();

  std::cout << "controller namespace: " << parent.c_str() << std::endl;
  if (setenv("FLOWVR_PARENT", parent.c_str(), 1))
  {
    std::cerr << "setting FLOWVR_PARENT environment variable failed." << std::endl;
  }
  
  {
    char buffer[1024];
    if (getcwd(buffer, sizeof(buffer))==NULL)
    {
      std::cerr << "getcwd failed." << std::endl;
    }
    else if (setenv("FLOWVR_PWD",buffer,1))
    {
      std::cerr << "setting FLOWVR_PWD environment variable failed." << std::endl;
    }
  }

  appController.start();


  // Register all modules that are expected to connect
  // (to sync the .run.xml with the .cmd.xml)
  {
    std::set<std::string> & allExpectedModules = appController.allExpectedModules; 
                
    for(DOMElement* cmd = fCmd->RootElement()->FirstChildElement(); 
        cmd; 
        cmd = cmd->NextSiblingElement()) {
      TiXmlElement *elt = cmd->ToElement();
      if(strcmp(cmd->Value(),            "addobject"               ) == 0 && elt && 
         strcmp(elt->Attribute("class"), "flowvr.plugins.Regulator") == 0) {
        allExpectedModules.insert(cmd->Attribute("id"));
      }
    }
  }    

  // CALL COMMANDS FROM .RUN.XML FILE
  std::cout << "Processing " << runFile.c_str();
  DOMElement* root = fRun->RootElement();
  DOMElement* cmd = root->FirstChildElement();
  while (cmd!=NULL)
  {
      if (!appController.ok())
      {
          std::string cmds="";
          std::cerr << "Cannot send command: " << std::endl 
            << "\t " << (cmds<<*cmd).c_str() << std::endl
            << "\tLaunching aborted" << std::endl;

          delete fRun;
          delete fCmd;
          return 1;
      }
      appController.processCommand(cmd);
      cmd = cmd->NextSiblingElement();
  }
  
  
  // CALL COMMANDS FROM .CMD.XML FILE
  std::cout << "Processing " << cmdFile.c_str();
  root = fCmd->RootElement();
  cmd = root->FirstChildElement();
  while (cmd!=NULL)
  {
      if (!appController.ok())
      {
          std::string cmds="";
          std::cerr << "Cannot send command: " << std::endl 
            << "\t " << (cmds<<*cmd).c_str() << std::endl
            << "\tLaunching aborted" << std::endl;
          

          delete fRun;
          delete fCmd;
          return 1;
      }
      appController.processCommand(cmd);
      cmd = cmd->NextSiblingElement();
  }
  
  
  // APPLICATION IS RUNNING
  // READ USER COMMANDS FROM THE TERMINAL
  while (appController.ok())
  {
      std::cout << ">"; //prompt
      std::string command = BasicController::getcmdline();
      if ( !command.empty())  
        appController.processCommand(command, true);
  }
  
  // RECEIVED STOP COMMAND

  appController.wait();

  std::cout << "Done!" << std::endl;
  delete fRun;
  delete fCmd;
  return 0;
};