Download this file

ohsender.cpp    171 lines (147 with data), 4.4 kB

  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
/* Copyright (C) 2013 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// Code to exercise the libupnpp OhSender class
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include "libupnpp/upnpplib.hxx"
#include "libupnpp/log.hxx"
#include "libupnpp/upnpputils.hxx"
#include "libupnpp/control/cdirectory.hxx"
#include "libupnpp/control/discovery.hxx"
#include "libupnpp/control/mediarenderer.hxx"
#include "libupnpp/control/ohsender.hxx"
#include "libupnpp/control/linnsongcast.hxx"
using namespace std;
using namespace UPnPClient;
using namespace UPnPP;
class MReporter : public UPnPClient::VarEventReporter {
public:
void changed(const char *nm, int value) {
cout << "Changed: " << nm << " : " << value << endl;
}
void changed(const char *nm, const char *value) {
cout << "Changed: " << nm << " : " << value << endl;
}
void changed(const char *nm, UPnPDirObject meta) {
cout << "Changed: " << nm << " : " << meta.dump() << endl;
}
};
void rdMonitor(OHSNH hdl)
{
MReporter reporter;
hdl->installReporter(&reporter);
while (true) {
sleep(2);
string uri;
string meta;
int ret;
if ((ret = hdl->metadata(uri, meta)) == 0) {
cout << "Uri: " << uri << " metadata " << meta << endl;
} else {
cerr << "Metadata: failed: " << ret << endl;
}
}
}
void metadata(OHSNH hdl)
{
string uri, meta;
int ret;
if ((ret = hdl->metadata(uri, meta)) != 0) {
cerr << "metadata failed: " << ret << endl;
return;
}
cout << "read: uri: [" << uri << "] meta: " << meta << endl;
}
static char *thisprog;
static char usage [] =
" -M <renderer>: monitor OHSender\n"
" -m <renderer>: run metadata\n"
" \n"
;
static void
Usage(void)
{
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
exit(1);
}
static int op_flags;
#define OPT_M 0x1
#define OPT_m 0x2
static struct option long_options[] = {
{0, 0, 0, 0}
};
int main(int argc, char *argv[])
{
string fname;
string arg;
thisprog = argv[0];
int ret;
int option_index = 0;
while ((ret = getopt_long(argc, argv, "Mm",
long_options, &option_index)) != -1) {
switch (ret) {
case 'M': if (op_flags) Usage(); op_flags |= OPT_M; break;
case 'm': if (op_flags) Usage(); op_flags |= OPT_m; break;
default:
Usage();
}
}
if (!op_flags)
Usage();
if (op_flags & (OPT_M|OPT_m)) {
if (optind != argc - 1)
Usage();
fname = argv[optind++];
}
if (Logger::getTheLog("/tmp/ohsender.log") == 0) {
cerr << "Can't initialize log" << endl;
return 1;
}
Logger::getTheLog("")->setLogLevel(Logger::LLDEB1);
LibUPnP *mylib = LibUPnP::getLibUPnP();
if (!mylib) {
cerr << "Can't get LibUPnP" << endl;
return 1;
}
if (!mylib->ok()) {
cerr << "Lib init failed: " <<
mylib->errAsString("main", mylib->getInitError()) << endl;
return 1;
}
mylib->setLogFileName("/tmp/libupnp.log", LibUPnP::LogLevelDebug);
string reason;
OHSNH hdl = Songcast::getSender(fname, reason);
if (!hdl) {
cerr << "Device has no OpenHome Sender service" << endl;
return 1;
}
if ((op_flags & OPT_M)) {
rdMonitor(hdl);
} else if ((op_flags & OPT_m)) {
metadata(hdl);
} else {
Usage();
}
return 0;
}