]> git.stg.codes - stg.git/blob - projects/stargazer/tariff_impl.h
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / projects / stargazer / tariff_impl.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  *    Date: 07.11.2007
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23  */
24
25 #pragma once
26
27 #include "stg/tariff.h"
28 #include "stg/tariff_conf.h"
29
30 #include <string>
31
32 #include <ctime>
33 #include <cstdint>
34
35 #define TARIFF_DAY     0
36 #define TARIFF_NIGHT   1
37
38 namespace STG
39 {
40
41 class TariffImpl : public Tariff {
42     public:
43         explicit TariffImpl(const std::string & name)
44             : tariffData(name)
45         {}
46         explicit TariffImpl(const TariffData & td)
47             : tariffData(td)
48         {}
49
50         TariffImpl(const TariffImpl&) = default;
51         TariffImpl& operator=(const TariffImpl&) = default;
52         TariffImpl(TariffImpl&&) = default;
53         TariffImpl& operator=(TariffImpl&&) = default;
54
55         double  GetPriceWithTraffType(uint64_t up,
56                                       uint64_t down,
57                                       int dir,
58                                       time_t t) const;
59         double  GetFreeMb() const { return tariffData.tariffConf.free; }
60         double  GetPassiveCost() const { return tariffData.tariffConf.passiveCost; }
61         double  GetFee() const { return tariffData.tariffConf.fee; }
62         double  GetFree() const { return tariffData.tariffConf.free; }
63         Period  GetPeriod() const { return tariffData.tariffConf.period; }
64         ChangePolicy GetChangePolicy() const { return tariffData.tariffConf.changePolicy; }
65         time_t GetChangePolicyTimeout() const { return tariffData.tariffConf.changePolicyTimeout; }
66
67         void    Print() const;
68
69         const   std::string & GetName() const { return tariffData.tariffConf.name; }
70         void    SetName(const std::string & name) { tariffData.tariffConf.name = name; }
71
72         int     GetTraffType() const { return tariffData.tariffConf.traffType; }
73         int64_t GetTraffByType(uint64_t up, uint64_t down) const;
74         int     GetThreshold(int dir) const;
75         const TariffData & GetTariffData() const { return tariffData; }
76
77         TariffImpl & operator=(const TariffData & td);
78         bool     operator==(const TariffImpl & rhs) const { return GetName() == rhs.GetName(); }
79         bool     operator!=(const TariffImpl & rhs) const { return GetName() != rhs.GetName(); }
80         std::string TariffChangeIsAllowed(const Tariff & to, time_t currentTime) const;
81
82     private:
83         TariffData     tariffData;
84
85         double  GetPriceWithoutFreeMb(int dir, int64_t mb, time_t t) const;
86         int     Interval(int dir, time_t t) const;
87 };
88
89 }