diff options
Diffstat (limited to 'resourceqt-client/client.cpp')
-rw-r--r-- | resourceqt-client/client.cpp | 683 |
1 files changed, 283 insertions, 400 deletions
diff --git a/resourceqt-client/client.cpp b/resourceqt-client/client.cpp index 5f1875b..adf1feb 100644 --- a/resourceqt-client/client.cpp +++ b/resourceqt-client/client.cpp @@ -1,5 +1,6 @@ #include <QtCore/QCoreApplication> #include <QtCore/QFile> +#include <QTextStream> #include <sys/time.h> #include <sys/types.h> @@ -9,160 +10,85 @@ using namespace ResourcePolicy; -Client::Client(QString appClass) : QObject() -{ - applicationClass = appClass; - resourceSet = NULL; +QMap<QString, CommandListArgs> Client::commandList; - standardInput = new QTextStream(stdin, QFile::ReadOnly); - mainTimerID = startTimer(0); -} - -Client::~Client() +CommandListArgs::CommandListArgs() + : args(), help() { - killTimer(mainTimerID); +} - if (resourceSet != NULL) { - delete resourceSet; - resourceSet = NULL; - } - if (standardInput != NULL) { - delete standardInput; - standardInput = NULL; - } +CommandListArgs::CommandListArgs(const QString &arguments, const QString &helpText) + : args(arguments), help(helpText) +{ } -uint32_t Client::parseResourceList(QString resourceListStr) +CommandListArgs::~CommandListArgs() { - struct { - uint32_t resourceType; - const char* resourceName; - } - resourceDef[] = { - { RES_AUDIO_PLAYBACK , "AudioPlayback" }, - { RES_VIDEO_PLAYBACK , "VideoPlayback" }, - { RES_AUDIO_RECORDING, "AudioRecording" }, - { RES_VIDEO_RECORDING, "VideoRecording" }, - { RES_VIBRA , "Vibra" }, - { RES_LEDS , "Leds" }, - { RES_BACKLIGHT , "BackLight" }, - { RES_SYSTEM_BUTTON , "SystemButton" }, - { RES_LOCK_BUTTON , "LockButton" }, - { RES_SCALE_BUTTON , "ScaleButton" }, - { RES_SNAP_BUTTON , "SnapButton" }, - { RES_LENS_COVER , "LensCover" }, - { RES_HEADSET_BUTTONS, "HeadsetButtons" }, - { 0 , NULL } - }; - - uint32_t resourceList = 0; - - if (resourceListStr.isEmpty() || resourceListStr.isNull()) { - return 0; - } - else { - QStringList resList = resourceListStr.split(",", QString::SkipEmptyParts); +} - for (int i = 0; i < resList.count(); i++) { - int pos = 0; - while (resourceDef[pos].resourceName != NULL) { - if (resList[i] == resourceDef[pos].resourceName) - break; +Client::Client() + : QObject(), standardInput(stdin, QIODevice::ReadOnly), stdInNotifier(0, QSocketNotifier::Read), applicationClass(), + resourceSet(NULL), output(stdout) +{ + mainTimerID = startTimer(0); + commandList["help"] = CommandListArgs("", "print this help message"); + commandList["quit"] = CommandListArgs("", "exit application"); + 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["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"); - pos++; - } +} - if (!resourceDef[pos].resourceName) { - const char* res = qPrintable(resList[i]); - printf("Ignoring invalid resource name '%s'\n", res); - } - else { - resourceList |= resourceDef[pos].resourceType; - } - } - } +Client::~Client() +{ + killTimer(mainTimerID); - return resourceList; + delete resourceSet; } void Client::showPrompt() { - printf("res-client> "); - fflush(stdout); + output << "res-client> " << flush; } -void Client::updateSet(uint32_t list, uint32_t optional, bool remove) +bool Client::initialize(const CommandLineParser &parser) { - uint32_t resources[] = { - RES_AUDIO_PLAYBACK, RES_VIDEO_PLAYBACK, RES_AUDIO_RECORDING, RES_VIDEO_RECORDING, - RES_VIBRA, RES_LEDS, RES_BACKLIGHT, RES_SYSTEM_BUTTON, RES_LOCK_BUTTON, - RES_SCALE_BUTTON, RES_SNAP_BUTTON, RES_LENS_COVER, RES_HEADSET_BUTTONS, 0 - }; - - int pos = 0; - while (resources[pos]) { - if ((list & resources[pos]) == resources[pos]) { - Resource* resource = NULL; - ResourceType res = getResourceType(resources[pos]); - bool opt = (optional & resources[pos]) == resources[pos]; - - if (remove) { - if (!resourceSet->contains(res)) { - continue; - } - - if (optional) { - resource = resourceSet->resource(res); - resource->setOptional(false); - } - else { - resourceSet->deleteResource(res); - } - } - else { - if (resourceSet->contains(res)) { - resource = resourceSet->resource(res); - if (resource->isOptional() != opt) { - resource->setOptional(opt); - } + QSet<ResourcePolicy::ResourceType> allResources; + QSet<ResourcePolicy::ResourceType> optionalResources; - continue; - } - - resource = allocateResource(res, opt); - if (resource) { - resourceSet->addResourceObject(resource); - } - } - } + if (parser.shouldAlwaysReply()) { + output << "client: AlwaysReply" << endl; + } - pos++; + if (parser.shouldAutoRelease()) { + output << "client: AutoRelease" << endl; } -} -bool Client::initialize(uint32_t all, uint32_t optional, bool alwaysReply, bool autoRelease) -{ - resourceSet = new ResourceSet(applicationClass); + resourceSet = new ResourceSet(parser.resourceApplicationClass(), this, + parser.shouldAlwaysReply(), + parser.shouldAutoRelease()); if (resourceSet == NULL) { return false; } - if (alwaysReply) { - qDebug("client: alwaysReply"); - resourceSet->setAlwaysReply(); - } - - if (autoRelease) { - qDebug("client: autoRelease"); - resourceSet->setAutoRelease(); + allResources.unite(parser.resources()); + optionalResources.unite(parser.optionalResources()); + foreach(ResourcePolicy::ResourceType resource, allResources) { + resourceSet->addResource(resource); + if (optionalResources.contains(resource)) { + resourceSet->resource(resource)->setOptional(); + } } - updateSet(all, optional, false); - if (!connect(resourceSet, SIGNAL(resourcesGranted(QList<ResourcePolicy::ResourceType>)), - this, SLOT(resourceAcquiredHandler(QList<ResourcePolicy::ResourceType>)))) - { + this, SLOT(resourceAcquiredHandler(QList<ResourcePolicy::ResourceType>)))) { return false; } @@ -173,150 +99,100 @@ bool Client::initialize(uint32_t all, uint32_t optional, bool alwaysReply, bool if (!connect(resourceSet, SIGNAL(lostResources()), this, SLOT(resourceLostHandler()))) { return false; } - connect(resourceSet, SIGNAL(resourcesReleased()), this, SLOT(resourceReleasedHandler())); - - connect(resourceSet, SIGNAL(resourcesBecameAvailable(const QList<ResourcePolicy::ResourceType> &)), - this, SLOT(resourcesBecameAvailableHandler(const QList<ResourcePolicy::ResourceType> &))); + if (!connect(resourceSet, SIGNAL(resourcesReleased()), this, SLOT(resourceReleasedHandler()))) { + return false; + } + if (!connect(resourceSet, SIGNAL(resourcesBecameAvailable(const QList<ResourcePolicy::ResourceType> &)), + this, SLOT(resourcesBecameAvailableHandler(const QList<ResourcePolicy::ResourceType> &)))) { + return false; + } + if (!connect(&stdInNotifier, SIGNAL(activated(int)), this, SLOT(readLine(int)))) { + return false; + } + if (!connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()), + this, SLOT(doExit()))) { + return false; + } showPrompt(); return true; } -Resource* Client::allocateResource(ResourceType resource, bool optional) +void Client::doExit() { - Resource* retValue = NULL; + if (resourceSet != NULL) + resourceSet->release(); +} - switch (resource) { +const char * resourceTypeToString(ResourceType type) +{ + switch (type) { case AudioPlaybackType: - retValue = new AudioResource(); - break; - case VideoPlaybackType: - retValue = new VideoResource(); - break; + return "AudioPlayback"; case AudioRecorderType: - retValue = new AudioRecorderResource(); - break; + return "AudioRecording"; + case VideoPlaybackType: + return "VideoPlayback"; case VideoRecorderType: - retValue = new VideoRecorderResource(); - break; + return "VideoRegording"; case VibraType: - retValue = new VibraResource(); - break; + return "Vibra"; case LedsType: - retValue = new LedsResource(); - break; + return "Leds"; case BacklightType: - retValue = new BacklightResource(); - break; + return "Backlight"; case SystemButtonType: - retValue = new SystemButtonResource(); - break; + return "SystemButton"; case LockButtonType: - retValue = new LockButtonResource(); - break; + return "LockButton"; case ScaleButtonType: - retValue = new ScaleButtonResource(); - break; + return "ScaleButton"; case SnapButtonType: - retValue = new SnapButtonResource(); - break; + return "SnapButton"; case LensCoverType: - retValue = new LensCoverResource(); - break; + return "LensCover"; case HeadsetButtonsType: - retValue = new HeadsetButtonsResource(); - break; - case NumberOfTypes: - return NULL; - } - - if (retValue) { - retValue->setOptional(optional); + return "HeadsetButtons"; + default: + return "Unknown/Invalid Resource"; } - else { - printf("Unknown resource type - %d\n", resource); - } - - return retValue; -} - -ResourceType Client::getResourceType(uint32_t resource) -{ - switch (resource) { - case RES_AUDIO_PLAYBACK: - return AudioPlaybackType; - case RES_VIDEO_PLAYBACK: - return VideoPlaybackType; - case RES_AUDIO_RECORDING: - return AudioRecorderType; - case RES_VIDEO_RECORDING: - return VideoRecorderType; - case RES_VIBRA: - return VibraType; - case RES_LEDS: - return LedsType; - case RES_BACKLIGHT: - return BacklightType; - case RES_SYSTEM_BUTTON: - return SystemButtonType; - case RES_LOCK_BUTTON: - return LockButtonType; - case RES_SCALE_BUTTON: - return ScaleButtonType; - case RES_SNAP_BUTTON: - return SnapButtonType; - case RES_LENS_COVER: - return LensCoverType; - case RES_HEADSET_BUTTONS: - return HeadsetButtonsType; - } - - return NumberOfTypes; } -void Client::showResources(const QList<ResourceType> resList) +void Client::showResources(const QList<ResourceType> &resList) { - const char* resTypes[] = { - "AudioPlayback", "VideoPlayback", "AudioRecorder", "VideoRecorder", "Vibra", - "Leds", "Backlight", "SystemButton", "LockButton", "ScaleButton", "SnapButton", - "LensCover", "HeadsetButtons" - }; - - for (int i = 0; i < resList.count(); i++) { - printf("\t%s\n", resTypes[resList[i]]); + output << "Resource Set:\n"; + foreach(ResourceType resource, resList) { + output << "\t" << resourceTypeToString(resource) << endl; } } -void Client::showResources(const QList<Resource*> resList) +void Client::showResources(const QList<Resource*> &resList) { - const char* resTypes[] = { - "AudioPlayback", "VideoPlayback", "AudioRecorder", "VideoRecorder", "Vibra", - "Leds", "Backlight", "SystemButton", "LockButton", "ScaleButton", "SnapButton", - "LensCover", "HeadsetButtons" - }; - - for (int i = 0; i < resList.count(); i++) { - Resource* r = resList[i]; - printf("\t%s%s%s\n", resTypes[r->type()], - r->isOptional() ? " (optional)" : "", - r->isGranted() ? " (granted)" : ""); + output << "Resource Set:\n"; + foreach(Resource* resource, resList) { + output << "\t" << resourceTypeToString(resource->type()); + if (resource->isOptional()) + output << " (optional)"; + if (resource->isGranted()) + output << " (granted)"; + output << endl; } } void Client::resourceAcquiredHandler(const QList<ResourceType>& /*grantedResList*/) { - if( timeStat.markEnd() ) { - timeStat.report("\nOperation took %.2f ms\n"); + long int ms = stop_timer(); + if (ms > 0) { + output << "Operation took " << ms << "ms"; } QList<Resource*> list = resourceSet->resources(); if (!list.count()) { - printf("\nGranted resource set is empty. Possible bug?\n"); + qFatal("Granted resource set is empty. Possible bug?"); } else { - printf("\nManager grants access to these resources:\n"); - printf("Resource set:\n"); + output << "\nReceived a grant.\n"; showResources(list); } showPrompt(); @@ -324,227 +200,234 @@ void Client::resourceAcquiredHandler(const QList<ResourceType>& /*grantedResList void Client::resourceDeniedHandler() { - if( timeStat.markEnd() ) { - timeStat.report("\nOperation took %.2f ms\n"); + long int ms = stop_timer(); + if (ms > 0) { + output << "Operation took " << ms << "ms"; } - printf("\nManager denies access to resources!\n"); + output << "\nManager denies access to resources!" << endl; showPrompt(); } void Client::resourceLostHandler() { - if( timeStat.markEnd() ) { - timeStat.report("\nOperation took %.2f ms\n"); + long int ms = stop_timer(); + if (ms > 0) { + output << "Operation took " << ms << "ms"; } - printf("\nLost resources from manager!\n"); + output << "\nLost resources from manager!" << endl; showPrompt(); } void Client::resourceReleasedHandler() { - if( timeStat.markEnd() ) { - timeStat.report("\nOperation took %.2f ms\n"); + long int ms = stop_timer(); + if (ms > 0) { + output << "Operation took " << ms << "ms"; } - printf("\nAll resources released\n"); + output << "\nAll resources released" << endl; showPrompt(); } void Client::resourcesBecameAvailableHandler(const QList<ResourcePolicy::ResourceType> &availableResources) { - printf("\nManager advice: These resources are available:\n"); - printf("Resource set:\n"); + output << "Manager advice: These resources are available:\n"; showResources(availableResources); showPrompt(); } -void Client::timerEvent(QTimerEvent*) +void Client::readLine(int) { - bool quitFlag = false; - - fd_set stdinfd; - FD_ZERO(&stdinfd); - FD_SET(0, &stdinfd); - timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 0; - - int ready = select(1, &stdinfd, NULL, NULL, &tv); - if (ready > 0) { - QString line = standardInput->readLine(); - if (!line.isNull()) { - QStringList params = line.split(" "); - if (params[0] == "quit") { - quitFlag = true; - QMetaObject::invokeMethod(QCoreApplication::instance(), "quit"); + QString line = standardInput.readLine(); + if (line.isNull() || line.isEmpty()) { + showPrompt(); + return; + } + QTextStream input(&line, QIODevice::ReadOnly); + QString command; + input >> command; + if (command.isNull() || command.isEmpty()) { + qDebug("Unable to read a command"); + return; + } + + if ((command == "quit") || (command == "exit")) { + QCoreApplication::quit(); + return; + } + else if (command == "help") { + output << "Available commands:\n"; + QMap<QString, CommandListArgs>::const_iterator i = + commandList.constBegin(); + while (i != commandList.constEnd()) { + output << qSetFieldWidth(10) << right << i.key() + << qSetFieldWidth(1) << " " + << qSetFieldWidth(55) << left << i.value().args + << qSetFieldWidth(0) << i.value().help << endl; + ++i; + } + } + else if (command == "show") { + if (!resourceSet) { + qCritical("%s failed!", qPrintable(command)); + } + else { + QList<Resource*> list = resourceSet->resources(); + if (!list.count()) { + output << "Resource set is empty, use add command to add some." + << endl; + } + else { + showResources(list); } - else if (params[0] == "help") { - printf("Available commands:\n"); - printf("\t help \tprint this help message\n"); - printf("\t quit \texit application\n"); - printf("\t free \tdestroy and free the resources\n"); - printf("\t acquire\tacquire required resources\n"); - printf("\t release\trelease resources\n"); - printf("\t update\tupdate modified resource set after add or remove command\n"); - printf("\t add reslist [-o]\tadd reosurce list, if -o provided, set as optional\n"); - printf("\t remove reslist [-o]\tremove reosurce list, if -o provided, removed only optional flag\n"); - printf("\t audio pid <pid> | group <audio group> | tag <name> <value>\tset audio properties\n"); - printf("\t addaudio <audio group> <pid> <tag name> <tag value>\n"); - printf("\t show \tshow resources\n"); + } + } + else if (command == "acquire") { + start_timer(); + if (!resourceSet || !resourceSet->acquire()) { + stop_timer(); + qCritical("%s failed!", qPrintable(command)); + } + } + else if (command == "release") { + start_timer(); + if (!resourceSet || !resourceSet->release()) { + stop_timer(); + qCritical("%s failed!", qPrintable(command)); + } + } + else if (command == "add") { + QString resourceList, flag; + input >> resourceList >> flag; + if (!resourceSet) { + qCritical("%s failed!", 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; } - else if (params[0] == "show") { - if (!resourceSet) { - printf("Show failed!\n"); + foreach(ResourcePolicy::ResourceType resource, resToAdd) { + if (!resourceSet->contains(resource)) { + resourceSet->addResource(resource); } - else { - QList<Resource*> list = resourceSet->resources(); - if (!list.count()) { - printf("Resource set is empty, use add command to add some ...\n"); - } - else { - printf("Resource set:\n"); - showResources(list); - } + if (optional) { + resourceSet->resource(resource)->setOptional(); } } - else if (params[0] == "acquire") { - timeStat.markStart(); - if (!resourceSet || !resourceSet->acquire()) { - if( timeStat.markEnd() ) { - timeStat.report("Operation took %.2f ms\n"); - } - - printf("Acquire failed!\n"); + } + } + else if (command == "remove") { + QString resourceList, flag; + input >> resourceList >> flag; + if (!resourceSet) { + qCritical("%s failed!", qPrintable(command)); + } + else if (resourceList.isEmpty() || resourceList.isNull()) { + output << "List of desired resources is missing. Use help."; + } + else { + QSet<ResourcePolicy::ResourceType> resToRemove; + CommandLineParser::parseResourceList(resourceList, resToRemove); + if (flag == "-o") { + foreach(ResourcePolicy::ResourceType resource, resToRemove) { + resourceSet->resource(resource)->setOptional(false); } } - else if (params[0] == "release") { - timeStat.markStart(); - if (!resourceSet || !resourceSet->release()) { - if( timeStat.markEnd() ) { - timeStat.report("Operation took %.2f ms\n"); - } - - printf("Release failed!\n"); + else { + foreach(ResourcePolicy::ResourceType resource, resToRemove) { + resourceSet->deleteResource(resource); } } - else if (params[0] == "add") { - if (!resourceSet) { - printf("Update failed!\n"); - } - else if (params.count() == 1 || params[1].isEmpty() || params[1].isNull()) { - printf("List of desired resources is missing. See help ...\n"); - } - else { - uint32_t temp = Client::parseResourceList(params[1]); - if (params.count() > 2 && params[2] == "-o") { - updateSet(temp, temp, false); - } - else { - updateSet(temp, 0, false); - } - } + } + } + else if (command == "update") { + if (!resourceSet || !resourceSet->update()) { + qCritical("%s failed!", qPrintable(command)); + } + } + else if (command == "audio") { + QString what, group, tagName, tagValue; + quint32 pid = 0; + input >> what; + + if (what.isEmpty() || what.isNull()) { + output << "Not enough parameters! See help" << endl; + } + else { + Resource *resource = resourceSet->resource(AudioPlaybackType); + AudioResource *audioResource = static_cast<AudioResource*>(resource); + qDebug("resource = %p audioResource = %p", resource, audioResource); + if (audioResource == NULL) { + output << "No AudioResource available in set!" << endl; } - else if (params[0] == "remove") { - if (!resourceSet || params.count() == 1) { - printf("Update failed!\n"); - } - else if (params.count() == 1 || params[1].isEmpty() || params[1].isNull()) { - printf("List of desired resources is missing. See help ...\n"); + else { + if (what == "group") { + input >> group; + audioResource->setAudioGroup(group); } - else { - uint32_t temp = Client::parseResourceList(params[1]); - if (params.count() > 2 && params[2] == "-o") { - updateSet(temp, temp, true); + else if (what == "pid") { + input >> pid; + if (pid != 0) { + qDebug("Setting audio PID to %u", pid); + audioResource->setProcessID(pid); } else { - updateSet(temp, 0, true); + output << "Bad pid parameter!" << endl; } } - } - else if (params[0] == "update") { - if (!resourceSet || !resourceSet->update()) { - printf("Update failed!\n"); - } - } - else if (params[0] == "audio") { - if (params.count() < 3) { - printf("Not enough parameters! See help!\n"); - } - else { - Resource *resource = resourceSet->resource(AudioPlaybackType); - AudioResource *audioResource = static_cast<AudioResource*>(resource); - qDebug("resource = %p audioResource = %p", resource, audioResource); - if (audioResource == NULL) { - printf("No AudioResource available in set!\n"); + else if (what == "tag") { + input >> tagName >> tagValue; + if (tagName.isEmpty() || tagName.isNull() || + tagValue.isEmpty() || tagValue.isNull()) { + output << "tag requires 2 parameters name and value. See help" + << endl; } else { - if (params[1] == "group") { - audioResource->setAudioGroup(params[2]); - } - else if (params[1] == "pid") { - bool ok=false; - quint32 pid = (quint32)params[2].toInt(&ok, 10); - if (ok && pid != 0) { - qDebug("Setting audio PID to %u", pid); - audioResource->setProcessID(pid); - } - else { - printf("Bad pid parameter!\n"); - } - } - else if (params[1] == "tag") { - if (params.count() < 4) { - printf("tag requires TWO parameters name and value. See help \n"); - } - else { - audioResource->setStreamTag(params[2], params[3]); - } - } - else { - printf("Unknown audio command!\n"); - } + audioResource->setStreamTag(tagValue, tagName); } } - } - else if (params[0] == "addaudio") { - if (params.count() < 5) { - printf("Not enough parameters! See help!\n"); - } else { - AudioResource *audioResource = new AudioResource(params[1]); - if (audioResource == NULL) { - printf("Failed to create an AudioResource object!\n"); - } - else { - bool ok=false; - quint32 pid = (quint32)params[2].toInt(&ok, 10); - if (ok && pid != 0) { - audioResource->setProcessID(pid); - } - else { - printf("Bad pid parameter!\n"); - } - audioResource->setStreamTag(params[3], params[4]); - resourceSet->addResourceObject(audioResource); - } + output << "Unknown audio command!"; } } - else if (params[0] == "free") { - delete resourceSet; - resourceSet = new ResourceSet(applicationClass); - } - else if (!params[0].isEmpty()) { - QByteArray ba = line.toLatin1(); - const char *c_line = ba.data(); - printf("unknown command '%s'\n", c_line); - } + } + } + else if (command == "addaudio") { + QString group, tagName, tagValue; + quint32 pid = 0; + input >> group >> pid >> tagName >> tagValue; - if (!quitFlag) { - showPrompt(); + if (group.isEmpty() || (pid == 0) || tagName.isEmpty() || tagValue.isEmpty()) { + output << "Invalid parameters! See help!" << endl; + } + else { + AudioResource *audioResource = new AudioResource(group); + if (audioResource == NULL) { + output << "Failed to create an AudioResource object!" << endl; + } + else { + audioResource->setProcessID(pid); + audioResource->setStreamTag(tagName, tagValue); + resourceSet->addResourceObject(audioResource); } } } + else if (command == "free") { + delete resourceSet; + resourceSet = new ResourceSet(applicationClass); + } + else { + output << "unknown command '" << command << "'" << endl; + } + + showPrompt(); } |