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 COPYING for details
10 //
12 
13 #ifndef SSDP_H
14 #define SSDP_H
15 
16 #include <QFile>
17 
18 #include "upnpexp.h"
19 #include "mthread.h"
20 #include "httpserver.h"
21 #include "taskqueue.h"
22 #include "msocketdevice.h"
23 #include "ssdpcache.h"
24 #include "upnptasknotify.h"
25 
26 #define SSDP_GROUP "239.255.255.250"
27 #define SSDP_PORT 1900
28 #define SSDP_SEARCHPORT 6549
29 
31 {
35 };
36 
38 {
42  SSDP_Notify = 3
43 };
44 
47 //
48 // SSDPThread Class Definition (Singleton)
49 //
52 
53 #define SocketIdx_Search 0
54 #define SocketIdx_Multicast 1
55 #define SocketIdx_Broadcast 2
56 
57 class UPNP_PUBLIC SSDP : public MThread
58 {
59  private:
60  // Singleton instance used by all.
61  static SSDP* g_pSSDP;
62 
63  QRegularExpression m_procReqLineExp {"\\s+"};
64  constexpr static int kNumberOfSockets = 3;
65  MSocketDevice *m_sockets[kNumberOfSockets] {nullptr,nullptr,nullptr};
66 
67  int m_nPort {SSDP_PORT};
68  int m_nSearchPort {SSDP_SEARCHPORT};
69  int m_nServicePort {0};
70 
71  UPnpNotifyTask *m_pNotifyTask {nullptr};
72  bool m_bAnnouncementsEnabled {false};
73 
74  bool m_bTermRequested {false};
75  QMutex m_lock {QMutex::NonRecursive};
76 
77  private:
78 
79  // ------------------------------------------------------------------
80  // Private so the singleton pattern can be inforced.
81  // ------------------------------------------------------------------
82 
83  SSDP ();
84 
85  protected:
86 
87  bool ProcessSearchRequest ( const QStringMap &sHeaders,
88  const QHostAddress& peerAddress,
89  quint16 peerPort ) const;
90  static bool ProcessSearchResponse( const QStringMap &sHeaders );
91  static bool ProcessNotify ( const QStringMap &sHeaders );
92 
93  bool IsTermRequested ();
94 
95  static QString GetHeaderValue ( const QStringMap &headers,
96  const QString &sKey,
97  const QString &sDefault );
98 
99  void ProcessData ( MSocketDevice *pSocket );
100 
101  SSDPRequestType ProcessRequestLine( const QString &sLine );
102 
103  void run() override; // MThread
104 
105  public:
106 
107  static SSDP* Instance();
108  static void Shutdown();
109 
110  ~SSDP() override;
111 
112  void RequestTerminate(void);
113 
114  void PerformSearch(const QString &sST, uint timeout_secs = 2);
115 
116  void EnableNotifications ( int nServicePort );
117  void DisableNotifications();
118 
119  // ------------------------------------------------------------------
120 
121  static void AddListener(QObject *listener)
122  { SSDPCache::Instance()->addListener(listener); }
123  static void RemoveListener(QObject *listener)
124  { SSDPCache::Instance()->removeListener(listener); }
125 
126  static SSDPCacheEntries *Find(const QString &sURI)
127  { return SSDPCache::Instance()->Find(sURI); }
128  static DeviceLocation *Find(const QString &sURI,
129  const QString &sUSN)
130  { return SSDPCache::Instance()->Find( sURI, sUSN ); }
131 };
132 
135 //
136 // SSDPExtension Class Definition
137 //
140 
142 {
143  private:
144 
147 
148  private:
149 
150  static SSDPMethod GetMethod( const QString &sURI );
151 
152  void GetDeviceDesc( HTTPRequest *pRequest ) const;
153  void GetFile ( HTTPRequest *pRequest, const QString& sFileName );
154  static void GetDeviceList( HTTPRequest *pRequest );
155 
156  public:
157  SSDPExtension( int nServicePort, const QString &sSharePath);
158  ~SSDPExtension( ) override = default;
159 
160  QStringList GetBasePaths() override; // HttpServerExtension
161 
162  bool ProcessRequest( HTTPRequest *pRequest ) override; // HttpServerExtension
163 };
164 
165 #endif // SSDP_H
SSDPExtension::m_nServicePort
int m_nServicePort
Definition: ssdp.h:146
HTTPRequest
Definition: httprequest.h:108
SSDP::IsTermRequested
bool IsTermRequested()
SSDPExtension::GetBasePaths
QStringList GetBasePaths() override
Definition: ssdp.cpp:716
UPnpNotifyTask
Definition: upnptasknotify.h:53
SSDP_PORT
#define SSDP_PORT
Definition: ssdp.h:27
SSDP::g_pSSDP
static SSDP * g_pSSDP
Definition: ssdp.h:61
SSDPExtension
Definition: ssdp.h:142
SSDP::Find
static SSDPCacheEntries * Find(const QString &sURI)
Definition: ssdp.h:126
SSDPM_GetDeviceList
@ SSDPM_GetDeviceList
Definition: ssdp.h:34
SSDP_MSearch
@ SSDP_MSearch
Definition: ssdp.h:40
DeviceLocation
Definition: upnpdevice.h:208
SSDPCache::Instance
static SSDPCache * Instance()
Definition: ssdpcache.cpp:236
SSDP::Find
static DeviceLocation * Find(const QString &sURI, const QString &sUSN)
Definition: ssdp.h:128
MythObservable::addListener
void addListener(QObject *listener)
Add a listener to the observable.
Definition: mythobservable.cpp:38
SSDP_MSearchResp
@ SSDP_MSearchResp
Definition: ssdp.h:41
SSDPExtension::GetMethod
static SSDPMethod GetMethod(const QString &sURI)
Definition: ssdp.cpp:704
SSDPCache::Find
SSDPCacheEntries * Find(const QString &sURI)
Finds the SSDPCacheEntries in the cache, returns nullptr when absent.
Definition: ssdpcache.cpp:292
QStringMap
QMap< QString, QString > QStringMap
Definition: upnputil.h:44
SSDPExtension::m_sUPnpDescPath
QString m_sUPnpDescPath
Definition: ssdp.h:145
SSDPExtension::~SSDPExtension
~SSDPExtension() override=default
SSDP_Notify
@ SSDP_Notify
Definition: ssdp.h:42
SSDPCacheEntries
Definition: ssdpcache.h:35
uint
unsigned int uint
Definition: compat.h:140
SSDPExtension::ProcessRequest
bool ProcessRequest(HTTPRequest *pRequest) override
Definition: ssdp.cpp:728
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:316
SSDP_Unknown
@ SSDP_Unknown
Definition: ssdp.h:39
taskqueue.h
SSDPM_Unknown
@ SSDPM_Unknown
Definition: ssdp.h:32
SSDPMethod
SSDPMethod
Definition: ssdp.h:31
ssdpcache.h
SSDPRequestType
SSDPRequestType
Definition: ssdp.h:38
SSDP_SEARCHPORT
#define SSDP_SEARCHPORT
Definition: ssdp.h:28
SSDPExtension::GetDeviceList
static void GetDeviceList(HTTPRequest *pRequest)
Definition: ssdp.cpp:806
SSDPExtension::GetDeviceDesc
void GetDeviceDesc(HTTPRequest *pRequest) const
Definition: ssdp.cpp:751
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
upnptasknotify.h
mthread.h
UPNP_PUBLIC
#define UPNP_PUBLIC
Definition: upnpexp.h:9
SSDPM_GetDeviceDesc
@ SSDPM_GetDeviceDesc
Definition: ssdp.h:33
SSDP
Definition: ssdp.h:58
SSDP::AddListener
static void AddListener(QObject *listener)
Definition: ssdp.h:121
httpserver.h
HttpServerExtension
Definition: httpserver.h:72
SSDPExtension::SSDPExtension
SSDPExtension(int nServicePort, const QString &sSharePath)
Definition: ssdp.cpp:691
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:774
SSDP::RemoveListener
static void RemoveListener(QObject *listener)
Definition: ssdp.h:123
upnpexp.h