]> git.stg.codes - stg.git/blob - projects/stargazer/tariffs_impl.cpp
Use std::jthread and C++17.
[stg.git] / projects / stargazer / tariffs_impl.cpp
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 /*
26  $Revision: 1.9 $
27  $Date: 2010/10/07 18:43:21 $
28  $Author: faust $
29  */
30
31 #include <cassert>
32 #include <algorithm>
33 #include <vector>
34
35 #include "stg/locker.h"
36 #include "stg/logger.h"
37 #include "stg/store.h"
38 #include "stg/admin.h"
39 #include "stg/admin_conf.h"
40 #include "tariffs_impl.h"
41
42 using STG::TariffsImpl;
43
44 //-----------------------------------------------------------------------------
45 TariffsImpl::TariffsImpl(Store * st)
46     : store(st),
47       WriteServLog(Logger::get()),
48       noTariff(NO_TARIFF_NAME)
49 {
50 }
51 //-----------------------------------------------------------------------------
52 int TariffsImpl::ReadTariffs()
53 {
54 std::lock_guard<std::mutex> lock(m_mutex);
55
56 std::vector<std::string> tariffsList;
57 if (store->GetTariffsList(&tariffsList))
58     {
59     WriteServLog("Cannot get tariffs list.");
60     WriteServLog("%s", store->GetStrError().c_str());
61     }
62
63 Data::size_type tariffsNum = tariffsList.size();
64
65 for (Data::size_type i = 0; i < tariffsNum; i++)
66     {
67     TariffData td;
68     if (store->RestoreTariff(&td, tariffsList[i]))
69         {
70         WriteServLog("Cannot read tariff %s.", tariffsList[i].c_str());
71         WriteServLog("%s", store->GetStrError().c_str());
72         return -1;
73         }
74     tariffs.push_back(TariffImpl(td));
75     }
76
77 return 0;
78 }
79 //-----------------------------------------------------------------------------
80 size_t TariffsImpl::Count() const
81 {
82 std::lock_guard<std::mutex> lock(m_mutex);
83 return tariffs.size();
84 }
85 //-----------------------------------------------------------------------------
86 const STG::Tariff* TariffsImpl::FindByName(const std::string & name) const
87 {
88 if (name == NO_TARIFF_NAME)
89     return &noTariff;
90
91 std::lock_guard<std::mutex> lock(m_mutex);
92 const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
93
94 if (ti != tariffs.end())
95     return &(*ti);
96
97 return NULL;
98 }
99 //-----------------------------------------------------------------------------
100 int TariffsImpl::Chg(const TariffData & td, const Admin * admin)
101 {
102 const auto& priv = admin->priv();
103
104 if (!priv.tariffChg)
105     {
106     std::string s = admin->logStr() + " Change tariff \'"
107                + td.tariffConf.name + "\'. Access denied.";
108     strError = "Access denied.";
109     WriteServLog(s.c_str());
110     return -1;
111     }
112
113 std::lock_guard<std::mutex> lock(m_mutex);
114
115 auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(td.tariffConf.name));
116
117 if (ti == tariffs.end())
118     {
119     strError = "Tariff \'" + td.tariffConf.name + "\' cannot be changed. Tariff does not exist.";
120     WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str());
121     return -1;
122     }
123
124 *ti = td;
125
126 if (store->SaveTariff(td, td.tariffConf.name))
127     {
128     std::string error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError();
129     WriteServLog(error.c_str());
130     return -1;
131     }
132
133 WriteServLog("%s Tariff \'%s\' changed.",
134              admin->logStr().c_str(), td.tariffConf.name.c_str());
135
136 return 0;
137 }
138 //-----------------------------------------------------------------------------
139 int TariffsImpl::Del(const std::string & name, const Admin * admin)
140 {
141 const auto& priv = admin->priv();
142
143 if (!priv.tariffChg)
144     {
145     std::string s = admin->logStr() + " Delete tariff \'"
146                + name + "\'. Access denied.";
147     strError = "Access denied.";
148     WriteServLog(s.c_str());
149     return -1;
150     }
151
152 TariffData td;
153
154     {
155     std::lock_guard<std::mutex> lock(m_mutex);
156
157     const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
158
159     if (ti == tariffs.end())
160         {
161         strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist.";
162         WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str());
163         return -1;
164         }
165
166     if (store->DelTariff(name))
167         {
168         WriteServLog("Cannot delete tariff %s.", name.c_str());
169         WriteServLog("%s", store->GetStrError().c_str());
170         return -1;
171         }
172
173     td = ti->GetTariffData();
174
175     tariffs.erase(ti);
176     }
177
178 auto ni = onDelNotifiers.begin();
179 while (ni != onDelNotifiers.end())
180     {
181     (*ni)->Notify(td);
182     ++ni;
183     }
184
185 WriteServLog("%s Tariff \'%s\' deleted.",
186              admin->logStr().c_str(),
187              name.c_str());
188 return 0;
189 }
190 //-----------------------------------------------------------------------------
191 int TariffsImpl::Add(const std::string & name, const Admin * admin)
192 {
193 const auto& priv = admin->priv();
194
195 if (!priv.tariffChg)
196     {
197     std::string s = admin->logStr() + " Add tariff \'"
198                + name + "\'. Access denied.";
199     strError = "Access denied.";
200     WriteServLog(s.c_str());
201     return -1;
202     }
203
204     {
205     std::lock_guard<std::mutex> lock(m_mutex);
206
207     const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
208
209     if (ti != tariffs.end())
210         {
211         strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
212         WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str());
213         return -1;
214         }
215
216     tariffs.push_back(TariffImpl(name));
217     }
218
219 if (store->AddTariff(name) < 0)
220     {
221     strError = "Tariff " + name + " adding error. " + store->GetStrError();
222     WriteServLog(strError.c_str());
223     return -1;
224     }
225
226 // Fire all "on add" notifiers
227 auto ni = onAddNotifiers.begin();
228 while (ni != onAddNotifiers.end())
229     {
230     (*ni)->Notify(tariffs.back().GetTariffData());
231     ++ni;
232     }
233
234 WriteServLog("%s Tariff \'%s\' added.",
235                  admin->logStr().c_str(), name.c_str());
236
237 return 0;
238 }
239 //-----------------------------------------------------------------------------
240 void TariffsImpl::GetTariffsData(std::vector<TariffData> * tdl) const
241 {
242 assert(tdl != NULL && "Tariffs data list is not null");
243 std::lock_guard<std::mutex> lock(m_mutex);
244
245 auto it = tariffs.begin();
246 for (; it != tariffs.end(); ++it)
247     {
248     tdl->push_back(it->GetTariffData());
249     }
250 }
251 //-----------------------------------------------------------------------------
252 void TariffsImpl::AddNotifierAdd(NotifierBase<TariffData> * n)
253 {
254 std::lock_guard<std::mutex> lock(m_mutex);
255 onAddNotifiers.insert(n);
256 }
257 //-----------------------------------------------------------------------------
258 void TariffsImpl::DelNotifierAdd(NotifierBase<TariffData> * n)
259 {
260 std::lock_guard<std::mutex> lock(m_mutex);
261 onAddNotifiers.erase(n);
262 }
263 //-----------------------------------------------------------------------------
264 void TariffsImpl::AddNotifierDel(NotifierBase<TariffData> * n)
265 {
266 std::lock_guard<std::mutex> lock(m_mutex);
267 onDelNotifiers.insert(n);
268 }
269 //-----------------------------------------------------------------------------
270 void TariffsImpl::DelNotifierDel(NotifierBase<TariffData> * n)
271 {
272 std::lock_guard<std::mutex> lock(m_mutex);
273 onDelNotifiers.erase(n);
274 }
275 //-----------------------------------------------------------------------------