Skip to content

Commit 91d1d47

Browse files
committed
fetching data async in background should work now
1 parent 8a2ec59 commit 91d1d47

File tree

8 files changed

+128
-22
lines changed

8 files changed

+128
-22
lines changed

ReaperDAWHub.Controller/Source/ProjectsController.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ std::vector<Project> ProjectsController::getProjects() {
1515
if(serviceClient != nullptr) {
1616
try
1717
{
18-
std::cout << "controller requesting projs\n";
19-
std::this_thread::sleep_for(std::chrono::seconds(5));
18+
std::this_thread::sleep_for(std::chrono::seconds(10));
2019
std::cout << "controller requested projs\n";
2120
result = serviceClient->getAvailableProjects();
2221
}

ReaperDAWHub.GUI.Demo/Source/GUIDemo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ It contains the basic startup code for a Juce application.
88
==============================================================================
99
*/
1010

11-
11+
#define WIN32_LEAN_AND_MEAN
1212
#include "../../JuceLibraryCode/JuceHeader.h"
1313
#include "../includes/GUIDemo.h"
1414
#include "../../ReaperDAWHub.GUI/includes/LoginWindow.h"

ReaperDAWHub.GUI/ReaperDAWHub.GUI.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
<SDLCheck>true</SDLCheck>
116116
<AdditionalIncludeDirectories>..\JuceLibraryCode;..\JuceLibraryCode\modules;.\includes;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
117117
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
118+
<PreprocessorDefinitions>BOOST_ALL_DYN_LINK;BOOST_ALL_NO_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
118119
</ClCompile>
119120
<Link>
120121
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
1+
#define BOOST_THREAD_PROVIDES_FUTURE
12
#include "../includes/ProjectBrowserComponent.h"
23
#include "../includes/ProjectEntryComponent.h"
3-
#define BOOST_THREAD_PROVIDES_FUTURE
4-
#include <iostream>
5-
#include <boost/asio.hpp>
6-
#include <boost/date_time/posix_time/posix_time.hpp>
7-
#include <future>
84
#include <string>
9-
#include <iostream>
105
#include <algorithm>
11-
#include <iostream>
126
#include <map>
13-
#include <functional>
147

158
template<typename R>
169
bool isReady(std::future<R> const& f)
1710
{
11+
Logger::writeToLog("check future");
1812
return f.wait_for(std::chrono::seconds(-1)) == std::future_status::ready;
1913
}
2014

2115
ProjectBrowserComponent::ProjectBrowserComponent() : m_btn("OK") {
22-
2316
addAndMakeVisible(m_tabs);
2417
setSize(500, 600);
2518
initData();
@@ -34,12 +27,23 @@ void ProjectBrowserComponent::resized() {
3427
}
3528

3629
void ProjectBrowserComponent::initData() {
37-
std::cout << "requesting projs";
38-
std::future<std::vector<Project>> f = std::async(std::launch::async, &ProjectsController::getProjects, &pc);
39-
std::cout << "requested projs";
40-
std::vector<Project> projs;
41-
if (isReady(f)) {
42-
projs = f.get();
43-
std::cout << "got projs";
30+
Logger::writeToLog("requesting projects");
31+
projectsFuture = std::async(std::launch::async, &ProjectsController::getProjects, &pc);
32+
Logger::writeToLog("requested projects");
33+
timer = new boost::asio::deadline_timer(io_service, boost::posix_time::seconds(interval_secs));
34+
timer->async_wait(boost::bind(&ProjectBrowserComponent::fetchData, this, boost::asio::placeholders::error, timer));
35+
ioSvcFuture = std::async(std::launch::async, static_cast<size_t(boost::asio::io_service::*)()>(&boost::asio::io_service::run), &io_service);
36+
}
37+
38+
void ProjectBrowserComponent::fetchData(const boost::system::error_code& /*e*/,
39+
boost::asio::deadline_timer* tmr) {
40+
if (isReady(projectsFuture)) {
41+
projects = projectsFuture.get();
42+
Logger::writeToLog("got projs");
43+
}
44+
else {
45+
tmr->expires_at(tmr->expires_at() + boost::posix_time::seconds(interval_secs));
46+
// Posts the event
47+
tmr->async_wait(boost::bind(&ProjectBrowserComponent::fetchData, this, boost::asio::placeholders::error, tmr));
4448
}
4549
}

ReaperDAWHub.GUI/Source/ProjectBrowserTabComponent.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
ProjectBrowserTabComponent::ProjectBrowserTabComponent() : TabbedComponent(TabbedButtonBar::TabsAtTop) {
66
ProjectEntryListComponent *pelc = new ProjectEntryListComponent();
77
LocalProjectEntryComponent *lpec = new LocalProjectEntryComponent();
8+
<<<<<<< HEAD
89
pelc->addListEntry(*lpec);
10+
=======
11+
pelc->addListEntry(lpec);
12+
>>>>>>> parent of 0d78d6d... apply mvc pattern
913
addTab("Remote projects", Colours::lightgrey, new ProjectEntryListComponent(), true);
1014
addTab("Local projects", Colours::lightgrey, pelc, true);
1115
addTab("Shared projects", Colours::lightgrey, new ProjectEntryListComponent(), true);

ReaperDAWHub.GUI/Source/ProjectEntryListComponent.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
1+
<<<<<<< HEAD
12
#include "../includes/ProjectEntryListComponent.h"
23
#include "../includes/LocalProjectEntryComponent.h"
34

45
ProjectEntryListComponent::ProjectEntryListComponent() {
56
setSize(500, 600);
67
addListEntry((*(new ProjectEntryComponent())));
8+
=======
9+
#define BOOST_THREAD_PROVIDES_FUTURE
10+
#include "../includes/ProjectEntryListComponent.h"
11+
#include "../includes/LocalProjectEntryComponent.h"
12+
#include "ProjectBrowserComponent.h"
13+
#include <string>
14+
15+
template<typename R>
16+
bool isReady(std::future<R> const& f)
17+
{
18+
Logger::writeToLog("check future");
19+
return f.wait_for(std::chrono::seconds(-1)) == std::future_status::ready;
20+
}
21+
22+
ProjectEntryListComponent::ProjectEntryListComponent() {
23+
setSize(500, 600);
24+
addListEntry((new ProjectEntryComponent()));
25+
initData();
26+
>>>>>>> parent of 0d78d6d... apply mvc pattern
727
}
828

929
ProjectEntryListComponent::~ProjectEntryListComponent() {
@@ -26,4 +46,43 @@ void ProjectEntryListComponent::addListEntryComponent(Component &pec) {
2646

2747
void ProjectEntryListComponent::resized() {
2848

49+
<<<<<<< HEAD
50+
=======
51+
}
52+
53+
void ProjectEntryListComponent::requestProjects()
54+
{
55+
Logger::writeToLog("requesting projects");
56+
projectsFuture = std::async(std::launch::async, &ProjectsController::getProjects, &pc);
57+
Logger::writeToLog("requested projects");
58+
}
59+
60+
void ProjectEntryListComponent::backgroundCheckFuture()
61+
{
62+
timer = new boost::asio::deadline_timer(io_service, boost::posix_time::seconds(interval_secs));
63+
timer->async_wait(boost::bind(&ProjectEntryListComponent::fetchData, this, boost::asio::placeholders::error, timer));
64+
ioSvcFuture = std::async(std::launch::async, static_cast<size_t(boost::asio::io_service::*)()>(&boost::asio::io_service::run), &io_service);
65+
}
66+
67+
void ProjectEntryListComponent::initData() {
68+
requestProjects();
69+
backgroundCheckFuture();
70+
}
71+
72+
void ProjectEntryListComponent::fetchData(const boost::system::error_code& /*e*/,
73+
boost::asio::deadline_timer* tmr) {
74+
if (isReady(projectsFuture)) {
75+
projects = projectsFuture.get();
76+
for(auto project : projects)
77+
{
78+
ProjectEntryComponent *pec = new ProjectEntryComponent(std::to_string(project.getId()), "222");
79+
addListEntry(pec);
80+
}
81+
Logger::writeToLog("got projs");
82+
}
83+
else {
84+
tmr->expires_at(tmr->expires_at() + boost::posix_time::seconds(interval_secs));
85+
tmr->async_wait(boost::bind(&ProjectEntryListComponent::fetchData, this, boost::asio::placeholders::error, tmr));
86+
}
87+
>>>>>>> parent of 0d78d6d... apply mvc pattern
2988
}

ReaperDAWHub.GUI/includes/ProjectBrowserComponent.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
#include "ProjectBrowserTabComponent.h"
55
#include "../includes/ButtonListenerHandler.h"
66
#include "../../ReaperDAWHub.Controller/includes/ProjectsController.h"
7-
7+
#include <future>
8+
#include <iostream>
9+
#include <boost/asio.hpp>
10+
#include <boost/bind.hpp>
811

912
class ProjectBrowserComponent : public Component, public ButtonListenerHandler
1013
{
@@ -13,12 +16,19 @@ class ProjectBrowserComponent : public Component, public ButtonListenerHandler
1316
~ProjectBrowserComponent();
1417
void resized() override;
1518
void initData();
16-
1719
private:
20+
void fetchData(const boost::system::error_code& /*e*/,
21+
boost::asio::deadline_timer* t);
1822
GroupComponent g_group;
1923
TextButton m_btn;
2024
Label lbl_name;
2125
Label lbl_version;
2226
ProjectBrowserTabComponent m_tabs;
27+
std::vector<Project> projects;
2328
ProjectsController pc;
29+
std::future<std::vector<Project>> projectsFuture;
30+
std::future<size_t> ioSvcFuture;
31+
int interval_secs = 1;
32+
boost::asio::io_service io_service;
33+
boost::asio::deadline_timer* timer;
2434
};

ReaperDAWHub.GUI/includes/ProjectEntryListComponent.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,46 @@
22
#include "../../JuceLibraryCode/JuceHeader.h"
33
#include "../includes/ProjectEntryComponent.h"
44
#include "../includes/LocalProjectEntryComponent.h"
5+
#include <future>
6+
#include <iostream>
7+
#include <boost/asio.hpp>
8+
#include <boost/bind.hpp>
9+
#include "../../ReaperDAWHub.Model/includes/Project.h"
10+
#include "../../ReaperDAWHub.Controller/includes/ProjectsController.h"
511

612
class ProjectEntryListComponent : public Component
713
{
814
public:
915
ProjectEntryListComponent();
1016
~ProjectEntryListComponent();
1117
void resized() override;
18+
<<<<<<< HEAD
1219
void addListEntry(ProjectEntryComponent &pec);
1320
void addListEntry(LocalProjectEntryComponent &pec);
21+
=======
22+
void addListEntry(ProjectEntryComponent *pec);
23+
void addListEntry(LocalProjectEntryComponent *pec);
24+
>>>>>>> parent of 0d78d6d... apply mvc pattern
1425

1526
private:
27+
void initData();
28+
void requestProjects();
29+
void backgroundCheckFuture();
30+
void fetchData(const boost::system::error_code& /*e*/,
31+
boost::asio::deadline_timer* t);
1632
std::vector<Component *> listEntries = std::vector<Component *>();
33+
<<<<<<< HEAD
1734
void addListEntryComponent(Component &pec);
18-
};
35+
};
36+
=======
37+
void addListEntryComponent(Component *pec);
38+
39+
std::vector<Project> projects;
40+
ProjectsController pc;
41+
std::future<std::vector<Project>> projectsFuture;
42+
std::future<size_t> ioSvcFuture;
43+
int interval_secs = 1;
44+
boost::asio::io_service io_service;
45+
boost::asio::deadline_timer* timer;
46+
};
47+
>>>>>>> parent of 0d78d6d... apply mvc pattern

0 commit comments

Comments
 (0)