libupnpp  0.16.0
A C++ wrapper for the Portable UPnP reference library
service.hxx
1 /* Copyright (C) 2006-2016 J.F.Dockes
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301 USA
17  */
18 #ifndef _SERVICE_H_X_INCLUDED_
19 #define _SERVICE_H_X_INCLUDED_
20 #include "libupnpp/config.h"
21 
22 #include <sys/types.h>
23 
24 #include <functional>
25 #include <iostream>
26 #include <string>
27 
28 #include <upnp/upnp.h>
29 
30 #include "libupnpp/control/cdircontent.hxx"
31 #include "libupnpp/log.hxx"
32 #include "libupnpp/soaphelp.hxx"
33 
34 namespace UPnPClient {
35 
36 class UPnPDeviceDesc;
37 class UPnPServiceDesc;
38 class Service;
39 
51 public:
52  virtual ~VarEventReporter() {}
54  virtual void changed(const char *nm, int val) = 0;
56  virtual void changed(const char *nm, const char *val) = 0;
59  virtual void changed(const char * /*nm*/, UPnPDirObject /*meta*/) {}
61  virtual void changed(const char * /*nm*/, std::vector<int> /*ids*/) {}
62 };
63 
69 typedef
70 std::function<void (const std::unordered_map<std::string, std::string>&)>
72 
73 class Service {
74 public:
76  Service(const UPnPDeviceDesc& device, const UPnPServiceDesc& service);
77 
81  Service();
82 
83  virtual ~Service();
84 
91  bool initFromDescription(const UPnPDeviceDesc& description);
92 
93  // Restart the subscription to get all the State variable values,
94  // in case we get the events before we are ready (e.g. before the
95  // connections are set in a qt app)
96  virtual void reSubscribe();
97 
98  const std::string& getFriendlyName() const;
99  const std::string& getDeviceId() const;
100  const std::string& getServiceType() const;
101  const std::string& getActionURL() const;
102  const std::string& getModelName() const;
103  const std::string& getManufacturer() const;
104 
111  virtual int runAction(const UPnPP::SoapOutgoing& args,
112  UPnPP::SoapIncoming& data);
113 
116  int runTrivialAction(const std::string& actionName);
117 
120  template <class T> int runSimpleGet(const std::string& actnm,
121  const std::string& valnm,
122  T *valuep);
123 
125  template <class T> int runSimpleAction(const std::string& actnm,
126  const std::string& valnm,
127  T value);
128 
135  virtual VarEventReporter *getReporter();
136 
138  virtual void installReporter(VarEventReporter* reporter);
139 
149  virtual bool serviceTypeMatch(const std::string& tp) = 0;
150 
151 protected:
152 
158  virtual bool serviceInit(const UPnPDeviceDesc& device,
159  const UPnPServiceDesc& service) {
160  return true;
161  }
162 
167  void registerCallback(evtCBFunc c);
168 
176  virtual void registerCallback() {}
177 
179  void unregisterCallback();
180 
181 private:
182  // Can't copy these because this does not make sense for the
183  // member function callback.
184  Service(Service const&);
185  Service& operator=(Service const&);
186 
187  class Internal;
188  Internal *m{nullptr};
189 };
190 
191 } // namespace UPnPClient
192 
193 #endif /* _SERVICE_H_X_INCLUDED_ */
std::function< void(const std::unordered_map< std::string, std::string > &)> evtCBFunc
Type of the event callbacks.
Definition: service.hxx:71
Store the values to be encoded in a SOAP response.
Definition: soaphelp.hxx:88
Decode incoming Soap call data.
Definition: soaphelp.hxx:32
virtual void changed(const char *, UPnPDirObject)
Report change to track metadata (parsed as as Content Directory entry).
Definition: service.hxx:59
virtual void changed(const char *nm, int val)=0
Report change to named integer state variable.
Definition: service.hxx:73
virtual void registerCallback()
To be overridden in classes which actually support events.
Definition: service.hxx:176
virtual bool serviceInit(const UPnPDeviceDesc &device, const UPnPServiceDesc &service)
Service-specific part of initialization.
Definition: service.hxx:158
Data holder for a UPnP service, parsed from the device XML description.
Definition: description.hxx:46
UPnP Media Server directory entry, converted from XML data.
Definition: cdircontent.hxx:62
Data holder for a UPnP device, parsed from the XML description obtained during discovery.
Definition: description.hxx:142
Definition: service.cxx:56
UPnP Description phase: interpreting the device description which we downloaded from the URL obtained...
Definition: avlastchg.cxx:28
To be implemented by upper-level client code for event reporting.
Definition: service.hxx:50
virtual void changed(const char *, std::vector< int >)
Special for ohplaylist.
Definition: service.hxx:61