MythTV master
upnp.cpp
Go to the documentation of this file.
1
2// Program Name: upnp.cpp
3// Created : Oct. 24, 2005
4//
5// Purpose : UPnp Main Class
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#include "upnp.h"
13
14#include <QNetworkInterface>
15
19
20#include "ssdp.h"
21#include "ssdpextension.h"
22
24// Global/Class Static variables
26
28QList<QHostAddress> UPnp::g_IPAddrList;
29
32//
33// UPnp Class implementaion
34//
37
39{
40 LOG(VB_UPNP, LOG_DEBUG, "UPnp - Constructor");
41 // N.B. Ask for 5 second delay to send Bye Bye twice
42 // TODO Check whether we actually send Bye Bye twice:)
43 m_power = MythPower::AcquireRelease(this, true, 5s);
44 if (m_power)
45 {
46 // NB We only listen for WillXXX signals which should give us time to send notifications
51 }
52}
53
55//
57
59{
60 LOG(VB_UPNP, LOG_DEBUG, "UPnp - Destructor");
61 CleanUp();
62 if (m_power)
63 MythPower::AcquireRelease(this, false);
64}
65
67//
69
70
72//
74
75bool UPnp::Initialize( int nServicePort, HttpServer *pHttpServer )
76{
77 QList<QHostAddress> sList = ServerPool::DefaultListen();
78 if (sList.contains(QHostAddress(QHostAddress::AnyIPv4)))
79 {
80 sList.removeAll(QHostAddress(QHostAddress::AnyIPv4));
81 sList.removeAll(QHostAddress(QHostAddress::AnyIPv6));
82 sList.append(QNetworkInterface::allAddresses());
83 }
84 return Initialize( sList, nServicePort, pHttpServer );
85}
86
88//
90
91bool UPnp::Initialize( QList<QHostAddress> &sIPAddrList, int nServicePort, HttpServer *pHttpServer )
92{
93 LOG(VB_UPNP, LOG_DEBUG, "UPnp::Initialize - Begin");
94
95 if (m_pHttpServer)
96 {
97 LOG(VB_GENERAL, LOG_ERR,
98 "UPnp::Initialize - Already initialized, programmer error.");
99 return false;
100 }
101
102 m_pHttpServer = pHttpServer;
103 if (m_pHttpServer == nullptr)
104 {
105 LOG(VB_GENERAL, LOG_ERR,
106 "UPnp::Initialize - Invalid Parameter (pHttpServer == NULL)");
107 return false;
108 }
109
110 g_IPAddrList = sIPAddrList;
111 bool ipv4 = gCoreContext->GetBoolSetting("IPv4Support",true);
112 bool ipv6 = gCoreContext->GetBoolSetting("IPv6Support",true);
113
114 for (int it = 0; it < g_IPAddrList.size(); ++it)
115 {
116 // If IPV4 support is disabled and this is an IPV4 address,
117 // remove this address
118 // If IPV6 support is disabled and this is an IPV6 address,
119 // remove this address
120 if ((g_IPAddrList[it].protocol() == QAbstractSocket::IPv4Protocol
121 && ! ipv4)
122 ||(g_IPAddrList[it].protocol() == QAbstractSocket::IPv6Protocol
123 && ! ipv6))
124 g_IPAddrList.removeAt(it--);
125 }
126
127 m_nServicePort = nServicePort;
128
129 // ----------------------------------------------------------------------
130 // Register any HttpServerExtensions
131 // ----------------------------------------------------------------------
132
135
136 LOG(VB_UPNP, LOG_DEBUG, "UPnp::Initialize - End");
137
138 return true;
139}
140
142// Delay startup of Discovery Threads until all Extensions are registered.
144
146{
147 LOG(VB_UPNP, LOG_DEBUG, "UPnp::Start - Enabling SSDP Notifications");
148 // ----------------------------------------------------------------------
149 // Turn on Device Announcements
150 // (this will also create/startup SSDP if not already done)
151 // ----------------------------------------------------------------------
152
154
155 LOG(VB_UPNP, LOG_DEBUG, "UPnp::Start - Returning");
156}
157
159//
161
163{
164 LOG(VB_UPNP, LOG_INFO, "UPnp::CleanUp() - disabling SSDP notifications");
165
167}
168
170//
172
174{
175 return UPnpDeviceDesc::Retrieve( sURL );
176}
177
178void UPnp::DisableNotifications(std::chrono::milliseconds /*unused*/)
179{
181}
182
183void UPnp::EnableNotificatins(std::chrono::milliseconds /*unused*/) const
184{
186}
QString GetSharePath(void) const
Definition: httpserver.h:128
void RegisterExtension(HttpServerExtension *pExtension)
Definition: httpserver.cpp:313
bool GetBoolSetting(const QString &key, bool defaultval=false)
void WokeUp(std::chrono::seconds SecondsAsleep)
void WillSuspend(std::chrono::milliseconds MilliSeconds=0ms)
static MythPower * AcquireRelease(void *Reference, bool Acquire, std::chrono::seconds MinimumDelay=0s)
Definition: mythpower.cpp:77
void WillShutDown(std::chrono::milliseconds MilliSeconds=0ms)
void WillRestart(std::chrono::milliseconds MilliSeconds=0ms)
void EnableNotifications(int nServicePort)
Definition: ssdp.cpp:110
void DisableNotifications()
Definition: ssdp.cpp:148
static SSDP * Instance()
Definition: ssdp.cpp:57
static QList< QHostAddress > DefaultListen(void)
Definition: serverpool.cpp:305
static UPnpDeviceDesc * Retrieve(QString &sURL)
Definition: upnpdevice.cpp:634
~UPnp() override
Definition: upnp.cpp:58
static QList< QHostAddress > g_IPAddrList
Definition: upnp.h:49
MythPower * m_power
Definition: upnp.h:76
static void CleanUp()
Definition: upnp.cpp:162
static UPnpDeviceDesc * GetDeviceDesc(QString &sURL)
Definition: upnp.cpp:173
HttpServer * m_pHttpServer
Definition: upnp.h:43
virtual void Start()
Definition: upnp.cpp:145
int m_nServicePort
Definition: upnp.h:44
static void DisableNotifications(std::chrono::milliseconds)
Definition: upnp.cpp:178
bool Initialize(int nServicePort, HttpServer *pHttpServer)
Definition: upnp.cpp:75
static UPnpDeviceDesc g_UPnpDeviceDesc
Definition: upnp.h:48
UPnp()
Definition: upnp.cpp:38
void EnableNotificatins(std::chrono::milliseconds) const
Definition: upnp.cpp:183
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39