summaryrefslogtreecommitdiff
path: root/resourceqt-client/client.cpp
blob: daf0486bfc13fdd04dcb7e8daaa44216f54ed5d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include <QtCore/QCoreApplication>
#include <QtCore/QFile>
#include <QTextStream>

#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>

#include "client.h"

using namespace ResourcePolicy;

QMap<QString, CommandListArgs> Client::commandList;

CommandListArgs::CommandListArgs()
    :args(), help()
{
}


CommandListArgs::CommandListArgs(const QString &arguments, const QString &helpText)
    :args(arguments), help(helpText)
{
}

CommandListArgs::~CommandListArgs()
{
}

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 reosurce list, if -o provided, set as optional");
    commandList["remove"] = CommandListArgs("reslist [-o]","remove reosurce 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 porpoerties");
    commandList["show"] = CommandListArgs("", "show resources");

}

Client::~Client()
{
    killTimer(mainTimerID);

    delete resourceSet;
}

void Client::showPrompt()
{
    output << "res-client> " << flush;
}

bool Client::initialize(const CommandLineParser &parser)
{
	QSet<ResourcePolicy::ResourceType> allResources;
	QSet<ResourcePolicy::ResourceType> optionalResources;

    if (parser.shouldAlwaysReply()) {
        output << "client: AlwaysReply" << endl;
    }

    if (parser.shouldAutoRelease()) {
        output << "client: AutoRelease" << endl;
    }

    resourceSet = new ResourceSet(parser.resourceApplicationClass(), this,
                                  parser.shouldAlwaysReply(),
                                  parser.shouldAutoRelease());
    if (resourceSet == NULL) {
        return false;
    }

    allResources.unite(parser.resources());
    optionalResources.unite(parser.optionalResources());
    foreach (ResourcePolicy::ResourceType resource, allResources) {
        resourceSet->addResource(resource);
        if (optionalResources.contains(resource)) {
            resourceSet->resource(resource)->setOptional();
        }
    }

    if (!connect(resourceSet, SIGNAL(resourcesGranted(QList<ResourcePolicy::ResourceType>)),
                 this, SLOT(resourceAcquiredHandler(QList<ResourcePolicy::ResourceType>))))
    {
        return false;
    }

    if (!connect(resourceSet, SIGNAL(resourcesDenied()), this, SLOT(resourceDeniedHandler()))) {
        return false;
    }

    if (!connect(resourceSet, SIGNAL(lostResources()), this, SLOT(resourceLostHandler()))) {
        return false;
    }
    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;
}

void Client::doExit ()
{
    if (resourceSet != NULL)
        resourceSet->release();
}

const char * resourceTypeToString(ResourceType type)
{
    switch (type) {
        case AudioPlaybackType:
            return "AudioPlayback";
        case AudioRecorderType:
            return "AudioRecording";
        case VideoPlaybackType:
            return "VideoPlayback";
        case VideoRecorderType:
            return "VideoRegording";
        case VibraType:
            return "Vibra";
        case LedsType:
            return "Leds";
        case BacklightType:
            return "Backlight";
        case SystemButtonType:
            return "SystemButton";
        case LockButtonType:
            return "LockButton";
        case ScaleButtonType:
            return "ScaleButton";
        case SnapButtonType:
            return "SnapButton";
        case LensCoverType:
            return "LensCover";
        case HeadsetButtonsType:
            return "HeadsetButtons";
        default:
            return "Unknown/Invalid Resource";
    }
}

void Client::showResources(const QList<ResourceType> &resList)
{
    output << "Resource Set:\n";
    foreach (ResourceType resource, resList) {
        output << "\t" << resourceTypeToString(resource) << endl;
    }
}

void Client::showResources(const QList<Resource*> &resList)
{
    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*/)
{
    double ms = stop_timer();
    if( ms > 0.0 ) {
        output << "Operation took " << (quint32)ms << "ms";
    }

    QList<Resource*> list = resourceSet->resources();
    if (!list.count()) {
        qFatal("Granted resource set is empty. Possible bug?");
    }
    else {
        output << "\nReceived a grant.\n";
        showResources(list);
    }
    showPrompt();
}

void Client::resourceDeniedHandler()
{
    double ms = stop_timer();
    if( ms > 0.0 ) {
        output << "Operation took " << (quint32)ms << "ms";
    }

    output << "\nManager denies access to resources!" << endl;
    showPrompt();
}

void Client::resourceLostHandler()
{
    double ms = stop_timer();
    if( ms > 0.0 ) {
        output << "Operation took " << (quint32)ms << "ms";
    }

    output << "\nLost resources from manager!" << endl;
    showPrompt();
}

void Client::resourceReleasedHandler()
{
    double ms = stop_timer();
    if( ms > 0.0 ) {
        output << "Operation took " << (quint32)ms << "ms";
    }

    output << "\nAll resources released" << endl;
    showPrompt();
}

void Client::resourcesBecameAvailableHandler(const QList<ResourcePolicy::ResourceType> &availableResources)
{
    output << "Manager advice: These resources are available:\n";
    showResources(availableResources);
    showPrompt();
}

void Client::readLine(int)
{
    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 (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;
            }
            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 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 {
                foreach (ResourcePolicy::ResourceType resource, resToRemove) {
                    resourceSet->deleteResource(resource);
                }
            }
        }
    }
    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 (what == "group") {
                    input >> group;
                    audioResource->setAudioGroup(group);
                }
                else if (what == "pid") {
                    input >> pid;
                    if (pid != 0) {
                        qDebug("Setting audio PID to %u", pid);
                        audioResource->setProcessID(pid);
                    }
                    else {
                        output << "Bad pid parameter!" << endl;
                    }
                }
                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 {
                        audioResource->setStreamTag(tagValue, tagName);
                    }
                }
                else {
                    output << "Unknown audio command!";
                }
            }
        }
    }
    else if (command == "addaudio") {
        QString group, tagName, tagValue;
        quint32 pid=0;
        input >> group >> pid >> tagName >> tagValue;

        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();
}