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, Catalog*> CatalogDict;
42 class Root::RootPrivate
47 maxAgeSeconds(60 * 60 * 24),
57 unsigned int maxAgeSeconds;
61 std::set<Catalog*> refreshing;
62 std::deque<Install*> updateDeque;
63 std::deque<HTTP::Request_ptr> httpPendingRequests;
66 SGPath Root::path() const
71 void Root::setMaxAgeSeconds(int seconds)
73 d->maxAgeSeconds = seconds;
76 void Root::setHTTPClient(HTTP::Client* aHTTP)
79 BOOST_FOREACH(HTTP::Request_ptr req, d->httpPendingRequests) {
80 d->http->makeRequest(req);
83 d->httpPendingRequests.clear();
86 void Root::makeHTTPRequest(HTTP::Request *req)
89 d->http->makeRequest(req);
93 d->httpPendingRequests.push_back(req);
96 Root::Root(const SGPath& aPath, const std::string& aVersion) :
100 d->version = aVersion;
101 if (getenv("LOCALE")) {
102 d->locale = getenv("LOCALE");
111 BOOST_FOREACH(SGPath c, dir.children(Dir::TYPE_DIR)) {
112 Catalog* cat = Catalog::createFromPath(this, c);
114 d->catalogs[cat->id()] = cat;
116 } // of child directories iteration
124 std::string Root::catalogVersion() const
129 Catalog* Root::getCatalogById(const std::string& aId) const
131 CatalogDict::const_iterator it = d->catalogs.find(aId);
132 if (it == d->catalogs.end()) {
139 Package* Root::getPackageById(const std::string& aName) const
141 size_t lastDot = aName.rfind('.');
144 if (lastDot == std::string::npos) {
146 CatalogDict::const_iterator it = d->catalogs.begin();
147 for (; it != d->catalogs.end(); ++it) {
148 pkg = it->second->getPackageById(aName);
157 std::string catalogId = aName.substr(0, lastDot);
158 std::string id = aName.substr(lastDot + 1);
159 Catalog* catalog = getCatalogById(catalogId);
164 return catalog->getPackageById(id);
167 CatalogList Root::catalogs() const
170 CatalogDict::const_iterator it = d->catalogs.begin();
171 for (; it != d->catalogs.end(); ++it) {
172 r.push_back(it->second);
179 Root::packagesMatching(const SGPropertyNode* aFilter) const
183 CatalogDict::const_iterator it = d->catalogs.begin();
184 for (; it != d->catalogs.end(); ++it) {
185 PackageList r2(it->second->packagesMatching(aFilter));
186 r.insert(r.end(), r2.begin(), r2.end());
193 Root::packagesNeedingUpdate() const
197 CatalogDict::const_iterator it = d->catalogs.begin();
198 for (; it != d->catalogs.end(); ++it) {
199 PackageList r2(it->second->packagesNeedingUpdate());
200 r.insert(r.end(), r2.begin(), r2.end());
206 void Root::refresh(bool aForce)
208 CatalogDict::iterator it = d->catalogs.begin();
209 for (; it != d->catalogs.end(); ++it) {
210 if (aForce || (it->second->ageInSeconds() > d->maxAgeSeconds)) {
211 it->second->refresh();
216 void Root::setDelegate(simgear::pkg::Delegate *aDelegate)
218 d->delegate = aDelegate;
221 void Root::setLocale(const std::string& aLocale)
226 std::string Root::getLocale() const
231 void Root::scheduleToUpdate(Install* aInstall)
234 sg_exception("missing argument to scheduleToUpdate");
237 PackageList deps = aInstall->package()->dependencies();
238 BOOST_FOREACH(Package* dep, deps) {
239 // will internally schedule for update if required
240 // hence be careful, this method is re-entered in here!
244 bool wasEmpty = d->updateDeque.empty();
245 d->updateDeque.push_back(aInstall);
248 aInstall->startUpdate();
252 void Root::startInstall(Install* aInstall)
255 d->delegate->startInstall(aInstall);
259 void Root::installProgress(Install* aInstall, unsigned int aBytes, unsigned int aTotal)
262 d->delegate->installProgress(aInstall, aBytes, aTotal);
266 void Root::startNext(Install* aCurrent)
268 if (d->updateDeque.front() != aCurrent) {
269 SG_LOG(SG_GENERAL, SG_ALERT, "current install of package not head of the deque");
271 d->updateDeque.pop_front();
274 if (!d->updateDeque.empty()) {
275 d->updateDeque.front()->startUpdate();
279 void Root::finishInstall(Install* aInstall)
282 d->delegate->finishInstall(aInstall);
288 void Root::failedInstall(Install* aInstall, Delegate::FailureCode aReason)
290 SG_LOG(SG_GENERAL, SG_ALERT, "failed to install package:"
291 << aInstall->package()->id() << ":" << aReason);
293 d->delegate->failedInstall(aInstall, aReason);
299 void Root::catalogRefreshBegin(Catalog* aCat)
301 d->refreshing.insert(aCat);
304 void Root::catalogRefreshComplete(Catalog* aCat, Delegate::FailureCode aReason)
306 CatalogDict::iterator catIt = d->catalogs.find(aCat->id());
307 if (aReason != Delegate::FAIL_SUCCESS) {
309 d->delegate->failedRefresh(aCat, aReason);
312 // if the failure is permanent, delete the catalog from our
313 // list (don't touch it on disk)
314 bool isPermanentFailure = (aReason == Delegate::FAIL_VERSION);
315 if (isPermanentFailure) {
316 SG_LOG(SG_GENERAL, SG_WARN, "permanent failure for catalog:" << aCat->id());
317 d->catalogs.erase(catIt);
319 } else if (catIt == d->catalogs.end()) {
320 // first fresh, add to our storage now
321 d->catalogs.insert(catIt, CatalogDict::value_type(aCat->id(), aCat));
324 d->refreshing.erase(aCat);
325 if (d->refreshing.empty()) {
327 d->delegate->refreshComplete();
332 } // of namespace pkg
334 } // of namespace simgear