]> git.mxchange.org Git - simgear.git/blob - simgear/package/Catalog.cxx
io: refactor and improve HTTP modules.
[simgear.git] / simgear / package / Catalog.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/Catalog.hxx>
19
20 #include <boost/foreach.hpp>
21 #include <algorithm>
22 #include <fstream>
23 #include <cstring>
24
25 #include <simgear/debug/logstream.hxx>
26 #include <simgear/props/props_io.hxx>
27 #include <simgear/io/HTTPRequest.hxx>
28 #include <simgear/io/HTTPClient.hxx>
29 #include <simgear/misc/sg_dir.hxx>
30 #include <simgear/structure/exception.hxx>
31 #include <simgear/package/Package.hxx>
32 #include <simgear/package/Root.hxx>
33 #include <simgear/package/Install.hxx>
34
35 namespace simgear {
36     
37 namespace pkg {
38
39 CatalogList static_catalogs;
40
41 //////////////////////////////////////////////////////////////////////////////
42
43 class Catalog::Downloader : public HTTP::Request
44 {
45 public:
46     Downloader(Catalog* aOwner, const std::string& aUrl) :
47         HTTP::Request(aUrl),
48         m_owner(aOwner)
49     {        
50     }
51     
52 protected:
53     virtual void gotBodyData(const char* s, int n)
54     {
55         m_buffer += std::string(s, n);
56     }
57     
58     virtual void onDone()
59     {        
60         if (responseCode() != 200) {
61             SG_LOG(SG_GENERAL, SG_ALERT, "catalog download failure:" << m_owner->url());
62             m_owner->refreshComplete(Delegate::FAIL_DOWNLOAD);
63             return;
64         }
65         
66         SGPropertyNode* props = new SGPropertyNode;
67         
68         try {
69             readProperties(m_buffer.data(), m_buffer.size(), props);
70             m_owner->parseProps(props);
71         } catch (sg_exception& e) {
72             SG_LOG(SG_GENERAL, SG_ALERT, "catalog parse failure:" << m_owner->url());
73             m_owner->refreshComplete(Delegate::FAIL_EXTRACT);
74             return;
75         }
76         
77         std::string ver(m_owner->root()->catalogVersion());
78         if (!checkVersion(ver, props)) {
79             SG_LOG(SG_GENERAL, SG_WARN, "downloaded catalog " << m_owner->url() << ", version mismatch:\n\t"
80                    << props->getStringValue("version") << " vs required " << ver);
81             m_owner->refreshComplete(Delegate::FAIL_VERSION);
82             return;
83         }
84         
85         // cache the catalog data, now we have a valid install root
86         Dir d(m_owner->installRoot());
87         SGPath p = d.file("catalog.xml");
88
89         std::ofstream f(p.c_str(), std::ios::out | std::ios::trunc);
90         f.write(m_buffer.data(), m_buffer.size());
91         f.close();
92         
93         time(&m_owner->m_retrievedTime);
94         m_owner->writeTimestamp();
95         m_owner->refreshComplete(Delegate::FAIL_SUCCESS);
96     }
97     
98 private:
99     bool checkVersion(const std::string& aVersion, SGPropertyNode* aProps)
100     {
101         BOOST_FOREACH(SGPropertyNode* v, aProps->getChildren("version")) {
102             if (v->getStringValue() == aVersion) {
103                 return true;
104             }
105         }
106         return false;
107     }
108     
109     Catalog* m_owner;  
110     std::string m_buffer;
111 };
112
113 //////////////////////////////////////////////////////////////////////////////
114
115 CatalogList Catalog::allCatalogs()
116 {
117     return static_catalogs;
118 }
119
120 Catalog::Catalog(Root *aRoot) :
121     m_root(aRoot),
122     m_retrievedTime(0)
123 {
124     static_catalogs.push_back(this);
125 }
126
127 Catalog::~Catalog()
128 {
129     CatalogList::iterator it = std::find(static_catalogs.begin(), static_catalogs.end(), this);
130     static_catalogs.erase(it);
131 }
132
133 Catalog* Catalog::createFromUrl(Root* aRoot, const std::string& aUrl)
134 {
135     Catalog* c = new Catalog(aRoot);
136     Downloader* dl = new Downloader(c, aUrl);
137     aRoot->makeHTTPRequest(dl);
138     
139     return c;
140 }
141     
142 Catalog* Catalog::createFromPath(Root* aRoot, const SGPath& aPath)
143 {
144     SGPath xml = aPath;
145     xml.append("catalog.xml");
146     if (!xml.exists()) {
147         return NULL;
148     }
149     
150     SGPropertyNode_ptr props;
151     try {
152         props = new SGPropertyNode;
153         readProperties(xml.str(), props);
154     } catch (sg_exception& e) {
155         return NULL;    
156     }
157     
158     if (props->getStringValue("version") != aRoot->catalogVersion()) {
159         SG_LOG(SG_GENERAL, SG_WARN, "skipping catalog at " << aPath << ", version mismatch:\n\t"
160                << props->getStringValue("version") << " vs required " << aRoot->catalogVersion());
161         return NULL;
162     }
163     
164     Catalog* c = new Catalog(aRoot);
165     c->m_installRoot = aPath;
166     c->parseProps(props);
167     c->parseTimestamp();
168     
169     return c;
170 }
171
172 PackageList
173 Catalog::packagesMatching(const SGPropertyNode* aFilter) const
174 {
175     PackageList r;
176     BOOST_FOREACH(Package* p, m_packages) {
177         if (p->matches(aFilter)) {
178             r.push_back(p);
179         }
180     }
181     return r;
182 }
183
184 PackageList
185 Catalog::packagesNeedingUpdate() const
186 {
187     PackageList r;
188     BOOST_FOREACH(Package* p, m_packages) {
189         if (!p->isInstalled()) {
190             continue;
191         }
192         
193         if (p->install()->hasUpdate()) {
194             r.push_back(p);
195         }
196     }
197     return r;
198 }
199
200 void Catalog::refresh()
201 {
202     Downloader* dl = new Downloader(this, url());
203     m_root->makeHTTPRequest(dl);
204     m_root->catalogRefreshBegin(this);
205 }
206
207 void Catalog::parseProps(const SGPropertyNode* aProps)
208 {
209     // copy everything except package children?
210     m_props = new SGPropertyNode;
211     
212     int nChildren = aProps->nChildren();
213     for (int i = 0; i < nChildren; i++) {
214         const SGPropertyNode* pkgProps = aProps->getChild(i);
215         if (strcmp(pkgProps->getName(), "package") == 0) {
216             Package* p = new Package(pkgProps, this);
217             m_packages.push_back(p);   
218         } else {
219             SGPropertyNode* c = m_props->getChild(pkgProps->getName(), pkgProps->getIndex(), true);
220             copyProperties(pkgProps, c);
221         }
222     } // of children iteration
223     
224     if (m_installRoot.isNull()) {
225         m_installRoot = m_root->path();
226         m_installRoot.append(id());
227         
228         Dir d(m_installRoot);
229         d.create(0755);
230     }
231 }
232
233 Package* Catalog::getPackageById(const std::string& aId) const
234 {
235     BOOST_FOREACH(Package* p, m_packages) {
236         if (p->id() == aId) {
237             return p;
238         }
239     }
240     
241     return NULL; // not found
242 }
243
244 std::string Catalog::id() const
245 {
246     return m_props->getStringValue("id");
247 }
248
249 std::string Catalog::url() const
250 {
251     return m_props->getStringValue("url");
252 }
253
254 std::string Catalog::description() const
255 {
256     return getLocalisedString(m_props, "description");
257 }
258     
259 SGPropertyNode* Catalog::properties() const
260 {
261     return m_props.ptr();
262 }
263
264 void Catalog::parseTimestamp()
265 {
266     SGPath timestampFile = m_installRoot;
267     timestampFile.append(".timestamp");
268     std::ifstream f(timestampFile.c_str(), std::ios::in);
269     f >> m_retrievedTime;
270 }
271
272 void Catalog::writeTimestamp()
273 {
274     SGPath timestampFile = m_installRoot;
275     timestampFile.append(".timestamp");
276     std::ofstream f(timestampFile.c_str(), std::ios::out | std::ios::trunc);
277     f << m_retrievedTime << std::endl;
278 }
279
280 unsigned int Catalog::ageInSeconds() const
281 {
282     time_t now;
283     time(&now);
284     int diff = ::difftime(now, m_retrievedTime);
285     return (diff < 0) ? 0 : diff;
286 }
287
288 bool Catalog::needsRefresh() const
289 {
290     unsigned int maxAge = m_props->getIntValue("max-age-sec", m_root->maxAgeSeconds());
291     return (ageInSeconds() > maxAge);
292 }
293     
294 std::string Catalog::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const
295 {
296     if (aRoot->hasChild(m_root->getLocale())) {
297         const SGPropertyNode* localeRoot = aRoot->getChild(m_root->getLocale().c_str());
298         if (localeRoot->hasChild(aName)) {
299             return localeRoot->getStringValue(aName);
300         }
301     }
302     
303     return aRoot->getStringValue(aName);
304 }
305
306 void Catalog::refreshComplete(Delegate::FailureCode aReason)
307 {
308     m_root->catalogRefreshComplete(this, aReason);
309 }
310
311
312 } // of namespace pkg
313
314 } // of namespace simgear