00001 /* 00002 * Copyright 2007 Baylor University 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef _PROPHET_ALARM_H_ 00018 #define _PROPHET_ALARM_H_ 00019 00020 #include <sys/time.h> 00021 #include <sys/types.h> 00022 #include <vector> 00023 #include <string> 00024 00025 namespace prophet 00026 { 00027 00031 class ExpirationHandler 00032 { 00033 public: 00037 ExpirationHandler(const std::string& name = "") 00038 : name_(name) {} 00039 00043 virtual ~ExpirationHandler() {} 00044 00048 virtual void handle_timeout() = 0; 00049 00053 const char* name() { return name_.c_str(); } 00054 00058 void set_name(const char* name) { name_.assign(name); } 00059 00060 protected: 00061 std::string name_; 00062 00063 }; // class ExpirationHandler 00064 00068 class Alarm 00069 { 00070 public: 00074 Alarm(ExpirationHandler* handler) 00075 : handler_(handler) {} 00076 00080 virtual ~Alarm() {} 00081 00085 virtual void schedule(u_int milliseconds) = 0; 00086 00090 virtual u_int time_remaining() const = 0; 00091 00097 virtual void cancel() = 0; 00098 00103 virtual void timeout() { handler_->handle_timeout(); } 00104 00106 virtual bool pending() const = 0; 00107 virtual bool cancelled() const = 0; 00109 00110 protected: 00111 ExpirationHandler* const handler_; 00112 00113 }; // class Alarm 00114 00115 }; // namespace prophet 00116 00117 #endif // _PROPHET_ALARM_H_