1 // HTTPClient.cxx -- Singleton HTTP client object
3 // Written by James Turner, started April 2012.
5 // Copyright (C) 2012 James Turner
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "HTTPClient.hxx"
25 #include <Main/fg_props.hxx>
26 #include <Include/version.h>
28 #include <simgear/sg_inlines.h>
30 #include <simgear/package/Root.hxx>
31 #include <simgear/package/Catalog.hxx>
32 #include <simgear/package/Delegate.hxx>
33 #include <simgear/package/Install.hxx>
34 #include <simgear/package/Package.hxx>
36 #include <simgear/nasal/cppbind/from_nasal.hxx>
37 #include <simgear/nasal/cppbind/to_nasal.hxx>
38 #include <simgear/nasal/cppbind/NasalHash.hxx>
39 #include <simgear/nasal/cppbind/Ghost.hxx>
41 #include <Scripting/NasalSys.hxx>
43 using namespace simgear;
45 typedef nasal::Ghost<pkg::RootRef> NasalPackageRoot;
46 typedef nasal::Ghost<pkg::PackageRef> NasalPackage;
47 typedef nasal::Ghost<pkg::CatalogRef> NasalCatalog;
48 typedef nasal::Ghost<pkg::InstallRef> NasalInstall;
52 class FGDelegate : public pkg::Delegate
55 virtual void refreshComplete()
57 SG_LOG(SG_IO, SG_INFO, "all Catalogs refreshed");
59 // auto-update; make this controlled by a property
60 pkg::Root* r = globals->packageRoot();
62 pkg::PackageList toBeUpdated(r->packagesNeedingUpdate());
63 pkg::PackageList::const_iterator it;
64 for (it = toBeUpdated.begin(); it != toBeUpdated.end(); ++it) {
65 assert((*it)->isInstalled());
66 SG_LOG(SG_IO, SG_INFO, "updating:" << (*it)->id());
67 r->scheduleToUpdate((*it)->install());
71 virtual void failedRefresh(pkg::Catalog* aCat, FailureCode aReason)
74 case pkg::Delegate::FAIL_SUCCESS:
75 SG_LOG(SG_IO, SG_WARN, "refresh of Catalog done");
79 SG_LOG(SG_IO, SG_WARN, "refresh of Catalog " << aCat->url() << " failed:" << aReason);
83 virtual void startInstall(pkg::Install* aInstall)
85 SG_LOG(SG_IO, SG_INFO, "beginning install of:" << aInstall->package()->id()
86 << " to local path:" << aInstall->path());
90 virtual void installProgress(pkg::Install* aInstall, unsigned int aBytes, unsigned int aTotal)
92 SG_LOG(SG_IO, SG_INFO, "installing:" << aInstall->package()->id() << ":"
93 << aBytes << " of " << aTotal);
96 virtual void finishInstall(pkg::Install* aInstall)
98 SG_LOG(SG_IO, SG_INFO, "finished install of:" << aInstall->package()->id()
99 << " to local path:" << aInstall->path());
103 virtual void failedInstall(pkg::Install* aInstall, FailureCode aReason)
105 SG_LOG(SG_IO, SG_WARN, "install failed of:" << aInstall->package()->id()
106 << " to local path:" << aInstall->path());
111 } // of anonymous namespace
113 FGHTTPClient::FGHTTPClient()
117 FGHTTPClient::~FGHTTPClient()
121 void FGHTTPClient::init()
123 _http.reset(new simgear::HTTP::Client);
125 std::string proxyHost(fgGetString("/sim/presets/proxy/host"));
126 int proxyPort(fgGetInt("/sim/presets/proxy/port"));
127 std::string proxyAuth(fgGetString("/sim/presets/proxy/auth"));
129 if (!proxyHost.empty()) {
130 _http->setProxy(proxyHost, proxyPort, proxyAuth);
133 pkg::Root* packageRoot = globals->packageRoot();
135 // package system needs access to the HTTP engine too
136 packageRoot->setHTTPClient(_http.get());
138 packageRoot->setDelegate(new FGDelegate);
140 const char * defaultCatalogId = fgGetString("/sim/package-system/default-catalog/id", "org.flightgear.default" );
141 const char * defaultCatalogUrl = fgGetString("/sim/package-system/default-catalog/url",
142 "http://fgfs.goneabitbursar.com/pkg/" FLIGHTGEAR_VERSION "/default-catalog.xml");
143 // setup default catalog if not present
144 pkg::Catalog* defaultCatalog = packageRoot->getCatalogById( defaultCatalogId );
145 if (!defaultCatalog) {
146 // always show this message
147 SG_LOG(SG_GENERAL, SG_ALERT, "default catalog not found, installing '"
148 << defaultCatalogId << "' from '" << defaultCatalogUrl << "'.");
149 pkg::Catalog::createFromUrl(packageRoot,defaultCatalogUrl);
152 // start a refresh now
153 packageRoot->refresh();
157 static naRef f_package_existingInstall( pkg::Package& pkg,
158 const nasal::CallContext& ctx )
161 pkg.existingInstall( ctx.getArg<pkg::Package::InstallCallback>(0) )
165 static naRef f_package_uninstall(pkg::Package& pkg, const nasal::CallContext& ctx)
167 pkg::InstallRef ins = pkg.existingInstall();
175 static SGPropertyNode_ptr queryPropsFromHash(const nasal::Hash& h)
177 SGPropertyNode_ptr props(new SGPropertyNode);
179 for (nasal::Hash::const_iterator it = h.begin(); it != h.end(); ++it) {
180 std::string const key = it->getKey();
181 if ((key == "name") || (key == "description")) {
182 props->setStringValue(key, it->getValue<std::string>());
183 } else if (strutils::starts_with(key, "rating-")) {
184 props->setIntValue(key, it->getValue<int>());
185 } else if (key == "tags") {
186 string_list tags = it->getValue<string_list>();
187 string_list::const_iterator tagIt;
189 for (tagIt = tags.begin(); tagIt != tags.end(); ++tagIt) {
190 SGPropertyNode_ptr tag = props->getChild("tag", tagCount++, true);
191 tag->setStringValue(*tagIt);
193 } else if (key == "installed") {
194 props->setBoolValue(key, it->getValue<bool>());
196 SG_LOG(SG_GENERAL, SG_WARN, "unknown filter term in hash:" << key);
203 static naRef f_root_search(pkg::Root& root, const nasal::CallContext& ctx)
205 SGPropertyNode_ptr query = queryPropsFromHash(ctx.requireArg<nasal::Hash>(0));
206 pkg::PackageList result = root.packagesMatching(query);
207 return ctx.to_nasal(result);
210 static naRef f_catalog_search(pkg::Catalog& cat, const nasal::CallContext& ctx)
212 SGPropertyNode_ptr query = queryPropsFromHash(ctx.requireArg<nasal::Hash>(0));
213 pkg::PackageList result = cat.packagesMatching(query);
214 return ctx.to_nasal(result);
217 static naRef f_package_variants(pkg::Package& pack, naContext c)
220 string_list vars(pack.variants());
221 for (string_list_iterator it = vars.begin(); it != vars.end(); ++it) {
222 h.set(*it, pack.nameForVariant(*it));
225 return h.get_naRef();
228 void FGHTTPClient::postinit()
230 NasalPackageRoot::init("PackageRoot")
231 .member("path", &pkg::Root::path)
232 .member("version", &pkg::Root::catalogVersion)
233 .method("refresh", &pkg::Root::refresh)
234 .method("catalogs", &pkg::Root::catalogs)
235 .method("packageById", &pkg::Root::getPackageById)
236 .method("catalogById", &pkg::Root::getCatalogById)
237 .method("search", &f_root_search);
239 NasalCatalog::init("Catalog")
240 .member("installRoot", &pkg::Catalog::installRoot)
241 .member("id", &pkg::Catalog::id)
242 .member("url", &pkg::Catalog::url)
243 .member("description", &pkg::Catalog::description)
244 .method("packages", &pkg::Catalog::packages)
245 .method("packageById", &pkg::Catalog::getPackageById)
246 .method("refresh", &pkg::Catalog::refresh)
247 .method("needingUpdate", &pkg::Catalog::packagesNeedingUpdate)
248 .member("installed", &pkg::Catalog::installedPackages)
249 .method("search", &f_catalog_search);
251 NasalPackage::init("Package")
252 .member("id", &pkg::Package::id)
253 .member("name", &pkg::Package::name)
254 .member("description", &pkg::Package::description)
255 .member("installed", &pkg::Package::isInstalled)
256 .member("thumbnails", &pkg::Package::thumbnailUrls)
257 .member("variants", &f_package_variants)
258 .member("revision", &pkg::Package::revision)
259 .member("catalog", &pkg::Package::catalog)
260 .method("install", &pkg::Package::install)
261 .method("uninstall", &f_package_uninstall)
262 .method("existingInstall", &f_package_existingInstall)
263 .method("lprop", &pkg::Package::getLocalisedProp)
264 .member("fileSize", &pkg::Package::fileSizeBytes);
266 typedef pkg::Install* (pkg::Install::*InstallCallback)
267 (const pkg::Install::Callback&);
268 typedef pkg::Install* (pkg::Install::*ProgressCallback)
269 (const pkg::Install::ProgressCallback&);
270 NasalInstall::init("Install")
271 .member("revision", &pkg::Install::revsion)
272 .member("pkg", &pkg::Install::package)
273 .member("path", &pkg::Install::path)
274 .member("hasUpdate", &pkg::Install::hasUpdate)
275 .method("startUpdate", &pkg::Install::startUpdate)
276 .method("uninstall", &pkg::Install::uninstall)
277 .method("done", static_cast<InstallCallback>(&pkg::Install::done))
278 .method("fail", static_cast<InstallCallback>(&pkg::Install::fail))
279 .method("always", static_cast<InstallCallback>(&pkg::Install::always))
280 .method("progress", static_cast<ProgressCallback>(&pkg::Install::progress));
282 pkg::Root* packageRoot = globals->packageRoot();
284 FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
285 nasal::Hash nasalGlobals = nasalSys->getGlobals();
286 nasal::Hash nasalPkg = nasalGlobals.createHash("pkg"); // module
287 nasalPkg.set("root", packageRoot);
291 void FGHTTPClient::shutdown()
296 void FGHTTPClient::update(double)
301 void FGHTTPClient::makeRequest(const simgear::HTTP::Request_ptr& req)
303 _http->makeRequest(req);