1 // Copyright (C) 2013 James Turner - zakalawe@mac.com
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #include <simgear/package/Root.hxx>
20 #include <boost/foreach.hpp>
26 #include <simgear/debug/logstream.hxx>
27 #include <simgear/props/props_io.hxx>
28 #include <simgear/io/HTTPRequest.hxx>
29 #include <simgear/io/HTTPClient.hxx>
30 #include <simgear/misc/sg_dir.hxx>
31 #include <simgear/structure/exception.hxx>
32 #include <simgear/package/Package.hxx>
33 #include <simgear/package/Install.hxx>
34 #include <simgear/package/Catalog.hxx>
40 typedef std::map<std::string, CatalogRef> CatalogDict;
41 typedef std::vector<Delegate*> DelegateVec;
42 typedef std::map<std::string, std::string> MemThumbnailCache;
43 typedef std::deque<std::string> StringDeque;
45 class Root::ThumbnailDownloader : public HTTP::Request
48 ThumbnailDownloader(Root::RootPrivate* aOwner, const std::string& aUrl) :
55 virtual void gotBodyData(const char* s, int n)
57 m_buffer += std::string(s, n);
60 virtual void onDone();
63 Root::RootPrivate* m_owner;
67 class Root::RootPrivate
72 maxAgeSeconds(60 * 60 * 24)
76 void fireStartInstall(InstallRef install)
78 DelegateVec::const_iterator it;
79 for (it = delegates.begin(); it != delegates.end(); ++it) {
80 (*it)->startInstall(install);
84 void fireInstallProgress(InstallRef install,
85 unsigned int aBytes, unsigned int aTotal)
87 DelegateVec::const_iterator it;
88 for (it = delegates.begin(); it != delegates.end(); ++it) {
89 (*it)->installProgress(install, aBytes, aTotal);
93 void fireFinishInstall(InstallRef install, Delegate::StatusCode status)
95 DelegateVec::const_iterator it;
96 for (it = delegates.begin(); it != delegates.end(); ++it) {
97 (*it)->finishInstall(install, status);
101 void fireRefreshStatus(CatalogRef catalog, Delegate::StatusCode status)
103 DelegateVec::const_iterator it;
104 for (it = delegates.begin(); it != delegates.end(); ++it) {
105 (*it)->catalogRefreshed(catalog, status);
110 void thumbnailDownloadComplete(HTTP::Request_ptr request,
111 Delegate::StatusCode status, const std::string& bytes)
113 std::string u(request->url());
114 if (status == Delegate::STATUS_SUCCESS) {
115 thumbnailCache[u] = bytes;
116 fireDataForThumbnail(u, bytes);
119 downloadNextPendingThumbnail();
122 void fireDataForThumbnail(const std::string& aUrl, const std::string& bytes)
124 DelegateVec::const_iterator it;
125 const uint8_t* data = reinterpret_cast<const uint8_t*>(bytes.data());
126 for (it = delegates.begin(); it != delegates.end(); ++it) {
127 (*it)->dataForThumbnail(aUrl, bytes.size(), data);
131 void downloadNextPendingThumbnail()
133 thumbnailDownloadRequest.clear();
134 if (pendingThumbnails.empty()) {
138 std::string u = pendingThumbnails.front();
139 pendingThumbnails.pop_front();
140 thumbnailDownloadRequest = new Root::ThumbnailDownloader(this, u);
143 http->makeRequest(thumbnailDownloadRequest);
145 httpPendingRequests.push_back(thumbnailDownloadRequest);
149 DelegateVec delegates;
154 CatalogDict catalogs;
155 unsigned int maxAgeSeconds;
158 std::set<CatalogRef> refreshing;
159 typedef std::deque<InstallRef> UpdateDeque;
160 UpdateDeque updateDeque;
161 std::deque<HTTP::Request_ptr> httpPendingRequests;
163 HTTP::Request_ptr thumbnailDownloadRequest;
164 StringDeque pendingThumbnails;
165 MemThumbnailCache thumbnailCache;
167 typedef std::map<PackageRef, InstallRef> InstallCache;
168 InstallCache m_installs;
172 void Root::ThumbnailDownloader::onDone()
174 if (responseCode() != 200) {
175 SG_LOG(SG_GENERAL, SG_ALERT, "thumbnail download failure:" << url());
176 m_owner->thumbnailDownloadComplete(this, Delegate::FAIL_DOWNLOAD, std::string());
180 m_owner->thumbnailDownloadComplete(this, Delegate::STATUS_SUCCESS, m_buffer);
181 //time(&m_owner->m_retrievedTime);
182 //m_owner->writeTimestamp();
183 //m_owner->refreshComplete(Delegate::STATUS_REFRESHED);
186 SGPath Root::path() const
191 void Root::setMaxAgeSeconds(unsigned int seconds)
193 d->maxAgeSeconds = seconds;
196 unsigned int Root::maxAgeSeconds() const
198 return d->maxAgeSeconds;
201 void Root::setHTTPClient(HTTP::Client* aHTTP)
204 BOOST_FOREACH(HTTP::Request_ptr req, d->httpPendingRequests) {
205 d->http->makeRequest(req);
208 d->httpPendingRequests.clear();
211 void Root::makeHTTPRequest(HTTP::Request *req)
214 d->http->makeRequest(req);
218 d->httpPendingRequests.push_back(req);
221 Root::Root(const SGPath& aPath, const std::string& aVersion) :
225 d->version = aVersion;
226 if (getenv("LOCALE")) {
227 d->locale = getenv("LOCALE");
236 BOOST_FOREACH(SGPath c, dir.children(Dir::TYPE_DIR)) {
237 CatalogRef cat = Catalog::createFromPath(this, c);
239 d->catalogs[cat->id()] = cat;
241 } // of child directories iteration
249 int Root::catalogVersion() const
254 std::string Root::applicationVersion() const
259 CatalogRef Root::getCatalogById(const std::string& aId) const
261 CatalogDict::const_iterator it = d->catalogs.find(aId);
262 if (it == d->catalogs.end()) {
269 PackageRef Root::getPackageById(const std::string& aName) const
271 size_t lastDot = aName.rfind('.');
273 PackageRef pkg = NULL;
274 if (lastDot == std::string::npos) {
276 CatalogDict::const_iterator it = d->catalogs.begin();
277 for (; it != d->catalogs.end(); ++it) {
278 pkg = it->second->getPackageById(aName);
287 std::string catalogId = aName.substr(0, lastDot);
288 std::string id = aName.substr(lastDot + 1);
289 CatalogRef catalog = getCatalogById(catalogId);
294 return catalog->getPackageById(id);
297 CatalogList Root::catalogs() const
300 CatalogDict::const_iterator it = d->catalogs.begin();
301 for (; it != d->catalogs.end(); ++it) {
302 r.push_back(it->second);
309 Root::allPackages() const
313 CatalogDict::const_iterator it = d->catalogs.begin();
314 for (; it != d->catalogs.end(); ++it) {
315 const PackageList& r2(it->second->packages());
316 r.insert(r.end(), r2.begin(), r2.end());
323 Root::packagesMatching(const SGPropertyNode* aFilter) const
327 CatalogDict::const_iterator it = d->catalogs.begin();
328 for (; it != d->catalogs.end(); ++it) {
329 PackageList r2(it->second->packagesMatching(aFilter));
330 r.insert(r.end(), r2.begin(), r2.end());
337 Root::packagesNeedingUpdate() const
341 CatalogDict::const_iterator it = d->catalogs.begin();
342 for (; it != d->catalogs.end(); ++it) {
343 PackageList r2(it->second->packagesNeedingUpdate());
344 r.insert(r.end(), r2.begin(), r2.end());
350 void Root::refresh(bool aForce)
352 bool didStartAny = false;
353 CatalogDict::iterator it = d->catalogs.begin();
354 for (; it != d->catalogs.end(); ++it) {
355 if (aForce || it->second->needsRefresh()) {
356 it->second->refresh();
362 // signal refresh complete to the delegate already
363 d->fireRefreshStatus(CatalogRef(), Delegate::STATUS_REFRESHED);
367 void Root::addDelegate(simgear::pkg::Delegate *aDelegate)
369 d->delegates.push_back(aDelegate);
372 void Root::removeDelegate(simgear::pkg::Delegate *aDelegate)
374 DelegateVec::iterator it = std::find(d->delegates.begin(),
375 d->delegates.end(), aDelegate);
376 if (it == d->delegates.end()) {
377 throw sg_exception("unknown delegate in removeDelegate");
379 d->delegates.erase(it);
382 void Root::setLocale(const std::string& aLocale)
387 std::string Root::getLocale() const
392 void Root::scheduleToUpdate(InstallRef aInstall)
395 throw sg_exception("missing argument to scheduleToUpdate");
398 PackageList deps = aInstall->package()->dependencies();
399 BOOST_FOREACH(Package* dep, deps) {
400 // will internally schedule for update if required
401 // hence be careful, this method is re-entered in here!
405 bool wasEmpty = d->updateDeque.empty();
406 d->updateDeque.push_back(aInstall);
409 aInstall->startUpdate();
413 bool Root::isInstallQueued(InstallRef aInstall) const
415 RootPrivate::UpdateDeque::const_iterator it =
416 std::find(d->updateDeque.begin(), d->updateDeque.end(), aInstall);
417 return (it != d->updateDeque.end());
420 void Root::startInstall(InstallRef aInstall)
422 d->fireStartInstall(aInstall);
425 void Root::installProgress(InstallRef aInstall, unsigned int aBytes, unsigned int aTotal)
427 d->fireInstallProgress(aInstall, aBytes, aTotal);
430 void Root::startNext(InstallRef aCurrent)
432 if (d->updateDeque.front() != aCurrent) {
433 SG_LOG(SG_GENERAL, SG_ALERT, "current install of package not head of the deque");
435 d->updateDeque.pop_front();
438 if (!d->updateDeque.empty()) {
439 d->updateDeque.front()->startUpdate();
443 void Root::finishInstall(InstallRef aInstall, Delegate::StatusCode aReason)
445 if (aReason != Delegate::STATUS_SUCCESS) {
446 SG_LOG(SG_GENERAL, SG_ALERT, "failed to install package:"
447 << aInstall->package()->id() << ":" << aReason);
450 // order matters here, so a call to 'isQueued' from a finish-install
451 // callback returns false, not true
453 d->fireFinishInstall(aInstall, aReason);
456 void Root::cancelDownload(InstallRef aInstall)
458 RootPrivate::UpdateDeque::iterator it =
459 std::find(d->updateDeque.begin(), d->updateDeque.end(), aInstall);
460 if (it != d->updateDeque.end()) {
461 bool startNext = (aInstall == d->updateDeque.front());
462 d->updateDeque.erase(it);
464 if (!d->updateDeque.empty()) {
465 d->updateDeque.front()->startUpdate();
467 } // of install was front item
468 } // of found install in queue
471 void Root::catalogRefreshStatus(CatalogRef aCat, Delegate::StatusCode aReason)
473 CatalogDict::iterator catIt = d->catalogs.find(aCat->id());
474 d->fireRefreshStatus(aCat, aReason);
476 if (aReason == Delegate::STATUS_IN_PROGRESS) {
477 d->refreshing.insert(aCat);
479 if (catIt == d->catalogs.end()) {
480 // first fresh, add to our storage now
481 d->catalogs.insert(catIt, CatalogDict::value_type(aCat->id(), aCat));
484 d->refreshing.erase(aCat);
487 if ((aReason != Delegate::STATUS_REFRESHED) && (aReason != Delegate::STATUS_IN_PROGRESS)) {
488 // if the failure is permanent, delete the catalog from our
489 // list (don't touch it on disk)
490 bool isPermanentFailure = (aReason == Delegate::FAIL_VERSION);
491 if (isPermanentFailure) {
492 SG_LOG(SG_GENERAL, SG_WARN, "permanent failure for catalog:" << aCat->id());
493 if (catIt != d->catalogs.end()) {
494 d->catalogs.erase(catIt);
499 if (d->refreshing.empty()) {
500 d->fireRefreshStatus(CatalogRef(), Delegate::STATUS_REFRESHED);
504 bool Root::removeCatalogById(const std::string& aId)
506 CatalogDict::iterator catIt = d->catalogs.find(aId);
507 if (catIt == d->catalogs.end()) {
508 SG_LOG(SG_GENERAL, SG_WARN, "removeCatalogById: unknown ID:" << aId);
512 CatalogRef cat = catIt->second;
514 // drop the reference
515 d->catalogs.erase(catIt);
517 bool ok = cat->uninstall();
519 SG_LOG(SG_GENERAL, SG_WARN, "removeCatalogById: catalog :" << aId
520 << "failed to uninstall");
526 void Root::requestThumbnailData(const std::string& aUrl)
528 MemThumbnailCache::iterator it = d->thumbnailCache.find(aUrl);
529 if (it == d->thumbnailCache.end()) {
530 // insert into cache to mark as pending
531 d->pendingThumbnails.push_front(aUrl);
532 d->thumbnailCache[aUrl] = std::string();
533 d->downloadNextPendingThumbnail();
534 } else if (!it->second.empty()) {
535 // already loaded, fire data synchronously
536 d->fireDataForThumbnail(aUrl, it->second);
538 // in cache but empty data, still fetching
542 InstallRef Root::existingInstallForPackage(PackageRef p) const
544 RootPrivate::InstallCache::const_iterator it =
545 d->m_installs.find(p);
546 if (it == d->m_installs.end()) {
547 // check if it exists on disk, create
548 SGPath path(p->pathOnDisk());
550 // this will add to our cache, and hence, modify m_installs
551 return Install::createFromPath(path, p->catalog());
554 // insert a null reference into the dictionary, so we don't call
555 // the pathOnDisk -> exists codepath repeatedley
556 d->m_installs[p] = InstallRef();
563 void Root::registerInstall(InstallRef ins)
569 d->m_installs[ins->package()] = ins;
572 void Root::unregisterInstall(InstallRef ins)
578 d->m_installs.erase(ins->package());
581 } // of namespace pkg
583 } // of namespace simgear