|
1 /* |
|
2 * Copyright 2007 Baylor University |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifndef _PROPHET_ALARM_H_ |
|
18 #define _PROPHET_ALARM_H_ |
|
19 |
|
20 #include <sys/time.h> |
|
21 #include <sys/types.h> |
|
22 #include <vector> |
|
23 #include <string> |
|
24 |
|
25 namespace prophet |
|
26 { |
|
27 |
|
28 /** |
|
29 * Alarm expiration handler |
|
30 */ |
|
31 class ExpirationHandler |
|
32 { |
|
33 public: |
|
34 /** |
|
35 * Constructor |
|
36 */ |
|
37 ExpirationHandler(const std::string& name = "") |
|
38 : name_(name) {} |
|
39 |
|
40 /** |
|
41 * Destructor |
|
42 */ |
|
43 virtual ~ExpirationHandler() {} |
|
44 |
|
45 /** |
|
46 * Method to invoke when associated alarm expires |
|
47 */ |
|
48 virtual void handle_timeout() = 0; |
|
49 |
|
50 /** |
|
51 * Accessor |
|
52 */ |
|
53 const char* name() { return name_.c_str(); } |
|
54 |
|
55 /** |
|
56 * Mutator |
|
57 */ |
|
58 void set_name(const char* name) { name_.assign(name); } |
|
59 |
|
60 protected: |
|
61 std::string name_; |
|
62 |
|
63 }; // class ExpirationHandler |
|
64 |
|
65 /** |
|
66 * Alarm registration |
|
67 */ |
|
68 class Alarm |
|
69 { |
|
70 public: |
|
71 /** |
|
72 * Constructor. |
|
73 */ |
|
74 Alarm(ExpirationHandler* handler) |
|
75 : handler_(handler) {} |
|
76 |
|
77 /** |
|
78 * Destructor. |
|
79 */ |
|
80 virtual ~Alarm() {} |
|
81 |
|
82 /** |
|
83 * How many milliseconds in the future to schedule this alarm |
|
84 */ |
|
85 virtual void schedule(u_int milliseconds) = 0; |
|
86 |
|
87 /** |
|
88 * Milliseconds remaining until alarm expires |
|
89 */ |
|
90 virtual u_int time_remaining() const = 0; |
|
91 |
|
92 /** |
|
93 * Disable the alarm; do not execute the handler. It is expected |
|
94 * that the host's timer implementation will cleanup a cancelled |
|
95 * Alarm's memory ... |
|
96 */ |
|
97 virtual void cancel() = 0; |
|
98 |
|
99 /** |
|
100 * Invoke timeout handler. It is expected that a successfully |
|
101 * executed ExpirationHandler will clean up Alarm's memory |
|
102 */ |
|
103 virtual void timeout() { handler_->handle_timeout(); } |
|
104 |
|
105 ///@{ Accessors |
|
106 virtual bool pending() const = 0; |
|
107 virtual bool cancelled() const = 0; |
|
108 ///@} |
|
109 |
|
110 protected: |
|
111 ExpirationHandler* const handler_; ///< action to perform when |
|
112 /// alarm expires |
|
113 }; // class Alarm |
|
114 |
|
115 }; // namespace prophet |
|
116 |
|
117 #endif // _PROPHET_ALARM_H_ |