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

Commit 44f487c

Browse files
committed
Hacky way to solve things
1 parent dbfa2e5 commit 44f487c

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

src/include/brain/brain.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <string>
1616
#include <cstdio>
1717
#include <utility>
18+
#include "capnp/ez-rpc.h"
19+
#include "peloton/capnp/peloton_service.capnp.h"
1820
#include "common/notifiable_task.h"
1921

2022
namespace peloton {
@@ -28,7 +30,19 @@ class BrainEnvironment {
2830
class BrainJob {
2931
public:
3032
// TODO(tianyu): Extend this interface for richer interaction
31-
virtual void RunJob(BrainEnvironment &env) { printf("foo"); };
33+
virtual void RunJob(BrainEnvironment *) = 0;
34+
};
35+
36+
class ExampleBrainJob: public BrainJob {
37+
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+
}
3246
};
3347

3448
class Brain {
@@ -39,11 +53,14 @@ class Brain {
3953
inline void RegisterJob(const struct timeval *period,
4054
std::string name,
4155
BrainJob &job) {
42-
auto callback = [&](int, short, void *env) {
43-
job.RunJob(*reinterpret_cast<BrainEnvironment *>(env));
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);
4459
};
60+
// TODO(tianyu) Deal with this memory
61+
auto *pair = new std::pair<BrainJob *, BrainEnvironment *>(&job, &env_);
4562
job_handles_[name] =
46-
scheduler_.RegisterPeriodicEvent(period, callback, &env);
63+
scheduler_.RegisterPeriodicEvent(period, callback, pair);
4764
}
4865

4966
inline void Run() {
@@ -57,8 +74,7 @@ class Brain {
5774
private:
5875
NotifiableTask scheduler_;
5976
std::unordered_map<std::string, struct event *> job_handles_;
60-
// TODO(tianyu): May need to have multiple env instead of shared
61-
BrainEnvironment env;
77+
BrainEnvironment env_;
6278
};
6379
}
6480
}

src/include/capnp/peloton_service.capnp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct CreateIndexRequest {
1212
}
1313

1414
struct CreateIndexResponse {
15-
15+
message @0 :Text;
1616
}
1717

1818
interface PelotonService {

src/main/peloton/peloton.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ void RunPelotonBrain() {
4747
peloton::brain::Brain brain;
4848
evthread_use_pthreads();
4949
// TODO(tianyu): register jobs here
50-
struct timeval *one_minute;
51-
one_minute->tv_sec = 60;
52-
brain.RegisterJob(one_minute, "test", peloton::brain::BrainJob());
50+
struct timeval one_second;
51+
one_second.tv_sec = 1;
52+
peloton::brain::ExampleBrainJob job;
53+
brain.RegisterJob(&one_second, "test", job);
5354
brain.Run();
5455
}
5556

0 commit comments

Comments
 (0)