]> git.stg.codes - stg.git/blob - include/user_stat.h
126c20f46d0424151ea61592d28268ec8978b453
[stg.git] / include / 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     uint32_t        ip;
78     int             dir;
79     #ifdef TRAFF_STAT_WITH_PORTS
80     uint16_t        port;
81     #endif
82 };
83 //-----------------------------------------------------------------------------
84 struct STAT_NODE
85 {
86     STAT_NODE(uint64_t _up,
87               uint64_t _down,
88               double   _cash)
89         : up(_up),
90           down(_down),
91           cash(_cash)
92     {}
93     uint64_t        up;
94     uint64_t        down;
95     double          cash;
96 };
97 //-----------------------------------------------------------------------------
98 struct USER_STAT
99 {
100     //USER_STAT & operator= (const USER_STAT_RES & usr);
101     USER_STAT()
102         : up(),
103           down(),
104           cash(0),
105           freeMb(0),
106           lastCashAdd(0),
107           lastCashAddTime(0),
108           passiveTime(0),
109           lastActivityTime(0)
110     {};
111
112     DIR_TRAFF   up;
113     DIR_TRAFF   down;
114     double      cash;
115     double      freeMb;
116     double      lastCashAdd;
117     time_t      lastCashAddTime;
118     time_t      passiveTime;
119     time_t      lastActivityTime;
120 };
121 //-----------------------------------------------------------------------------
122 typedef std::map<IP_DIR_PAIR, STAT_NODE> TRAFF_STAT;
123 //-----------------------------------------------------------------------------
124 struct USER_STAT_RES
125 {
126     USER_STAT_RES()
127         : cash(),
128           freeMb(),
129           lastCashAdd(),
130           lastCashAddTime(),
131           passiveTime(),
132           lastActivityTime(),
133           up(),
134           down()
135     {}
136
137     USER_STAT_RES & operator= (const USER_STAT & us)
138     {
139         cash             = us.cash;
140         freeMb           = us.freeMb;
141         lastCashAdd      = us.lastCashAdd;
142         lastCashAddTime  = us.lastCashAddTime;
143         passiveTime      = us.passiveTime;
144         lastActivityTime = us.lastActivityTime;
145         up = us.up;
146         down = us.down;
147         return * this;
148     };
149     operator USER_STAT()
150     {
151         USER_STAT us;
152         us.cash             = cash;
153         us.freeMb           = freeMb;
154         us.lastCashAdd      = lastCashAdd;
155         us.lastCashAddTime  = lastCashAddTime;
156         us.passiveTime      = passiveTime;
157         us.lastActivityTime = lastActivityTime;
158         us.up               = up;
159         us.down             = down;
160         return us;
161     };
162
163     RESETABLE<double>      cash;
164     RESETABLE<double>      freeMb;
165     RESETABLE<double>      lastCashAdd;
166     RESETABLE<time_t>      lastCashAddTime;
167     RESETABLE<time_t>      passiveTime;
168     RESETABLE<time_t>      lastActivityTime;
169     RESETABLE<DIR_TRAFF>   up;
170     RESETABLE<DIR_TRAFF>   down;
171 };
172 //-----------------------------------------------------------------------------
173 #endif
174
175
176