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 {
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  QRegExp m_procReqLineExp {"[ \r\n][ \r\n]*"};
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 );
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();
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 );
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  virtual ~SSDPExtension( ) = default;
159 
160  QStringList GetBasePaths() override; // HttpServerExtension
161 
162  bool ProcessRequest( HTTPRequest *pRequest ) override; // HttpServerExtension
163 };
164 
165 #endif
SSDPRequestType
Definition: ssdp.h:37
static SSDPCache * Instance()
Definition: ssdpcache.cpp:240
QMap< QString, QString > QStringMap
Definition: upnputil.h:40
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
SSDPMethod
Definition: ssdp.h:30
SSDPExtension(int nServicePort, const QString &sSharePath)
Definition: ssdp.cpp:690
void GetFile(HTTPRequest *pRequest, const QString &sFileName)
Definition: ssdp.cpp:773
void GetDeviceDesc(HTTPRequest *pRequest)
Definition: ssdp.cpp:750
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
void removeListener(QObject *listener)
Remove a listener to the observable.
static void GetDeviceList(HTTPRequest *pRequest)
Definition: ssdp.cpp:805
static void AddListener(QObject *listener)
Definition: ssdp.h:121
Definition: ssdp.h:57
void addListener(QObject *listener)
Add a listener to the observable.
int m_nServicePort
Definition: ssdp.h:146
QStringList GetBasePaths() override
Definition: ssdp.cpp:715
unsigned int uint
Definition: compat.h:140
static void RemoveListener(QObject *listener)
Definition: ssdp.h:123
#define UPNP_PUBLIC
Definition: upnpexp.h:9
virtual ~SSDPExtension()=default
static SSDP * g_pSSDP
Definition: ssdp.h:61
QString m_sUPnpDescPath
Definition: ssdp.h:145
static DeviceLocation * Find(const QString &sURI, const QString &sUSN)
Definition: ssdp.h:128
bool ProcessRequest(HTTPRequest *pRequest) override
Definition: ssdp.cpp:727
#define SSDP_PORT
Definition: ssdp.h:27
static SSDPCacheEntries * Find(const QString &sURI)
Definition: ssdp.h:126
static SSDPMethod GetMethod(const QString &sURI)
Definition: ssdp.cpp:703
#define SSDP_SEARCHPORT
Definition: ssdp.h:28
SSDPCacheEntries * Find(const QString &sURI)
Finds the SSDPCacheEntries in the cache, returns nullptr when absent.
Definition: ssdpcache.cpp:297