Temporal images are now fetched on separate threads, to not freeze the program while waiting for response

This commit is contained in:
Kollberg
2019-05-31 14:46:53 -04:00
parent 91df0d409a
commit d12d192920
3 changed files with 189 additions and 46 deletions
+8 -8
View File
@@ -5,22 +5,22 @@ local propertyHelper = asset.require('util/property_helper')
-- Specifying which other assets should be loaded in this scene
asset.require('spice/base')
assetHelper.requestAll(asset, 'scene/solarsystem/sun')
-- asset.require('scene/solarsystem/planets')
-- asset.require('scene/solarsystem/planets/mars/moons/phobos')
-- asset.require('scene/solarsystem/planets/mars/moons/deimos')
-- asset.require('scene/solarsystem/dwarf_planets/pluto/system')
-- assetHelper.requestAll(asset, 'scene/digitaluniverse')
asset.require('scene/solarsystem/planets')
asset.require('scene/solarsystem/planets/mars/moons/phobos')
asset.require('scene/solarsystem/planets/mars/moons/deimos')
asset.require('scene/solarsystem/dwarf_planets/pluto/system')
assetHelper.requestAll(asset, 'scene/digitaluniverse')
-- Load default key bindings applicable to most scenes
asset.require('util/default_keybindings')
asset.require('util/default_dashboard')
-- asset.require('util/default_joystick')
asset.require('util/default_joystick')
-- asset.require('util/webgui')
-- asset.request('customization/globebrowsing')
asset.request('customization/globebrowsing')
-- local earthAsset = asset.require('scene/solarsystem/planets/earth/earth')
local earthAsset = asset.require('scene/solarsystem/planets/earth/earth')
local sunAsset = asset.require('scene/solarsystem/sun/sun')
local sunRadius = 695508000
+158 -36
View File
@@ -48,44 +48,117 @@ SunTextureManager::SunTextureManager(){
}
void SunTextureManager::update(std::unique_ptr<ghoul::opengl::Texture>& texture){
std::string current = getOpenSpaceDateTime();
if(_counter == 200){ // first time
std::string next = checkNextTextureId(current, 1);
if(next != "Not found!"){
startDownloadTexture(next);
uploadTextureFromName(next);
}
}
// check if there's a texture for the current timestamp (minute)
else if((_activeTextureDate != current) && (_textureListGPU.find(current) != _textureListGPU.end()) ){
LERROR("switching to texture with id: " + current);
_textureListGPU[_activeTextureDate] = std::move(texture);
texture = std::move(_textureListGPU[current]);
_activeTextureDate = current;
float dir = global::timeManager.deltaTime();
std::string next = checkNextTextureId(current, dir);
////Check if the server is alive, otherwise we dont wany anything to happen!
//
//std::string current = getOpenSpaceDateTime();
//if (_counter == 200) { // first time
// std::string next = checkNextTextureId(current, 1);
// if (next != "Not found!") {
// startDownloadTexture(next);
// uploadTextureFromName(next);
// }
//}
//// check if there's a texture for the current timestamp (minute)
//else if ((_activeTextureDate != current) && (_textureListGPU.find(current) != _textureListGPU.end())) {
// LERROR("switching to texture with id: " + current);
// _textureListGPU[_activeTextureDate] = std::move(texture);
// texture = std::move(_textureListGPU[current]);
// _activeTextureDate = current;
if(next != "Not found!" ){
if (std::find(_textureListDisk.begin(), _textureListDisk.end(), next) == _textureListDisk.end()) {
startDownloadTexture(next);
uploadTextureFromName(next);
}
else {
if (_textureListGPU.find("20" + parseMagnetogramDate(next).substr(0,10)) == _textureListGPU.end()) {
LERROR("Texture didn't exist on gpu, uploading it: 20" + parseMagnetogramDate(next).substr(0,10));
uploadTextureFromName(next);
// float dir = global::timeManager.deltaTime();
// std::string next = checkNextTextureId(current, dir);
// if (next != "Not found!") {
// if (std::find(_textureListDisk.begin(), _textureListDisk.end(), next) == _textureListDisk.end()) {
// startDownloadTexture(next);
// uploadTextureFromName(next);
// }
// else {
// if (_textureListGPU.find("20" + parseMagnetogramDate(next).substr(0, 10)) == _textureListGPU.end()) {
// LERROR("Texture didn't exist on gpu, uploading it: 20" + parseMagnetogramDate(next).substr(0, 10));
// uploadTextureFromName(next);
// }
// }
// }
//}
//_counter++;
switch (_stage)
{
//This stage just checks what the next image applied should be,
case 0: {
_current = getOpenSpaceDateTime();
if (_counter % 1000 == 0 && !_working && !_dldthread.joinable()) {
_dldthread = std::thread([=] { getNextTexture(_current, 1.0f, &_next); });
}
if (!_working && _dldthread.joinable()) {
_dldthread.join();
if (_next != "Not found!") {
if (std::find(_textureListDisk.begin(), _textureListDisk.end(), _next) == _textureListDisk.end()) {
// We didn't find it on disk, proceed to download
_stage = 1;
}
else {
if (_textureListGPU.find("20" + parseMagnetogramDate(_next).substr(0, 10)) == _textureListGPU.end()) {
// Found on disk but not on GPU, proceed to upload to GPU
_stage = 2;
}
else {
// Found on disk, and on the GPU, all we wanted, all week was to swap the texture
_stage = 3;
}
}
}
}
break;
}
case 1: {
//LERROR("in case 1");
if (!_working && !_dldthread.joinable()) {
_dldthread = std::thread([=] { downloadTexture(_next); });
}
if (!_working && _dldthread.joinable()) {
_dldthread.join();
checkFilesInDirectory();
_stage = 2;
}
break;
}
case 2: {
//LERROR("in case 2");
uploadTextureFromName(_next);
_stage = 3;
break;
}
case 3: {
//LERROR("in case 3");
std::string current = "20" + parseMagnetogramDate(_next).substr(0, 10);
if ((_activeTextureDate != current) && ((_textureListGPU.find(current) != _textureListGPU.end()))) {
_textureListGPU[_activeTextureDate] = std::move(texture);
texture = std::move(_textureListGPU[current]);
_activeTextureDate = current;
}
_stage = 0;
break;
}
default:
break;
}
if (_working) {
//LERROR("Thread is working");
}
_counter++;
}
// not using this right now
void SunTextureManager::initialDownloadBatch(){
// check what files we have in the synd directory
// check what files we have in the sync directory
checkFilesInDirectory();
std::string current = getOpenSpaceDateTime();
@@ -117,14 +190,28 @@ SunTextureManager::SunTextureManager(){
HttpRequest::RequestOptions opt = {};
opt.requestTimeoutSeconds = 0;
ashd.start(opt);
LERROR("nedladdning i startDownloadTexture");
ashd.wait();
LERROR("Texture " + textureId + " downloaded to disk" );
}
// The same as startDownloadTexture, but uses lock_guard and flags _working
void SunTextureManager::downloadTexture(std::string textureId) {
_working = true;
std::lock_guard<std::mutex> guard(_GPUListBlock);
std::string url = "http://localhost:3000/get/" + textureId;
//std::string destinationpath = absPath("../../../../../sync/magnetograms/" + textureId); //mac
std::string destinationpath = absPath("../../../sync/magnetograms/" + textureId);
AsyncHttpFileDownload ashd = AsyncHttpFileDownload(url, destinationpath, HttpFileDownload::Overwrite::Yes);
HttpRequest::RequestOptions opt = {};
opt.requestTimeoutSeconds = 0;
ashd.start(opt);
ashd.wait();
LERROR("Texture " + textureId + " downloaded to disk");
_working = false;
}
std::string SunTextureManager::checkNextTextureId(std::string current, float dir){
SyncHttpMemoryDownload mmryDld = SyncHttpMemoryDownload("http://localhost:3000/getmenextfitsimage/" + current + "/" + std::to_string(dir));
HttpRequest::RequestOptions opt = {};
opt.requestTimeoutSeconds = 0;
@@ -136,6 +223,27 @@ SunTextureManager::SunTextureManager(){
});
return s;
}
void SunTextureManager::getNextTexture(std::string current, float dir, std::string *toReturn){
_working = true;
std::lock_guard<std::mutex> guard(_GPUListBlock);
std::string url = "http://localhost:3000/getmenextfitsimage/" + current + "/" + std::to_string(dir);
//std::string destinationpath = absPath("../../../../../sync/magnetograms/" + textureId); //mac
//std::string destinationpath = absPath("../../../sync/magnetograms/mrzqs190511t1514c2217_226.fits.gz");
AsyncHttpMemoryDownload ashd = AsyncHttpMemoryDownload(url);
HttpRequest::RequestOptions opt = {};
opt.requestTimeoutSeconds = 0;
ashd.start(opt);
ashd.wait();
std::string s;
std::transform(ashd.downloadedData().begin(), ashd.downloadedData().end(), std::back_inserter(s),
[](char c) {
return c;
});
*toReturn = std::move(s);
//LERROR("Texture mrzqs190511t1514c2217_226.fits.gz downloaded to disk");
_working = false;
}
void SunTextureManager::startUploadTextures(){
@@ -162,6 +270,7 @@ SunTextureManager::SunTextureManager(){
void SunTextureManager::uploadTextureFromName(std::string filename){
FitsFileReader fitsFileReader(false);
//const auto tempBild = fitsFileReader.readImageFloat("../../../../../sync/magnetograms/" + filename); // mac
@@ -169,20 +278,21 @@ SunTextureManager::SunTextureManager(){
std::string dateID = parseMagnetogramDate(*fitsFileReader.readHeaderValueString("DATE"));
const float stdvalue = *fitsFileReader.readHeaderValueFloat("IMGRMS01");
const float minvalue = *fitsFileReader.readHeaderValueFloat("IMGMIN01");
const float maxvalue = *fitsFileReader.readHeaderValueFloat("IMGMAX01");
//const float stdvalue = *fitsFileReader.readHeaderValueFloat("IMGRMS01");
std::vector<float> fitsImage;
for(float c : tempBild->contents){
fitsImage.push_back((c+stdvalue)/stdvalue);
fitsImage.push_back((c - minvalue) / (maxvalue - minvalue)); // Normalized
//fitsImage.push_back((c + stdvalue) / stdvalue); // Standard Deviation
}
//LERROR("laddar upp texture till GPU med id: " + dateID);
LERROR("laddar upp texture till GPU med id: " + dateID);
auto textureFits = std::make_unique<ghoul::opengl::Texture>(fitsImage.data(), glm::vec3(360, 180, 1),ghoul::opengl::Texture::Format::Red, GL_R32F,GL_FLOAT);
textureFits->uploadTexture();
textureFits->setName(dateID);
_textureQueueGPU.push(dateID);
_textureListGPU[dateID] = std::move(textureFits);
trimGPUList();
}
@@ -190,7 +300,6 @@ SunTextureManager::SunTextureManager(){
void SunTextureManager::uploadTexturesFromList(std::vector<std::string>& filelist){
LERROR("antal filer: " + std::to_string(filelist.size()));
for (const auto & entry : filelist) {
uploadTextureFromName(entry);
@@ -223,6 +332,19 @@ SunTextureManager::SunTextureManager(){
}
return dateID;
}
bool SunTextureManager::checkServerAliveness() {
SyncHttpMemoryDownload mmryDld = SyncHttpMemoryDownload("http://localhost:3000/");
HttpRequest::RequestOptions opt = {};
opt.requestTimeoutSeconds = 0;
mmryDld.download(opt);
std::string s;
std::transform(mmryDld.downloadedData().begin(), mmryDld.downloadedData().end(), std::back_inserter(s),
[](char c) {
return c;
});
return s == "You are at ROOT";
}
void SunTextureManager::trimGPUList(){
if(_textureQueueGPU.size() > _maxTexturesOnGPU){
+23 -2
View File
@@ -30,6 +30,7 @@
#include <ghoul/opengl/texture.h>
#include <thread>
#include <queue>
#include <atomic>
namespace ghoul::opengl {
class Texture;
@@ -45,25 +46,45 @@ public:
void initialDownload(std::unique_ptr<ghoul::opengl::Texture>& texture);
void checkFilesInDirectory();
void getNextTexture(std::string current, float dir, std::string * toReturn);
private:
void initialDownloadBatch();
void startUploadTextures();
void startDownloadTexture(std::string textureId);
void downloadTexture(std::string textureId);
void uploadTexturesFromList(std::vector<std::string>& filelist);
void uploadTextureFromName(std::string);
void trimGPUList();
std::string parseMagnetogramDate(std::string name);
bool checkServerAliveness();
std::string checkNextTextureId(std::string current, float dir);
std::string getOpenSpaceDateTime();
int _counter = 0;
int _counter2 = 0;
//std::thread _dldthread;
bool connected = true;
// The _working atomic, is used to notify if a thread is being used and is currently being worked on
std::atomic_bool _working = false;
/* The _stage atomic, is to describe in what stage the suntexture manager is in,
Kind of like a queue system if you will:
Stage = 0 - Nothing going on right now, at this stage it is allowed to check what to do next
Stage = 1 - Is or has downloaded a texture
Stage = 2 - Is or has uploaded a texture from file to GPU
Stage = 3 - Is ready to swap texture to a texture that has been uploaded to the gpu
*/
std::atomic_size_t _stage = 0;
mutable std::mutex _GPUListBlock;
std::string _next = "";
std::string _current;
std::thread _dldthread;
std::string _activeTextureDate = "NODATE";
const unsigned int _maxTexturesOnGPU = 5; //every texture is around 250kb in size