]> git.stg.codes - stg.git/blob - include/stg/user_stat.h
Fix multiple typo: "alredy" -> "already"
[stg.git] / include / stg / user_stat.h
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21  /*
22  $Revision: 1.15 $
23  $Date: 2010/03/11 14:42:05 $
24  $Author: faust $
25  */
26
27 #ifndef USER_STAT_H
28 #define USER_STAT_H
29
30 #include <ctime>
31 #include <map>
32
33 #include "os_int.h"
34 #include "resetable.h"
35 #include "user_traff.h"
36 //-----------------------------------------------------------------------------
37 struct IP_DIR_PAIR
38 {
39     #ifdef TRAFF_STAT_WITH_PORTS
40     IP_DIR_PAIR(uint32_t _ip,
41                 int _dir,
42                 uint16_t _port)
43         : ip(_ip),
44           dir(_dir),
45           port(_port)
46     {}
47     #else
48     IP_DIR_PAIR(uint32_t _ip,
49                 int _dir)
50         : ip(_ip),
51           dir(_dir)
52     {}
53     #endif
54     //------------------------
55     bool operator<(const IP_DIR_PAIR & idp) const
56         {
57         if (ip < idp.ip)
58             return true;
59
60         if (ip > idp.ip)
61             return false;
62
63         #ifdef TRAFF_STAT_WITH_PORTS
64         if (port < idp.port)
65             return true;
66
67         if (port > idp.port)
68             return false;
69         #endif
70
71         if (dir < idp.dir)
72             return true;
73
74         return false;
75         }
76     //------------------------
77     bool operator!=(const IP_DIR_PAIR & rvalue) const
78         {
79         if (ip != rvalue.ip)
80             return true;
81
82         #ifdef TRAFF_STAT_WITH_PORTS
83         if (port != rvalue.port)
84             return true;
85         #endif
86
87         if (dir != rvalue.dir)
88             return true;
89
90         return false;
91         }
92     //------------------------
93     uint32_t        ip;
94     int             dir;
95     #ifdef TRAFF_STAT_WITH_PORTS
96     uint16_t        port;
97     #endif
98 };
99 //-----------------------------------------------------------------------------
100 struct STAT_NODE
101 {
102     STAT_NODE(uint64_t _up,
103               uint64_t _down,
104               double   _cash)
105         : up(_up),
106           down(_down),
107           cash(_cash)
108     {}
109     uint64_t        up;
110     uint64_t        down;
111     double          cash;
112 };
113 //-----------------------------------------------------------------------------
114 struct USER_STAT
115 {
116     //USER_STAT & operator= (const USER_STAT_RES & usr);
117     USER_STAT()
118         : up(),
119           down(),
120           cash(0),
121           freeMb(0),
122           lastCashAdd(0),
123           lastCashAddTime(0),
124           passiveTime(0),
125           lastActivityTime(0)
126     {};
127
128     DIR_TRAFF   up;
129     DIR_TRAFF   down;
130     double      cash;
131     double      freeMb;
132     double      lastCashAdd;
133     time_t      lastCashAddTime;
134     time_t      passiveTime;
135     time_t      lastActivityTime;
136 };
137 //-----------------------------------------------------------------------------
138 typedef std::map<IP_DIR_PAIR, STAT_NODE> TRAFF_STAT;
139 //-----------------------------------------------------------------------------
140 struct USER_STAT_RES
141 {
142     USER_STAT_RES()
143         : cash(),
144           freeMb(),
145           lastCashAdd(),
146           lastCashAddTime(),
147           passiveTime(),
148           lastActivityTime(),
149           up(),
150           down()
151     {}
152
153     USER_STAT_RES & operator= (const USER_STAT & us)
154     {
155         cash             = us.cash;
156         freeMb           = us.freeMb;
157         lastCashAdd      = us.lastCashAdd;
158         lastCashAddTime  = us.lastCashAddTime;
159         passiveTime      = us.passiveTime;
160         lastActivityTime = us.lastActivityTime;
161         up = us.up;
162         down = us.down;
163         return * this;
164     };
165     operator USER_STAT() const
166     {
167         USER_STAT us;
168         us.cash             = cash;
169         us.freeMb           = freeMb;
170         us.lastCashAdd      = lastCashAdd;
171         us.lastCashAddTime  = lastCashAddTime;
172         us.passiveTime      = passiveTime;
173         us.lastActivityTime = lastActivityTime;
174         us.up               = up;
175         us.down             = down;
176         return us;
177     };
178
179     RESETABLE<double>      cash;
180     RESETABLE<double>      freeMb;
181     RESETABLE<double>      lastCashAdd;
182     RESETABLE<time_t>      lastCashAddTime;
183     RESETABLE<time_t>      passiveTime;
184     RESETABLE<time_t>      lastActivityTime;
185     RESETABLE<DIR_TRAFF>   up;
186     RESETABLE<DIR_TRAFF>   down;
187 };
188 //-----------------------------------------------------------------------------
189 #endif