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;
42 class Root::RootPrivate
47 maxAgeSeconds(60 * 60 * 24),
57 unsigned int maxAgeSeconds;
61 std::set<CatalogRef> refreshing;
62 std::deque<InstallRef> updateDeque;
63 std::deque<HTTP::Request_ptr> httpPendingRequests;
66 SGPath Root::path() const
71 void Root::setMaxAgeSeconds(unsigned int seconds)
73 d->maxAgeSeconds = seconds;
76 unsigned int Root::maxAgeSeconds() const
78 return d->maxAgeSeconds;
81 void Root::setHTTPClient(HTTP::Client* aHTTP)
84 BOOST_FOREACH(HTTP::Request_ptr req, d->httpPendingRequests) {
85 d->http->makeRequest(req);
88 d->httpPendingRequests.clear();
91 void Root::makeHTTPRequest(HTTP::Request *req)
94 d->http->makeRequest(req);
98 d->httpPendingRequests.push_back(req);
101 Root::Root(const SGPath& aPath, const std::string& aVersion) :
105 d->version = aVersion;
106 if (getenv("LOCALE")) {
107 d->locale = getenv("LOCALE");
116 BOOST_FOREACH(SGPath c, dir.children(Dir::TYPE_DIR)) {
117 CatalogRef cat = Catalog::createFromPath(this, c);
119 d->catalogs[cat->id()] = cat;
121 } // of child directories iteration
129 std::string Root::catalogVersion() const
134 CatalogRef Root::getCatalogById(const std::string& aId) const
136 CatalogDict::const_iterator it = d->catalogs.find(aId);
137 if (it == d->catalogs.end()) {
144 PackageRef Root::getPackageById(const std::string& aName) const
146 size_t lastDot = aName.rfind('.');
148 PackageRef pkg = NULL;
149 if (lastDot == std::string::npos) {
151 CatalogDict::const_iterator it = d->catalogs.begin();
152 for (; it != d->catalogs.end(); ++it) {
153 pkg = it->second->getPackageById(aName);
162 std::string catalogId = aName.substr(0, lastDot);
163 std::string id = aName.substr(lastDot + 1);
164 CatalogRef catalog = getCatalogById(catalogId);
169 return catalog->getPackageById(id);
172 CatalogList Root::catalogs() const
175 CatalogDict::const_iterator it = d->catalogs.begin();
176 for (; it != d->catalogs.end(); ++it) {
177 r.push_back(it->second);
184 Root::packagesMatching(const SGPropertyNode* aFilter) const
188 CatalogDict::const_iterator it = d->catalogs.begin();
189 for (; it != d->catalogs.end(); ++it) {
190 PackageList r2(it->second->packagesMatching(aFilter));
191 r.insert(r.end(), r2.begin(), r2.end());
198 Root::packagesNeedingUpdate() const
202 CatalogDict::const_iterator it = d->catalogs.begin();
203 for (; it != d->catalogs.end(); ++it) {
204 PackageList r2(it->second->packagesNeedingUpdate());
205 r.insert(r.end(), r2.begin(), r2.end());
211 void Root::refresh(bool aForce)
213 CatalogDict::iterator it = d->catalogs.begin();
214 for (; it != d->catalogs.end(); ++it) {
215 if (aForce || it->second->needsRefresh()) {
216 it->second->refresh();
221 void Root::setDelegate(simgear::pkg::Delegate *aDelegate)
223 d->delegate = aDelegate;
226 void Root::setLocale(const std::string& aLocale)
231 std::string Root::getLocale() const
236 void Root::scheduleToUpdate(InstallRef aInstall)
239 throw sg_exception("missing argument to scheduleToUpdate");
242 PackageList deps = aInstall->package()->dependencies();
243 BOOST_FOREACH(Package* dep, deps) {
244 // will internally schedule for update if required
245 // hence be careful, this method is re-entered in here!
249 bool wasEmpty = d->updateDeque.empty();
250 d->updateDeque.push_back(aInstall);
253 aInstall->startUpdate();
257 void Root::startInstall(InstallRef aInstall)
260 d->delegate->startInstall(aInstall.ptr());
264 void Root::installProgress(InstallRef aInstall, unsigned int aBytes, unsigned int aTotal)
267 d->delegate->installProgress(aInstall.ptr(), aBytes, aTotal);
271 void Root::startNext(InstallRef aCurrent)
273 if (d->updateDeque.front() != aCurrent) {
274 SG_LOG(SG_GENERAL, SG_ALERT, "current install of package not head of the deque");
276 d->updateDeque.pop_front();
279 if (!d->updateDeque.empty()) {
280 d->updateDeque.front()->startUpdate();
284 void Root::finishInstall(InstallRef aInstall)
287 d->delegate->finishInstall(aInstall.ptr());
293 void Root::failedInstall(InstallRef aInstall, Delegate::FailureCode aReason)
295 SG_LOG(SG_GENERAL, SG_ALERT, "failed to install package:"
296 << aInstall->package()->id() << ":" << aReason);
298 d->delegate->failedInstall(aInstall.ptr(), aReason);
304 void Root::catalogRefreshBegin(CatalogRef aCat)
306 d->refreshing.insert(aCat);
309 void Root::catalogRefreshComplete(CatalogRef aCat, Delegate::FailureCode aReason)
311 CatalogDict::iterator catIt = d->catalogs.find(aCat->id());
312 if (aReason != Delegate::FAIL_SUCCESS) {
314 d->delegate->failedRefresh(aCat, aReason);
317 // if the failure is permanent, delete the catalog from our
318 // list (don't touch it on disk)
319 bool isPermanentFailure = (aReason == Delegate::FAIL_VERSION);
320 if (isPermanentFailure) {
321 SG_LOG(SG_GENERAL, SG_WARN, "permanent failure for catalog:" << aCat->id());
322 if (catIt != d->catalogs.end()) {
323 d->catalogs.erase(catIt);
326 } else if (catIt == d->catalogs.end()) {
327 // first fresh, add to our storage now
328 d->catalogs.insert(catIt, CatalogDict::value_type(aCat->id(), aCat));
331 d->refreshing.erase(aCat);
332 if (d->refreshing.empty()) {
334 d->delegate->refreshComplete();
339 } // of namespace pkg
341 } // of namespace simgear