|
a/libupnpp/discovery.cxx |
|
b/libupnpp/discovery.cxx |
|
... |
|
... |
33 |
#include "expatmm.hxx"
|
33 |
#include "expatmm.hxx"
|
34 |
#include "upnpplib.hxx"
|
34 |
#include "upnpplib.hxx"
|
35 |
#include "description.hxx"
|
35 |
#include "description.hxx"
|
36 |
#include "cdirectory.hxx"
|
36 |
#include "cdirectory.hxx"
|
37 |
#include "discovery.hxx"
|
37 |
#include "discovery.hxx"
|
|
|
38 |
#include "log.hxx"
|
38 |
|
39 |
|
39 |
// The service type string we are looking for.
|
40 |
// The service type string we are looking for.
|
40 |
static const string
|
41 |
static const string
|
41 |
ContentDirectorySType("urn:schemas-upnp-org:service:ContentDirectory:1");
|
42 |
ContentDirectorySType("urn:schemas-upnp-org:service:ContentDirectory:1");
|
42 |
// We don't include a version in comparisons, as we are satisfied with
|
43 |
// We don't include a version in comparisons, as we are satisfied with
|
|
... |
|
... |
133 |
discoveredQueue.workerExit();
|
134 |
discoveredQueue.workerExit();
|
134 |
return (void*)1;
|
135 |
return (void*)1;
|
135 |
}
|
136 |
}
|
136 |
PLOGDEB("discoExplorer: alive %d deviceId [%s] URL [%s]\n",
|
137 |
PLOGDEB("discoExplorer: alive %d deviceId [%s] URL [%s]\n",
|
137 |
tsk->alive, tsk->deviceId.c_str(), tsk->url.c_str());
|
138 |
tsk->alive, tsk->deviceId.c_str(), tsk->url.c_str());
|
138 |
PTMutexLocker lock(contentDirectories.m_mutex);
|
|
|
139 |
if (!tsk->alive) {
|
139 |
if (!tsk->alive) {
|
140 |
// Device signals it is going off.
|
140 |
// Device signals it is going off.
|
|
|
141 |
PTMutexLocker lock(contentDirectories.m_mutex);
|
141 |
DirPoolIt it = contentDirectories.m_directories.find(tsk->deviceId);
|
142 |
DirPoolIt it = contentDirectories.m_directories.find(tsk->deviceId);
|
142 |
if (it != contentDirectories.m_directories.end()) {
|
143 |
if (it != contentDirectories.m_directories.end()) {
|
143 |
contentDirectories.m_directories.erase(it);
|
144 |
contentDirectories.m_directories.erase(it);
|
144 |
PLOGDEB("discoExplorer: delete [%s]\n", tsk->deviceId.c_str());
|
145 |
//LOGDEB("discoExplorer: delete " << tsk->deviceId.c_str() <<
|
|
|
146 |
// endl);
|
145 |
}
|
147 |
}
|
146 |
} else {
|
148 |
} else {
|
147 |
// Device signals its existence and well-being. Perform the
|
149 |
// Device signals its existence and well-being. Perform the
|
148 |
// UPnP "description" phase by downloading and decoding the
|
150 |
// UPnP "description" phase by downloading and decoding the
|
149 |
// description document.
|
151 |
// description document.
|
150 |
char *buf;
|
152 |
char *buf = 0;
|
151 |
// LINE_SIZE is defined by libupnp's upnp.h...
|
153 |
// LINE_SIZE is defined by libupnp's upnp.h...
|
152 |
char contentType[LINE_SIZE];
|
154 |
char contentType[LINE_SIZE];
|
153 |
int code = UpnpDownloadUrlItem(tsk->url.c_str(), &buf, contentType);
|
155 |
int code = UpnpDownloadUrlItem(tsk->url.c_str(), &buf, contentType);
|
154 |
if (code != UPNP_E_SUCCESS) {
|
156 |
if (code != UPNP_E_SUCCESS) {
|
155 |
cerr << LibUPnP::errAsString("discoExplorer", code) << endl;
|
157 |
LOGERR(LibUPnP::errAsString("discoExplorer", code) << endl);
|
156 |
continue;
|
158 |
continue;
|
157 |
}
|
159 |
}
|
158 |
string sdesc(buf);
|
160 |
string sdesc(buf);
|
|
|
161 |
free(buf);
|
|
|
162 |
|
159 |
PLOGDEB("discoExplorer: downloaded description document of "
|
163 |
//LOGDEB("discoExplorer: downloaded description document of " <<
|
160 |
"%d bytes\n", int(sdesc.size()));
|
164 |
// sdesc.size() << " bytes" << endl);
|
161 |
|
165 |
|
162 |
// Update or insert the device
|
166 |
// Update or insert the device
|
163 |
ContentDirectoryDescriptor d(tsk->url, sdesc,
|
167 |
ContentDirectoryDescriptor d(tsk->url, sdesc,
|
164 |
time(0), tsk->expires);
|
168 |
time(0), tsk->expires);
|
165 |
if (!d.device.ok) {
|
169 |
if (!d.device.ok) {
|
166 |
PLOGDEB("discoExplorer: description parse failed\n");
|
170 |
LOGERR("discoExplorer: description parse failed for " <<
|
|
|
171 |
tsk->deviceId << endl);
|
|
|
172 |
delete tsk;
|
167 |
continue;
|
173 |
continue;
|
168 |
}
|
174 |
}
|
|
|
175 |
PTMutexLocker lock(contentDirectories.m_mutex);
|
169 |
PLOGDEB("discoExplorer: inserting id [%s]\n", tsk->deviceId.c_str());
|
176 |
//LOGDEB("discoExplorer: inserting id "<< tsk->deviceId.c_str() <<
|
|
|
177 |
// endl);
|
170 |
contentDirectories.m_directories[tsk->deviceId] = d;
|
178 |
contentDirectories.m_directories[tsk->deviceId] = d;
|
171 |
}
|
179 |
}
|
172 |
delete tsk;
|
180 |
delete tsk;
|
173 |
}
|
181 |
}
|
174 |
}
|
182 |
}
|
|
... |
|
... |
230 |
bool didsomething = false;
|
238 |
bool didsomething = false;
|
231 |
|
239 |
|
232 |
for (DirPoolIt it = contentDirectories.m_directories.begin();
|
240 |
for (DirPoolIt it = contentDirectories.m_directories.begin();
|
233 |
it != contentDirectories.m_directories.end();) {
|
241 |
it != contentDirectories.m_directories.end();) {
|
234 |
if (now - it->second.last_seen > it->second.expires) {
|
242 |
if (now - it->second.last_seen > it->second.expires) {
|
235 |
PLOGDEB("expireDevices: deleting [%s] [%s]\n",
|
243 |
//LOGDEB("expireDevices: deleting " << it->first.c_str() << " " <<
|
236 |
it->first.c_str(), it->second.device.friendlyName.c_str());
|
244 |
// it->second.device.friendlyName.c_str() << endl);
|
237 |
contentDirectories.m_directories.erase(it++);
|
245 |
contentDirectories.m_directories.erase(it++);
|
238 |
didsomething = true;
|
246 |
didsomething = true;
|
239 |
} else {
|
247 |
} else {
|
240 |
it++;
|
248 |
it++;
|
241 |
}
|
249 |
}
|
|
... |
|
... |
307 |
if (theDevDir && !theDevDir->ok())
|
315 |
if (theDevDir && !theDevDir->ok())
|
308 |
return 0;
|
316 |
return 0;
|
309 |
return theDevDir;
|
317 |
return theDevDir;
|
310 |
}
|
318 |
}
|
311 |
|
319 |
|
|
|
320 |
void UPnPDeviceDirectory::terminate()
|
|
|
321 |
{
|
|
|
322 |
discoveredQueue.setTerminateAndWait();
|
|
|
323 |
}
|
|
|
324 |
|
312 |
time_t UPnPDeviceDirectory::getRemainingDelay()
|
325 |
time_t UPnPDeviceDirectory::getRemainingDelay()
|
313 |
{
|
326 |
{
|
314 |
time_t now = time(0);
|
327 |
time_t now = time(0);
|
315 |
if (now - m_lastSearch >= m_searchTimeout)
|
328 |
if (now - m_lastSearch >= m_searchTimeout)
|
316 |
return 0;
|
329 |
return 0;
|
317 |
return m_searchTimeout - (now - m_lastSearch);
|
330 |
return m_searchTimeout - (now - m_lastSearch);
|
318 |
}
|
331 |
}
|
319 |
|
332 |
|
320 |
bool UPnPDeviceDirectory::getDirServices(vector<ContentDirectoryService>& out)
|
333 |
bool UPnPDeviceDirectory::getDirServices(vector<ContentDirectoryService>& out)
|
321 |
{
|
334 |
{
|
|
|
335 |
//LOGDEB("UPnPDeviceDirectory::getDirServices" << endl);
|
322 |
if (m_ok == false)
|
336 |
if (m_ok == false)
|
323 |
return false;
|
337 |
return false;
|
324 |
|
338 |
|
325 |
if (getRemainingDelay() > 0)
|
339 |
if (getRemainingDelay() > 0)
|
326 |
sleep(getRemainingDelay());
|
340 |
sleep(getRemainingDelay());
|