]> git.mxchange.org Git - simgear.git/blob - simgear/package/Root.cxx
75eef3bb3c53c1dea737c1cf857daba1abef9f53
[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, Catalog*> CatalogDict;
41     
42 class Root::RootPrivate
43 {
44 public:
45     RootPrivate() :
46         http(NULL),
47         maxAgeSeconds(60 * 60 * 24),
48         delegate(NULL)
49     {
50         
51     }
52     
53     SGPath path;
54     std::string locale;
55     HTTP::Client* http;
56     CatalogDict catalogs;
57     unsigned int maxAgeSeconds;
58     Delegate* delegate;
59     std::string version;
60     
61     std::set<Catalog*> refreshing;
62     std::deque<Install*> updateDeque;
63     std::deque<HTTP::Request_ptr> httpPendingRequests;
64 };
65     
66 SGPath Root::path() const
67 {
68     return d->path;
69 }
70     
71 void Root::setMaxAgeSeconds(int seconds)
72 {
73     d->maxAgeSeconds = seconds;
74 }
75
76 void Root::setHTTPClient(HTTP::Client* aHTTP)
77 {
78     d->http = aHTTP;
79     BOOST_FOREACH(HTTP::Request_ptr req, d->httpPendingRequests) {
80         d->http->makeRequest(req);
81     }
82
83     d->httpPendingRequests.clear();
84 }
85
86 void Root::makeHTTPRequest(HTTP::Request *req)
87 {
88     if (d->http) {
89         d->http->makeRequest(req);
90         return;
91     }
92     
93     d->httpPendingRequests.push_back(req);
94 }
95     
96 Root::Root(const SGPath& aPath, const std::string& aVersion) :
97     d(new RootPrivate)
98 {
99     d->path = aPath;
100     d->version = aVersion;
101     if (getenv("LOCALE")) {
102         d->locale = getenv("LOCALE");
103     }
104     
105     Dir dir(aPath);
106     if (!dir.exists()) {
107         dir.create(0755);
108         return;
109     }
110     
111     BOOST_FOREACH(SGPath c, dir.children(Dir::TYPE_DIR)) {
112         Catalog* cat = Catalog::createFromPath(this, c);
113         if (cat) {
114            d->catalogs[cat->id()] = cat;
115         }
116     } // of child directories iteration
117 }
118
119 Root::~Root()
120 {
121     
122 }
123     
124 std::string Root::catalogVersion() const
125 {
126     return d->version;
127 }
128     
129 Catalog* Root::getCatalogById(const std::string& aId) const
130 {
131     CatalogDict::const_iterator it = d->catalogs.find(aId);
132     if (it == d->catalogs.end()) {
133         return NULL;
134     }
135     
136     return it->second;
137 }
138
139 Package* Root::getPackageById(const std::string& aName) const
140 {
141     size_t lastDot = aName.rfind('.');
142     
143     Package* pkg = NULL;
144     if (lastDot == std::string::npos) {
145         // naked package ID
146         CatalogDict::const_iterator it = d->catalogs.begin();
147         for (; it != d->catalogs.end(); ++it) {
148             pkg = it->second->getPackageById(aName);
149             if (pkg) {
150                 return pkg;
151             }
152         }
153         
154         return NULL;
155     }
156     
157     std::string catalogId = aName.substr(0, lastDot);
158     std::string id = aName.substr(lastDot + 1);    
159     Catalog* catalog = getCatalogById(catalogId);
160     if (!catalog) {
161         return NULL;
162     }
163             
164     return catalog->getPackageById(id);
165 }
166
167 CatalogList Root::catalogs() const
168 {
169     CatalogList r;
170     CatalogDict::const_iterator it = d->catalogs.begin();
171     for (; it != d->catalogs.end(); ++it) {
172         r.push_back(it->second);
173     }
174     
175     return r;
176 }
177
178 PackageList
179 Root::packagesMatching(const SGPropertyNode* aFilter) const
180 {
181     PackageList r;
182     
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());
187     }
188     
189     return r;
190 }
191
192 PackageList
193 Root::packagesNeedingUpdate() const
194 {
195     PackageList r;
196     
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());
201     }
202     
203     return r;
204 }
205
206 void Root::refresh(bool aForce)
207 {
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();
212         }
213     }
214 }
215
216 void Root::setDelegate(simgear::pkg::Delegate *aDelegate)
217 {
218     d->delegate = aDelegate;
219 }
220     
221 void Root::setLocale(const std::string& aLocale)
222 {
223     d->locale = aLocale;
224 }
225
226 std::string Root::getLocale() const
227 {
228     return d->locale;
229 }
230
231 void Root::scheduleToUpdate(Install* aInstall)
232 {
233     if (!aInstall) {
234         sg_exception("missing argument to scheduleToUpdate");
235     }
236     
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!
241         dep->install();
242     }
243
244     bool wasEmpty = d->updateDeque.empty();
245     d->updateDeque.push_back(aInstall);
246     
247     if (wasEmpty) {
248         aInstall->startUpdate();
249     }
250 }
251
252 void Root::startInstall(Install* aInstall)
253 {
254     if (d->delegate) {
255         d->delegate->startInstall(aInstall);
256     }
257 }
258
259 void Root::installProgress(Install* aInstall, unsigned int aBytes, unsigned int aTotal)
260 {
261     if (d->delegate) {
262         d->delegate->installProgress(aInstall, aBytes, aTotal);
263     }
264 }
265
266 void Root::startNext(Install* aCurrent)
267 {
268     if (d->updateDeque.front() != aCurrent) {
269         SG_LOG(SG_GENERAL, SG_ALERT, "current install of package not head of the deque");
270     } else {
271         d->updateDeque.pop_front();
272     }
273     
274     if (!d->updateDeque.empty()) {
275         d->updateDeque.front()->startUpdate();
276     }
277 }
278
279 void Root::finishInstall(Install* aInstall)
280 {
281     if (d->delegate) {
282         d->delegate->finishInstall(aInstall);
283     }
284     
285     startNext(aInstall);
286 }
287
288 void Root::failedInstall(Install* aInstall, Delegate::FailureCode aReason)
289 {
290     SG_LOG(SG_GENERAL, SG_ALERT, "failed to install package:" 
291         << aInstall->package()->id() << ":" << aReason);
292     if (d->delegate) {
293         d->delegate->failedInstall(aInstall, aReason);
294     }
295     
296     startNext(aInstall);
297 }
298
299 void Root::catalogRefreshBegin(Catalog* aCat)
300 {
301     d->refreshing.insert(aCat);
302 }
303
304 void Root::catalogRefreshComplete(Catalog* aCat, Delegate::FailureCode aReason)
305 {
306     CatalogDict::iterator catIt = d->catalogs.find(aCat->id());
307     if (aReason != Delegate::FAIL_SUCCESS) {
308         if (d->delegate) {
309             d->delegate->failedRefresh(aCat, aReason);
310         }
311         
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);
318         }
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));
322     }
323     
324     d->refreshing.erase(aCat);
325     if (d->refreshing.empty()) {
326         if (d->delegate) {
327             d->delegate->refreshComplete();
328         }
329     }
330 }
331
332 } // of namespace pkg
333
334 } // of namespace simgear