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 
30 typedef enum
31 {
35 
36 } SSDPMethod;
37 
38 typedef enum
39 {
44 
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 #define NumberOfSockets (sizeof( m_Sockets ) / sizeof( MSocketDevice * ))
60 
61 class UPNP_PUBLIC SSDP : public MThread
62 {
63  private:
64  // Singleton instance used by all.
65  static SSDP* g_pSSDP;
66 
67  QRegExp m_procReqLineExp {"[ \r\n][ \r\n]*"};
68  MSocketDevice *m_Sockets[3];
69 
70  int m_nPort {SSDP_PORT};
71  int m_nSearchPort {SSDP_SEARCHPORT};
72  int m_nServicePort {0};
73 
74  UPnpNotifyTask *m_pNotifyTask {nullptr};
75  bool m_bAnnouncementsEnabled {false};
76 
77  bool m_bTermRequested {false};
78  QMutex m_lock {QMutex::NonRecursive};
79 
80  private:
81 
82  // ------------------------------------------------------------------
83  // Private so the singleton pattern can be inforced.
84  // ------------------------------------------------------------------
85 
86  SSDP ();
87 
88  protected:
89 
90  bool ProcessSearchRequest ( const QStringMap &sHeaders,
91  const QHostAddress& peerAddress,
92  quint16 peerPort );
93  bool ProcessSearchResponse( const QStringMap &sHeaders );
94  bool ProcessNotify ( const QStringMap &sHeaders );
95 
96  bool IsTermRequested ();
97 
98  QString GetHeaderValue ( const QStringMap &headers,
99  const QString &sKey,
100  const QString &sDefault );
101 
102  void ProcessData ( MSocketDevice *pSocket );
103 
104  SSDPRequestType ProcessRequestLine( const QString &sLine );
105 
106  void run() override; // MThread
107 
108  public:
109 
110  static SSDP* Instance();
111  static void Shutdown();
112 
113  ~SSDP();
114 
115  void RequestTerminate(void);
116 
117  void PerformSearch(const QString &sST, uint timeout_secs = 2);
118 
119  void EnableNotifications ( int nServicePort );
120  void DisableNotifications();
121 
122  // ------------------------------------------------------------------
123 
124  static void AddListener(QObject *listener)
125  { SSDPCache::Instance()->addListener(listener); }
126  static void RemoveListener(QObject *listener)
127  { SSDPCache::Instance()->removeListener(listener); }
128 
129  static SSDPCacheEntries *Find(const QString &sURI)
130  { return SSDPCache::Instance()->Find(sURI); }
131  static DeviceLocation *Find(const QString &sURI,
132  const QString &sUSN)
133  { return SSDPCache::Instance()->Find( sURI, sUSN ); }
134 };
135 
138 //
139 // SSDPExtension Class Definition
140 //
143 
145 {
146  private:
147 
150 
151  private:
152 
153  SSDPMethod GetMethod( const QString &sURI );
154 
155  void GetDeviceDesc( HTTPRequest *pRequest );
156  void GetFile ( HTTPRequest *pRequest, const QString& sFileName );
157  void GetDeviceList( HTTPRequest *pRequest );
158 
159  public:
160  SSDPExtension( int nServicePort, const QString &sSharePath);
161  virtual ~SSDPExtension( ) = default;
162 
163  QStringList GetBasePaths() override; // HttpServerExtension
164 
165  bool ProcessRequest( HTTPRequest *pRequest ) override; // HttpServerExtension
166 };
167 
168 #endif
SSDPRequestType
Definition: ssdp.h:38
static SSDPCache * Instance()
Definition: ssdpcache.cpp:240
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:694
void GetFile(HTTPRequest *pRequest, const QString &sFileName)
Definition: ssdp.cpp:777
void GetDeviceDesc(HTTPRequest *pRequest)
Definition: ssdp.cpp:754
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.
void GetDeviceList(HTTPRequest *pRequest)
Definition: ssdp.cpp:809
static void AddListener(QObject *listener)
Definition: ssdp.h:124
unsigned int uint
Definition: compat.h:140
Definition: ssdp.h:61
void addListener(QObject *listener)
Add a listener to the observable.
QMap< QString, QString > QStringMap
Definition: upnputil.h:40
int m_nServicePort
Definition: ssdp.h:149
QStringList GetBasePaths() override
Definition: ssdp.cpp:719
static void RemoveListener(QObject *listener)
Definition: ssdp.h:126
#define UPNP_PUBLIC
Definition: upnpexp.h:9
virtual ~SSDPExtension()=default
static SSDP * g_pSSDP
Definition: ssdp.h:65
QString m_sUPnpDescPath
Definition: ssdp.h:148
static DeviceLocation * Find(const QString &sURI, const QString &sUSN)
Definition: ssdp.h:131
bool ProcessRequest(HTTPRequest *pRequest) override
Definition: ssdp.cpp:731
#define SSDP_PORT
Definition: ssdp.h:27
static SSDPCacheEntries * Find(const QString &sURI)
Definition: ssdp.h:129
SSDPMethod GetMethod(const QString &sURI)
Definition: ssdp.cpp:707
#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