23
23
namespace peloton {
24
24
namespace brain {
25
25
26
+ /* *
27
+ * Provides an access point to the various resources available to the jobs in
28
+ * the brain, such as RPC and Catalog.
29
+ */
26
30
class BrainEnvironment {
27
- // TODO(tianyu): provide interface for accessing various resources of the brain,
28
- // such as network connection to a peloton engine.
31
+ // TODO(tianyu): fill in as needed
29
32
};
30
33
34
+ /* *
35
+ * Interface that represents a piece of task to be run on Brain. To use this
36
+ * abstract class, extend it with a concrete class and fill in the method
37
+ * OnJobInvocation.
38
+ */
31
39
class BrainJob {
32
- friend class Brain ;
33
40
public:
34
41
explicit BrainJob (BrainEnvironment *env) : env_(env) {}
42
+
35
43
virtual ~BrainJob () = default ;
44
+
45
+ // This is separate from the user-defined OnJobInvocation to allow for better
46
+ // interfacing with the libevent API.
47
+ /* *
48
+ * Invokes this job to be run. The brain framework will call this method.
49
+ *
50
+ */
36
51
inline void Invoke () { OnJobInvocation (env_); }
37
- // TODO(tianyu): Extend this interface for richer interaction
52
+ // TODO(tianyu): Extend this interface for richer behavior
53
+ /* *
54
+ * Executed as the main body of the job, filled in by the user. Use the
55
+ * provided BrainEnvironment for interaction with Brain's resources.
56
+ */
38
57
virtual void OnJobInvocation (BrainEnvironment *) = 0;
39
58
private:
40
59
BrainEnvironment *env_;
41
60
};
42
61
62
+ /* *
63
+ * Simple implementation of a BrainJob.
64
+ */
43
65
class SimpleBrainJob : public BrainJob {
44
66
public:
45
67
explicit SimpleBrainJob (BrainEnvironment *env,
@@ -50,15 +72,19 @@ class SimpleBrainJob : public BrainJob {
50
72
std::function<void (BrainEnvironment *)> task_;
51
73
};
52
74
75
+ /* *
76
+ * Main running component of the brain. Events can be registered on this event
77
+ * loop and once Run is called, it will invoke handlers every specified time
78
+ * interval
79
+ */
53
80
class Brain {
54
81
public:
55
82
// TODO(tianyu): Add necessary parameters to initialize the brain's resources
56
83
Brain () : scheduler_(0 ) {}
84
+
57
85
~Brain () {
58
86
for (auto entry : jobs_)
59
87
delete entry.second ;
60
- for (auto entry : job_handles_)
61
- event_free (entry.second );
62
88
}
63
89
64
90
template <typename BrainJob, typename ... Args>
0 commit comments