Fix compilation with wxWidgets 2.8.12
[amule.git] / src / UPnPBase.h
blob9eafbd1430f30128b68ba9438080364320bb5e46
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2011 Marcelo Roberto Jimenez ( phoenix@amule.org )
5 // Copyright (c) 2006-2011 aMule Team ( admin@amule.org / http://www.amule.org )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 // This define must not conflict with the one in the standard header
28 #ifndef AMULE_UPNP_H
29 #define AMULE_UPNP_H
32 #include <map>
33 #include <string>
34 #include <sstream>
35 #include <memory>
37 #include "UPnPCompatibility.h"
39 extern std::string stdEmptyString;
42 class CUPnPPortMapping
44 private:
45 std::string m_port;
46 std::string m_protocol;
47 std::string m_enabled;
48 std::string m_description;
49 std::string m_key;
51 public:
52 CUPnPPortMapping(
53 int port = 0,
54 const std::string &protocol = stdEmptyString,
55 bool enabled = false,
56 const std::string &description = stdEmptyString);
57 ~CUPnPPortMapping() {}
59 const std::string &getPort() const
60 { return m_port; }
61 const std::string &getProtocol() const
62 { return m_protocol; }
63 const std::string &getEnabled() const
64 { return m_enabled; }
65 const std::string &getDescription() const
66 { return m_description; }
67 const std::string &getKey() const
68 { return m_key; }
72 namespace IXML
74 namespace Document {
75 IXML_Element *GetRootElement(IXML_Document *doc);
76 void Free(IXML_Document *doc);
79 namespace Element {
80 IXML_Element *GetFirstChild(IXML_Element *parent);
81 IXML_Element *GetNextSibling(IXML_Element *child);
82 const DOMString GetTag(IXML_Element *element);
83 const std::string GetTextValue(IXML_Element *element);
84 const std::string GetChildValueByTag(IXML_Element *element, const DOMString tag);
85 IXML_Element *GetFirstChildByTag(IXML_Element *element, const DOMString tag);
86 IXML_Element *GetNextSiblingByTag(IXML_Element *element, const DOMString tag);
87 const std::string GetAttributeByTag(IXML_Element *element, const DOMString tag);
92 class CUPnPControlPoint;
94 template <typename T, char const *XML_ELEMENT_NAME, char const *XML_LIST_NAME>
95 class CXML_List : public std::map<const std::string, T *>
97 public:
98 CXML_List(
99 const CUPnPControlPoint &upnpControlPoint,
100 IXML_Element *parent,
101 const std::string &url);
102 ~CXML_List();
106 template <typename T, char const *XML_ELEMENT_NAME, char const *XML_LIST_NAME>
107 CXML_List<T, XML_ELEMENT_NAME, XML_LIST_NAME>::CXML_List(
108 const CUPnPControlPoint &upnpControlPoint,
109 IXML_Element *parent,
110 const std::string &url)
112 IXML_Element *elementList = IXML::Element::GetFirstChildByTag(parent, XML_LIST_NAME);
113 unsigned int i = 0;
114 for ( IXML_Element *element = IXML::Element::GetFirstChildByTag(elementList, XML_ELEMENT_NAME);
115 element;
116 element = IXML::Element::GetNextSiblingByTag(element, XML_ELEMENT_NAME)) {
117 // Add a new element to the element list
118 T *upnpElement = new T(upnpControlPoint, element, url);
119 (*this)[upnpElement->GetKey()] = upnpElement;
120 ++i;
122 std::ostringstream msg;
123 msg << "\n " << XML_LIST_NAME << ": " <<
124 i << " " << XML_ELEMENT_NAME << "s.";
125 AddDebugLogLineN(logUPnP, msg);
129 template <typename T, char const *XML_ELEMENT_NAME, char const *XML_LIST_NAME>
130 CXML_List<T, XML_ELEMENT_NAME, XML_LIST_NAME>::~CXML_List()
132 typename CXML_List<T, XML_ELEMENT_NAME, XML_LIST_NAME>::iterator it;
133 for(it = this->begin(); it != this->end(); ++it) {
134 delete (*it).second;
138 extern const char s_argument[];
139 extern const char s_argumentList[];
140 extern const char s_action[];
141 extern const char s_actionList[];
142 extern const char s_allowedValue[];
143 extern const char s_allowedValueList[];
144 extern const char s_stateVariable[];
145 extern const char s_serviceStateTable[];
146 extern const char s_service[];
147 extern const char s_serviceList[];
148 extern const char s_device[];
149 extern const char s_deviceList[];
153 class CUPnPArgument;
154 typedef CXML_List<CUPnPArgument, s_argument, s_argumentList> ArgumentList;
155 class CUPnPAction;
156 typedef CXML_List<CUPnPAction, s_action, s_actionList> ActionList;
157 class CUPnPStateVariable;
158 typedef CXML_List<CUPnPStateVariable, s_stateVariable, s_serviceStateTable> ServiceStateTable;
159 class CUPnPAllowedValue;
160 typedef CXML_List<CUPnPAllowedValue, s_allowedValue, s_allowedValueList> AllowedValueList;
161 class CUPnPService;
162 typedef CXML_List<CUPnPService, s_service, s_serviceList> ServiceList;
163 class CUPnPDevice;
164 typedef CXML_List<CUPnPDevice, s_device, s_deviceList> DeviceList;
167 class CUPnPError
169 private:
170 IXML_Element *m_root;
171 const std::string m_ErrorCode;
172 const std::string m_ErrorDescription;
173 public:
174 CUPnPError(IXML_Document *errorDoc);
175 ~CUPnPError() {}
176 const std::string &getErrorCode() const
177 { return m_ErrorCode; }
178 const std::string &getErrorDescription() const
179 { return m_ErrorDescription; }
183 class CUPnPArgument
185 private:
186 const std::string m_name;
187 const std::string m_direction;
188 bool m_retval;
189 const std::string m_relatedStateVariable;
191 public:
192 CUPnPArgument(
193 const CUPnPControlPoint &upnpControlPoint,
194 IXML_Element *argument,
195 const std::string &SCPDURL);
196 ~CUPnPArgument() {}
197 const std::string &GetName() const
198 { return m_name; }
199 const std::string &GetDirection() const
200 { return m_direction; }
201 bool GetRetVal() const
202 { return m_retval; }
203 const std::string &GetRelatedStateVariable() const
204 { return m_relatedStateVariable; }
205 const std::string &GetKey() const
206 { return m_name; }
211 class CUPnPAction
213 private:
214 ArgumentList m_ArgumentList;
215 const std::string m_name;
217 public:
218 CUPnPAction(
219 const CUPnPControlPoint &upnpControlPoint,
220 IXML_Element *action,
221 const std::string &SCPDURL);
222 ~CUPnPAction() {}
223 const std::string &GetName() const
224 { return m_name; }
225 const std::string &GetKey() const
226 { return m_name; }
227 const ArgumentList &GetArgumentList() const
228 { return m_ArgumentList; }
232 class CUPnPAllowedValue
234 private:
235 const std::string m_allowedValue;
237 public:
238 CUPnPAllowedValue(
239 const CUPnPControlPoint &upnpControlPoint,
240 IXML_Element *allowedValue,
241 const std::string &SCPDURL);
242 ~CUPnPAllowedValue() {}
243 const std::string &GetAllowedValue() const
244 { return m_allowedValue; }
245 const std::string &GetKey() const
246 { return m_allowedValue; }
250 class CUPnPStateVariable
252 private:
253 AllowedValueList m_AllowedValueList;
254 const std::string m_name;
255 const std::string m_dataType;
256 const std::string m_defaultValue;
257 const std::string m_sendEvents;
259 public:
260 CUPnPStateVariable(
261 const CUPnPControlPoint &upnpControlPoint,
262 IXML_Element *stateVariable,
263 const std::string &URLBase);
264 ~CUPnPStateVariable() {}
265 const std::string &GetNname() const
266 { return m_name; }
267 const std::string &GetDataType() const
268 { return m_dataType; }
269 const std::string &GetDefaultValue() const
270 { return m_defaultValue; }
271 const std::string &GetKey() const
272 { return m_name; }
273 const AllowedValueList &GetAllowedValueList() const
274 { return m_AllowedValueList; }
278 class CUPnPSCPD
280 private:
281 ActionList m_ActionList;
282 ServiceStateTable m_ServiceStateTable;
283 const std::string m_SCPDURL;
285 public:
286 CUPnPSCPD(
287 const CUPnPControlPoint &upnpControlPoint,
288 IXML_Element *scpd,
289 const std::string &SCPDURL);
290 ~CUPnPSCPD() {}
291 const ActionList &GetActionList() const
292 { return m_ActionList; }
293 const ServiceStateTable &GetServiceStateTable() const
294 { return m_ServiceStateTable; }
298 class CUPnPArgumentValue
300 private:
301 std::string m_argument;
302 std::string m_value;
304 public:
305 CUPnPArgumentValue();
306 CUPnPArgumentValue(const std::string &argument, const std::string &value);
307 ~CUPnPArgumentValue() {}
309 const std::string &GetArgument() const { return m_argument; }
310 const std::string &GetValue() const { return m_value; }
311 const std::string &SetArgument(const std::string& argument) { return m_argument = argument; }
312 const std::string &SetValue(const std::string &value) { return m_value = value; }
316 class CUPnPService
318 private:
319 const CUPnPControlPoint &m_UPnPControlPoint;
320 const std::string m_serviceType;
321 const std::string m_serviceId;
322 const std::string m_SCPDURL;
323 const std::string m_controlURL;
324 const std::string m_eventSubURL;
325 std::string m_absSCPDURL;
326 std::string m_absControlURL;
327 std::string m_absEventSubURL;
328 int m_timeout;
329 Upnp_SID m_SID;
330 std::auto_ptr<CUPnPSCPD> m_SCPD;
332 public:
333 CUPnPService(
334 const CUPnPControlPoint &upnpControlPoint,
335 IXML_Element *service,
336 const std::string &URLBase);
337 ~CUPnPService();
339 const std::string &GetServiceType() const
340 { return m_serviceType; }
341 const std::string &GetServiceId() const
342 { return m_serviceId; }
343 const std::string &GetSCPDURL() const
344 { return m_SCPDURL; }
345 const std::string &GetAbsSCPDURL() const
346 { return m_absSCPDURL; }
347 const std::string &GetControlURL() const
348 { return m_controlURL; }
349 const std::string &GetEventSubURL() const
350 { return m_eventSubURL; }
351 const std::string &GetAbsControlURL() const
352 { return m_absControlURL; }
353 const std::string &GetAbsEventSubURL() const
354 { return m_absEventSubURL; }
355 int GetTimeout() const
356 { return m_timeout; }
357 void SetTimeout(int t)
358 { m_timeout = t; }
359 int *GetTimeoutAddr()
360 { return &m_timeout; }
361 char *GetSID()
362 { return m_SID; }
363 void SetSID(const char *s)
364 { memcpy(m_SID, s, sizeof(Upnp_SID)); }
365 const std::string &GetKey() const
366 { return m_serviceId; }
367 bool IsSubscribed() const
368 { return m_SCPD.get() != NULL; }
369 void SetSCPD(CUPnPSCPD *SCPD)
370 { m_SCPD.reset(SCPD); }
372 bool Execute(
373 const std::string &ActionName,
374 const std::vector<CUPnPArgumentValue> &ArgValue) const;
375 const std::string GetStateVariable(
376 const std::string &stateVariableName) const;
380 class CUPnPDevice
382 private:
383 // Please, lock these lists before use
384 DeviceList m_DeviceList;
385 ServiceList m_ServiceList;
387 const std::string m_deviceType;
388 const std::string m_friendlyName;
389 const std::string m_manufacturer;
390 const std::string m_manufacturerURL;
391 const std::string m_modelDescription;
392 const std::string m_modelName;
393 const std::string m_modelNumber;
394 const std::string m_modelURL;
395 const std::string m_serialNumber;
396 const std::string m_UDN;
397 const std::string m_UPC;
398 std::string m_presentationURL;
400 public:
401 CUPnPDevice(
402 const CUPnPControlPoint &upnpControlPoint,
403 IXML_Element *device,
404 const std::string &URLBase);
405 ~CUPnPDevice() {}
407 const std::string &GetUDN() const
408 { return m_UDN; }
409 const std::string &GetDeviceType() const
410 { return m_deviceType; }
411 const std::string &GetFriendlyName() const
412 { return m_friendlyName; }
413 const std::string &GetPresentationURL() const
414 { return m_presentationURL; }
415 const std::string &GetKey() const
416 { return m_UDN; }
420 class CUPnPRootDevice : public CUPnPDevice
422 private:
423 const std::string m_URLBase;
424 const std::string m_location;
425 int m_expires;
427 public:
428 CUPnPRootDevice(
429 const CUPnPControlPoint &upnpControlPoint,
430 IXML_Element *rootDevice,
431 const std::string &OriginalURLBase,
432 const std::string &FixedURLBase,
433 const char *location,
434 int expires);
435 ~CUPnPRootDevice() {}
437 const std::string &GetURLBase() const
438 { return m_URLBase; }
439 const std::string &GetLocation() const
440 { return m_location; }
441 int GetExpires() const
442 { return m_expires; }
443 void SetExpires(int expires)
444 { m_expires = expires; }
448 typedef std::map<const std::string, CUPnPRootDevice *> RootDeviceMap;
449 typedef std::map<const std::string, CUPnPService *> ServiceMap;
450 typedef std::map<const std::string, CUPnPPortMapping> PortMappingMap;
453 class CUPnPControlPoint
455 private:
456 static CUPnPControlPoint *s_CtrlPoint;
457 // upnp stuff
458 UpnpClient_Handle m_UPnPClientHandle;
459 RootDeviceMap m_RootDeviceMap;
460 ServiceMap m_ServiceMap;
461 PortMappingMap m_ActivePortMappingsMap;
462 CUPnPMutex m_RootDeviceListMutex;
463 bool m_IGWDeviceDetected;
464 //#warning This variable is for testing purposes only and should disappear on release.
465 CUPnPService *m_WanService;
466 CUPnPMutex m_WaitForSearchTimeoutMutex;
468 public:
469 CUPnPControlPoint(unsigned short udpPort);
470 ~CUPnPControlPoint();
471 void Subscribe(CUPnPService &service);
472 void Unsubscribe(CUPnPService &service);
473 bool AddPortMappings(
474 std::vector<CUPnPPortMapping> &upnpPortMapping);
475 bool DeletePortMappings(
476 std::vector<CUPnPPortMapping> &upnpPortMapping);
478 UpnpClient_Handle GetUPnPClientHandle() const
479 { return m_UPnPClientHandle; }
481 bool GetIGWDeviceDetected() const
482 { return m_IGWDeviceDetected; }
483 void SetIGWDeviceDetected(bool b)
484 { m_IGWDeviceDetected = b; }
485 bool WanServiceDetected() const
486 { return !m_ServiceMap.empty(); }
487 void SetWanService(CUPnPService *service)
488 { m_WanService = service; }
490 // Callback function
491 static int Callback(
492 Upnp_EventType EventType,
493 void* Event,
494 void* Cookie);
496 private:
497 void OnEventReceived(
498 const std::string &Sid,
499 int EventKey,
500 IXML_Document *ChangedVariables);
501 void AddRootDevice(
502 IXML_Element *rootDevice,
503 const std::string &urlBase,
504 const char *location,
505 int expires);
506 void RemoveRootDevice(
507 const char *udn);
508 void RefreshPortMappings();
509 bool PrivateAddPortMapping(
510 CUPnPPortMapping &upnpPortMapping);
511 bool PrivateDeletePortMapping(
512 CUPnPPortMapping &upnpPortMapping);
516 #endif /* AMULE_UPNP_H */
518 // File_checked_for_headers