15
15
#include < string>
16
16
#include < cstdio>
17
17
#include < utility>
18
+ #include " capnp/ez-rpc.h"
19
+ #include " peloton/capnp/peloton_service.capnp.h"
18
20
#include " common/notifiable_task.h"
19
21
20
22
namespace peloton {
@@ -28,7 +30,19 @@ class BrainEnvironment {
28
30
class BrainJob {
29
31
public:
30
32
// 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
+ }
32
46
};
33
47
34
48
class Brain {
@@ -39,11 +53,14 @@ class Brain {
39
53
inline void RegisterJob (const struct timeval *period,
40
54
std::string name,
41
55
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 );
44
59
};
60
+ // TODO(tianyu) Deal with this memory
61
+ auto *pair = new std::pair<BrainJob *, BrainEnvironment *>(&job, &env_);
45
62
job_handles_[name] =
46
- scheduler_.RegisterPeriodicEvent (period, callback, &env );
63
+ scheduler_.RegisterPeriodicEvent (period, callback, pair );
47
64
}
48
65
49
66
inline void Run () {
@@ -57,8 +74,7 @@ class Brain {
57
74
private:
58
75
NotifiableTask scheduler_;
59
76
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_;
62
78
};
63
79
}
64
80
}
0 commit comments