Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit ca0dc71

Browse files
committed
Refactor BrainJob to be more usable
1 parent da74d36 commit ca0dc71

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

src/include/brain/brain.h

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <string>
1616
#include <cstdio>
1717
#include <utility>
18+
#include <functional>
1819
#include "capnp/ez-rpc.h"
1920
#include "peloton/capnp/peloton_service.capnp.h"
2021
#include "common/notifiable_task.h"
@@ -28,39 +29,48 @@ class BrainEnvironment {
2829
};
2930

3031
class BrainJob {
32+
friend class Brain;
3133
public:
34+
explicit BrainJob(BrainEnvironment *env) : env_(env) {}
35+
virtual ~BrainJob() = default;
36+
inline void Invoke() { OnJobInvocation(env_); }
3237
// TODO(tianyu): Extend this interface for richer interaction
33-
virtual void RunJob(BrainEnvironment *) = 0;
38+
virtual void OnJobInvocation(BrainEnvironment *) = 0;
39+
private:
40+
BrainEnvironment *env_;
3441
};
3542

36-
class ExampleBrainJob: public BrainJob {
43+
class SimpleBrainJob : public BrainJob {
3744
public:
38-
void RunJob(BrainEnvironment *) override {
39-
// TODO(tianyu): Replace with real address
40-
capnp::EzRpcClient client("localhost:15445");
41-
PelotonService::Client peloton_service = client.getMain<PelotonService>();
42-
auto request = peloton_service.createIndexRequest();
43-
request.getRequest().setIndexKeys(42);
44-
auto response = request.send().wait(client.getWaitScope());
45-
}
45+
explicit SimpleBrainJob(BrainEnvironment *env,
46+
std::function<void(BrainEnvironment *)> task)
47+
: BrainJob(env), task_(std::move(task)) {}
48+
inline void OnJobInvocation(BrainEnvironment *env) override { task_(env); }
49+
private:
50+
std::function<void(BrainEnvironment *)> task_;
4651
};
4752

4853
class Brain {
4954
public:
5055
// TODO(tianyu): Add necessary parameters to initialize the brain's resources
5156
Brain() : scheduler_(0) {}
57+
~Brain() {
58+
for (auto entry : jobs_)
59+
delete entry.second;
60+
for (auto entry : job_handles_)
61+
event_free(entry.second);
62+
}
5263

64+
template <typename BrainJob, typename... Args>
5365
inline void RegisterJob(const struct timeval *period,
54-
std::string name,
55-
BrainJob &job) {
56-
auto callback = [](int, short, void *pair) {
57-
auto *job_env_pair = reinterpret_cast<std::pair<BrainJob *, BrainEnvironment *> *>(pair);
58-
job_env_pair->first->RunJob(job_env_pair->second);
66+
std::string name, Args... args) {
67+
auto *job = new BrainJob(&env_, args...);
68+
jobs_[name] = job;
69+
auto callback = [](int, short, void *arg) {
70+
reinterpret_cast<BrainJob *>(arg)->Invoke();
5971
};
60-
// TODO(tianyu) Deal with this memory
61-
auto *pair = new std::pair<BrainJob *, BrainEnvironment *>(&job, &env_);
6272
job_handles_[name] =
63-
scheduler_.RegisterPeriodicEvent(period, callback, pair);
73+
scheduler_.RegisterPeriodicEvent(period, callback, job);
6474
}
6575

6676
inline void Run() {
@@ -73,6 +83,7 @@ class Brain {
7383

7484
private:
7585
NotifiableTask scheduler_;
86+
std::unordered_map<std::string, BrainJob *> jobs_;
7687
std::unordered_map<std::string, struct event *> job_handles_;
7788
BrainEnvironment env_;
7889
};

src/include/common/event_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "common/logger.h"
2222

2323
namespace peloton {
24+
2425
/**
2526
* Static utility class with wrappers for libevent functions.
2627
*

src/include/common/notifiable_task.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class NotifiableTask {
6262
*/
6363
inline int Id() const { return task_id_; }
6464

65+
6566
/**
6667
* @brief Register an event with the event base associated with this
6768
* notifiable task.

src/main/peloton/peloton.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,17 @@ void RunPelotonBrain() {
4949
// TODO(tianyu): register jobs here
5050
struct timeval one_second;
5151
one_second.tv_sec = 1;
52-
peloton::brain::ExampleBrainJob job;
53-
brain.RegisterJob(&one_second, "test", job);
52+
53+
constexpr auto example_task = [](peloton::brain::BrainEnvironment *) {
54+
// TODO(tianyu): Replace with real address
55+
capnp::EzRpcClient client("localhost:15445");
56+
PelotonService::Client peloton_service = client.getMain<PelotonService>();
57+
auto request = peloton_service.createIndexRequest();
58+
request.getRequest().setIndexKeys(42);
59+
auto response = request.send().wait(client.getWaitScope());
60+
};
61+
62+
brain.RegisterJob<peloton::brain::SimpleBrainJob>(&one_second, "test", example_task);
5463
brain.Run();
5564
}
5665

0 commit comments

Comments
 (0)