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 "upnpexp.h"
21 #include "mthread.h"
22 #include "httpserver.h"
23 #include "taskqueue.h"
24 #include "msocketdevice.h"
25 #include "ssdpcache.h"
26 #include "upnptasknotify.h"
27 
28 #define SSDP_GROUP "239.255.255.250"
29 #define SSDP_PORT 1900
30 #define SSDP_SEARCHPORT 6549
31 
33 {
37 };
38 
40 {
45 };
46 
49 //
50 // SSDPThread Class Definition (Singleton)
51 //
54 
55 #define SocketIdx_Search 0
56 #define SocketIdx_Multicast 1
57 #define SocketIdx_Broadcast 2
58 
59 class UPNP_PUBLIC SSDP : public MThread
60 {
61  private:
62  // Singleton instance used by all.
63  static SSDP* g_pSSDP;
64 
65  QRegularExpression m_procReqLineExp {"\\s+"};
66  constexpr static int kNumberOfSockets = 3;
67  std::array<MSocketDevice*,kNumberOfSockets> m_sockets {nullptr,nullptr,nullptr};
68 
69  int m_nPort {SSDP_PORT};
70  int m_nSearchPort {SSDP_SEARCHPORT};
71  int m_nServicePort {0};
72 
73  UPnpNotifyTask *m_pNotifyTask {nullptr};
74  bool m_bAnnouncementsEnabled {false};
75 
76  bool m_bTermRequested {false};
77  QMutex m_lock;
78 
79  private:
80 
81  // ------------------------------------------------------------------
82  // Private so the singleton pattern can be enforced.
83  // ------------------------------------------------------------------
84 
85  SSDP ();
86 
87  protected:
88 
89  bool ProcessSearchRequest ( const QStringMap &sHeaders,
90  const QHostAddress& peerAddress,
91  quint16 peerPort ) const;
92  static bool ProcessSearchResponse( const QStringMap &sHeaders );
93  static bool ProcessNotify ( const QStringMap &sHeaders );
94 
95  bool IsTermRequested ();
96 
97  static QString GetHeaderValue ( const QStringMap &headers,
98  const QString &sKey,
99  const QString &sDefault );
100 
101  void ProcessData ( MSocketDevice *pSocket );
102 
103  SSDPRequestType ProcessRequestLine( const QString &sLine );
104 
105  void run() override; // MThread
106 
107  public:
108 
109  static SSDP* Instance();
110  static void Shutdown();
111 
112  ~SSDP() override;
113 
114  void RequestTerminate(void);
115 
116  void PerformSearch(const QString &sST, std::chrono::seconds timeout = 2s);
117 
118  void EnableNotifications ( int nServicePort );
119  void DisableNotifications();
120 
121  // ------------------------------------------------------------------
122 
123  static void AddListener(QObject *listener)
124  { SSDPCache::Instance()->addListener(listener); }
125  static void RemoveListener(QObject *listener)
126  { SSDPCache::Instance()->removeListener(listener); }
127 
128  static SSDPCacheEntries *Find(const QString &sURI)
129  { return SSDPCache::Instance()->Find(sURI); }
130  static DeviceLocation *Find(const QString &sURI,
131  const QString &sUSN)
132  { return SSDPCache::Instance()->Find( sURI, sUSN ); }
133 };
134 
137 //
138 // SSDPExtension Class Definition
139 //
142 
144 {
145  private:
146 
149 
150  private:
151 
152  static SSDPMethod GetMethod( const QString &sURI );
153 
154  void GetDeviceDesc( HTTPRequest *pRequest ) const;
155  void GetFile ( HTTPRequest *pRequest, const QString& sFileName );
156  static void GetDeviceList( HTTPRequest *pRequest );
157 
158  public:
159  SSDPExtension( int nServicePort, const QString &sSharePath);
160  ~SSDPExtension( ) override = default;
161 
162  QStringList GetBasePaths() override; // HttpServerExtension
163 
164  bool ProcessRequest( HTTPRequest *pRequest ) override; // HttpServerExtension
165 };
166 
167 #endif // SSDP_H
SSDPExtension::m_nServicePort
int m_nServicePort
Definition: ssdp.h:148
HTTPRequest
Definition: httprequest.h:108
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
SSDPExtension::GetBasePaths
QStringList GetBasePaths() override
Definition: ssdp.cpp:725
UPnpNotifyTask
Definition: upnptasknotify.h:52
SSDP_PORT
#define SSDP_PORT
Definition: ssdp.h:29
SSDP::g_pSSDP
static SSDP * g_pSSDP
Definition: ssdp.h:63
SSDPExtension
Definition: ssdp.h:143
SSDP::Find
static SSDPCacheEntries * Find(const QString &sURI)
Definition: ssdp.h:128
SSDPM_GetDeviceList
@ SSDPM_GetDeviceList
Definition: ssdp.h:36
SSDP_MSearch
@ SSDP_MSearch
Definition: ssdp.h:42
DeviceLocation
Definition: upnpdevice.h:209
SSDPCache::Instance
static SSDPCache * Instance()
Definition: ssdpcache.cpp:244
SSDP::Find
static DeviceLocation * Find(const QString &sURI, const QString &sUSN)
Definition: ssdp.h:130
MythObservable::addListener
void addListener(QObject *listener)
Add a listener to the observable.
Definition: mythobservable.cpp:38
SSDP_MSearchResp
@ SSDP_MSearchResp
Definition: ssdp.h:43
SSDPExtension::GetMethod
static SSDPMethod GetMethod(const QString &sURI)
Definition: ssdp.cpp:713
SSDPCache::Find
SSDPCacheEntries * Find(const QString &sURI)
Finds the SSDPCacheEntries in the cache, returns nullptr when absent.
Definition: ssdpcache.cpp:300
QStringMap
QMap< QString, QString > QStringMap
Definition: upnputil.h:44
SSDPExtension::m_sUPnpDescPath
QString m_sUPnpDescPath
Definition: ssdp.h:147
SSDPExtension::~SSDPExtension
~SSDPExtension() override=default
SSDP_Notify
@ SSDP_Notify
Definition: ssdp.h:44
SSDPCacheEntries
Definition: ssdpcache.h:34
SSDPExtension::ProcessRequest
bool ProcessRequest(HTTPRequest *pRequest) override
Definition: ssdp.cpp:737
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:318
SSDP_Unknown
@ SSDP_Unknown
Definition: ssdp.h:41
taskqueue.h
SSDPM_Unknown
@ SSDPM_Unknown
Definition: ssdp.h:34
SSDPMethod
SSDPMethod
Definition: ssdp.h:32
ssdpcache.h
SSDPRequestType
SSDPRequestType
Definition: ssdp.h:39
SSDP_SEARCHPORT
#define SSDP_SEARCHPORT
Definition: ssdp.h:30
SSDPExtension::GetDeviceList
static void GetDeviceList(HTTPRequest *pRequest)
Definition: ssdp.cpp:815
SSDPExtension::GetDeviceDesc
void GetDeviceDesc(HTTPRequest *pRequest) const
Definition: ssdp.cpp:760
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:35
SSDP
Definition: ssdp.h:59
SSDP::AddListener
static void AddListener(QObject *listener)
Definition: ssdp.h:123
httpserver.h
HttpServerExtension
Definition: httpserver.h:68
SSDP::m_lock
QMutex m_lock
Definition: ssdp.h:77
SSDPExtension::SSDPExtension
SSDPExtension(int nServicePort, const QString &sSharePath)
Definition: ssdp.cpp:700
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:783
SSDP::RemoveListener
static void RemoveListener(QObject *listener)
Definition: ssdp.h:125
upnpexp.h