diff options
Diffstat (limited to 'resourceqt-client/client.cpp')
-rw-r--r-- | resourceqt-client/client.cpp | 165 |
1 files changed, 110 insertions, 55 deletions
diff --git a/resourceqt-client/client.cpp b/resourceqt-client/client.cpp index 7fdb2f6..fcfe36c 100644 --- a/resourceqt-client/client.cpp +++ b/resourceqt-client/client.cpp @@ -60,9 +60,8 @@ Client::Client() commandList["free"] = CommandListArgs("", "destroy and free the resources"); commandList["acquire"] = CommandListArgs("", "acquire required resources"); commandList["release"] = CommandListArgs("", "release resources"); - commandList["update"] = CommandListArgs("", "update modified resource set after add or remove command"); - commandList["add"] = CommandListArgs("reslist [-o]", "add resource list, if -o provided, set as optional"); - commandList["remove"] = CommandListArgs("reslist [-o]", "remove resource list, if -o provided, removed only optional flag"); + commandList["update"] = CommandListArgs("update <all>[:opt] where 'all' and 'opt' are comma separated resources", + "update the resource set by specifying the new set"); commandList["audio"] = CommandListArgs("pid <pid> | group <audio group> | tag <name> <value>", "set audio properties"); commandList["addaudio"] = CommandListArgs("<audio group> <pid> <tag name> <tag value>", "Add an audio resource and set the properties"); commandList["show"] = CommandListArgs("", "show resources"); @@ -130,16 +129,25 @@ bool Client::initialize(const CommandLineParser &parser) this, SLOT(resourcesBecameAvailableHandler(const QList<ResourcePolicy::ResourceType> &)))) { return false; } + if (!connect(resourceSet, SIGNAL(resourcesReleasedByManager()), + this, SLOT(resourceReleasedByManagerHandler()))) { + return false; + } if (!connect(&stdInNotifier, SIGNAL(activated(int)), this, SLOT(readLine(int)))) { return false; } + if (!connect(resourceSet , SIGNAL(connectedToManager()), this, SLOT(stopConnectTimerHandler()))) { + return false; + } if (!connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()), this, SLOT(doExit()))) { return false; } + + startTimer(); + resourceSet->initAndConnect(); OUTPUT << "accepting input" << endl; showPrompt(); - return true; } @@ -149,6 +157,73 @@ void Client::doExit() resourceSet->release(); } + +void Client::modifyResources(const QString &resString) +{ + //resString example: [mand_resources:opt_resources] res1,res2,res3:res1,res3 + if ( resString.isEmpty() || resString.isNull()){ + qDebug("Client::modifyResources(): no resources in string."); + return; + } + QStringList newAllAndOpt = resString.split(":"); + + if (newAllAndOpt.size()==1) + qDebug("There are only mandatory resources."); + + //Every optional res. is also in allSet + QSet<ResourcePolicy::ResourceType> newAllSet; + QSet<ResourcePolicy::ResourceType> newOptSet; + + if ( !CommandLineParser::parseResourceList(newAllAndOpt.at(0), newAllSet) ) { + qDebug("Client::modifyResources(): could not parse all resources."); + return; + } + + if (newAllAndOpt.size()>1) { + if ( !CommandLineParser::parseResourceList(newAllAndOpt.at(1), newOptSet) ){ + qDebug("Client::modifyResources(): could not parse optional resources."); + return; + } + bool optNotInAll = false; + //If the user forgot to add resource to all when specifying optional -> add to all. + foreach(ResourceType newOptRes, newOptSet){ + if ( !newAllSet.contains(newOptRes) ){ + optNotInAll = true; + newAllSet.insert(newOptRes); + } + } + if (optNotInAll) + qDebug("Client::modifyResources(): optional resources should be added to all as well."); + } + + //Check if new resources are in current resource set. + foreach ( ResourceType newRes, newAllSet) { + if ( resourceSet->contains(newRes) ){ + if ( resourceSet->resource(newRes)->isOptional() && !newOptSet.contains(newRes) ) { + //New mandatory is in set, but is not optional in the new set. + resourceSet->resource(newRes)->setOptional(false); + } + else if ( !resourceSet->resource(newRes)->isOptional() && newOptSet.contains(newRes) ){ + //New mandatory is in set, but is optional. + resourceSet->resource(newRes)->setOptional(true); + } + } + else { //Add new resource. + resourceSet->addResource(newRes); + + if ( newOptSet.contains(newRes) ) + resourceSet->resource(newRes)->setOptional(true); + } + } + QList<Resource*> resList = resourceSet->resources(); + //Check if there are current resources not in the new set (i.e. removed). + foreach(Resource* resource, resList) + if ( !newAllSet.contains(resource->type()) ) + resourceSet->deleteResource(resource->type()); + +} + + const char * resourceTypeToString(ResourceType type) { switch (type) { @@ -204,6 +279,11 @@ void Client::showResources(const QList<Resource*> &resList) } } +void Client::stopConnectTimerHandler() +{ + stopTimer(); +} + void Client::resourceAcquiredHandler(const QList<ResourceType>&) { stopTimer(); @@ -250,6 +330,15 @@ void Client::resourceReleasedHandler() showPrompt(); } +void Client::resourceReleasedByManagerHandler() +{ + stopTimer(); + + QList<Resource*> allResources = resourceSet->resources(); + outputln << "mgr-released:"<< allResources << endl; + showPrompt(); +} + void Client::resourcesBecameAvailableHandler(const QList<ResourcePolicy::ResourceType> &availableResources) { if (pendingAddAudio) { @@ -320,60 +409,26 @@ void Client::readLine(int) qCritical("%s failed!", qPrintable(command)); } } - else if (command == "add") { - QString resourceList, flag; - input >> resourceList >> flag; - if (!resourceSet) { - qCritical("%s%s failed!", qPrintable(prefix), qPrintable(command)); - } - else if (resourceList.isEmpty() || resourceList.isNull()) { - OUTPUT << "List of desired resources is missing. Use help."; - } - else { - bool optional = false; - QSet<ResourcePolicy::ResourceType> resToAdd; - CommandLineParser::parseResourceList(resourceList, resToAdd); - if (flag == "-o") { - optional = true; - } - foreach(ResourcePolicy::ResourceType resource, resToAdd) { - if (!resourceSet->contains(resource)) { - resourceSet->addResource(resource); - } - if (optional) { - resourceSet->resource(resource)->setOptional(); - } - } - } - } - else if (command == "remove") { - QString resourceList, flag; - input >> resourceList >> flag; - if (!resourceSet) { - qCritical("%s%s failed!", qPrintable(prefix), qPrintable(command)); - } - else if (resourceList.isEmpty() || resourceList.isNull()) { - OUTPUT << "List of desired resources is missing. Use help."; + else if (command == "update") { + + QString resourceList; + input >> resourceList; + + if (!resourceSet) + qCritical("%s failed!", qPrintable(command)); + + if (resourceList.isEmpty() || resourceList.isNull()) { + qCritical("%s failed! List of desired resources is missing. Use help.", + qPrintable(command)); } else { - QSet<ResourcePolicy::ResourceType> resToRemove; - CommandLineParser::parseResourceList(resourceList, resToRemove); - if (flag == "-o") { - foreach(ResourcePolicy::ResourceType resource, resToRemove) { - resourceSet->resource(resource)->setOptional(false); - } - } - else { - foreach(ResourcePolicy::ResourceType resource, resToRemove) { - resourceSet->deleteResource(resource); - } - } - } - } - else if (command == "update") { - if (!resourceSet || !resourceSet->update()) { - qCritical("%s%s failed!", qPrintable(prefix), qPrintable(command)); + startTimer(); + modifyResources(resourceList); + + if (!resourceSet->update()) + qCritical("%s failed!", qPrintable(command)); } + } else if (command == "audio") { QString what, group, tagName, tagValue; |