28 #include "../gui/namelocationview.h"
29 #include "../gui/scenarioitem.h"
42 #include <QMessageBox>
44 #include <QApplication>
45 #include <SimoxUtility/algorithm/string/string_tools.h>
48 using namespace Controller;
49 using namespace Data_Structure;
50 using namespace Parser;
53 #define UPDATE_TIMER 500 //ms
59 packages(packages), executor(executor)
61 qRegisterMetaType<ApplicationInstanceStatusMap>(
"ApplicationInstanceStatusMap");
62 qRegisterMetaType<FilterableTreeModelSortFilterProxyModelPtr>(
"FilterableTreeModelSortFilterProxyModelPtr");
64 QTimer* timer =
new QTimer(
this);
65 QObject::connect(timer, SIGNAL(timeout()),
this, SLOT(
updateStati()));
71 }, 100,
false,
"ScenarioManagerUpdateStatusTask");
74 model->setDynamicSortFilter(
true);
75 model->setSourceModel(&treemodel);
77 QObject::connect(&createScenarioView, SIGNAL(created(std::string, std::string)),
80 QObject::connect(&treemodel, SIGNAL(applicationsDrop(QList<QPair<QString, ScenarioManager::Data_Structure::Application*>>,
int, QModelIndex)),
81 this, SLOT(
addApplicationsToScenario(QList<QPair<QString, ScenarioManager::Data_Structure::Application*>>,
int, QModelIndex)));
102 if (parent.isValid())
104 if (parent.parent().isValid())
111 executor->stopApplication(instance);
121 executor->stopApplication(app);
132 executor->removeScenario(scenario);
136 executor->stopScenario(scenario);
153 if (parent.isValid())
161 executor->setStarter(StarterFactory::getFactory()->getIceStarter(iceAdmin), scenario);
164 if (!executor->isScenarioDeployed(scenario))
168 executor->deployScenario(scenario).get();
170 catch (IceGrid::DeploymentException& ex)
172 ShowWarningDialog(QString::fromStdString(ex.reason));
181 if (parent.parent().isValid())
188 instance->updateFound();
189 if (!instance->getFound())
191 ShowWarningDialog(
"The binary `" + QString::fromStdString(instance->getExecutableAbsPath()) +
"` for the application " + QString::fromStdString(instance->getName()) +
" could not be found");
195 auto scenario = instance->getScenario();
197 if (
s.isIceScenario(scenario))
201 executor->startApplication(instance);
222 if (!app->getFound())
224 ShowWarningDialog(
"The Binary for the Application " + QString::fromStdString(app->getName()) +
" could not be found");
229 executor->startApplication(app);
235 startScenario(item, mode);
249 if (item ==
nullptr || !item->
isEnabled())
253 if (parent.isValid())
255 if (parent.parent().isValid())
265 executor->restartApplication(app);
276 catch (IceGrid::ServerStartException& ex)
278 ShowWarningDialog(
"Ice had an launching error. Please make sure your remote launch settings are correct");
286 int row,
const QModelIndex& parent)
289 QModelIndex scenarioIndex;
291 if (parent.parent().isValid() && parent.parent().parent().isValid())
296 else if (parent.parent().isValid())
299 scenarioIndex = parent.parent();
304 scenarioIndex = parent;
313 if (!scenario->isScenarioFileWriteable())
315 QMessageBox::critical(0,
"Adding application failed!",
"The scenario '" + QString::fromStdString(scenario->getName()) +
"' is read only. You cannot add an application to this scenario!");
319 std::vector<std::string> acceptedApps = generator.
getDependencieTree(scenario->getPackage()->getName());
322 for (
const auto& pair : applications)
326 for (
const auto& package : *packages)
328 for (
const auto& pApp : *package->getApplications())
330 if (pApp->getName().compare(app->
getName()) == 0)
332 appPackage = package;
337 if (appPackage.get() ==
nullptr)
342 if (std::find(acceptedApps.begin(), acceptedApps.end(), appPackage->getName()) == acceptedApps.end())
344 const std::string dependenciesStr = simox::alg::join(acceptedApps,
", ");
347 QString
message(
"You cannot drop an app from ");
348 message.append(QString::fromStdString(appPackage->getName()));
349 message.append(
" into a scenario from ");
350 message.append(QString::fromStdString(scenario->getPackage()->getName()));
351 message.append(QString::fromStdString(
". Package `" + scenario->getPackage()->getName() +
"` depends on " + dependenciesStr));
358 const std::string appName = app->
getName();
360 int instanceCounter = 0;
361 std::string instanceName = pair.first.toStdString();
362 if (instanceName.empty())
366 if (instances->getName().compare(appName) == 0)
371 if (instanceCounter == 0)
385 if (instances->getInstanceName().compare(instanceName) == 0)
390 if (instanceCounter > 0)
397 std::string configPath = scenario->getPath();
398 configPath = configPath.substr(0, configPath.find_last_of(
'/'));
399 configPath.append(
"/config/");
402 configPath.append(app->
getName());
403 if (!instanceName.empty())
405 configPath.append(
".");
406 configPath.append(instanceName);
408 configPath.append(
".cfg");
410 std::ofstream out(configPath);
417 executor->loadAndSetCachedProperties(appInstance, cacheDir,
true);
418 appInstance->load(
true);
420 for (
const auto& elem : app->
getProperties()->getProperties()->getPropertiesForPrefix(
""))
422 appInstance->modifyProperty(elem.first, elem.second);
423 appInstance->setDefaultPropertyEnabled(elem.first,
true);
428 scenario->addApplication(appInstance);
430 QModelIndex subScenarioIndex = findSubScenarioModelIndexByScenarioIndex(scenarioIndex, appPackage->getName());;
435 if (!subScenarioIndex.isValid())
437 std::vector<ApplicationInstancePtr>
list;
441 treemodel.
insertRow(0, subScenarioItem, scenarioIndex);
443 subScenarioIndex = findSubScenarioModelIndexByScenarioIndex(scenarioIndex, appPackage->getName());
459 if (item.parent().isValid() && item.parent().parent().isValid())
463 if (scenItem ==
nullptr)
474 treemodel.
removeRow(model->mapToSource(item).row(), model->mapToSource(item.parent()));
476 else if (item.parent().isValid())
480 if (scenItem ==
nullptr)
491 scenario->removeApplication(app);
494 treemodel.
removeRow(model->mapToSource(item).row(), model->mapToSource(item.parent()));
500 if (scenItem ==
nullptr)
508 QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
509 QStringList scenarios = settings.value(
"scenarios").toStringList();
510 scenarios.removeDuplicates();
511 QString toFind(QString::fromStdString(scen->getName()));
512 toFind.append(
"::Package::");
513 toFind.append(QString::fromStdString(scen->getPackage()->getName()));
514 scenarios.removeAt(scenarios.indexOf(toFind));
515 settings.setValue(
"scenarios", scenarios);
517 for (
const auto& package : *packages)
519 for (
auto it = package->getScenarios()->begin(); it != package->getScenarios()->end(); ++it)
521 if (scen.get() == it->get())
523 package->getScenarios()->erase(it);
528 treemodel.
removeRow(model->mapToSource(item).row(), model->mapToSource(item.parent()));
537 if (packages->size() != 0)
539 QVector<QPair<QString, bool> > packageNames;
540 for (
const auto& package : *packages)
542 packageNames << qMakePair(QString::fromStdString(package->getName()), package->isScenarioPathWritable());
545 createScenarioView.exec();
549 QMessageBox messageBox;
550 messageBox.setText(
"You have to have at least one Package open to create an Scenario");
562 for (
unsigned int i = 0; i < packages->size(); i++)
564 if (packages->at(i)->getName().compare(packageStr) == 0)
566 package = packages->at(i);
570 if (package.get() ==
nullptr)
575 if (
parser.isScenarioexistent(name, package))
578 QString
message(
"The Scenario " + QString::fromStdString(name) +
" already exists in Package " + QString::fromStdString(packageStr));
585 if (ptr.get() ==
nullptr)
590 for (
const auto& package : *packages)
592 if (package->getName().compare(packageStr) == 0)
594 package->addScenario(ptr);
601 QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
602 QStringList scenarios = settings.value(
"scenarios").toStringList();
603 scenarios.removeDuplicates();
605 toAdd.append(QString::fromStdString(ptr->getName()));
606 toAdd.append(
"::Package::");
607 toAdd.append(QString::fromStdString(package->getName()));
608 scenarios.append(toAdd);
609 settings.setValue(
"scenarios", scenarios);
617 if (
index.column() > 0 || item ==
nullptr)
622 if (
index.parent().isValid())
624 if (
index.parent().parent().isValid())
627 appInstance->updateFound();
628 if (!appInstance->getFound())
631 message.setText(
"Could not find Application " + item->
data(0).toString() +
" at " + QString::fromStdString(appInstance->getPathToExecutable()) +
".");
633 if (!appInstance->getFound())
658 for (
const auto& package : *packages)
660 auto scenarios = package->getScenarios();
661 for (
const auto& scenario : *scenarios)
663 std::map<std::string, std::vector<ApplicationInstancePtr>> packageSubtrees;
664 for (
const auto& app : *scenario->getApplications())
668 if (packageSubtrees.count(packageName) == 0)
670 packageSubtrees[packageName] = std::vector<ApplicationInstancePtr>();
672 packageSubtrees[packageName].push_back(app);
676 int index = findScenario(rootItem, scenario->getName(), scenario->getPackage()->getName());
681 for (
const auto& it : packageSubtrees)
692 for (
int i = 0; i < scenarioItem->
childCount(); i++)
695 std::string name = subPackageItem->
data(0).toString().toStdString();
697 std::vector<ApplicationInstancePtr> currentBuildSubtree = packageSubtrees[name];
699 for (
size_t k = 0; k < currentTreeSubtree->size(); k++)
702 for (
size_t j = 0; j < currentBuildSubtree.size(); j++)
704 if (currentTreeSubtree->at(k)->getName() == currentBuildSubtree[j]->getName()
705 && currentTreeSubtree->at(k)->getInstanceName() == currentBuildSubtree[j]->getInstanceName())
713 treemodel.
insertRow(0,
new ScenarioItem(currentTreeSubtree->at(k)), findSubScenarioModelIndex(scenario->getName(), name));
724 QModelIndex subScenarioModelIndex = findSubScenarioModelIndex(application->getScenario()->getName(), application->getPackageName());
725 if (!subScenarioModelIndex.isValid())
727 return QModelIndex();
730 QModelIndex appIndex = subScenarioModelIndex.child(0, 0);
733 appIndex = appIndex.sibling(count, 0);
734 if (!appIndex.isValid())
736 return QModelIndex();
738 if (application->getInstanceName().empty())
740 if (application->getName() == appIndex.data().toString().toStdString())
747 if (application->getInstanceName() +
"." + application->getName() == appIndex.data().toString().toStdString())
757 QModelIndex ScenarioListController::findSubScenarioModelIndex(std::string scenarioName, std::string packageName)
759 QModelIndex scenarioIndex = treemodel.
index(0, 0);
761 bool scenarioFound =
false;
763 while (!scenarioFound)
765 scenarioIndex = scenarioIndex.sibling(count, 0);
767 if (!scenarioIndex.isValid())
769 return QModelIndex();
771 if (scenarioName == scenarioIndex.data().toString().toStdString())
773 scenarioFound =
true;
783 QModelIndex currentSubPackageIndex = scenarioIndex.child(count, 0);
784 if (!currentSubPackageIndex.isValid())
786 return QModelIndex();
788 if (packageName == currentSubPackageIndex.data().toString().toStdString())
790 return currentSubPackageIndex;
795 return QModelIndex();
798 QModelIndex ScenarioListController::findSubScenarioModelIndexByScenarioIndex(QModelIndex scenarioIndex, std::string packageName)
803 QModelIndex currentSubPackageIndex = scenarioIndex.child(count, 0);
804 if (packageName == currentSubPackageIndex.data().toString().toStdString())
806 return currentSubPackageIndex;
808 if (!currentSubPackageIndex.isValid())
810 return QModelIndex();
815 return QModelIndex();
819 int ScenarioListController::findScenario(
ScenarioItem* rootItem, std::string name, std::string packageName)
821 for (
int i = 0; i < rootItem->
childCount(); i++)
836 std::vector<ApplicationInstancePtr> apps;
838 std::unique_lock lock(applicationInstanceMutex);
839 apps.swap(applicationInstances);
850 std::string
status = executor->getApplicationStatus(appInst);
861 StopStrategyFactory stopStrategyFactory;
871 applicationInstance->load();
874 if (statusManager.
isIceScenario(scenario) && scenario->getStatus() != ApplicationStatus::Stopped)
876 QMessageBox::StandardButton reply;
877 reply = QMessageBox::question(
nullptr,
"",
"The Scenario is currently running via Ice do you want to remove it ?",
QMessageBox::Yes |
QMessageBox::No);
884 executor->setStarter(StarterFactory::getFactory()->getIceStarter(iceAdmin), scenario);
885 executor->setStopStrategy(stopStrategyFactory.getKillStrategy(StopperFactory::getFactory()->getIceStopper(iceAdmin)), scenario);
888 executor->removeScenario(scenario);
892 executor->setStarter(StarterFactory::getFactory()->getStarter(), scenario);
893 executor->setStopStrategy(executor->getDefaultStopStrategy(), scenario);
897 if (!scenario->allApplicationsFound())
899 ShowWarningDialog(
"Not all binaries in the Scenario " + QString::fromStdString(scenario->getName()) +
" could be found",
true, QString::fromStdString(scenario->getName() +
"BinariesMissing"));
902 executor->startScenario(scenario);
906 if (!statusManager.
isIceScenario(scenario) && scenario->getStatus() != ApplicationStatus::Stopped)
908 QMessageBox::StandardButton reply;
909 reply = QMessageBox::question(
nullptr,
"",
"The Scenario is currently running locally - do you want to stop it ?",
QMessageBox::Yes |
QMessageBox::No);
916 executor->setStarter(StarterFactory::getFactory()->getStarter(), scenario);
917 executor->setStopStrategy(executor->getDefaultStopStrategy(), scenario);
919 executor->stopScenario(scenario);
923 executor->setStarter(StarterFactory::getFactory()->getIceStarter(iceAdmin), scenario);
924 executor->setStopStrategy(stopStrategyFactory.getKillStrategy(StopperFactory::getFactory()->getIceStopper(iceAdmin)), scenario);
926 if (!scenario->allApplicationsFound())
928 ShowWarningDialog(
"Not all binaries in the Scenario " + QString::fromStdString(scenario->getName()) +
" could be found",
true, QString::fromStdString(scenario->getName() +
"BinariesMissing"));
934 if (!executor->isScenarioDeployed(scenario))
938 executor->deployScenario(scenario).get();
940 catch (IceGrid::DeploymentException& ex)
942 ShowWarningDialog(QString::fromStdString(ex.reason));
951 executor->startScenario(scenario).get();
953 catch (IceGrid::ServerStartException& ex)
955 ShowWarningDialog(
"Ice had an launching error. Please make sure your remote launch settings are correct. Did you sync everything?\nId: " + QString::fromStdString(ex.id)
956 +
"\nReason: " + QString::fromStdString(ex.reason)) ;
970 std::vector<ApplicationInstancePtr> apps;
979 apps.push_back(appInst);
985 std::unique_lock lock(applicationInstanceMutex);
986 apps.swap(applicationInstances);
992 bool changed =
false;
993 for (
auto& pair : stati)
996 bool statusChanged = pair.first->setStatus(pair.second);
997 changed |= statusChanged;
1002 auto start = IceUtil::Time ::now();
1012 for (
const auto& package : *packages)
1014 for (
const auto& scenario : *package->getScenarios())
1016 if (application->getScenario()->getName() == scenario->getName())
1019 executor->loadAndSetCachedProperties(application, cacheDir,
false,
false);
1020 qApp->processEvents();
1031 this->iceAdmin = iceAdmin;
1034 void ScenarioListController::ShowWarningDialog(QString
message,
bool showOnce, QString messageId)
1036 static std::set<QString> dialogIds;
1040 if (dialogIds.count(messageId))
1044 dialogIds.insert(messageId);