]> git.stg.codes - stg.git/blob - include/stg/user_stat.h
84fbba78d578d5ef5670705f020c56c8039149c5
[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()
117         : up(),
118           down(),
119           cash(0),
120           freeMb(0),
121           lastCashAdd(0),
122           lastCashAddTime(0),
123           passiveTime(0),
124           lastActivityTime(0)
125     {}
126
127     DIR_TRAFF   up;
128     DIR_TRAFF   down;
129     double      cash;
130     double      freeMb;
131     double      lastCashAdd;
132     time_t      lastCashAddTime;
133     time_t      passiveTime;
134     time_t      lastActivityTime;
135 };
136 //-----------------------------------------------------------------------------
137 typedef std::map<IP_DIR_PAIR, STAT_NODE> TRAFF_STAT;
138 //-----------------------------------------------------------------------------
139 struct USER_STAT_RES
140 {
141     USER_STAT_RES()
142         : cash(),
143           freeMb(),
144           lastCashAdd(),
145           lastCashAddTime(),
146           passiveTime(),
147           lastActivityTime(),
148           up(),
149           down()
150     {}
151
152     USER_STAT_RES & operator= (const USER_STAT & us)
153     {
154         cash             = us.cash;
155         freeMb           = us.freeMb;
156         lastCashAdd      = us.lastCashAdd;
157         lastCashAddTime  = us.lastCashAddTime;
158         passiveTime      = us.passiveTime;
159         lastActivityTime = us.lastActivityTime;
160         up               = us.up;
161         down             = us.down;
162         return *this;
163     }
164     USER_STAT GetData() const
165     {
166         USER_STAT us;
167         us.cash             = cash.data();
168         us.freeMb           = freeMb.data();
169         us.lastCashAdd      = lastCashAdd.data();
170         us.lastCashAddTime  = lastCashAddTime.data();
171         us.passiveTime      = passiveTime.data();
172         us.lastActivityTime = lastActivityTime.data();
173         us.up               = up.GetData();
174         us.down             = down.GetData();
175         return us;
176     }
177
178     RESETABLE<double>      cash;
179     RESETABLE<double>      freeMb;
180     RESETABLE<double>      lastCashAdd;
181     RESETABLE<time_t>      lastCashAddTime;
182     RESETABLE<time_t>      passiveTime;
183     RESETABLE<time_t>      lastActivityTime;
184     DIR_TRAFF_RES          up;
185     DIR_TRAFF_RES          down;
186 };
187 //-----------------------------------------------------------------------------
188 #endif