Switch to unified view

a b/libupnpp/control/mediaserver.cxx
1
/* Copyright (C) 2014 J.F.Dockes
2
 *       This program is free software; you can redistribute it and/or modify
3
 *       it under the terms of the GNU General Public License as published by
4
 *       the Free Software Foundation; either version 2 of the License, or
5
 *       (at your option) any later version.
6
 *
7
 *       This program is distributed in the hope that it will be useful,
8
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *       GNU General Public License for more details.
11
 *
12
 *       You should have received a copy of the GNU General Public License
13
 *       along with this program; if not, write to the
14
 *       Free Software Foundation, Inc.,
15
 *       59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
18
#include <string>
19
#include <vector>
20
#include <unordered_map>
21
#include <functional>
22
using namespace std;
23
using namespace std::placeholders;
24
25
#include "libupnpp/log.hxx"
26
#include "libupnpp/control/discovery.hxx"
27
#include "libupnpp/control/mediaserver.hxx"
28
#include "libupnpp/control/cdirectory.hxx"
29
30
namespace UPnPClient {
31
32
const string 
33
MediaServer::DType("urn:schemas-upnp-org:device:MediaServer:1");
34
35
// We don't include a version in comparisons, as we are satisfied with
36
// version 1
37
bool MediaServer::isMSDevice(const string& st)
38
{
39
    const string::size_type sz(DType.size()-2);
40
    return !DType.compare(0, sz, st, 0, sz);
41
}
42
43
static bool MDAccum(unordered_map<string, UPnPDeviceDesc>* out,
44
                    const string& friendlyName,
45
                    const UPnPDeviceDesc& device, 
46
                    const UPnPServiceDesc& service)
47
{
48
    //LOGDEB("MDAccum: friendlyname: " << friendlyName << 
49
    //    " dev friendlyName " << device.friendlyName << endl);
50
    if (ContentDirectory::isCDService(service.serviceType) &&
51
        (friendlyName.empty() ? true : 
52
         !friendlyName.compare(device.friendlyName))) {
53
        //LOGDEB("MDAccum setting " << device.UDN << endl);
54
        (*out)[device.UDN] = device;
55
    }
56
    return true;
57
}
58
59
bool MediaServer::getDeviceDescs(vector<UPnPDeviceDesc>& devices, 
60
                                   const string& friendlyName)
61
{
62
    unordered_map<string, UPnPDeviceDesc> mydevs;
63
64
    UPnPDeviceDirectory::Visitor visitor = bind(MDAccum, &mydevs, friendlyName,
65
                                                _1, _2);
66
    UPnPDeviceDirectory::getTheDir()->traverse(visitor);
67
    for (auto& entry : mydevs)
68
        devices.push_back(entry.second);
69
    return !devices.empty();
70
}
71
72
MediaServer::MediaServer(const UPnPDeviceDesc& desc)
73
{
74
    bool found = false;
75
    for (auto& entry : desc.services) {
76
        if (ContentDirectory::isCDService(entry.serviceType)) {
77
            m_cds = CDSH(new ContentDirectory(desc, entry));
78
            found = true;
79
            break;
80
        }
81
    }
82
    if (!found) {
83
        LOGERR("MediaServer::MediaServer: ContentDirectory service not "
84
               "found in device" << endl);
85
    }
86
}
87
88
}
89