00001 /* 00002 * Copyright 2006 Intel Corporation 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 _OASYS_TOKEN_BUCKET_H_ 00018 #define _OASYS_TOKEN_BUCKET_H_ 00019 00020 #include "Time.h" 00021 #include "../debug/Logger.h" 00022 00023 namespace oasys { 00024 00028 class TokenBucket : public Logger { 00029 public: 00033 TokenBucket(const char* logpath, 00034 u_int32_t depth, /* in tokens */ 00035 u_int32_t rate /* in seconds */); 00036 00046 bool drain(u_int32_t length); 00047 00053 void update(); 00054 00059 u_int32_t time_to_fill(); 00060 00062 u_int32_t depth() const { return depth_; } 00063 u_int32_t rate() const { return rate_; } 00064 u_int32_t tokens() const { return tokens_; } 00066 00068 void set_depth(u_int32_t depth) { depth_ = depth; update(); } 00069 void set_rate(u_int32_t rate) { rate_ = rate; update(); } 00071 00075 void empty(); 00076 00077 protected: 00078 u_int32_t depth_; 00079 u_int32_t rate_; 00080 u_int32_t tokens_; 00081 Time last_update_; 00082 }; 00083 00084 } // namespace oasys 00085 00086 #endif /* _OASYS_TOKEN_BUCKET_H_ */