1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
/*****************************************************************
|
| Platinum - UPnP Engine
|
| Copyright (c) 2004-2010, Plutinosoft, LLC.
| All rights reserved.
| http://www.plutinosoft.com
|
| This program is free software; you can redistribute it and/or
| modify it under the terms of the GNU General Public License
| as published by the Free Software Foundation; either version 2
| of the License, or (at your option) any later version.
|
| OEMs, ISVs, VARs and other distributors that combine and
| distribute commercially licensed software with Platinum software
| and do not wish to distribute the source code for the commercially
| licensed software under version 2, or (at your option) any later
| version, of the GNU General Public License (the "GPL") must enter
| into a commercial license agreement with Plutinosoft, LLC.
| licensing@plutinosoft.com
|
| This program is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with this program; see the file LICENSE.txt. If not, write to
| the Free Software Foundation, Inc.,
| 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
| http://www.gnu.org/licenses/gpl-2.0.html
|
****************************************************************/
/*----------------------------------------------------------------------
| includes
+---------------------------------------------------------------------*/
#include "Neptune.h"
#include "PltVersion.h"
#include "PltUPnP.h"
#include "PltDeviceHost.h"
#include "PltCtrlPoint.h"
#include "PltSsdp.h"
NPT_SET_LOCAL_LOGGER("platinum.core.upnp")
/*----------------------------------------------------------------------
| PLT_UPnP_CtrlPointStartIterator class
+---------------------------------------------------------------------*/
class PLT_UPnP_CtrlPointStartIterator
{
public:
PLT_UPnP_CtrlPointStartIterator(PLT_SsdpListenTask* listen_task) :
m_ListenTask(listen_task) {}
virtual ~PLT_UPnP_CtrlPointStartIterator() {}
NPT_Result operator()(PLT_CtrlPointReference& ctrl_point) const {
NPT_CHECK_SEVERE(ctrl_point->Start(m_ListenTask));
return NPT_SUCCESS;
}
private:
PLT_SsdpListenTask* m_ListenTask;
};
/*----------------------------------------------------------------------
| PLT_UPnP_CtrlPointStopIterator class
+---------------------------------------------------------------------*/
class PLT_UPnP_CtrlPointStopIterator
{
public:
PLT_UPnP_CtrlPointStopIterator(PLT_SsdpListenTask* listen_task) :
m_ListenTask(listen_task) {}
virtual ~PLT_UPnP_CtrlPointStopIterator() {}
NPT_Result operator()(PLT_CtrlPointReference& ctrl_point) const {
return ctrl_point->Stop(m_ListenTask);
}
private:
PLT_SsdpListenTask* m_ListenTask;
};
/*----------------------------------------------------------------------
| PLT_UPnP_DeviceStartIterator class
+---------------------------------------------------------------------*/
class PLT_UPnP_DeviceStartIterator
{
public:
PLT_UPnP_DeviceStartIterator(PLT_SsdpListenTask* listen_task) :
m_ListenTask(listen_task) {}
virtual ~PLT_UPnP_DeviceStartIterator() {}
NPT_Result operator()(PLT_DeviceHostReference& device_host) const {
// We should always increment the boot id on restart
// so it is used in place of boot id during initial announcement
device_host->SetBootId(device_host->GenerateNextBootId());
device_host->SetNextBootId(0);
NPT_CHECK_SEVERE(device_host->Start(m_ListenTask));
return NPT_SUCCESS;
}
private:
PLT_SsdpListenTask* m_ListenTask;
};
/*----------------------------------------------------------------------
| PLT_UPnP_DeviceStopIterator class
+---------------------------------------------------------------------*/
class PLT_UPnP_DeviceStopIterator
{
public:
PLT_UPnP_DeviceStopIterator(PLT_SsdpListenTask* listen_task) :
m_ListenTask(listen_task) {}
virtual ~PLT_UPnP_DeviceStopIterator() {}
NPT_Result operator()(PLT_DeviceHostReference& device_host) const {
return device_host->Stop(m_ListenTask);
}
private:
PLT_SsdpListenTask* m_ListenTask;
};
/*----------------------------------------------------------------------
| PLT_UPnP::PLT_UPnP
+---------------------------------------------------------------------*/
PLT_UPnP::PLT_UPnP() :
m_TaskManager(NULL),
m_Started(false),
m_SsdpListenTask(NULL),
m_IgnoreLocalUUIDs(true)
{
}
/*----------------------------------------------------------------------
| PLT_UPnP::~PLT_UPnP
+---------------------------------------------------------------------*/
PLT_UPnP::~PLT_UPnP()
{
Stop();
m_CtrlPoints.Clear();
m_Devices.Clear();
}
/*----------------------------------------------------------------------
| PLT_UPnP::Start()
+---------------------------------------------------------------------*/
NPT_Result
PLT_UPnP::Start()
{
NPT_LOG_INFO("Starting UPnP...");
NPT_AutoLock lock(m_Lock);
if (m_Started) NPT_CHECK_WARNING(NPT_ERROR_INVALID_STATE);
NPT_List<NPT_IpAddress> ips;
PLT_UPnPMessageHelper::GetIPAddresses(ips);
/* Create multicast socket and bind on 1900. If other apps didn't
play nicely by setting the REUSE_ADDR flag, this could fail */
NPT_Reference<NPT_UdpMulticastSocket> socket(new NPT_UdpMulticastSocket(NPT_SOCKET_FLAG_CANCELLABLE));
NPT_CHECK_SEVERE(socket->Bind(NPT_SocketAddress(NPT_IpAddress::Any, 1900), true));
/* Join multicast group for every ip we found */
NPT_CHECK_SEVERE(ips.ApplyUntil(PLT_SsdpInitMulticastIterator(socket.AsPointer()),
NPT_UntilResultNotEquals(NPT_SUCCESS)));
/* create the ssdp listener */
m_SsdpListenTask = new PLT_SsdpListenTask(socket.AsPointer());
socket.Detach();
NPT_Reference<PLT_TaskManager> taskManager(new PLT_TaskManager());
NPT_CHECK_SEVERE(taskManager->StartTask(m_SsdpListenTask));
/* start devices & ctrlpoints */
m_CtrlPoints.Apply(PLT_UPnP_CtrlPointStartIterator(m_SsdpListenTask));
m_Devices.Apply(PLT_UPnP_DeviceStartIterator(m_SsdpListenTask));
m_TaskManager = taskManager;
m_Started = true;
return NPT_SUCCESS;
}
/*----------------------------------------------------------------------
| PLT_UPnP::Stop
+---------------------------------------------------------------------*/
NPT_Result
PLT_UPnP::Stop()
{
NPT_AutoLock lock(m_Lock);
if (!m_Started) NPT_CHECK_WARNING(NPT_ERROR_INVALID_STATE);
NPT_LOG_INFO("Stopping UPnP...");
// Stop ctrlpoints and devices first
m_CtrlPoints.Apply(PLT_UPnP_CtrlPointStopIterator(m_SsdpListenTask));
m_Devices.Apply(PLT_UPnP_DeviceStopIterator(m_SsdpListenTask));
// stop remaining tasks
m_TaskManager->Abort();
m_SsdpListenTask = NULL;
m_TaskManager = NULL;
m_Started = false;
return NPT_SUCCESS;
}
/*----------------------------------------------------------------------
| PLT_UPnP::AddDevice
+---------------------------------------------------------------------*/
NPT_Result
PLT_UPnP::AddDevice(PLT_DeviceHostReference& device)
{
NPT_AutoLock lock(m_Lock);
// tell all our controllers to ignore this device
if (m_IgnoreLocalUUIDs) {
for (NPT_List<PLT_CtrlPointReference>::Iterator iter =
m_CtrlPoints.GetFirstItem();
iter;
iter++) {
(*iter)->IgnoreUUID(device->GetUUID());
}
}
if (m_Started) {
NPT_LOG_INFO("Starting Device...");
NPT_CHECK_SEVERE(device->Start(m_SsdpListenTask));
}
m_Devices.Add(device);
return NPT_SUCCESS;
}
/*----------------------------------------------------------------------
| PLT_UPnP::RemoveDevice
+---------------------------------------------------------------------*/
NPT_Result
PLT_UPnP::RemoveDevice(PLT_DeviceHostReference& device)
{
NPT_AutoLock lock(m_Lock);
if (m_Started) {
device->Stop(m_SsdpListenTask);
}
return m_Devices.Remove(device);
}
/*----------------------------------------------------------------------
| PLT_UPnP::AddCtrlPoint
+---------------------------------------------------------------------*/
NPT_Result
PLT_UPnP::AddCtrlPoint(PLT_CtrlPointReference& ctrl_point)
{
NPT_AutoLock lock(m_Lock);
// tell the control point to ignore our own running devices
if (m_IgnoreLocalUUIDs) {
for (NPT_List<PLT_DeviceHostReference>::Iterator iter =
m_Devices.GetFirstItem();
iter;
iter++) {
ctrl_point->IgnoreUUID((*iter)->GetUUID());
}
}
if (m_Started) {
NPT_LOG_INFO("Starting Ctrlpoint...");
NPT_CHECK_SEVERE(ctrl_point->Start(m_SsdpListenTask));
}
m_CtrlPoints.Add(ctrl_point);
return NPT_SUCCESS;
}
/*----------------------------------------------------------------------
| PLT_UPnP::RemoveCtrlPoint
+---------------------------------------------------------------------*/
NPT_Result
PLT_UPnP::RemoveCtrlPoint(PLT_CtrlPointReference& ctrl_point)
{
NPT_AutoLock lock(m_Lock);
if (m_Started) {
ctrl_point->Stop(m_SsdpListenTask);
}
return m_CtrlPoints.Remove(ctrl_point);
}
|