1 /*****************************************************************
3 | Platinum - Control/Event
5 | Copyright (c) 2004-2010, Plutinosoft, LLC.
7 | http://www.plutinosoft.com
9 | This program is free software; you can redistribute it and/or
10 | modify it under the terms of the GNU General Public License
11 | as published by the Free Software Foundation; either version 2
12 | of the License, or (at your option) any later version.
14 | OEMs, ISVs, VARs and other distributors that combine and
15 | distribute commercially licensed software with Platinum software
16 | and do not wish to distribute the source code for the commercially
17 | licensed software under version 2, or (at your option) any later
18 | version, of the GNU General Public License (the "GPL") must enter
19 | into a commercial license agreement with Plutinosoft, LLC.
20 | licensing@plutinosoft.com
22 | This program is distributed in the hope that it will be useful,
23 | but WITHOUT ANY WARRANTY; without even the implied warranty of
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 | GNU General Public License for more details.
27 | You should have received a copy of the GNU General Public License
28 | along with this program; see the file LICENSE.txt. If not, write to
29 | the Free Software Foundation, Inc.,
30 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 | http://www.gnu.org/licenses/gpl-2.0.html
33 ****************************************************************/
35 /*----------------------------------------------------------------------
37 +---------------------------------------------------------------------*/
38 #include "PltTaskManager.h"
40 #include "PltService.h"
42 #include "PltDeviceData.h"
43 #include "PltUtilities.h"
44 #include "PltCtrlPointTask.h"
46 NPT_SET_LOCAL_LOGGER("platinum.core.event")
48 /*----------------------------------------------------------------------
49 | PLT_EventNotification::PLT_EventNotification
50 +---------------------------------------------------------------------*/
51 PLT_EventNotification
*
52 PLT_EventNotification::Parse(const NPT_HttpRequest
& request
,
53 const NPT_HttpRequestContext
& context
,
54 NPT_HttpResponse
& response
)
56 NPT_COMPILER_UNUSED(context
);
58 PLT_LOG_HTTP_REQUEST(NPT_LOG_LEVEL_FINER
, "PLT_CtrlPoint::ProcessHttpNotify:", &request
);
60 PLT_EventNotification
*notification
= new PLT_EventNotification();
61 notification
->m_RequestUrl
= request
.GetUrl();
63 const NPT_String
* sid
= PLT_UPnPMessageHelper::GetSID(request
);
64 const NPT_String
* nt
= PLT_UPnPMessageHelper::GetNT(request
);
65 const NPT_String
* nts
= PLT_UPnPMessageHelper::GetNTS(request
);
67 if (!sid
|| sid
->GetLength() == 0) {
68 NPT_CHECK_LABEL_WARNING(NPT_FAILURE
, bad_request
);
70 notification
->m_SID
= *sid
;
72 if (!nt
|| nt
->GetLength() == 0 || !nts
|| nts
->GetLength() == 0) {
73 response
.SetStatus(400, "Bad request");
74 NPT_CHECK_LABEL_WARNING(NPT_FAILURE
, bad_request
);
77 if (nt
->Compare("upnp:event", true) || nts
->Compare("upnp:propchange", true)) {
78 NPT_CHECK_LABEL_WARNING(NPT_FAILURE
, bad_request
);
81 // if the sequence number is less than our current one, we got it out of order
83 PLT_UPnPMessageHelper::GetSeq(request
, notification
->m_EventKey
);
86 if (NPT_FAILED(PLT_HttpHelper::GetBody(request
, notification
->m_XmlBody
))) {
87 NPT_CHECK_LABEL_WARNING(NPT_FAILURE
, bad_request
);
93 NPT_LOG_SEVERE("CtrlPoint received bad event notify request\r\n");
94 if (response
.GetStatusCode() == 200) {
95 response
.SetStatus(412, "Precondition Failed");
101 /*----------------------------------------------------------------------
102 | PLT_EventSubscriber::PLT_EventSubscriber
103 +---------------------------------------------------------------------*/
104 PLT_EventSubscriber::PLT_EventSubscriber(PLT_TaskManagerReference task_manager
,
105 PLT_Service
* service
,
107 NPT_Timeout timeout_secs
/* = -1 */) :
108 m_TaskManager(task_manager
),
111 m_SubscriberTask(NULL
),
114 NPT_LOG_FINE_1("Creating new subscriber (%s)", m_SID
.GetChars());
115 SetTimeout(timeout_secs
);
118 /*----------------------------------------------------------------------
119 | PLT_EventSubscriber::~PLT_EventSubscriber
120 +---------------------------------------------------------------------*/
121 PLT_EventSubscriber::~PLT_EventSubscriber()
123 NPT_LOG_FINE_1("Deleting subscriber (%s)", m_SID
.GetChars());
124 if (m_SubscriberTask
) {
125 m_SubscriberTask
->Kill();
126 m_SubscriberTask
= NULL
;
130 /*----------------------------------------------------------------------
131 | PLT_EventSubscriber::GetService
132 +---------------------------------------------------------------------*/
134 PLT_EventSubscriber::GetService()
139 /*----------------------------------------------------------------------
140 | PLT_EventSubscriber::GetEventKey
141 +---------------------------------------------------------------------*/
143 PLT_EventSubscriber::GetEventKey()
148 /*----------------------------------------------------------------------
149 | PLT_EventSubscriber::SetEventKey
150 +---------------------------------------------------------------------*/
152 PLT_EventSubscriber::SetEventKey(NPT_Ordinal value
)
158 /*----------------------------------------------------------------------
159 | PLT_EventSubscriber::GetLocalIf
160 +---------------------------------------------------------------------*/
162 PLT_EventSubscriber::GetLocalIf()
167 /*----------------------------------------------------------------------
168 | PLT_EventSubscriber::SetLocalIf
169 +---------------------------------------------------------------------*/
171 PLT_EventSubscriber::SetLocalIf(NPT_SocketAddress value
)
177 /*----------------------------------------------------------------------
178 | PLT_EventSubscriber::GetExpirationTime
179 +---------------------------------------------------------------------*/
180 // a TimeStamp of 0 means no expiration
182 PLT_EventSubscriber::GetExpirationTime()
184 return m_ExpirationTime
;
187 /*----------------------------------------------------------------------
188 | PLT_EventSubscriber::SetExpirationTime
189 +---------------------------------------------------------------------*/
191 PLT_EventSubscriber::SetTimeout(NPT_Timeout seconds
)
193 NPT_LOG_FINE_2("subscriber (%s) expiring in %d seconds",
197 // -1 means infinite but we default to 300 secs
198 if (seconds
== -1) seconds
= 300;
200 NPT_System::GetCurrentTimeStamp(m_ExpirationTime
);
201 m_ExpirationTime
+= NPT_TimeInterval((double)seconds
);
206 /*----------------------------------------------------------------------
207 | PLT_EventSubscriber::FindCallbackURL
208 +---------------------------------------------------------------------*/
210 PLT_EventSubscriber::FindCallbackURL(const char* callback_url
)
213 return NPT_ContainerFind(m_CallbackURLs
,
214 NPT_StringFinder(callback_url
),
218 /*----------------------------------------------------------------------
219 | PLT_EventSubscriber::AddCallbackURL
220 +---------------------------------------------------------------------*/
222 PLT_EventSubscriber::AddCallbackURL(const char* callback_url
)
224 NPT_CHECK_POINTER_FATAL(callback_url
);
226 NPT_LOG_FINE_2("Adding callback \"%s\" to subscriber %s",
229 return m_CallbackURLs
.Add(callback_url
);
232 /*----------------------------------------------------------------------
233 | PLT_EventSubscriber::Notify
234 +---------------------------------------------------------------------*/
236 PLT_EventSubscriber::Notify(NPT_List
<PLT_StateVariable
*>& vars
)
238 // verify we have eventable variables
239 bool foundVars
= false;
240 NPT_Reference
<NPT_XmlElementNode
> propertyset(new NPT_XmlElementNode("e", "propertyset"));
241 NPT_CHECK_SEVERE(propertyset
->SetNamespaceUri(
243 "urn:schemas-upnp-org:event-1-0"));
245 NPT_List
<PLT_StateVariable
*>::Iterator var
= vars
.GetFirstItem();
247 if ((*var
)->IsSendingEvents()) {
248 NPT_XmlElementNode
* property
= new NPT_XmlElementNode("e", "property");
249 NPT_CHECK_FATAL(propertyset
->AddChild(property
));
250 NPT_CHECK_FATAL(PLT_XmlHelper::AddChildText(property
,
252 (*var
)->GetValue()));
258 // no eventable state variables found!
259 if (foundVars
== false) {
263 // format the body with the xml
265 if (NPT_FAILED(PLT_XmlHelper::Serialize(*propertyset
, xml
))) {
266 NPT_CHECK_FATAL(NPT_FAILURE
);
270 // parse the callback url
271 NPT_HttpUrl
url(m_CallbackURLs
[0]);
272 if (!url
.IsValid()) {
273 NPT_CHECK_FATAL(NPT_FAILURE
);
276 NPT_HttpRequest
* request
=
277 new NPT_HttpRequest(url
,
279 NPT_HTTP_PROTOCOL_1_1
);
280 NPT_HttpEntity
* entity
;
281 PLT_HttpHelper::SetBody(*request
, xml
, &entity
);
283 // add the extra headers
284 entity
->SetContentType("text/xml; charset=\"utf-8\"");
285 PLT_UPnPMessageHelper::SetNT(*request
, "upnp:event");
286 PLT_UPnPMessageHelper::SetNTS(*request
, "upnp:propchange");
287 PLT_UPnPMessageHelper::SetSID(*request
, m_SID
);
288 PLT_UPnPMessageHelper::SetSeq(*request
, m_EventKey
);
290 // wrap around sequence to 1
291 if (++m_EventKey
== 0) m_EventKey
= 1;
293 // start the task now if not started already
294 if (!m_SubscriberTask
) {
295 // TODO: the subscriber task should inform subscriber if
296 // a notification failed to be received so it can be removed
297 // from the list of subscribers inside the device host
298 NPT_Reference
<PLT_HttpClientSocketTask
> task(new PLT_HttpClientSocketTask(request
, true));
300 // short connection time out in case subscriber is not alive
301 NPT_HttpClient::Config config
;
302 config
.m_ConnectionTimeout
= 2000;
303 task
->SetHttpClientConfig(config
);
305 // add initial delay to make sure ctrlpoint receives response to subscription
306 // before our first NOTIFY. Also make sure task is not auto-destroy
307 // since we want to destroy it manually when the subscriber goes away.
308 NPT_TimeInterval
delay(0.05f
);
309 NPT_CHECK_FATAL(m_TaskManager
->StartTask(task
.AsPointer(), NULL
/*&delay*/, false));
311 // Task successfully started, keep around for future notifications
312 m_SubscriberTask
= task
.AsPointer();
315 m_SubscriberTask
->AddRequest(request
);
321 /*----------------------------------------------------------------------
322 | PLT_EventSubscriberFinderByService::operator()
323 +---------------------------------------------------------------------*/
325 PLT_EventSubscriberFinderByService::operator()(PLT_EventSubscriberReference
const & eventSub
) const
327 return (m_Service
== eventSub
->GetService());