Mercurial > projects > hoofbaby
comparison deps/Platinum/Source/Devices/MediaServer/PltSyncMediaBrowser.cpp @ 0:3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
author | fraserofthenight |
---|---|
date | Mon, 06 Jul 2009 08:06:28 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3425707ddbf6 |
---|---|
1 /***************************************************************** | |
2 | | |
3 | Platinum - Synchronous Media Browser | |
4 | | |
5 | Copyright (c) 2004-2008, Plutinosoft, LLC. | |
6 | All rights reserved. | |
7 | http://www.plutinosoft.com | |
8 | | |
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. | |
13 | | |
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 | | |
21 | This program is distributed in the hope that it will be useful, | |
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | GNU General Public License for more details. | |
25 | | |
26 | You should have received a copy of the GNU General Public License | |
27 | along with this program; see the file LICENSE.txt. If not, write to | |
28 | the Free Software Foundation, Inc., | |
29 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
30 | http://www.gnu.org/licenses/gpl-2.0.html | |
31 | | |
32 ****************************************************************/ | |
33 | |
34 /*---------------------------------------------------------------------- | |
35 | includes | |
36 +---------------------------------------------------------------------*/ | |
37 #include "PltSyncMediaBrowser.h" | |
38 | |
39 NPT_SET_LOCAL_LOGGER("platinum.media.server.syncbrowser") | |
40 | |
41 /*---------------------------------------------------------------------- | |
42 | PLT_SyncMediaBrowser::PLT_SyncMediaBrowser | |
43 +---------------------------------------------------------------------*/ | |
44 PLT_SyncMediaBrowser::PLT_SyncMediaBrowser(PLT_CtrlPointReference& ctrlPoint, | |
45 bool use_cache /* = false */, | |
46 PLT_MediaContainerChangesListener* listener /* = NULL */) : | |
47 PLT_MediaBrowser(ctrlPoint), | |
48 m_ContainerListener(listener), | |
49 m_UseCache(use_cache) | |
50 { | |
51 SetDelegate(this); | |
52 } | |
53 | |
54 /*---------------------------------------------------------------------- | |
55 | PLT_SyncMediaBrowser::~PLT_SyncMediaBrowser | |
56 +---------------------------------------------------------------------*/ | |
57 PLT_SyncMediaBrowser::~PLT_SyncMediaBrowser() | |
58 { | |
59 } | |
60 | |
61 /* Blocks forever waiting for a response from a request | |
62 * It is expected the request to succeed or to timeout and return an error eventually | |
63 */ | |
64 /*---------------------------------------------------------------------- | |
65 | PLT_SyncMediaBrowser::WaitForResponse | |
66 +---------------------------------------------------------------------*/ | |
67 NPT_Result | |
68 PLT_SyncMediaBrowser::WaitForResponse(NPT_SharedVariable& shared_var) | |
69 { | |
70 return shared_var.WaitUntilEquals(1, 30000); | |
71 } | |
72 | |
73 /*---------------------------------------------------------------------- | |
74 | PLT_SyncMediaBrowser::OnDeviceAdded | |
75 +---------------------------------------------------------------------*/ | |
76 NPT_Result | |
77 PLT_SyncMediaBrowser::OnDeviceAdded(PLT_DeviceDataReference& device) | |
78 { | |
79 NPT_String uuid = device->GetUUID(); | |
80 | |
81 // test if it's a media server | |
82 PLT_Service* service; | |
83 if (NPT_SUCCEEDED(device->FindServiceByType("urn:schemas-upnp-org:service:ContentDirectory:1", service))) { | |
84 NPT_AutoLock lock(m_MediaServers); | |
85 m_MediaServers.Put(uuid, device); | |
86 } | |
87 | |
88 return PLT_MediaBrowser::OnDeviceAdded(device); | |
89 } | |
90 | |
91 /*---------------------------------------------------------------------- | |
92 | PLT_SyncMediaBrowser::OnDeviceRemoved | |
93 +---------------------------------------------------------------------*/ | |
94 NPT_Result | |
95 PLT_SyncMediaBrowser::OnDeviceRemoved(PLT_DeviceDataReference& device) | |
96 { | |
97 NPT_String uuid = device->GetUUID(); | |
98 | |
99 // Remove from our list of servers first if found | |
100 { | |
101 NPT_AutoLock lock(m_MediaServers); | |
102 m_MediaServers.Erase(uuid); | |
103 } | |
104 | |
105 // clear cache for that device | |
106 if (m_UseCache) m_Cache.Clear(device.AsPointer()->GetUUID()); | |
107 | |
108 return PLT_MediaBrowser::OnDeviceRemoved(device); | |
109 } | |
110 | |
111 /*---------------------------------------------------------------------- | |
112 | PLT_SyncMediaBrowser::Find | |
113 +---------------------------------------------------------------------*/ | |
114 NPT_Result | |
115 PLT_SyncMediaBrowser::Find(const char* ip, PLT_DeviceDataReference& device) | |
116 { | |
117 NPT_AutoLock lock(m_MediaServers); | |
118 const NPT_List<PLT_DeviceMapEntry*>::Iterator it = | |
119 m_MediaServers.GetEntries().Find(PLT_DeviceMapFinderByIp(ip)); | |
120 if (it) { | |
121 device = (*it)->GetValue(); | |
122 return NPT_SUCCESS; | |
123 } | |
124 return NPT_FAILURE; | |
125 } | |
126 | |
127 /*---------------------------------------------------------------------- | |
128 | PLT_SyncMediaBrowser::OnBrowseResult | |
129 +---------------------------------------------------------------------*/ | |
130 void | |
131 PLT_SyncMediaBrowser::OnBrowseResult(NPT_Result res, | |
132 PLT_DeviceDataReference& device, | |
133 PLT_BrowseInfo* info, | |
134 void* userdata) | |
135 { | |
136 NPT_COMPILER_UNUSED(device); | |
137 | |
138 if (!userdata) return; | |
139 | |
140 PLT_BrowseDataReference* data = (PLT_BrowseDataReference*) userdata; | |
141 (*data)->res = res; | |
142 if (NPT_SUCCEEDED(res) && info) { | |
143 (*data)->info = *info; | |
144 } | |
145 (*data)->shared_var.SetValue(1); | |
146 delete data; | |
147 } | |
148 | |
149 /*---------------------------------------------------------------------- | |
150 | PLT_SyncMediaBrowser::OnMSStateVariablesChanged | |
151 +---------------------------------------------------------------------*/ | |
152 void | |
153 PLT_SyncMediaBrowser::OnMSStateVariablesChanged(PLT_Service* service, | |
154 NPT_List<PLT_StateVariable*>* vars) | |
155 { | |
156 NPT_AutoLock lock(m_MediaServers); | |
157 | |
158 PLT_DeviceDataReference device; | |
159 const NPT_List<PLT_DeviceMapEntry*>::Iterator it = | |
160 m_MediaServers.GetEntries().Find(PLT_DeviceMapFinderByUUID(service->GetDevice()->GetUUID())); | |
161 if (!it) return; // device with this service has gone away | |
162 | |
163 device = (*it)->GetValue(); | |
164 PLT_StateVariable* var = PLT_StateVariable::Find(*vars, "ContainerUpdateIDs"); | |
165 if (var) { | |
166 // variable found, parse value | |
167 NPT_String value = var->GetValue(); | |
168 NPT_String item_id, update_id; | |
169 int index; | |
170 | |
171 while (value.GetLength()) { | |
172 // look for container id | |
173 index = value.Find(','); | |
174 if (index < 0) break; | |
175 item_id = value.Left(index); | |
176 value = value.SubString(index+1); | |
177 | |
178 // look for update id | |
179 if (value.GetLength()) { | |
180 index = value.Find(','); | |
181 update_id = (index<0)?value:value.Left(index); | |
182 value = (index<0)?"":value.SubString(index+1); | |
183 | |
184 // clear cache for that device | |
185 if (m_UseCache) m_Cache.Clear(device->GetUUID(), item_id); | |
186 | |
187 // notify listener | |
188 if (m_ContainerListener) m_ContainerListener->OnContainerChanged(device, item_id, update_id); | |
189 } | |
190 } | |
191 } | |
192 } | |
193 | |
194 /*---------------------------------------------------------------------- | |
195 | PLT_SyncMediaBrowser::BrowseSync | |
196 +---------------------------------------------------------------------*/ | |
197 NPT_Result | |
198 PLT_SyncMediaBrowser::BrowseSync(PLT_BrowseDataReference& browse_data, | |
199 PLT_DeviceDataReference& device, | |
200 const char* object_id, | |
201 NPT_Int32 index, | |
202 NPT_Int32 count, | |
203 bool browse_metadata, | |
204 const char* filter, | |
205 const char* sort) | |
206 { | |
207 NPT_Result res; | |
208 | |
209 browse_data->shared_var.SetValue(0); | |
210 | |
211 // send off the browse packet. Note that this will | |
212 // not block. There is a call to WaitForResponse in order | |
213 // to block until the response comes back. | |
214 res = PLT_MediaBrowser::Browse(device, | |
215 (const char*)object_id, | |
216 index, | |
217 count, | |
218 browse_metadata, | |
219 filter, | |
220 sort, | |
221 new PLT_BrowseDataReference(browse_data)); | |
222 NPT_CHECK_SEVERE(res); | |
223 | |
224 return WaitForResponse(browse_data->shared_var); | |
225 } | |
226 | |
227 /*---------------------------------------------------------------------- | |
228 | PLT_SyncMediaBrowser::BrowseSync | |
229 +---------------------------------------------------------------------*/ | |
230 NPT_Result | |
231 PLT_SyncMediaBrowser::BrowseSync(PLT_DeviceDataReference& device, | |
232 const char* object_id, | |
233 PLT_MediaObjectListReference& list) | |
234 { | |
235 NPT_Result res = NPT_FAILURE; | |
236 NPT_Int32 index = 0; | |
237 | |
238 // reset output params | |
239 list = NULL; | |
240 | |
241 // look into cache first | |
242 if (m_UseCache && NPT_SUCCEEDED(m_Cache.Get(device->GetUUID(), object_id, list))) return NPT_SUCCESS; | |
243 | |
244 do { | |
245 PLT_BrowseDataReference browse_data(new PLT_BrowseData()); | |
246 | |
247 // send off the browse packet. Note that this will | |
248 // not block. There is a call to WaitForResponse in order | |
249 // to block until the response comes back. | |
250 res = BrowseSync( | |
251 browse_data, | |
252 device, | |
253 (const char*)object_id, | |
254 index, | |
255 1024, | |
256 false, | |
257 "*", | |
258 ""); | |
259 NPT_CHECK_LABEL_WARNING(res, done); | |
260 | |
261 if (NPT_FAILED(browse_data->res)) { | |
262 res = browse_data->res; | |
263 NPT_CHECK_LABEL_WARNING(res, done); | |
264 } | |
265 | |
266 if (browse_data->info.items->GetItemCount() == 0) | |
267 break; | |
268 | |
269 if (list.IsNull()) { | |
270 list = browse_data->info.items; | |
271 } else { | |
272 list->Add(*browse_data->info.items); | |
273 // clear the list items so that the data inside is not | |
274 // cleaned up by PLT_MediaItemList dtor since we copied | |
275 // each pointer into the new list. | |
276 browse_data->info.items->Clear(); | |
277 } | |
278 | |
279 // stop now if our list contains exactly what the server said it had | |
280 if (browse_data->info.tm && browse_data->info.tm == list->GetItemCount()) | |
281 break; | |
282 | |
283 // ask for the next chunk of entries | |
284 index = list->GetItemCount(); | |
285 } while(1); | |
286 | |
287 done: | |
288 // cache the result | |
289 if (m_UseCache && NPT_SUCCEEDED(res) && !list.IsNull() && list->GetItemCount()) { | |
290 m_Cache.Put(device->GetUUID(), object_id, list); | |
291 } | |
292 | |
293 // clear entire cache data for device if failed, the device could be gone | |
294 if (NPT_FAILED(res) && m_UseCache) m_Cache.Clear(device->GetUUID()); | |
295 | |
296 return res; | |
297 } | |
298 | |
299 /*---------------------------------------------------------------------- | |
300 | PLT_SyncMediaBrowser::IsCached | |
301 +---------------------------------------------------------------------*/ | |
302 bool | |
303 PLT_SyncMediaBrowser::IsCached(const char* uuid, const char* object_id) | |
304 { | |
305 NPT_AutoLock lock(m_MediaServers); | |
306 const NPT_List<PLT_DeviceMapEntry*>::Iterator it = | |
307 m_MediaServers.GetEntries().Find(PLT_DeviceMapFinderByUUID(uuid)); | |
308 if (!it) { | |
309 m_Cache.Clear(uuid); | |
310 return false; // device with this service has gone away | |
311 } | |
312 | |
313 PLT_MediaObjectListReference list; | |
314 return NPT_SUCCEEDED(m_Cache.Get(uuid, object_id, list))?true:false; | |
315 } | |
316 |