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
|
/*****************************************************************
|
| Platinum - Control/Event
|
| 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 "PltTaskManager.h"
#include "PltEvent.h"
#include "PltService.h"
#include "PltUPnP.h"
#include "PltDeviceData.h"
#include "PltUtilities.h"
#include "PltCtrlPointTask.h"
NPT_SET_LOCAL_LOGGER("platinum.core.event")
/*----------------------------------------------------------------------
| PLT_EventSubscriber::PLT_EventSubscriber
+---------------------------------------------------------------------*/
PLT_EventSubscriber::PLT_EventSubscriber(PLT_TaskManager* task_manager,
PLT_Service* service,
const char* sid,
NPT_Timeout timeout_secs /* = -1 */) :
m_TaskManager(task_manager),
m_Service(service),
m_EventKey(0),
m_SubscriberTask(NULL),
m_SID(sid)
{
NPT_LOG_FINE_1("Creating new subscriber (%s)", m_SID.GetChars());
SetTimeout(timeout_secs);
}
/*----------------------------------------------------------------------
| PLT_EventSubscriber::~PLT_EventSubscriber
+---------------------------------------------------------------------*/
PLT_EventSubscriber::~PLT_EventSubscriber()
{
NPT_LOG_FINE_1("Deleting subscriber (%s)", m_SID.GetChars());
if (m_SubscriberTask) {
m_SubscriberTask->Kill();
m_SubscriberTask = NULL;
}
}
/*----------------------------------------------------------------------
| PLT_EventSubscriber::GetService
+---------------------------------------------------------------------*/
PLT_Service*
PLT_EventSubscriber::GetService()
{
return m_Service;
}
/*----------------------------------------------------------------------
| PLT_EventSubscriber::GetEventKey
+---------------------------------------------------------------------*/
NPT_Ordinal
PLT_EventSubscriber::GetEventKey()
{
return m_EventKey;
}
/*----------------------------------------------------------------------
| PLT_EventSubscriber::SetEventKey
+---------------------------------------------------------------------*/
NPT_Result
PLT_EventSubscriber::SetEventKey(NPT_Ordinal value)
{
m_EventKey = value;
return NPT_SUCCESS;
}
/*----------------------------------------------------------------------
| PLT_EventSubscriber::GetLocalIf
+---------------------------------------------------------------------*/
NPT_SocketAddress
PLT_EventSubscriber::GetLocalIf()
{
return m_LocalIf;
}
/*----------------------------------------------------------------------
| PLT_EventSubscriber::SetLocalIf
+---------------------------------------------------------------------*/
NPT_Result
PLT_EventSubscriber::SetLocalIf(NPT_SocketAddress value)
{
m_LocalIf = value;
return NPT_SUCCESS;
}
/*----------------------------------------------------------------------
| PLT_EventSubscriber::GetExpirationTime
+---------------------------------------------------------------------*/
// a TimeStamp of 0 means no expiration
NPT_TimeStamp
PLT_EventSubscriber::GetExpirationTime()
{
return m_ExpirationTime;
}
/*----------------------------------------------------------------------
| PLT_EventSubscriber::SetExpirationTime
+---------------------------------------------------------------------*/
NPT_Result
PLT_EventSubscriber::SetTimeout(NPT_Timeout seconds)
{
NPT_LOG_FINE_2("subscriber (%s) expiring in %d seconds",
m_SID.GetChars(),
seconds);
// -1 means infinite but we default to 300 secs
if (seconds == -1) seconds = 300;
NPT_System::GetCurrentTimeStamp(m_ExpirationTime);
m_ExpirationTime += NPT_TimeInterval((double)seconds);
return NPT_SUCCESS;
}
/*----------------------------------------------------------------------
| PLT_EventSubscriber::FindCallbackURL
+---------------------------------------------------------------------*/
NPT_Result
PLT_EventSubscriber::FindCallbackURL(const char* callback_url)
{
NPT_String res;
return NPT_ContainerFind(m_CallbackURLs,
NPT_StringFinder(callback_url),
res);
}
/*----------------------------------------------------------------------
| PLT_EventSubscriber::AddCallbackURL
+---------------------------------------------------------------------*/
NPT_Result
PLT_EventSubscriber::AddCallbackURL(const char* callback_url)
{
NPT_CHECK_POINTER_FATAL(callback_url);
NPT_LOG_FINE_2("Adding callback \"%s\" to subscriber %s",
callback_url,
m_SID.GetChars());
return m_CallbackURLs.Add(callback_url);
}
/*----------------------------------------------------------------------
| PLT_EventSubscriber::Notify
+---------------------------------------------------------------------*/
NPT_Result
PLT_EventSubscriber::Notify(NPT_List<PLT_StateVariable*>& vars)
{
// verify we have eventable variables
bool foundVars = false;
NPT_XmlElementNode* propertyset =
new NPT_XmlElementNode("e", "propertyset");
NPT_CHECK_SEVERE(propertyset->SetNamespaceUri(
"e",
"urn:schemas-upnp-org:event-1-0"));
NPT_List<PLT_StateVariable*>::Iterator var = vars.GetFirstItem();
while (var) {
if ((*var)->IsSendingEvents()) {
NPT_XmlElementNode* property =
new NPT_XmlElementNode("e", "property");
propertyset->AddChild(property);
PLT_XmlHelper::AddChildText(property,
(*var)->GetName(),
(*var)->GetValue());
foundVars = true;
}
++var;
}
// no eventable state variables found!
if (foundVars == false) {
delete propertyset;
return NPT_FAILURE;
}
// format the body with the xml
NPT_String xml;
if (NPT_FAILED(PLT_XmlHelper::Serialize(*propertyset, xml))) {
delete propertyset;
NPT_CHECK_FATAL(NPT_FAILURE);
}
delete propertyset;
// parse the callback url
NPT_HttpUrl url(m_CallbackURLs[0]);
if (!url.IsValid()) {
NPT_CHECK_FATAL(NPT_FAILURE);
}
// format request
NPT_HttpRequest* request =
new NPT_HttpRequest(url,
"NOTIFY",
NPT_HTTP_PROTOCOL_1_1);
NPT_HttpEntity* entity;
PLT_HttpHelper::SetBody(*request, xml, &entity);
// add the extra headers
entity->SetContentType("text/xml; charset=\"utf-8\"");
PLT_UPnPMessageHelper::SetNT(*request, "upnp:event");
PLT_UPnPMessageHelper::SetNTS(*request, "upnp:propchange");
PLT_UPnPMessageHelper::SetSID(*request, m_SID);
PLT_UPnPMessageHelper::SetSeq(*request, m_EventKey);
// wrap around sequence to 1
if (++m_EventKey == 0) m_EventKey = 1;
// start the task now if not started already
if (!m_SubscriberTask) {
// TODO: the subscriber task should inform subscriber if
// a notification failed to be received so it can be removed
// from the list of subscribers inside the device host
m_SubscriberTask = new PLT_HttpClientSocketTask(request, true);
// short connection time out in case subscriber is not alive
NPT_HttpClient::Config config;
config.m_ConnectionTimeout = 2000;
m_SubscriberTask->SetHttpClientConfig(config);
// add initial delay to make sure ctrlpoint receives response to subscription
// before our first NOTIFY. Also make sure task is not auto-destroy
// since we want to destroy it manually when the subscriber goes away.
NPT_TimeInterval delay(0.05f);
NPT_CHECK_FATAL(m_TaskManager->StartTask(m_SubscriberTask, NULL /*&delay*/, false));
} else {
m_SubscriberTask->AddRequest(request);
}
return NPT_SUCCESS;
}
/*----------------------------------------------------------------------
| PLT_EventSubscriberFinderByService::operator()
+---------------------------------------------------------------------*/
bool
PLT_EventSubscriberFinderByService::operator()(PLT_EventSubscriber* const & eventSub) const
{
return (m_Service == eventSub->GetService());
}
|