Child: [8e6aa5] (diff)

Download this file

service.cxx    92 lines (79 with data), 3.1 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
/* Copyright (C) 2014 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.
*/
#include <string>
using namespace std;
#include "libupnpp/log.hxx"
#include "libupnpp/upnpplib.hxx"
#include "libupnpp/control/service.hxx"
#include "libupnpp/control/cdirectory.hxx"
namespace UPnPClient {
Service *service_factory(const string& servicetype,
const UPnPDeviceDesc& device,
const UPnPServiceDesc& service)
{
if (ContentDirectoryService::isCDService(servicetype)) {
return new ContentDirectoryService(device, service);
} else {
LOGERR("service_factory: unknown service type " << servicetype << endl);
return 0;
}
}
// A small helper class for the functions which perform
// UpnpSendAction calls: get rid of IXML docs when done.
class IxmlCleaner {
public:
IXML_Document **rqpp, **rspp;
IxmlCleaner(IXML_Document** _rqpp, IXML_Document **_rspp)
: rqpp(_rqpp), rspp(_rspp) {}
~IxmlCleaner()
{
if (*rqpp) ixmlDocument_free(*rqpp);
if (*rspp) ixmlDocument_free(*rspp);
}
};
int Service::runAction(const SoapEncodeInput& args, SoapDecodeOutput& data)
{
LibUPnP* lib = LibUPnP::getLibUPnP();
if (lib == 0) {
LOGINF("Service::runAction: no lib" << endl);
return UPNP_E_OUTOF_MEMORY;
}
UpnpClient_Handle hdl = lib->getclh();
IXML_Document *request(0);
IXML_Document *response(0);
IxmlCleaner cleaner(&request, &response);
if ((request = buildSoapBody(args, false)) == 0) {
LOGINF("Service::runAction: buildSoapBody failed" << endl);
return UPNP_E_OUTOF_MEMORY;
}
LOGDEB("Action xml: [" << ixmlPrintDocument(request) << "]" << endl);
int ret = UpnpSendAction(hdl, m_actionURL.c_str(), m_serviceType.c_str(),
0 /*devUDN*/, request, &response);
if (ret != UPNP_E_SUCCESS) {
LOGINF("Service::runAction: UpnpSendAction failed: " <<
UpnpGetErrorMessage(ret) << endl);
return ret;
}
LOGDEB("Result xml: [" << ixmlPrintDocument(response) << "]" << endl);
if (!decodeSoapBody(args.name.c_str(), response, &data)) {
LOGERR("Service::runAction: Could not decode response: " <<
ixmlPrintDocument(response) << endl);
return UPNP_E_BAD_RESPONSE;
}
return UPNP_E_SUCCESS;
}
}