]> git.mxchange.org Git - simgear.git/blob - simgear/package/Root.cxx
Package support hacking
[simgear.git] / simgear / package / Root.cxx
1 // Copyright (C) 2013  James Turner - zakalawe@mac.com
2 //
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.
7 //
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.
12 //
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.
16 //
17
18 #include <simgear/package/Root.hxx>
19
20 #include <boost/foreach.hpp>
21 #include <cstring>
22 #include <map>
23 #include <deque>
24 #include <set>
25
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>
35
36 namespace simgear {
37     
38 namespace pkg {
39
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;
44
45 class Root::ThumbnailDownloader : public HTTP::Request
46 {
47 public:
48     ThumbnailDownloader(Root::RootPrivate* aOwner, const std::string& aUrl) :
49     HTTP::Request(aUrl),
50     m_owner(aOwner)
51     {
52     }
53     
54 protected:
55     virtual void gotBodyData(const char* s, int n)
56     {
57         m_buffer += std::string(s, n);
58     }
59     
60     virtual void onDone();
61     
62 private:
63     Root::RootPrivate* m_owner;
64     std::string m_buffer;
65 };
66     
67 class Root::RootPrivate
68 {
69 public:
70     RootPrivate() :
71         http(NULL),
72         maxAgeSeconds(60 * 60 * 24)
73     {
74     }
75     
76     void fireStartInstall(InstallRef install)
77     {
78         DelegateVec::const_iterator it;
79         for (it = delegates.begin(); it != delegates.end(); ++it) {
80             (*it)->startInstall(install);
81         }
82     }
83     
84     void fireInstallProgress(InstallRef install,
85                              unsigned int aBytes, unsigned int aTotal)
86     {
87         DelegateVec::const_iterator it;
88         for (it = delegates.begin(); it != delegates.end(); ++it) {
89             (*it)->installProgress(install, aBytes, aTotal);
90         }
91     }
92     
93     void fireFinishInstall(InstallRef install, Delegate::StatusCode status)
94     {
95         DelegateVec::const_iterator it;
96         for (it = delegates.begin(); it != delegates.end(); ++it) {
97             (*it)->finishInstall(install, status);
98         }
99     }
100     
101     void fireRefreshStatus(CatalogRef catalog, Delegate::StatusCode status)
102     {
103         DelegateVec::const_iterator it;
104         for (it = delegates.begin(); it != delegates.end(); ++it) {
105             (*it)->catalogRefreshed(catalog, status);
106         }
107     }
108     
109     
110     void thumbnailDownloadComplete(HTTP::Request_ptr request,
111                                    Delegate::StatusCode status, const std::string& bytes)
112     {
113         std::string u(request->url());
114         SG_LOG(SG_IO, SG_INFO, "downloaded thumbnail:" << u);
115         if (status == Delegate::STATUS_SUCCESS) {
116             thumbnailCache[u] = bytes;
117             fireDataForThumbnail(u, bytes);
118         }
119         
120         downloadNextPendingThumbnail();
121     }
122     
123     void fireDataForThumbnail(const std::string& aUrl, const std::string& bytes)
124     {
125         DelegateVec::const_iterator it;
126         const uint8_t* data = reinterpret_cast<const uint8_t*>(bytes.data());
127         for (it = delegates.begin(); it != delegates.end(); ++it) {
128             (*it)->dataForThumbnail(aUrl, bytes.size(), data);
129         }
130     }
131     
132     void downloadNextPendingThumbnail()
133     {
134         thumbnailDownloadRequest.clear();
135         if (pendingThumbnails.empty()) {
136             return;
137         }
138         
139         std::string u = pendingThumbnails.front();
140         pendingThumbnails.pop_front();
141         thumbnailDownloadRequest = new Root::ThumbnailDownloader(this, u);
142         
143         if (http) {
144             http->makeRequest(thumbnailDownloadRequest);
145         } else {
146             httpPendingRequests.push_back(thumbnailDownloadRequest);
147         }
148     }
149     
150     DelegateVec delegates;
151     
152     SGPath path;
153     std::string locale;
154     HTTP::Client* http;
155     CatalogDict catalogs;
156     unsigned int maxAgeSeconds;
157     std::string version;
158     
159     std::set<CatalogRef> refreshing;
160     typedef std::deque<InstallRef> UpdateDeque;
161     UpdateDeque updateDeque;
162     std::deque<HTTP::Request_ptr> httpPendingRequests;
163     
164     HTTP::Request_ptr thumbnailDownloadRequest;
165     StringDeque pendingThumbnails;
166     MemThumbnailCache thumbnailCache;
167     
168     typedef std::map<PackageRef, InstallRef> InstallCache;
169     InstallCache m_installs;
170 };
171     
172     
173 void Root::ThumbnailDownloader::onDone()
174 {
175     if (responseCode() != 200) {
176         SG_LOG(SG_GENERAL, SG_ALERT, "thumbnail download failure:" << url());
177         m_owner->thumbnailDownloadComplete(this, Delegate::FAIL_DOWNLOAD, std::string());
178         return;
179     }
180     
181     m_owner->thumbnailDownloadComplete(this, Delegate::STATUS_SUCCESS, m_buffer);
182     //time(&m_owner->m_retrievedTime);
183     //m_owner->writeTimestamp();
184     //m_owner->refreshComplete(Delegate::STATUS_REFRESHED);
185 }
186     
187 SGPath Root::path() const
188 {
189     return d->path;
190 }
191     
192 void Root::setMaxAgeSeconds(unsigned int seconds)
193 {
194     d->maxAgeSeconds = seconds;
195 }
196     
197 unsigned int Root::maxAgeSeconds() const
198 {
199     return d->maxAgeSeconds;
200 }
201
202 void Root::setHTTPClient(HTTP::Client* aHTTP)
203 {
204     d->http = aHTTP;
205     BOOST_FOREACH(HTTP::Request_ptr req, d->httpPendingRequests) {
206         d->http->makeRequest(req);
207     }
208
209     d->httpPendingRequests.clear();
210 }
211
212 void Root::makeHTTPRequest(HTTP::Request *req)
213 {
214     if (d->http) {
215         d->http->makeRequest(req);
216         return;
217     }
218     
219     d->httpPendingRequests.push_back(req);
220 }
221     
222 Root::Root(const SGPath& aPath, const std::string& aVersion) :
223     d(new RootPrivate)
224 {
225     d->path = aPath;
226     d->version = aVersion;
227     if (getenv("LOCALE")) {
228         d->locale = getenv("LOCALE");
229     }
230     
231     Dir dir(aPath);
232     if (!dir.exists()) {
233         dir.create(0755);
234         return;
235     }
236     
237     BOOST_FOREACH(SGPath c, dir.children(Dir::TYPE_DIR)) {
238         CatalogRef cat = Catalog::createFromPath(this, c);
239         if (cat) {
240            d->catalogs[cat->id()] = cat;
241         }
242     } // of child directories iteration
243 }
244
245 Root::~Root()
246 {
247     
248 }
249     
250 std::string Root::catalogVersion() const
251 {
252     return d->version;
253 }
254     
255 CatalogRef Root::getCatalogById(const std::string& aId) const
256 {
257     CatalogDict::const_iterator it = d->catalogs.find(aId);
258     if (it == d->catalogs.end()) {
259         return NULL;
260     }
261     
262     return it->second;
263 }
264
265 PackageRef Root::getPackageById(const std::string& aName) const
266 {
267     size_t lastDot = aName.rfind('.');
268     
269     PackageRef pkg = NULL;
270     if (lastDot == std::string::npos) {
271         // naked package ID
272         CatalogDict::const_iterator it = d->catalogs.begin();
273         for (; it != d->catalogs.end(); ++it) {
274             pkg = it->second->getPackageById(aName);
275             if (pkg) {
276                 return pkg;
277             }
278         }
279         
280         return NULL;
281     }
282     
283     std::string catalogId = aName.substr(0, lastDot);
284     std::string id = aName.substr(lastDot + 1);    
285     CatalogRef catalog = getCatalogById(catalogId);
286     if (!catalog) {
287         return NULL;
288     }
289             
290     return catalog->getPackageById(id);
291 }
292
293 CatalogList Root::catalogs() const
294 {
295     CatalogList r;
296     CatalogDict::const_iterator it = d->catalogs.begin();
297     for (; it != d->catalogs.end(); ++it) {
298         r.push_back(it->second);
299     }
300     
301     return r;
302 }
303
304 PackageList
305 Root::allPackages() const
306 {
307     PackageList r;
308     
309     CatalogDict::const_iterator it = d->catalogs.begin();
310     for (; it != d->catalogs.end(); ++it) {
311         const PackageList& r2(it->second->packages());
312         r.insert(r.end(), r2.begin(), r2.end());
313     }
314     
315     return r;
316 }
317     
318 PackageList
319 Root::packagesMatching(const SGPropertyNode* aFilter) const
320 {
321     PackageList r;
322     
323     CatalogDict::const_iterator it = d->catalogs.begin();
324     for (; it != d->catalogs.end(); ++it) {
325         PackageList r2(it->second->packagesMatching(aFilter));
326         r.insert(r.end(), r2.begin(), r2.end());
327     }
328     
329     return r;
330 }
331
332 PackageList
333 Root::packagesNeedingUpdate() const
334 {
335     PackageList r;
336     
337     CatalogDict::const_iterator it = d->catalogs.begin();
338     for (; it != d->catalogs.end(); ++it) {
339         PackageList r2(it->second->packagesNeedingUpdate());
340         r.insert(r.end(), r2.begin(), r2.end());
341     }
342     
343     return r;
344 }
345
346 void Root::refresh(bool aForce)
347 {
348     bool didStartAny = false;
349     CatalogDict::iterator it = d->catalogs.begin();
350     for (; it != d->catalogs.end(); ++it) {
351         if (aForce || it->second->needsRefresh()) {
352             it->second->refresh();
353             didStartAny = true;
354         }
355     }
356     
357     if (!didStartAny) {
358         // signal refresh complete to the delegate already
359         d->fireRefreshStatus(CatalogRef(), Delegate::STATUS_REFRESHED);
360     }
361 }
362
363 void Root::addDelegate(simgear::pkg::Delegate *aDelegate)
364 {
365     d->delegates.push_back(aDelegate);
366 }
367     
368 void Root::removeDelegate(simgear::pkg::Delegate *aDelegate)
369 {
370     DelegateVec::iterator it = std::find(d->delegates.begin(),
371                                          d->delegates.end(), aDelegate);
372     if (it == d->delegates.end()) {
373         throw sg_exception("unknown delegate in removeDelegate");
374     }
375     d->delegates.erase(it);
376 }
377     
378 void Root::setLocale(const std::string& aLocale)
379 {
380     d->locale = aLocale;
381 }
382
383 std::string Root::getLocale() const
384 {
385     return d->locale;
386 }
387
388 void Root::scheduleToUpdate(InstallRef aInstall)
389 {
390     if (!aInstall) {
391         throw sg_exception("missing argument to scheduleToUpdate");
392     }
393     
394     PackageList deps = aInstall->package()->dependencies();
395     BOOST_FOREACH(Package* dep, deps) {
396         // will internally schedule for update if required
397         // hence be careful, this method is re-entered in here!
398         dep->install();
399     }
400
401     bool wasEmpty = d->updateDeque.empty();
402     d->updateDeque.push_back(aInstall);
403     
404     if (wasEmpty) {
405         aInstall->startUpdate();
406     }
407 }
408
409 bool Root::isInstallQueued(InstallRef aInstall) const
410 {
411     RootPrivate::UpdateDeque::const_iterator it =
412         std::find(d->updateDeque.begin(), d->updateDeque.end(), aInstall);
413     return (it != d->updateDeque.end());
414 }
415     
416 void Root::startInstall(InstallRef aInstall)
417 {
418     d->fireStartInstall(aInstall);
419 }
420
421 void Root::installProgress(InstallRef aInstall, unsigned int aBytes, unsigned int aTotal)
422 {
423     d->fireInstallProgress(aInstall, aBytes, aTotal);
424 }
425
426 void Root::startNext(InstallRef aCurrent)
427 {
428     if (d->updateDeque.front() != aCurrent) {
429         SG_LOG(SG_GENERAL, SG_ALERT, "current install of package not head of the deque");
430     } else {
431         d->updateDeque.pop_front();
432     }
433     
434     if (!d->updateDeque.empty()) {
435         d->updateDeque.front()->startUpdate();
436     }
437 }
438
439 void Root::finishInstall(InstallRef aInstall, Delegate::StatusCode aReason)
440 {
441     if (aReason != Delegate::STATUS_SUCCESS) {
442         SG_LOG(SG_GENERAL, SG_ALERT, "failed to install package:"
443                << aInstall->package()->id() << ":" << aReason);
444     }
445     
446     d->fireFinishInstall(aInstall, aReason);
447     startNext(aInstall);
448 }
449     
450 void Root::cancelDownload(InstallRef aInstall)
451 {
452     RootPrivate::UpdateDeque::iterator it =
453         std::find(d->updateDeque.begin(), d->updateDeque.end(), aInstall);
454     if (it != d->updateDeque.end()) {
455         bool startNext = (aInstall == d->updateDeque.front());
456         d->updateDeque.erase(it);
457         if (startNext) {
458             if (!d->updateDeque.empty()) {
459                 d->updateDeque.front()->startUpdate();
460             }
461         } // of install was front item
462     } // of found install in queue
463 }
464
465 void Root::catalogRefreshStatus(CatalogRef aCat, Delegate::StatusCode aReason)
466 {
467     CatalogDict::iterator catIt = d->catalogs.find(aCat->id());
468     d->fireRefreshStatus(aCat, aReason);
469
470     if (aReason == Delegate::STATUS_IN_PROGRESS) {
471         d->refreshing.insert(aCat);
472
473         if (catIt == d->catalogs.end()) {
474             // first fresh, add to our storage now
475             d->catalogs.insert(catIt, CatalogDict::value_type(aCat->id(), aCat));
476         }
477     } else {
478         d->refreshing.erase(aCat);
479     }
480     
481     if ((aReason != Delegate::STATUS_REFRESHED) && (aReason != Delegate::STATUS_IN_PROGRESS)) {
482         // if the failure is permanent, delete the catalog from our
483         // list (don't touch it on disk)
484         bool isPermanentFailure = (aReason == Delegate::FAIL_VERSION);
485         if (isPermanentFailure) {
486             SG_LOG(SG_GENERAL, SG_WARN, "permanent failure for catalog:" << aCat->id());
487             if (catIt != d->catalogs.end()) {
488                 d->catalogs.erase(catIt);
489             }
490         }
491     }
492     
493     if (d->refreshing.empty()) {
494         d->fireRefreshStatus(CatalogRef(), Delegate::STATUS_REFRESHED);
495     }
496 }
497
498 bool Root::removeCatalogById(const std::string& aId)
499 {
500     CatalogDict::iterator catIt = d->catalogs.find(aId);
501     if (catIt == d->catalogs.end()) {
502         SG_LOG(SG_GENERAL, SG_WARN, "removeCatalogById: unknown ID:" << aId);
503         return false;
504     }
505     
506     CatalogRef cat = catIt->second;
507     
508     // drop the reference
509     d->catalogs.erase(catIt);
510     
511     bool ok = cat->uninstall();
512     if (!ok) {
513         SG_LOG(SG_GENERAL, SG_WARN, "removeCatalogById: catalog :" << aId
514             << "failed to uninstall");
515     }
516     
517     return ok;
518 }
519     
520 void Root::requestThumbnailData(const std::string& aUrl)
521 {
522     MemThumbnailCache::iterator it = d->thumbnailCache.find(aUrl);
523     if (it == d->thumbnailCache.end()) {
524         // insert into cache to mark as pending
525         d->pendingThumbnails.push_front(aUrl);
526         d->thumbnailCache[aUrl] = std::string();
527         d->downloadNextPendingThumbnail();
528     } else if (!it->second.empty()) {
529         // already loaded, fire data synchronously
530         d->fireDataForThumbnail(aUrl, it->second);
531     } else {
532         // in cache but empty data, still fetching
533     }
534 }
535     
536 InstallRef Root::existingInstallForPackage(PackageRef p) const
537 {
538     RootPrivate::InstallCache::const_iterator it =
539         d->m_installs.find(p);
540     if (it == d->m_installs.end()) {
541         // check if it exists on disk, create
542         SGPath path(p->pathOnDisk());
543         if (path.exists()) {
544             // this will add to our cache, and hence, modify m_installs
545             return Install::createFromPath(path, p->catalog());
546         }
547  
548         return InstallRef();
549     }
550     
551     return it->second;
552 }
553     
554 void Root::registerInstall(InstallRef ins)
555 {
556     if (!ins.valid()) {
557         return;
558     }
559     
560     d->m_installs[ins->package()] = ins;
561 }
562
563 void Root::unregisterInstall(InstallRef ins)
564 {
565     if (!ins .valid()) {
566         return;
567     }
568     
569     d->m_installs.erase(ins->package());
570 }
571     
572 } // of namespace pkg
573
574 } // of namespace simgear