diff --git a/include/actsim_agent/downloader.hpp b/include/actsim_agent/downloader.hpp index 4ba4d63..24fcb61 100644 --- a/include/actsim_agent/downloader.hpp +++ b/include/actsim_agent/downloader.hpp @@ -44,7 +44,7 @@ class Downloader { void thread_run(); bool fetch_tasks(size_t n); - bool fetch_design(const db::uuid_t& id); + bool fetch_design(const db::uuid_t& id, std::string& design); void reopen_task(const db::uuid_t& id); std::unique_ptr downloader_thread; diff --git a/src/actsim_agent/downloader.cpp b/src/actsim_agent/downloader.cpp index 63b18cc..2c0a906 100644 --- a/src/actsim_agent/downloader.cpp +++ b/src/actsim_agent/downloader.cpp @@ -77,6 +77,7 @@ void Downloader::thread_run() { auto task = this->interface.pop_fresh(empty); if (empty) continue; this->reopen_task(task->id); + this->interface.decrement_design(task->design); } } @@ -93,13 +94,18 @@ bool Downloader::fetch_tasks(size_t n) { // see if we already have the desing locally; if not, load it if (!this->interface.increment_design(task->design)) { + + DEBUG_PRINT("Fetching new design with ID " + db::to_string(task->design)); + std::string design; // if we could not load the design, reopen it in the database - if (!this->fetch_design(task->design)) { + if (!this->fetch_design(task->design, design)) { std::cerr << "Error: Could not load design for task " << task->id << ", reopening it." << std::endl; this->reopen_task(task->id); continue; } + + this->interface.store_design(task->design, design); } // push the task to the list of open tasks @@ -110,7 +116,8 @@ bool Downloader::fetch_tasks(size_t n) { return true; } -bool Downloader::fetch_design(const db::uuid_t& id) { +bool Downloader::fetch_design(const db::uuid_t& id, std::string& design) { + design = "test design"; return true; } diff --git a/src/actsim_agent/task_interface.cpp b/src/actsim_agent/task_interface.cpp index 0165e71..9a13a5a 100644 --- a/src/actsim_agent/task_interface.cpp +++ b/src/actsim_agent/task_interface.cpp @@ -128,32 +128,44 @@ std::unique_ptr TaskInterface::pop_finished(bool& empty) { bool TaskInterface::increment_design(const db::uuid_t& id) { std::lock_guard lock (this->designs_mutex); + DEBUG_PRINT("Looking for design with ID " + db::to_string(id)); // make sure the requested design is in the list of available designs - if (this->designs.find(id) == this->designs.end()) return false; + if (this->designs.find(id) == this->designs.end()) { + DEBUG_PRINT("Design not found."); + return false; + } std::pair& design_entry = designs[id]; // if so, increment its reference counters ++design_entry.first; + DEBUG_PRINT("Design found. Incrementing reference counter. New counter is " + std::to_string(design_entry.first)); return true; } void TaskInterface::decrement_design(const db::uuid_t& id) { std::lock_guard lock (this->designs_mutex); + DEBUG_PRINT("Looking to decrement design with ID " + db::to_string(id)); // make sure the requested design is in the list of available designs - if (this->designs.find(id) == this->designs.end()) return; + if (this->designs.find(id) == this->designs.end()) { + DEBUG_PRINT("Could not find design. Not decrementing."); + return; + } std::pair& design_entry = designs[id]; // if so, decrement its reference counters --design_entry.first; + DEBUG_PRINT("Design found. Decrementing reference counter. New counter is " + std::to_string(design_entry.first)); + // if the reference counter hit 0, erase the design entry from the list // of available designs if (design_entry.first == 0) { + DEBUG_PRINT("Erasing design from store."); this->designs.erase(id); } } @@ -173,9 +185,13 @@ std::string TaskInterface::get_design(const db::uuid_t& id) { void TaskInterface::store_design(const db::uuid_t& id, std::string& design) { std::lock_guard lock (this->designs_mutex); + DEBUG_PRINT("Henlo Storing new design with ID " + db::to_string(id)); + std::cerr << "This should really be visible here" << std::endl; + // make sure the design isn't already in the list of design entries // if it is, just increment its reference counter if (this->designs.find(id) != this->designs.end()) { + DEBUG_PRINT("Design is already in here, incrementing reference counter instead."); ++(this->designs[id]).first; return; }