00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __LocalDB_hpp_
00022 #define __LocalDB_hpp_
00023 #include <cstdio>
00024 #include <map>
00025 #include <list>
00026 #include <boost/thread/shared_mutex.hpp>
00027
00028 #define LIMIT_SERVER_USER_NOT_FOUND 2
00029 #define LIMIT_SERVER_ACCESS_GRANT 3
00030 #define LIMIT_SERVER_ACCESS_DENY 4
00031
00032 #include "types.hpp"
00033 #include "hashtable.hpp"
00034
00035 class DataDB;
00036
00037 typedef std::list<std::pair<t_uid, t_traffic> > listL;
00038 typedef HashMap<t_uid, DataDB> mapL;
00039 typedef std::pair<t_uid, DataDB> pairL;
00040 typedef std::pair<t_uid, t_traffic> pairLL;
00044 class DataDB
00045 {
00046 public :
00047
00048 t_traffic traffic;
00049 t_traffic specyfic_traffic;
00050 bool always_allow;
00051
00052 long long last_up;
00053 listL::iterator ptr;
00054 DataDB();
00055 DataDB(t_traffic _traffic);
00056 DataDB(const DataDB& ref) {
00057 traffic = ref.traffic;
00058 specyfic_traffic = ref.specyfic_traffic;
00059 this->last_up = ref.last_up;
00060 ptr = ref.ptr;
00061 }
00062 DataDB& operator=(const DataDB& ref) {
00063 if(&ref == this) return *this;
00064
00065 traffic = ref.traffic;
00066 specyfic_traffic = ref.specyfic_traffic;
00067 last_up = ref.last_up;
00068 ptr = ref.ptr;
00069 return *this;
00070 }
00071 t_traffic add(t_traffic_int size);
00072 };
00073
00077 class LocalDB
00078 {
00079 long long pos;
00080
00081 public:
00082 mapL *DB;
00083 listL update;
00084 long long last_up;
00085
00086 LocalDB();
00087 ~LocalDB();
00088
00089
00090
00091
00092 void add_new_user(const t_uid& uid, t_traffic traffic = 0, t_traffic limit = 0, bool always_allow = false);
00093 void add_new_user_no_update(const t_uid& uid, t_traffic traffic, t_traffic limit, bool always_allow, bool ban);
00094
00095 void add_traffic_to_user(const t_uid& uid, t_traffic_int diff);
00096 void add_full_traffic_to_user(const t_uid& uid, const t_traffic& diff);
00105 bool get_traffic_by_uid(const t_uid& uid, t_traffic& ret);
00106 t_traffic get_data_by_uid(const t_uid& uid);
00107 int get_access_by_uid(const t_uid& uid, t_traffic limit);
00108 int update_base();
00109 int lock_base();
00110 int unlock_base();
00111 void set_specyfic_data(const t_uid& uid, t_traffic limit, bool always_allow);
00112 void set_data(const t_uid& uid, t_traffic traffic);
00113 int next(std::pair<t_uid, DataDB> &next);
00114 size_t size() { return DB->size(); }
00115
00116 void drop_outdated(int ttl);
00117 };
00118 #endif