Cogs.Core
HTTPRasterSource.cpp
1#include "HTTPRasterSource.h"
2
3#include "../Bridge/TerrainProviderFunctions.h"
4
5#include "Desktop/HTTP/BasicAuth.h"
6#include "Desktop/HTTP/HTTPDataFetcher.h"
7
8#include "Context.h"
9#include "Services/Services.h"
10
11#include "Foundation/Logging/Logger.h"
12
13namespace
14{
15 using namespace Cogs::Core;
16 Cogs::Logging::Log logger = Cogs::Logging::getLogger("HTTPRasterSource");
17}
18
19Cogs::Core::TerrainProvider::HTTPRasterSource::HTTPRasterSource(Context* context)
20 : BaseRasterSource(context)
21 , httpDataFetcher(context)
22{
23}
24
25bool Cogs::Core::TerrainProvider::HTTPRasterSource::init(const HTTPConfig& conf, std::unique_ptr<ICache>&& icache)
26{
27 baseUrl = conf.baseUrl;
28 if (baseUrl == NoString) {
29 LOG_ERROR(logger, "No Base URL set");
30 return false;
31 }
32 username = conf.username;
33 password = conf.password;
34
35 if (password != NoString && password != NoString) {
36 Cogs::Desktop::BasicAuth::Configuration authConfig = {
37 Strings::get(baseUrl).to_string(),
38 Strings::get(username).to_string(),
39 Strings::get(password).to_string()
40 };
41 httpDataFetcher.addAuthProvider(std::make_shared<Cogs::Desktop::BasicAuth>(std::move(authConfig)));
42 }
43
44 return BaseRasterSource::init(conf, std::move(icache));
45}
46
47void Cogs::Core::TerrainProvider::HTTPRasterSource::getConfig(HTTPConfig& conf) const
48{
49 BaseRasterSource::getConfig(conf);
50 conf.baseUrl = baseUrl;
51 conf.username = username;
52 conf.password = password;
53}
54
55void Cogs::Core::TerrainProvider::HTTPRasterSource::requestTile(Request* req)
56{
57 std::string url;
58 if (createFetchTileUrl(url, req->id)) {
59
60 auto fetchCb = [this, req, url](Cogs::FileContents::Ptr&& contents) {
61 if (!contents) {
62 std::string debugLog = "URL: " + url;
63 debugLog.append("\n\nSuccess: false" \
64 "\n\nNo Content");
65 addTileFailure(req, debugLog);
66 return;
67 }
68
69 MimeType kind = parseMimeType(contents->mimeTypeHash);
70 Memory::MemoryBuffer contentsBuff = contents->take();
71
72
73 if (kind == MimeType::XML || kind == MimeType::None) {
74 std::string debugLog = "URL: " + url;
75 debugLog.append("\n\nSuccess: false");
76
77 if (kind == MimeType::XML) {
78 debugLog.append("\n\nContent Type: ");
79 debugLog.append(Strings::getC(mimeTypeString(kind)));
80 debugLog.append("\n\nContent:\n");
81 debugLog.append(static_cast<const char*>(contentsBuff.data()), contentsBuff.size());
82 }
83
84 addTileFailure(req, debugLog);
85 return;
86 }
87
88 std::string debugLog;
89 if (emitDebugLog) {
90 debugLog.append("URL: ");
91 debugLog.append(url);
92 debugLog.append("\n\nSuccess: true");
93 debugLog.append("\n\nContent Type: ");
94 debugLog.append(Strings::getC(mimeTypeString(kind)));
95 debugLog.append("\n\nContent:\n");
96 debugLog.append(static_cast<const char*>(contentsBuff.data()), contentsBuff.size());
97 }
98
99 addTile(contentsBuff, kind, req, debugLog);
100 };
101
102 httpDataFetcher.fetchAsync(url, fetchCb, 0, 0, 0, Cogs::FileContentsHints::None);
103 }
104}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Log implementation class.
Definition: LogManager.h:139
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180