MythTV  master
ssdp.h
Go to the documentation of this file.
1 // Program Name: ssdp.h
3 // Created : Oct. 1, 2005
4 //
5 // Purpose : SSDP Discovery Service Implmenetation
6 //
7 // Copyright (c) 2005 David Blain <dblain@mythtv.org>
8 //
9 // Licensed under the GPL v2 or later, see LICENSE for details
10 //
12 
13 #ifndef SSDP_H
14 #define SSDP_H
15 
16 #include <array>
17 
18 #include <QFile>
19 
20 #include "libmythbase/mthread.h"
21 
22 #include "upnpexp.h"
23 #include "httpserver.h"
24 #include "taskqueue.h"
25 #include "msocketdevice.h"
26 #include "ssdpcache.h"
27 #include "upnptasknotify.h"
28 
29 static constexpr const char* SSDP_GROUP { "239.255.255.250" };
30 static constexpr uint16_t SSDP_PORT { 1900 };
31 static constexpr uint16_t SSDP_SEARCHPORT { 6549 };
32 
34 {
38 };
39 
41 {
46 };
47 
50 //
51 // SSDPThread Class Definition (Singleton)
52 //
55 
57 {
61 };
62 
63 class UPNP_PUBLIC SSDP : public MThread
64 {
65  private:
66  // Singleton instance used by all.
67  static SSDP* g_pSSDP;
68 
69  QRegularExpression m_procReqLineExp {"\\s+"};
70  constexpr static int kNumberOfSockets = 3;
71  std::array<MSocketDevice*,kNumberOfSockets> m_sockets {nullptr,nullptr,nullptr};
72 
73  int m_nPort {SSDP_PORT};
74  int m_nSearchPort {SSDP_SEARCHPORT};
75  int m_nServicePort {0};
76 
77  UPnpNotifyTask *m_pNotifyTask {nullptr};
78  bool m_bAnnouncementsEnabled {false};
79 
80  bool m_bTermRequested {false};
81  QMutex m_lock;
82 
83  private:
84 
85  // ------------------------------------------------------------------
86  // Private so the singleton pattern can be enforced.
87  // ------------------------------------------------------------------
88 
89  SSDP ();
90 
91  protected:
92 
93  bool ProcessSearchRequest ( const QStringMap &sHeaders,
94  const QHostAddress& peerAddress,
95  quint16 peerPort ) const;
96  static bool ProcessSearchResponse( const QStringMap &sHeaders );
97  static bool ProcessNotify ( const QStringMap &sHeaders );
98 
99  bool IsTermRequested ();
100 
101  static QString GetHeaderValue ( const QStringMap &headers,
102  const QString &sKey,
103  const QString &sDefault );
104 
105  void ProcessData ( MSocketDevice *pSocket );
106 
107  SSDPRequestType ProcessRequestLine( const QString &sLine );
108 
109  void run() override; // MThread
110 
111  public:
112 
113  static SSDP* Instance();
114  static void Shutdown();
115 
116  ~SSDP() override;
117 
118  void RequestTerminate(void);
119 
120  void PerformSearch(const QString &sST, std::chrono::seconds timeout = 2s);
121 
122  void EnableNotifications ( int nServicePort );
123  void DisableNotifications();
124 
125  // ------------------------------------------------------------------
126 
127  static void AddListener(QObject *listener)
128  { SSDPCache::Instance()->addListener(listener); }
129  static void RemoveListener(QObject *listener)
130  { SSDPCache::Instance()->removeListener(listener); }
131 
132  static SSDPCacheEntries *Find(const QString &sURI)
133  { return SSDPCache::Instance()->Find(sURI); }
134  static DeviceLocation *Find(const QString &sURI,
135  const QString &sUSN)
136  { return SSDPCache::Instance()->Find( sURI, sUSN ); }
137 };
138 
141 //
142 // SSDPExtension Class Definition
143 //
146 
148 {
149  private:
150 
153 
154  private:
155 
156  static SSDPMethod GetMethod( const QString &sURI );
157 
158  void GetDeviceDesc( HTTPRequest *pRequest ) const;
159  void GetFile ( HTTPRequest *pRequest, const QString& sFileName );
160  static void GetDeviceList( HTTPRequest *pRequest );
161 
162  public:
163  SSDPExtension( int nServicePort, const QString &sSharePath);
164  ~SSDPExtension( ) override = default;
165 
166  QStringList GetBasePaths() override; // HttpServerExtension
167 
168  bool ProcessRequest( HTTPRequest *pRequest ) override; // HttpServerExtension
169 };
170 
171 #endif // SSDP_H
SSDPExtension::m_nServicePort
int m_nServicePort
Definition: ssdp.h:152
HTTPRequest
Definition: httprequest.h:109
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
SSDPExtension::GetBasePaths
QStringList GetBasePaths() override
Definition: ssdp.cpp:717
UPnpNotifyTask
Definition: upnptasknotify.h:53
SSDP::g_pSSDP
static SSDP * g_pSSDP
Definition: ssdp.h:67
SSDPExtension
Definition: ssdp.h:147
SSDP::Find
static SSDPCacheEntries * Find(const QString &sURI)
Definition: ssdp.h:132
SSDPM_GetDeviceList
@ SSDPM_GetDeviceList
Definition: ssdp.h:37
SSDP_MSearch
@ SSDP_MSearch
Definition: ssdp.h:43
DeviceLocation
Definition: upnpdevice.h:210
SSDPCache::Instance
static SSDPCache * Instance()
Definition: ssdpcache.cpp:245
SSDP::Find
static DeviceLocation * Find(const QString &sURI, const QString &sUSN)
Definition: ssdp.h:134
MythObservable::addListener
void addListener(QObject *listener)
Add a listener to the observable.
Definition: mythobservable.cpp:38
SocketIdx_Broadcast
@ SocketIdx_Broadcast
Definition: ssdp.h:60
SSDP_MSearchResp
@ SSDP_MSearchResp
Definition: ssdp.h:44
SocketIdx_Multicast
@ SocketIdx_Multicast
Definition: ssdp.h:59
SSDPExtension::GetMethod
static SSDPMethod GetMethod(const QString &sURI)
Definition: ssdp.cpp:705
SSDPCache::Find
SSDPCacheEntries * Find(const QString &sURI)
Finds the SSDPCacheEntries in the cache, returns nullptr when absent.
Definition: ssdpcache.cpp:301
QStringMap
QMap< QString, QString > QStringMap
Definition: upnputil.h:32
SSDPExtension::m_sUPnpDescPath
QString m_sUPnpDescPath
Definition: ssdp.h:151
SSDPExtension::~SSDPExtension
~SSDPExtension() override=default
SSDP_Notify
@ SSDP_Notify
Definition: ssdp.h:45
SSDPCacheEntries
Definition: ssdpcache.h:35
SSDP_SEARCHPORT
static constexpr uint16_t SSDP_SEARCHPORT
Definition: ssdp.h:31
SSDPExtension::ProcessRequest
bool ProcessRequest(HTTPRequest *pRequest) override
Definition: ssdp.cpp:729
MThread::run
virtual void run(void)
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
Definition: mthread.cpp:315
SSDP_Unknown
@ SSDP_Unknown
Definition: ssdp.h:42
SocketIdxType
SocketIdxType
Definition: ssdp.h:56
taskqueue.h
SSDPM_Unknown
@ SSDPM_Unknown
Definition: ssdp.h:35
SSDPMethod
SSDPMethod
Definition: ssdp.h:33
ssdpcache.h
SSDPRequestType
SSDPRequestType
Definition: ssdp.h:40
SSDPExtension::GetDeviceList
static void GetDeviceList(HTTPRequest *pRequest)
Definition: ssdp.cpp:807
SSDPExtension::GetDeviceDesc
void GetDeviceDesc(HTTPRequest *pRequest) const
Definition: ssdp.cpp:752
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
upnptasknotify.h
mthread.h
UPNP_PUBLIC
#define UPNP_PUBLIC
Definition: upnpexp.h:9
SSDPM_GetDeviceDesc
@ SSDPM_GetDeviceDesc
Definition: ssdp.h:36
SSDP
Definition: ssdp.h:63
SocketIdx_Search
@ SocketIdx_Search
Definition: ssdp.h:58
SSDP::AddListener
static void AddListener(QObject *listener)
Definition: ssdp.h:127
uint16_t
unsigned short uint16_t
Definition: iso6937tables.h:3
httpserver.h
SSDP_GROUP
static constexpr const char * SSDP_GROUP
Definition: ssdp.h:29
HttpServerExtension
Definition: httpserver.h:71
SSDP::m_lock
QMutex m_lock
Definition: ssdp.h:81
SSDPExtension::SSDPExtension
SSDPExtension(int nServicePort, const QString &sSharePath)
Definition: ssdp.cpp:692
MythObservable::removeListener
void removeListener(QObject *listener)
Remove a listener to the observable.
Definition: mythobservable.cpp:55
SSDPExtension::GetFile
void GetFile(HTTPRequest *pRequest, const QString &sFileName)
Definition: ssdp.cpp:775
SSDP::RemoveListener
static void RemoveListener(QObject *listener)
Definition: ssdp.h:129
SSDP_PORT
static constexpr uint16_t SSDP_PORT
Definition: ssdp.h:30
upnpexp.h