Switch to unified view

a b/src/typedservice.cpp
1
#include <string>
2
#include <iostream>
3
4
#include "libupnpp/upnpplib.hxx"
5
#include "libupnpp/control/typedservice.hxx"
6
7
using namespace std;
8
using namespace UPnPClient;
9
using namespace UPnPP;
10
11
class MReporter : public UPnPClient::VarEventReporter {
12
public:
13
    void changed(const char *nm, int value) {
14
        cerr << "Reporter: changed(char *, int) invoked for nm " << nm <<
15
            " ??\n";
16
    }
17
    void changed(const char *nm, const char *value)  {
18
        cout << "Changed: " << nm << " : " << value << endl;
19
    }
20
};
21
22
int main(int argc, char **argv)
23
{
24
    argv++;argc--;
25
    if (argc < 3) {
26
        cerr << "Usage: tpservice NameOrUid partialservicetype action "
27
            "[arg [...]]\n";
28
        return 1;
29
    }
30
    string devname(*argv++);
31
    argc--;
32
    string servtp(*argv++);
33
    argc--;
34
    string actnm(*argv++);
35
    argc--;
36
37
    vector<string> args;
38
    while (argc--) {
39
        args.push_back(*argv++);
40
    }
41
42
    // Initialize libupnpp logging
43
    Logger::getTheLog("stderr")->setLogLevel(Logger::LLDEB1);
44
    // Explicitely initialize libupnpp so that we can display a
45
    // possible error
46
    LibUPnP *mylib = LibUPnP::getLibUPnP();
47
    if (!mylib) {
48
        cerr << "Can't get LibUPnP" << endl;
49
        return 1;
50
    }
51
    if (!mylib->ok()) {
52
        cerr << "Lib init failed: " <<
53
            mylib->errAsString("main", mylib->getInitError()) << endl;
54
        return 1;
55
    }
56
57
58
    TypedService *srv = findTypedService(devname, servtp, true);
59
60
    if (!srv) {
61
        cerr << "Service " << devname << "/" << servtp << " not found" << endl;
62
        return 1;
63
    }
64
65
    map<string, string> data;
66
    int ret = srv->runAction(actnm, args, data);
67
    if (ret == 0) {
68
        for (auto& entry: data) {
69
            cout << entry.first << "->" << entry.second << endl;
70
        }
71
    } else {
72
        cerr << "runAction failed with code " << ret << endl;
73
        return 1;
74
    }
75
76
    MReporter reporter;
77
    srv->installReporter(&reporter);
78
    sleep(1000);
79
    return 0;
80
}