Esempio n. 1
0
void Drone::runUpdateLoop()
{
	int runTime = 0;
	int updateInterval = 1000/40;

	boost::timer::cpu_timer timer;

	while(!_stop_flag)
	{
		timer.start();

		// TODO: Think this through really well, find memory leaks!

		// Check connection status and handle unexpected loss of connection
		if(!_connected.load()) {
			notifyConnectionLost();
			connectionLost();
			return;
		}

		// Call any miscellaneous functionality needed by implementation
		beforeUpdate();

		// Process command queue
		_commandmutex.lock();
		if(_commandqueue.empty())
		{
			_connected.store(processNoCommand());
			if(!_connected.load())
			{
				continue;
			}
		}
		else
		{
			for(drone::command command : _commandqueue)
			{
				_connected.store(processCommand(command));
				if(!_connected.load())
				{
					_commandqueue.clear();
					continue;
				}
			}
		}
		_commandqueue.clear();
		_commandmutex.unlock();

		// Retrieve and process navdata
		std::shared_ptr<drone::navdata> navdata;
		bool newNavdata = decodeNavdata(navdata);

		if(newNavdata)
		{
			notifyNavdataListeners(navdata);
		}

		// Call any miscellaneous functionality needed by implementation
		updateCycle();

		// Run at continuous update rate
		runTime = timer.elapsed().wall / 1000000;
		timer.stop();

		if(updateInterval - runTime > 0)
		{
			boost::this_thread::sleep_for(boost::chrono::milliseconds(updateInterval - runTime));
		}
	}
}
int ControlApplication::run()
{
  ControlCommandLine cmdLineParser;

  // Check command line for valid.
  try {
    WinCommandLineArgs cmdArgs(m_commandLine.getString());
    cmdLineParser.parse(&cmdArgs);
  } catch (CommandLineFormatException &) {
    TvnServerHelp::showUsage();
    return 1;
  }

  // Run configuration dialog and exit.
  if (cmdLineParser.hasConfigAppFlag() || cmdLineParser.hasConfigServiceFlag()) {
    return runConfigurator(cmdLineParser.hasConfigServiceFlag(), cmdLineParser.hasDontElevateFlag());
  }

  if (cmdLineParser.hasCheckServicePasswords()) {
    return checkServicePasswords(cmdLineParser.hasDontElevateFlag());
  }

  // Change passwords and exit.
  if (cmdLineParser.hasSetControlPasswordFlag() || cmdLineParser.hasSetVncPasswordFlag()) {
    Configurator::getInstance()->setServiceFlag(true);
    Configurator::getInstance()->load();
    ServerConfig *config = Configurator::getInstance()->getServerConfig();
    UINT8 cryptedPass[8];
    if (cmdLineParser.hasSetControlPasswordFlag()) {
      getCryptedPassword(cryptedPass, cmdLineParser.getControlPassword());
      config->setControlPassword((const unsigned char *)cryptedPass);
      config->useControlAuth(true);
    } else {
      getCryptedPassword(cryptedPass, cmdLineParser.getPrimaryVncPassword());
      config->setPrimaryPassword((const unsigned char *)cryptedPass);
      config->useAuthentication(true);
    }
    Configurator::getInstance()->save();
    return 0;
  }

  int retCode = 0;

  // If we are in the "-controlservice -slave" mode, make sure there are no
  // other "service slaves" in this session, exit if there is one already.

  GlobalMutex *appGlobalMutex = 0;

  if (cmdLineParser.hasControlServiceFlag() && cmdLineParser.isSlave()) {
    try {
      appGlobalMutex = new GlobalMutex(
        ServerApplicationNames::CONTROL_APP_INSTANCE_MUTEX_NAME, false, true);
    } catch (...) {
      return 1;
    }
  }

  ZombieKiller zombieKiller;

  // Connect to server.
  try {
    connect(cmdLineParser.hasControlServiceFlag(), cmdLineParser.isSlave());
  } catch (Exception &) {
    if (!cmdLineParser.isSlave() && !cmdLineParser.hasCheckServicePasswords()) {
      const TCHAR *msg = StringTable::getString(IDS_FAILED_TO_CONNECT_TO_CONTROL_SERVER);
      const TCHAR *caption = StringTable::getString(IDS_MBC_TVNCONTROL);
      MessageBox(0, msg, caption, MB_OK | MB_ICONERROR);
    }
    return 1;
  }

  // Execute command (if specified) and exit.
  if (cmdLineParser.isCommandSpecified()) {
    Command *command = 0;

    StringStorage passwordFile;
    cmdLineParser.getPasswordFile(&passwordFile);
    m_serverControl->setPasswordProperties(passwordFile.getString(), true,
                                           cmdLineParser.hasControlServiceFlag());

    if (cmdLineParser.hasKillAllFlag()) {
      command = new DisconnectAllCommand(m_serverControl);
    } else if (cmdLineParser.hasReloadFlag()) {
      command = new ReloadConfigCommand(m_serverControl);
    } else if (cmdLineParser.hasConnectFlag()) {
      StringStorage hostName;
      cmdLineParser.getConnectHostName(&hostName);
      command = new ConnectCommand(m_serverControl, hostName.getString());
    } else if (cmdLineParser.hasShutdownFlag()) {
      command = new ShutdownCommand(m_serverControl);
    } else if (cmdLineParser.hasSharePrimaryFlag()) {
      command = new SharePrimaryCommand(m_serverControl);
    } else if (cmdLineParser.hasShareDisplay()) {
      unsigned char displayNumber = cmdLineParser.getShareDisplayNumber();
      command = new ShareDisplayCommand(m_serverControl, displayNumber);
    } else if (cmdLineParser.hasShareWindow()) {
      StringStorage shareWindowName;
      cmdLineParser.getShareWindowName(&shareWindowName);
      command = new ShareWindowCommand(m_serverControl, &shareWindowName);
    } else if (cmdLineParser.hasShareRect()) {
      Rect shareRect = cmdLineParser.getShareRect();
      command = new ShareRectCommand(m_serverControl, &shareRect);
    } else if (cmdLineParser.hasShareFull()) {
      command = new ShareFullCommand(m_serverControl);
    } else if (cmdLineParser.hasShareApp()) {
      command = new ShareAppCommand(m_serverControl, cmdLineParser.getSharedAppProcessId());
    }

    retCode = runControlCommand(command);

    if (command != 0) {
      delete command;
    }
  } else {
    bool showIcon = true;

    if (cmdLineParser.isSlave()) {
      m_slaveModeEnabled = true;
      try {
        try {
          showIcon = m_serverControl->getShowTrayIconFlag();
        } catch (RemoteException &remEx) {
          notifyServerSideException(remEx.getMessage());
        }
        try {
          m_serverControl->updateTvnControlProcessId(GetCurrentProcessId());
        } catch (RemoteException &remEx) {
          notifyServerSideException(remEx.getMessage());
        }
      } catch (IOException &) {
        notifyConnectionLost();
        return 1;
      } catch (Exception &) {
        _ASSERT(FALSE);
      }
    }

    retCode = runControlInterface(showIcon);
  }

  if (appGlobalMutex != 0) {
    delete appGlobalMutex;
  }

  return retCode;
}